View source
<?php
namespace Drupal\Tests\eck\Functional;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
abstract class FunctionalTestBase extends BrowserTestBase {
use StringTranslationTrait;
protected $adminUser;
protected static $modules = [
'eck',
];
protected $defaultTheme = 'classy';
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser($this
->getAdministratorPermissions());
$this
->drupalLogin($this->adminUser);
}
protected function getAdministratorPermissions() {
return [
'administer eck entity types',
'administer eck entities',
'administer eck entity bundles',
'bypass eck entity access',
];
}
protected function createEntityType(array $fields = [], $label = '') {
$label = empty($label) ? $this
->randomMachineName() : $label;
$fields = empty($fields) ? $this
->getConfigurableBaseFields() : $fields;
$edit = [
'label' => $label,
'id' => $id = strtolower($label),
];
foreach ($fields as $field) {
$edit[$field] = TRUE;
}
$this
->drupalGet(Url::fromRoute('eck.entity_type.add'));
$this
->submitForm($edit, 'Create entity type');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains("Entity type <em class=\"placeholder\">{$label}</em> has been added.");
\Drupal::entityTypeManager()
->clearCachedDefinitions();
return [
'id' => $id,
'label' => $label,
];
}
protected function getConfigurableBaseFields() {
return [
'created',
'changed',
'uid',
'title',
'status',
];
}
protected function createEntityBundle($entity_type, $label = '', $title_overrides = []) {
if (empty($label)) {
$label = $this
->randomMachineName();
}
$bundle = strtolower($label);
$edit = [
'name' => $label,
'type' => $bundle,
];
foreach ($title_overrides as $field => $title_override) {
$edit[$field . '_title_override'] = $title_override;
}
$this
->drupalGet(Url::fromRoute("eck.entity.{$entity_type}_type.add"));
$this
->submitForm($edit, 'Save bundle');
$this
->assertSession()
->responseContains("The entity bundle <em class=\"placeholder\">{$label}</em> has been added.");
return $edit;
}
protected function editEntityBundle($entity_type, $bundle, $label, $title_overrides = []) {
$this
->drupalGet(Url::fromRoute("entity.{$entity_type}_type.edit_form", [
"{$entity_type}_type" => $bundle,
]));
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'name' => $label,
];
foreach ($title_overrides as $field => $title_override) {
$edit[$field . '_title_override'] = $title_override;
}
$this
->submitForm($edit, 'Save bundle');
$this
->assertSession()
->responseContains("The entity bundle <em class=\"placeholder\">{$label}</em> has been updated.");
}
protected function createEntity($entity_type, array $values) {
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$entity = $storage
->create($values);
$entity
->save();
return $entity;
}
}