You are here

public function NavigationalStructureTest::entityTypeDeleteWithContent in Entity Construction Kit (ECK) 8

Tests entity type deletion if content is available.

@test

Throws

\Drupal\Core\Entity\EntityStorageException

\Exception

File

tests/src/Functional/NavigationalStructureTest.php, line 383

Class

NavigationalStructureTest
Test Entity Construction Kit's navigational structure.

Namespace

Drupal\Tests\eck\Functional

Code

public function entityTypeDeleteWithContent() {
  $field_machine_name = strtolower($this
    ->randomMachineName());

  // Create a field.
  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();

  // Create an entity.
  \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.");

  // Create a second entity.
  \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.");
}