View source
<?php
namespace Drupal\Tests\entity_reference_integrity_enforce\Kernel;
use Drupal\entity_reference_integrity_enforce\Plugin\Action\DeleteAction;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\user\Entity\User;
class DeleteActionTest extends KernelTestBase {
use EntityReferenceTestTrait;
protected static $modules = [
'system',
'field',
'user',
'node',
'entity_test',
'entity_reference_integrity',
'entity_reference_integrity_enforce',
];
protected $actionManager;
protected $dependencyManager;
protected $testUser;
protected $testNode;
protected $referencedNode;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this->actionManager = $this->container
->get('plugin.manager.action');
$this->dependencyManager = $this->container
->get('entity_reference_integrity.dependency_manager');
NodeType::create([
'type' => $node_type = mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
])
->save();
$this
->createEntityReferenceField('node', $node_type, 'test_reference_field', 'Test reference field', 'node');
$this->referencedNode = Node::create([
'title' => 'Node to delete',
'type' => $node_type,
]);
$this->referencedNode
->save();
$this->testNode = Node::create([
'title' => 'Referenced node',
'type' => $node_type,
'test_reference_field' => [
'entity' => $this->referencedNode,
],
]);
$this->testNode
->save();
\Drupal::configFactory()
->getEditable('entity_reference_integrity_enforce.settings')
->set('enabled_entity_type_ids', [
'node' => 'node',
])
->save();
$this->testUser = User::create([
'name' => 'Gerald',
]);
$this->testUser
->save();
\Drupal::service('current_user')
->setAccount($this->testUser);
}
public function testDeleteAction() {
$actions = $this->actionManager
->getDefinitions();
$this
->assertArrayHasKey('entity:delete_action:node', $actions);
$this
->assertEquals(DeleteAction::class, $actions['entity:delete_action:node']['class']);
$action = $this->actionManager
->createInstance('entity:delete_action:node');
$this
->assertTrue($this->dependencyManager
->hasDependents($this->referencedNode));
$this
->assertFalse($action
->access($this->referencedNode, $this->testUser));
$this->testNode->test_reference_field = [];
$this->testNode
->save();
$this
->assertFalse($this->dependencyManager
->hasDependents($this->referencedNode));
$this
->assertTrue($action
->access($this->referencedNode, $this->testUser));
}
}