Commit de216c7c authored by john_a's avatar john_a Committed by john_a
Browse files

Issue #2979238 by john_a, bhanuprakashnani: the thephpleague/json-guard and...

Issue #2979238 by john_a, bhanuprakashnani: the thephpleague/json-guard and thephpleague/json-reference packages have been abandoned
parent a96b17bc
......@@ -45,15 +45,14 @@ render the JSON from that. Instead, it is up to the developer to produce the
JSON object, and the service will then validate the JSON object against the
schema to make sure that it meets minimum requirements.
The schema can be edited and validated, and Json validation can we switched
The schema can be edited and Json validation can we switched
on/off in the settings.
REQUIREMENTS
============
* [json-guard](https://packagist.org/packages/league/json-guard)
* [json-reference](https://packagist.org/packages/league/json-guard)
* [Swaggest PHP Json Schema](https://github.com/swaggest/php-json-schema)
CONFIGURATION
......@@ -77,8 +76,6 @@ CONFIGURE MARKETING CLOUD
* Client Secret: The client secret from Salesforce for your app.
* Validate JSON: Uncheck this to prevent the module from validating any
JSON body payloads.
* Validate JSON Schema: Check this to force the module to validating the
JSON schema on every API call. This is useful for debugging.
* Do not send API request: This is only used for automated testing.
* Salesforce API URL base: The Salesforce exact target API URL.
* Salesforce auth token request URL: The URL defined by SalesForce to fetch
......
{
"name": "drupal/marketing_cloud",
"description": "Provides Drupal 8 integration with Marketing Cloud",
"type": "drupal-module",
"homepage": "http://drupal.org/project/marketing_cloud",
"authors": [
{
"name": "John Avery",
"homepage": "https://www.drupal.org/u/john_a"
"name": "drupal/marketing_cloud",
"description": "Provides Drupal 8 integration with Marketing Cloud",
"type": "drupal-module",
"homepage": "http://drupal.org/project/marketing_cloud",
"authors": [
{
"name": "John Avery",
"homepage": "https://www.drupal.org/u/john_a"
}
],
"support": {
"issues": "https://www.drupal.org/project/issues/marketing_cloud",
"irc": "irc://irc.freenode.org/drupal-contribute",
"source": "https://cgit.drupalcode.org/marketing_cloud"
},
"license": "GPL-2.0+",
"require": {
"swaggest/json-schema": "^0.12.0"
},
"require-dev": {
"drupal/webform": "5.x-dev"
}
],
"support": {
"issues": "https://www.drupal.org/project/issues/marketing_cloud",
"irc": "irc://irc.freenode.org/drupal-contribute",
"source": "https://cgit.drupalcode.org/marketing_cloud"
},
"license": "GPL-2.0+",
"require": {
"league/json-guard": "^1.0",
"league/json-reference": "^1.0"
}
}
client_id: ""
client_secret: ""
validate_json: true
validate_schema: false
do_not_send: false
base_url: "https://www.exacttargetapis.com"
request_token_url: "https://auth.exacttargetapis.com/v1/requestToken"
......
......@@ -16,9 +16,6 @@ marketing_cloud.settings:
do_not_send:
type: boolean
label: 'Do not send'
validate_schema:
type: boolean
label: 'Validate Schema'
request_token_url:
type: string
label: 'Salesforce auth token request URL'
......
<?php
/**
* @file
* Install, update and uninstall functions for the marketing_cloud module.
*/
/**
* Remove validate_schema from the default configuration of marketing_cloud.
*/
function marketing_cloud_update_8001() {
$config_name = 'marketing_cloud.settings';
$key = 'validate_schema';
$config = \Drupal::service('config.factory')
->getEditable($config_name);
if ($config->get($key) !== NULL) {
$config->clear($key)->save();
}
}
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_address\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudAddressTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,13 +38,8 @@ class MarketingCloudAddressTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_address.service');
// Get marketing_cloud_assets config object.
......@@ -63,8 +55,6 @@ class MarketingCloudAddressTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for validate_email is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for validate_email is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for validate_email.");
// Test validateEmail against expected inputs.
$data = $this->validateEmailData();
$result = $this->service
......
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_assets\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudAssetsTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,11 +38,8 @@ class MarketingCloudAssetsTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_assets.service');
// Get marketing_cloud_assets config object.
......@@ -256,8 +250,6 @@ class MarketingCloudAssetsTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for $machineName definition is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for $machineName definition is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for $machineName.");
}
/**
......
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_campaigns\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudCampaignsTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,11 +38,8 @@ class MarketingCloudCampaignsTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_campaigns.service');
// Get marketing_cloud_assets config object.
......@@ -249,8 +243,6 @@ class MarketingCloudCampaignsTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for $machineName definition is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for $machineName definition is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for $machineName.");
}
/**
......
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_contacts\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudContactsTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,11 +38,8 @@ class MarketingCloudContactsTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_contacts.service');
// Get marketing_cloud_assets config object.
......@@ -656,8 +650,6 @@ class MarketingCloudContactsTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for $machineName definition is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for $machineName definition is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for $machineName.");
}
/**
......
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_data_events\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudDataEventsTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,11 +38,8 @@ class MarketingCloudDataEventsTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_data_events.service');
// Get marketing_cloud_data_events config object.
......@@ -175,8 +169,6 @@ class MarketingCloudDataEventsTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for $machineName definition is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for $machineName definition is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for $machineName.");
}
/**
......
......@@ -8,4 +8,4 @@ dependencies:
- drupal:marketing_cloud
- drupal:marketing_cloud_sms
- drupal:webform
- webform_ui
- drupal:webform_ui
......@@ -21,6 +21,8 @@ class MarketingCloudExampleTest extends BrowserTestBase {
* @var array
*/
public static $modules = [
'webform',
'webform_ui',
'marketing_cloud',
'marketing_cloud_sms',
'marketing_cloud_example',
......
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_interaction\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudInteractionTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,11 +38,8 @@ class MarketingCloudInteractionTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_interaction.service');
// Get marketing_cloud_assets config object.
......@@ -346,8 +340,6 @@ class MarketingCloudInteractionTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for $machineName definition is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for $machineName definition is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for $machineName.");
}
/**
......
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_messages\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudMessagesTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,11 +38,8 @@ class MarketingCloudMessagesTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_messages.service');
// Get marketing_cloud_assets config object.
......@@ -129,8 +123,6 @@ class MarketingCloudMessagesTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for $machineName definition is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for $machineName definition is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for $machineName.");
}
/**
......
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_platform\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudPlatformTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,11 +38,8 @@ class MarketingCloudPlatformTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_platform.service');
// Get config object.
......@@ -121,8 +115,6 @@ class MarketingCloudPlatformTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for $machineName definition is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for $machineName definition is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for $machineName.");
}
}
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_push\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudPushTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,11 +38,8 @@ class MarketingCloudPushTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_push.service');
// Get config object.
......@@ -683,8 +677,6 @@ class MarketingCloudPushTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for $machineName definition is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for $machineName definition is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for $machineName.");
}
/**
......
......@@ -2,8 +2,6 @@
namespace Drupal\Tests\marketing_cloud_sms\Functional;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -24,7 +22,6 @@ class MarketingCloudSMSTest extends BrowserTestBase {
protected $adminUser;
protected $service;
protected $metaSchema;
protected $moduleConfig;
/**
......@@ -41,11 +38,8 @@ class MarketingCloudSMSTest extends BrowserTestBase {
->set('client_id', 'testingid')
->set('client_secret', 'testingsecret')
->set('validate_json', TRUE)
->set('validate_schema', TRUE)
->set('do_not_send', TRUE)
->save();
// Get schema validator.
$this->metaSchema = Dereferencer::draft6()->dereference('http://json-schema.org/draft-06/schema#');
// Create service.
$this->service = \Drupal::service('marketing_cloud_sms.service');
// Get config object.
......@@ -720,8 +714,6 @@ class MarketingCloudSMSTest extends BrowserTestBase {
$this->assertNotEmpty($schema, "json schema for $machineName definition is empty.");
$schema = json_decode($schema);
$this->assertNotEmpty($schema, "json schema for $machineName definition is invalid json.");
$validator = new Validator($schema, $this->metaSchema);
$this->assertNotFalse($validator->passes(), "schema validation failed for $machineName.");
}
/**
......
......@@ -52,13 +52,6 @@ class MarketingCloudSettings extends ConfigFormBase {
'#default_value' => $config->get('validate_json'),
];
$form['validate_schema'] = [
'#type' => 'checkbox',
'#title' => $this->t('Validate JSON Schema'),
'#description' => $this->t('Perform validation on the JSON schemas in the definitions. This will slow down performance, and should only be used for development or debugging.'),
'#default_value' => $config->get('validate_schema'),
];
$form['do_not_send'] = [
'#type' => 'checkbox',
'#title' => $this->t('Do not send API request'),
......@@ -118,7 +111,6 @@ class MarketingCloudSettings extends ConfigFormBase {
$config->set('client_id', $form_state->getValue('client_id'));
$config->set('client_secret', $form_state->getValue('client_secret'));
$config->set('validate_json', $form_state->getValue('validate_json'));
$config->set('validate_schema', $form_state->getValue('validate_schema'));
$config->set('do_not_send', $form_state->getValue('do_not_send'));
$config->set('base_url', $form_state->getValue('base_url'));
$config->set('request_token_url', $form_state->getValue('request_token_url'));
......
......@@ -2,14 +2,15 @@
namespace Drupal\marketing_cloud;
use League\JsonGuard\Validator;
use League\JsonReference\Dereferencer;
use Swaggest\JsonSchema\InvalidValue;
use Swaggest\JsonSchema\Schema;
use GuzzleHttp\Exception\RequestException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactory;
use Drupal\Core\Messenger\MessengerInterface;
use GuzzleHttp\Client;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Swaggest\JsonSchema\Exception;
/**
* Class MarketingCloudService.
......@@ -66,6 +67,8 @@ abstract class MarketingCloudService {
* Name/value pairs for extra arguments to be added to the URI,
* e.g. ['foo' => 'var'] gives &foo=bar.
*
* @throws Exception
*
* @return array|bool|int|mixed|string
* The result of the API call or FALSE on failure.
*
......@@ -73,7 +76,6 @@ abstract class MarketingCloudService {
*/
protected function apiCall($moduleName, $machineName, $data, array $urlParams = [], array $params = []) {
$marketingCloudConfig = $this->configFactory->getEditable('marketing_cloud.settings');
$validateSchema = $marketingCloudConfig->get('validate_schema');
$validateJson = $marketingCloudConfig->get('validate_json');
$doNotSend = $marketingCloudConfig->get('do_not_send');
// Create module settings path string.
......@@ -82,17 +84,16 @@ abstract class MarketingCloudService {
$subModuleSettingsPath = "$moduleName.settings";
$subModuleConfig = $this->configFactory->getEditable($subModuleSettingsPath);
// Ensure correct object/array types for schema.
if (!is_array($data) || !is_object($data)) {
$data = json_decode(json_encode($data));
if ($data === NULL && json_last_error() !== JSON_ERROR_NONE) {
$message = $this->t('Could not send %machine_name, invalid JSON data',
['%machine_name' => $machineName]
);
$this->messenger->addError($message);
$this->loggerFactory->get(__METHOD__)->error($message);
return FALSE;
}
// Ensure correct object/array types for schema, e.g. associative arrays
// are converted to objects.
$data = json_decode(json_encode($data));
if ($data === NULL && json_last_error() !== JSON_ERROR_NONE) {
$message = $this->t('Could not send %machine_name, invalid JSON data',
['%machine_name' => $machineName]
);
$this->messenger->addError($message);
$this->loggerFactory->get(__METHOD__)->error($message);
return FALSE;
}
// Fetch method.
......@@ -123,7 +124,7 @@ abstract class MarketingCloudService {
return FALSE;
}
if ($validateJson || $validateSchema) {
if ($validateJson) {
// Fetch endpoint JSON schema.
$schema = $subModuleConfig->get("definitions.$machineName.schema");