entity_type_access_conditions-1.0.1/tests/src/Kernel/NodeAccessConditionsTest.php
tests/src/Kernel/NodeAccessConditionsTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\entity_type_access_conditions\Kernel;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
/**
* Tests access conditions for Node entities.
*
* @group entity_type_access_conditions
*/
class NodeAccessConditionsTest extends EntityTypeAccessTestBase {
use ContentTypeCreationTrait;
/**
* The node type used for testing.
*
* @var \Drupal\node\Entity\NodeType
*/
protected $nodeType;
/**
* The access handler.
*
* @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
*/
protected $nodeAccessHandler;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create a node type for testing.
$this->nodeType = $this->createContentType(['type' => 'article']);
$this->accessHandler = $this->container->get('entity_type.manager')->getAccessControlHandler('node_type');
$this->nodeAccessHandler = $this->container->get('entity_type.manager')->getAccessControlHandler('node');
$administer_perm = 'administer content types';
$this->grantPermissionsToTestRole([$administer_perm]);
}
/**
* Tests access to the node add form.
*/
public function testNodeCreateAccess(): void {
$node_add_route_params = ['node_type' => $this->nodeType->id()];
$add_perm = 'create ' . $this->nodeType->id() . ' content';
// 1. Grant permission to create content, condition not set.
$this->grantPermissionsToTestRole([$add_perm, 'access content', 'access content overview']);
$this->assertTrue($this->checkRouteAccess('node.add', $node_add_route_params)->isAllowed(), 'User should be able to access node add form with permission.');
// 2. Apply condition, set to met.
$this->clearConditionsFromEntityTypeConfig('node_type', $this->nodeType->id());
$this->applyConditionToEntityTypeConfig('node_type', $this->nodeType->id(), 'test_condition', ['condition_met' => TRUE]);
$this->nodeType = NodeType::load($this->nodeType->id());
$this->nodeAccessHandler->resetCache();
$this->assertTrue($this->checkRouteAccess('node.add', $node_add_route_params)->isAllowed(), 'User should be able to access node add form when condition is met.');
// 3. Set condition to not met.
$this->clearConditionsFromEntityTypeConfig('node_type', $this->nodeType->id());
$this->applyConditionToEntityTypeConfig('node_type', $this->nodeType->id(), 'test_condition', ['condition_met' => FALSE]);
$this->nodeType = NodeType::load($this->nodeType->id());
$this->nodeAccessHandler->resetCache();
$this->assertFalse($this->checkRouteAccess('node.add', $node_add_route_params)->isAllowed(), 'User should NOT be able to access node add form when condition is not met.');
// 4. Clear conditions.
$this->clearConditionsFromEntityTypeConfig('node_type', $this->nodeType->id());
$this->nodeType = NodeType::load($this->nodeType->id());
$this->nodeAccessHandler->resetCache();
$this->assertTrue($this->checkRouteAccess('node.add', $node_add_route_params)->isAllowed(), 'User should be able to access node add form after conditions are cleared.');
}
/**
* Tests access to the node edit form.
*/
public function testNodeEditAccess(): void {
// 1. Update node type, condition not set.
$this->assertTrue($this->nodeType->access('update', $this->testUser), 'User should have update access to node types.');
// 2. Apply condition, set to met.
$this->clearConditionsFromEntityTypeConfig('node_type', $this->nodeType->id());
$this->applyConditionToEntityTypeConfig('node_type', $this->nodeType->id(), 'test_condition', ['condition_met' => TRUE]);
$this->nodeType = NodeType::load($this->nodeType->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->nodeType->access('update', $this->testUser), 'User should have update access when condition is met.');
// 3. Set condition to not met.
$this->clearConditionsFromEntityTypeConfig('node_type', $this->nodeType->id());
$this->applyConditionToEntityTypeConfig('node_type', $this->nodeType->id(), 'test_condition', ['condition_met' => FALSE]);
$this->nodeType = NodeType::load($this->nodeType->id());
$this->accessHandler->resetCache();
$this->assertFalse($this->nodeType->access('update', $this->testUser), 'User should NOT have update access when condition is not met.');
// 4. Clear conditions.
$this->clearConditionsFromEntityTypeConfig('node_type', $this->nodeType->id());
$this->nodeType = NodeType::load($this->nodeType->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->nodeType->access('update', $this->testUser), 'User should have update access after conditions are cleared.');
}
/**
* Tests access to the node delete form.
*/
public function testNodeDeleteAccess(): void {
// 1. Delete node type, condition not set.
$this->assertTrue($this->nodeType->access('delete', $this->testUser), 'User should have delete access to node node types.');
// 2. Apply condition, set to met.
$this->clearConditionsFromEntityTypeConfig('node_type', $this->nodeType->id());
$this->applyConditionToEntityTypeConfig('node_type', $this->nodeType->id(), 'test_condition', ['condition_met' => TRUE]);
$this->nodeType = NodeType::load($this->nodeType->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->nodeType->access('delete', $this->testUser), 'User should have delete access when condition is met.');
// 3. Set condition to not met.
$this->clearConditionsFromEntityTypeConfig('node_type', $this->nodeType->id());
$this->applyConditionToEntityTypeConfig('node_type', $this->nodeType->id(), 'test_condition', ['condition_met' => FALSE]);
$this->nodeType = NodeType::load($this->nodeType->id());
$this->accessHandler->resetCache();
$this->assertFalse($this->nodeType->access('delete', $this->testUser), 'User should NOT have delete access when condition is not met.');
// 4. Clear conditions.
$this->clearConditionsFromEntityTypeConfig('node_type', $this->nodeType->id());
$this->nodeType = NodeType::load($this->nodeType->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->nodeType->access('delete', $this->testUser), 'User should have delete access after conditions are cleared.');
}
}
