View source
<?php
namespace Drupal\Tests\acquia_contenthub\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
abstract class ContentHubTestBase extends BrowserTestBase {
use StringTranslationTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'node',
'acquia_contenthub',
];
protected $adminUser;
protected $adminUserPermissions = [
'bypass node access',
'administer acquia content hub',
'administer content types',
'access administration pages',
];
protected $unauthorizedUser;
protected $anonymousUser;
protected $urlGenerator;
protected $config;
public function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser($this->adminUserPermissions);
$this->unauthorizedUser = $this
->drupalCreateUser([
'access administration pages',
]);
$this->anonymousUser = $this
->drupalCreateUser();
$this->urlGenerator = $this->container
->get('url_generator');
$this->config = $this->container
->get('config.factory')
->getEditable('acquia_contenthub.admin_settings');
$this
->setContentHubConnection();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
}
public function configureContentHubContentTypes($entity_type, array $bundles) {
$this
->drupalGet('admin/config/services/acquia-contenthub/configuration');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [];
foreach ($bundles as $bundle) {
$edit['entities[' . $entity_type . '][' . $bundle . '][enable_index]'] = 1;
}
$this
->submitForm($edit, $this
->t('Save configuration'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/config/services/acquia-contenthub/configuration');
$this
->assertSession()
->statusCodeEquals(200);
}
public function setContentHubConnection() {
$config = [
'hostname' => \Drupal::request()
->getHost(),
'api' => '11111111-0000-0000-0000-000000000000',
'secret' => '22222222-0000-0000-0000-000000000000',
'origin' => '00000000-0000-0000-0000-000000000000',
'client_name' => 'mytestsite',
];
$this->config
->set('hostname', $config['hostname']);
$this->config
->set('api_key', $config['api']);
$this->config
->set('secret_key', $config['secret']);
$this->config
->set('origin', $config['origin']);
$this->config
->set('client_name', $config['client_name']);
$this->config
->save();
$hostname = $this->config
->get('hostname');
$api = $this->config
->get('api_key');
$secret = $this->config
->get('secret_key');
$origin = $this->config
->get('origin');
$client_name = $this->config
->get('client_name');
$this
->assertEquals($hostname, $config['hostname']);
$this
->assertEquals($api, $config['api']);
$this
->assertEquals($secret, $config['secret']);
$this
->assertEquals($origin, $config['origin']);
$this
->assertEquals($client_name, $config['client_name']);
$client_manager = $this->container
->get('acquia_contenthub.client_manager');
$client_manager
->resetConnection($config);
$this
->assertTrue($client_manager
->isConnected(), 'Content Hub Client is connected successfully.');
}
public function setRoleFor($role) {
$this
->drupalGet('admin/config/services/acquia-contenthub/configuration');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'user_role' => $role,
];
$this
->submitForm($edit, $this
->t('Save configuration'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/config/services/acquia-contenthub/configuration');
$this
->assertSession()
->statusCodeEquals(200);
}
protected function drupalGetCdf($path, array $options = [], array $headers = []) {
$options = array_merge($options, [
'query' => [
'_format' => 'acquia_contenthub_cdf',
],
]);
return Json::decode($this
->drupalGet($path, $options, $headers));
}
public function enableViewModeFor($entity_type, $bundle, $view_mode) {
$this
->drupalGet('admin/config/services/acquia-contenthub/configuration');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'entities[' . $entity_type . '][' . $bundle . '][enable_viewmodes]' => TRUE,
];
if (is_array($view_mode)) {
foreach ($view_mode as $value) {
$edit['entities[' . $entity_type . '][' . $bundle . '][rendering][]'][$value] = $value;
}
}
else {
$edit['entities[' . $entity_type . '][' . $bundle . '][rendering][]'] = [
$view_mode,
];
}
$this
->submitForm($edit, $this
->t('Save configuration'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/config/services/acquia-contenthub/configuration');
$this
->assertSession()
->statusCodeEquals(200);
}
protected function drupalGetWithFormat($path, $format, array $options = [], array $headers = []) {
$options = array_merge_recursive([
'query' => [
'_format' => $format,
],
], $options);
return $this
->drupalGet($path, $options, $headers);
}
}