View source
<?php
namespace Drupal\Tests\entity_reference_integrity_enforce\Functional;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\entity_reference_integrity_enforce\Exception\ProtectedEntityException;
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
class EnforcedIntegrityTest extends BrowserTestBase {
protected static $modules = [
'node',
'entity_test',
'block',
'entity_reference_integrity_enforce',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this->rootUser);
}
public function testNodeIntegrity() {
$node = $this
->createNode([
'title' => 'foo',
]);
$this
->assertEntityDeleteProtected($node, 'Cannot delete "node" of type "page" with label "foo" and ID "1" because other content is referencing it and the integrity of this entity type is enforced.');
}
public function testBlockIntegrity() {
$block = $this
->placeBlock('system_branding_block', [
'id' => 'foo',
'label' => 'bar',
]);
$this
->assertEntityDeleteProtected($block, 'Cannot delete "block" of type "block" with label "bar" and ID "foo" because other content is referencing it and the integrity of this entity type is enforced.');
}
protected function assertEntityDeleteProtected(EntityInterface $entity, $message) {
$dependent = $this
->createDependentEntity($entity);
$this
->drupalGet($entity
->toUrl('delete-form'));
$this
->assertSession()
->elementsCount('css', '.form-submit', 1);
$this
->assertSession()
->elementNotExists('css', '.form-submit[disabled]');
$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'));
$this
->assertSession()
->elementNotExists('css', '.form-submit');
$this
->assertSession()
->pageTextContains($dependent
->getEntityType()
->getLabel());
$this
->assertSession()
->linkExists($dependent
->label());
$this
->assertDeleteThrowsException($entity, $message);
$reloaded_entity = \Drupal::entityTypeManager()
->getStorage($entity
->getEntityTypeId())
->load($entity
->id());
$this
->assertNotNull($reloaded_entity
->label());
$this
->assertEquals($entity
->label(), $reloaded_entity
->label());
$dependent
->delete();
$entity
->delete();
}
protected function assertDeleteThrowsException(EntityInterface $entity, $message) {
try {
$entity
->delete();
} catch (\Exception $e) {
if ($e instanceof EntityStorageException) {
$this
->assertInstanceOf(ProtectedEntityException::class, $e
->getPrevious());
$this
->assertEquals($message, $e
->getPrevious()
->getMessage());
}
else {
$this
->assertInstanceOf(ProtectedEntityException::class, $e);
$this
->assertEquals($message, $e
->getMessage());
}
return;
}
$this
->fail('An exception was not thrown when attempting to delete a protected entity.');
}
protected function createDependentEntity(EntityInterface $entity) {
entity_test_create_bundle($entity
->getEntityTypeId());
FieldStorageConfig::create([
'field_name' => 'test_reference_field',
'entity_type' => 'entity_test',
'type' => 'entity_reference',
'settings' => [
'target_type' => $entity
->getEntityTypeId(),
],
])
->save();
FieldConfig::create([
'field_name' => 'test_reference_field',
'entity_type' => 'entity_test',
'bundle' => $entity
->getEntityTypeId(),
'label' => 'Reference Field',
])
->save();
$dependent = EntityTest::create([
'name' => $this
->randomMachineName(),
'type' => $entity
->getEntityTypeId(),
'test_reference_field' => [
'entity' => $entity,
],
]);
$dependent
->save();
return $dependent;
}
}