View source
<?php
namespace Drupal\Tests\eck\Functional;
use Behat\Mink\Element\NodeElement;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class NavigationalStructureTest extends FunctionalTestBase {
protected static $modules = [
'system',
'node',
'block',
'field',
];
private $baseCrumbs = [
'Home',
'Administration',
];
private $entityTypeMachineName;
private $entityTypeLabel;
private $entityBundleMachineName;
private $entityBundleLabel;
protected function getAdministratorPermissions() {
return array_merge([
'access administration pages',
'access content overview',
], parent::getAdministratorPermissions());
}
public function setUp() {
parent::setUp();
$entity_type = $this
->createEntityType();
$this->entityTypeMachineName = $entity_type['id'];
$this->entityTypeLabel = $entity_type['label'];
$bundle = $this
->createEntityBundle($this->entityTypeMachineName);
$this->entityBundleMachineName = $bundle['type'];
$this->entityBundleLabel = $bundle['name'];
$this
->placeBlock('system_breadcrumb_block');
$this
->placeBlock('page_title_block');
}
private function getEntityStorageHandler() {
return \Drupal::entityTypeManager()
->getStorage($this->entityTypeMachineName);
}
private function assertCorrectPageOnRoute($route, array $routeArguments, $expectedUrl, $expectedTitle, array $crumbs = []) {
$url = Url::fromRoute($route, $routeArguments);
self::assertEquals($expectedUrl, $url
->getInternalPath());
$this
->drupalGet($url);
$this
->assertTitleEquals($expectedTitle);
$this
->assertBreadcrumbsVisible(array_merge($this->baseCrumbs, $crumbs));
}
private function assertTitleEquals($expectedTitle) {
$titleElement = $this
->getSession()
->getPage()
->find('css', '.page-title');
$actualTitle = $titleElement instanceof NodeElement ? $titleElement
->getText() : '';
$this
->assertEquals($expectedTitle, $actualTitle);
}
private function assertBreadcrumbsVisible(array $expectedBreadcrumbs) {
$breadcrumbs = $this
->getSession()
->getPage()
->findAll('css', '.breadcrumb a');
$actualCrumbs = [];
do {
$actualCrumbs[] = array_shift($breadcrumbs)
->getText();
} while (!empty($breadcrumbs));
self::assertEquals($expectedBreadcrumbs, $actualCrumbs);
}
public function entityTypeList() {
$route = 'eck.entity_type.list';
$routeArguments = [];
$expectedUrl = 'admin/structure/eck';
$expectedTitle = 'ECK Entity Types';
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, [
'Structure',
]);
}
public function entityTypeAdd() {
$routeArguments = [];
$route = 'eck.entity_type.add';
$expectedUrl = 'admin/structure/eck/add';
$expectedTitle = 'Add entity type';
$crumbs = [
'Structure',
'ECK Entity Types',
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityTypeEdit() {
$route = 'entity.eck_entity_type.edit_form';
$routeArguments = [
'eck_entity_type' => $this->entityTypeMachineName,
];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}";
$expectedTitle = 'Edit entity type';
$crumbs = [
'Structure',
'ECK Entity Types',
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityTypeDelete() {
$route = 'entity.eck_entity_type.delete_form';
$routeArguments = [
'eck_entity_type' => $this->entityTypeMachineName,
];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/delete";
$expectedTitle = "Are you sure you want to delete entity type {$this->entityTypeLabel}?";
$crumbs = [
'Structure',
'ECK Entity Types',
"Edit entity type",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
$this
->assertSession()
->pageTextContains("Configuration deletions The listed configuration will be deleted.{$this->entityTypeLabel} type");
$this
->assertSession()
->pageTextContains($this->entityBundleLabel);
}
public function entityTypeDeleteWithMultipleBundles() {
$extra_bundle = $this
->createEntityBundle($this->entityTypeMachineName);
$extra_bundle_label = $extra_bundle['name'];
$route = 'entity.eck_entity_type.delete_form';
$routeArguments = [
'eck_entity_type' => $this->entityTypeMachineName,
];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/delete";
$expectedTitle = "Are you sure you want to delete entity type {$this->entityTypeLabel}?";
$crumbs = [
'Structure',
'ECK Entity Types',
"Edit entity type",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
$this
->assertSession()
->pageTextContains("Configuration deletions The listed configuration will be deleted.{$this->entityTypeLabel} type");
$this
->assertSession()
->pageTextContains($extra_bundle_label);
$this
->assertSession()
->pageTextContains($this->entityBundleLabel);
}
public function entityTypeDeleteWithMatchingBundle() {
$this
->createEntityBundle($this->entityTypeMachineName, $this->entityTypeLabel);
$this->entityBundleMachineName = $this->entityTypeMachineName;
$this->entityBundleLabel = $this->entityTypeLabel;
\Drupal::entityTypeManager()
->getStorage($this->entityTypeMachineName . '_type')
->load($this->entityBundleMachineName)
->delete();
$route = 'entity.eck_entity_type.delete_form';
$routeArguments = [
'eck_entity_type' => $this->entityTypeMachineName,
];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/delete";
$expectedTitle = "Are you sure you want to delete entity type {$this->entityTypeLabel}?";
$crumbs = [
'Structure',
'ECK Entity Types',
"Edit entity type",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
$this
->assertSession()
->pageTextContains("This action cannot be undone.");
$this
->submitForm([], 'Delete entity type');
$this
->assertSession()
->statusCodeEquals(200);
$entity_type = \Drupal::entityTypeManager()
->getStorage($this->entityTypeMachineName)
->load($this->entityTypeMachineName);
$this
->assertNull($entity_type);
$this
->entityTypeList();
}
public function entityTypeDeleteWithField() {
\Drupal::entityTypeManager()
->getStorage($this->entityTypeMachineName . '_type')
->load($this->entityBundleMachineName)
->delete();
$this->entityBundleMachineName = $this->entityTypeMachineName;
$this->entityBundleLabel = $this->entityTypeLabel;
$this
->createEntityBundle($this->entityTypeMachineName, $this->entityBundleLabel);
FieldStorageConfig::create([
'entity_type' => $this->entityTypeMachineName,
'field_name' => 'field_decimal',
'type' => 'decimal',
])
->save();
FieldConfig::create([
'entity_type' => $this->entityTypeMachineName,
'field_name' => 'field_decimal',
'bundle' => $this->entityBundleMachineName,
])
->save();
$route = 'entity.eck_entity_type.delete_form';
$routeArguments = [
'eck_entity_type' => $this->entityTypeMachineName,
];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/delete";
$expectedTitle = "Are you sure you want to delete entity type {$this->entityTypeLabel}?";
$crumbs = [
'Structure',
'ECK Entity Types',
"Edit entity type",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
$this
->assertSession()
->pageTextContains("This action cannot be undone.");
$this
->assertSession()
->pageTextContains("Configuration deletions The listed configuration will be deleted.{$this->entityTypeLabel} type{$this->entityTypeLabel}Fieldfield_decimal");
$this
->submitForm([], 'Delete entity type');
$this
->assertSession()
->responseContains('The eck entity type <em class="placeholder">' . $this->entityTypeLabel . '</em> has been deleted.');
$entity_type = \Drupal::entityTypeManager()
->clearCachedDefinitions();
\Drupal::entityTypeManager()
->getDefinition($this->entityTypeMachineName, FALSE);
$this
->assertNull($entity_type);
$this
->entityTypeList();
}
public function entityTypeDeleteWithContent() {
$field_machine_name = strtolower($this
->randomMachineName());
FieldStorageConfig::create([
'entity_type' => $this->entityTypeMachineName,
'field_name' => $field_machine_name,
'type' => 'decimal',
])
->save();
FieldConfig::create([
'entity_type' => $this->entityTypeMachineName,
'field_name' => $field_machine_name,
'bundle' => $this->entityBundleMachineName,
])
->save();
\Drupal::entityTypeManager()
->getStorage($this->entityTypeMachineName)
->create([
'type' => $this->entityBundleMachineName,
$field_machine_name => random_int(1, 1000),
])
->save();
$route = 'entity.eck_entity_type.delete_form';
$routeArguments = [
'eck_entity_type' => $this->entityTypeMachineName,
];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/delete";
$expectedTitle = "Delete entity type";
$crumbs = [
'Structure',
'ECK Entity Types',
"Edit entity type",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
$this
->assertSession()
->pageTextContains("There is 1 {$this->entityTypeLabel} entity. You can not remove this entity type until you have removed all of the {$this->entityTypeLabel} entities.");
\Drupal::entityTypeManager()
->getStorage($this->entityTypeMachineName)
->create([
'type' => $this->entityBundleMachineName,
$field_machine_name => random_int(1, 1000),
])
->save();
$route = 'entity.eck_entity_type.delete_form';
$routeArguments = [
'eck_entity_type' => $this->entityTypeMachineName,
];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/delete";
$expectedTitle = "Delete entity type";
$crumbs = [
'Structure',
'ECK Entity Types',
"Edit entity type",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
$this
->assertSession()
->pageTextContains("There are 2 {$this->entityTypeLabel} entities. You may not remove {$this->entityTypeLabel} until you have removed all of the {$this->entityTypeLabel} entities.");
}
public function entityList() {
$route = "eck.entity.{$this->entityTypeMachineName}.list";
$routeArguments = [];
$expectedUrl = "admin/content/{$this->entityTypeMachineName}";
$expectedTitle = ucfirst("{$this->entityTypeLabel} content");
$crumbs = [
'Content',
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityAddPage() {
$route = 'eck.entity.add_page';
$routeArguments = [
'eck_entity_type' => $this->entityTypeMachineName,
];
$expectedUrl = "admin/content/{$this->entityTypeMachineName}/add";
$expectedTitle = "Add {$this->entityTypeLabel} content";
$crumbs = [
'Content',
ucfirst("{$this->entityTypeLabel} content"),
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityAdd() {
$route = 'eck.entity.add';
$routeArguments = [
'eck_entity_type' => $this->entityTypeMachineName,
'eck_entity_bundle' => $this->entityBundleMachineName,
];
$expectedUrl = "admin/content/{$this->entityTypeMachineName}/add/{$this->entityBundleMachineName}";
$expectedTitle = "Add {$this->entityBundleLabel} content";
$crumbs = [
'Content',
ucfirst("{$this->entityTypeLabel} content"),
"Add {$this->entityTypeLabel} content",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityView() {
$entityTitle = $this
->randomString();
$entity = $this
->getEntityStorageHandler()
->create([
'type' => $this->entityBundleMachineName,
'title' => $entityTitle,
]);
$entity
->save();
$route = "entity.{$this->entityTypeMachineName}.canonical";
$routeArguments = [
$this->entityTypeMachineName => $entity
->id(),
];
$expectedUrl = "{$this->entityTypeMachineName}/{$entity->id()}";
$expectedTitle = $entityTitle;
$this->baseCrumbs = [
"Home",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle);
}
public function entityEdit() {
$entityTitle = $this
->randomString();
$entity = $this
->getEntityStorageHandler()
->create([
'type' => $this->entityBundleMachineName,
'title' => $entityTitle,
]);
$entity
->save();
$route = "entity.{$this->entityTypeMachineName}.edit_form";
$routeArguments = [
$this->entityTypeMachineName => $entity
->id(),
];
$expectedUrl = "{$this->entityTypeMachineName}/{$entity->id()}/edit";
$expectedTitle = "Edit {$this->entityBundleLabel} {$entityTitle}";
$this->baseCrumbs = [
'Home',
];
$crumbs = [
$entityTitle,
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityDelete() {
$entityTitle = $this
->randomString();
$entity_type_label = strtolower($this->entityTypeLabel);
$entity = $this
->getEntityStorageHandler()
->create([
'type' => $this->entityBundleMachineName,
'title' => $entityTitle,
]);
$entity
->save();
$route = "entity.{$this->entityTypeMachineName}.delete_form";
$routeArguments = [
$this->entityTypeMachineName => $entity
->id(),
];
$expectedUrl = "{$this->entityTypeMachineName}/{$entity->id()}/delete";
$expectedTitle = "Are you sure you want to delete the {$entity_type_label} {$entityTitle}?";
$this->baseCrumbs = [
'Home',
];
$crumbs = [
$entityTitle,
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityBundleList() {
$route = "eck.entity.{$this->entityTypeMachineName}_type.list";
$routeArguments = [];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/bundles";
$expectedTitle = ucfirst("{$this->entityTypeLabel} bundles");
$crumbs = [
'Structure',
"ECK Entity Types",
"Edit entity type",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityBundleAdd() {
$route = "eck.entity.{$this->entityTypeMachineName}_type.add";
$routeArguments = [];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/bundles/add";
$expectedTitle = "Add {$this->entityTypeLabel} bundle";
$crumbs = [
'Structure',
'ECK Entity Types',
"Edit entity type",
ucfirst("{$this->entityTypeLabel} bundles"),
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityBundleEdit() {
$route = "entity.{$this->entityTypeMachineName}_type.edit_form";
$routeArguments = [
"{$this->entityTypeMachineName}_type" => $this->entityBundleMachineName,
];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/bundles/{$this->entityBundleMachineName}";
$expectedTitle = "Edit {$this->entityTypeLabel} bundle";
$crumbs = [
'Structure',
'ECK Entity Types',
"Edit entity type",
ucfirst("{$this->entityTypeLabel} bundles"),
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
public function entityBundleDelete() {
$route = "entity.{$this->entityTypeMachineName}_type.delete_form";
$routeArguments = [
"{$this->entityTypeMachineName}_type" => $this->entityBundleMachineName,
];
$expectedUrl = "admin/structure/eck/{$this->entityTypeMachineName}/bundles/{$this->entityBundleMachineName}/delete";
$expectedTitle = "Are you sure you want to delete the entity bundle {$this->entityBundleLabel}?";
$crumbs = [
'Structure',
'ECK Entity Types',
"Edit entity type",
ucfirst("{$this->entityTypeLabel} bundles"),
"Edit {$this->entityTypeLabel} bundle",
];
$this
->assertCorrectPageOnRoute($route, $routeArguments, $expectedUrl, $expectedTitle, $crumbs);
}
}