You are here

protected function EnforcedIntegrityTest::assertEntityDeleteProtected in Entity Reference Integrity 8

Check that the delete form of an entity is protected.

2 calls to EnforcedIntegrityTest::assertEntityDeleteProtected()
EnforcedIntegrityTest::testBlockIntegrity in modules/entity_reference_integrity_enforce/tests/src/Functional/EnforcedIntegrityTest.php
Test a typical config entity.
EnforcedIntegrityTest::testNodeIntegrity in modules/entity_reference_integrity_enforce/tests/src/Functional/EnforcedIntegrityTest.php
Test a typical content entity.

File

modules/entity_reference_integrity_enforce/tests/src/Functional/EnforcedIntegrityTest.php, line 67

Class

EnforcedIntegrityTest
Test enforcing referential integrity.

Namespace

Drupal\Tests\entity_reference_integrity_enforce\Functional

Code

protected function assertEntityDeleteProtected(EntityInterface $entity, $message) {
  $dependent = $this
    ->createDependentEntity($entity);
  $this
    ->drupalGet($entity
    ->toUrl('delete-form'));

  // Initially, deleting an entity should be fine if it's not enabled.
  $this
    ->assertSession()
    ->elementsCount('css', '.form-submit', 1);
  $this
    ->assertSession()
    ->elementNotExists('css', '.form-submit[disabled]');

  // Enable protection of the entity type.
  $this
    ->drupalPostForm('admin/config/content/entity-reference-integrity', [
    sprintf('enabled_entity_type_ids[%s]', $entity
      ->getEntityTypeId()) => TRUE,
  ], 'Save configuration');
  $this
    ->drupalGet($entity
    ->toUrl('delete-form'));

  // There should only be a single submit, which is also disabled.
  $this
    ->assertSession()
    ->elementNotExists('css', '.form-submit');
  $this
    ->assertSession()
    ->pageTextContains($dependent
    ->getEntityType()
    ->getLabel());
  $this
    ->assertSession()
    ->linkExists($dependent
    ->label());

  // Attempt to delete the entity and see what happens.
  $this
    ->assertDeleteThrowsException($entity, $message);

  // Make sure we can load the entity and it's still there.
  $reloaded_entity = \Drupal::entityTypeManager()
    ->getStorage($entity
    ->getEntityTypeId())
    ->load($entity
    ->id());
  $this
    ->assertNotNull($reloaded_entity
    ->label());
  $this
    ->assertEquals($entity
    ->label(), $reloaded_entity
    ->label());

  // Ensure deletes work after references have been cleaned up.
  $dependent
    ->delete();
  $entity
    ->delete();
}