View source
<?php
namespace Drupal\Tests\acquia_contenthub\Functional;
use Drupal\acquia_contenthub_test\MockDataProvider;
use Drupal\Tests\BrowserTestBase;
class ContentHubSettingsFormTest extends BrowserTestBase {
protected $authorizedUser;
protected $unauthorizedUser;
const CH_SETTINGS_FORM_PATH = '/admin/config/services/acquia-contenthub';
protected $defaultTheme = 'stable';
public static $modules = [
'acquia_contenthub',
'acquia_contenthub_test',
'acquia_contenthub_server_test',
'user',
];
protected function setUp() {
parent::setUp();
$this->authorizedUser = $this
->drupalCreateUser([
'administer acquia content hub',
]);
$this->unauthorizedUser = $this
->drupalCreateUser();
$this
->drupalLogin($this->authorizedUser);
}
public function testContentHubSettingsPagePermissions() {
$session = $this
->assertSession();
$this
->drupalGet(self::CH_SETTINGS_FORM_PATH);
$session
->pageTextContains('Acquia Content Hub Settings');
$session
->statusCodeEquals(200);
$this
->drupalLogout();
$this
->drupalLogin($this->unauthorizedUser);
$this
->drupalGet(self::CH_SETTINGS_FORM_PATH);
$session
->pageTextContains('Access denied');
$session
->statusCodeEquals(403);
}
public function testContentHubSettingsFormRenderedProperly() {
$session = $this
->assertSession();
$this
->drupalGet(self::CH_SETTINGS_FORM_PATH);
$session
->fieldExists('Acquia Content Hub Hostname');
$session
->fieldExists('API Key');
$session
->fieldExists('Secret Key');
$session
->fieldExists('Client Name');
$session
->fieldExists('Send updates to Content Hub Service.');
$session
->fieldDisabled('Send updates to Content Hub Service.');
$session
->fieldValueEquals('Send updates to Content Hub Service.', TRUE);
$session
->fieldExists('Publicly Accessible URL');
$session
->pageTextContains('Site\'s Origin UUID');
$session
->buttonExists('Register Site');
$session
->buttonNotExists('Update Public URL');
$session
->buttonNotExists('Unregister Site');
$session
->fieldValueEquals('webhook', $GLOBALS['base_url']);
}
public function testContentHubSettingsPageNoData() {
$session = $this
->assertSession();
$settings = [
'webhook' => 'httpp://invalid-url.com',
];
$this
->drupalPostForm(self::CH_SETTINGS_FORM_PATH, $settings, 'Register Site');
$session
->pageTextContains('Acquia Content Hub Hostname field is required.');
$session
->pageTextContains('API Key field is required.');
$session
->pageTextContains('Secret Key field is required.');
$session
->pageTextContains('Client Name field is required.');
$session
->pageTextContains('Please type a publicly accessible url.');
}
public function testContentHubSettingsPageWithValidData() {
$session = $this
->assertSession();
$settings = [
'hostname' => MockDataProvider::VALID_HOSTNAME,
'api_key' => MockDataProvider::VALID_API_KEY,
'secret_key' => MockDataProvider::VALID_SECRET,
'client_name' => MockDataProvider::VALID_CLIENT_NAME,
'webhook' => MockDataProvider::VALID_WEBHOOK_URL,
];
$this
->drupalPostForm(self::CH_SETTINGS_FORM_PATH, $settings, 'Register Site');
$session
->pageTextContains('Site successfully connected to Content Hub. To change connection settings, unregister the site first.');
$session
->statusCodeEquals(200);
$session
->buttonNotExists('Register Site');
$session
->buttonExists('Update Public URL');
$session
->linkExists('Unregister Site');
}
public function testContentHubSettingsPageWithPartiallyValidData() {
$session = $this
->assertSession();
$settings = [
'hostname' => MockDataProvider::VALID_HOSTNAME,
'api_key' => MockDataProvider::VALID_API_KEY,
'secret_key' => MockDataProvider::VALID_SECRET,
'client_name' => MockDataProvider::VALID_CLIENT_NAME,
'webhook' => 'http://invalid-url.com',
];
$this
->drupalPostForm(self::CH_SETTINGS_FORM_PATH, $settings, 'Register Site');
$session
->pageTextContains('Site successfully connected to Content Hub. To change connection settings, unregister the site first.');
$session
->statusCodeEquals(200);
$session
->buttonNotExists('Register Site');
$session
->buttonExists('Update Public URL');
$session
->linkExists('Unregister Site');
$settings = [
'webhook' => MockDataProvider::ALREADY_REGISTERED_WEBHOOK,
];
$this
->drupalPostForm(self::CH_SETTINGS_FORM_PATH, $settings, 'Update Public URL');
$session
->pageTextContains('This webhook is already being used.');
$session
->pageTextContains('Please insert another one, or unregister the existing one first.');
$settings = [
'webhook' => MockDataProvider::VALID_WEBHOOK_URL,
];
$this
->drupalPostForm(self::CH_SETTINGS_FORM_PATH, $settings, 'Update Public URL');
$session
->pageTextContains('Site successfully connected to Content Hub. To change connection settings, unregister the site first.');
$session
->pageTextContains('Successfully Updated Public URL.');
}
public function testContentHubSettingsPageWithInvalidData() {
$session = $this
->assertSession();
$settings = [
'hostname' => 'https://invalid-url.com',
'api_key' => 'invalid',
'secret_key' => 'invalid',
'client_name' => 'test',
'webhook' => MockDataProvider::VALID_WEBHOOK_URL,
];
$this
->drupalPostForm(self::CH_SETTINGS_FORM_PATH, $settings, 'Register Site');
$session
->pageTextContains(sprintf('Could not get authorization from Content Hub to register client %s. Are your credentials inserted correctly?', $settings['client_name']));
$session
->pageTextContains('There is a problem connecting to Acquia Content Hub. Please ensure that your hostname and credentials are correct.');
$settings['hostname'] = MockDataProvider::VALID_HOSTNAME;
$this
->drupalPostForm(self::CH_SETTINGS_FORM_PATH, $settings, 'Register Site');
$session
->pageTextContains(sprintf('[4001] Not Found: Customer Key %s could not be found.', $settings['api_key']));
$settings['api_key'] = MockDataProvider::VALID_API_KEY;
$this
->drupalPostForm(self::CH_SETTINGS_FORM_PATH, $settings, 'Register Site');
$session
->pageTextContains('[4001] Signature for the message does not match expected signature for API key.');
}
}