entity_type_access_conditions-1.0.1/tests/src/Kernel/TaxonomyAccessConditionsTest.php
tests/src/Kernel/TaxonomyAccessConditionsTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\entity_type_access_conditions\Kernel;
use Drupal\taxonomy\Entity\Vocabulary;
/**
* Tests access conditions for Taxonomy Vocabularies and Terms.
*
* @group entity_type_access_conditions
*/
class TaxonomyAccessConditionsTest extends EntityTypeAccessTestBase {
/**
* The vocabulary used for testing.
*
* @var \Drupal\taxonomy\Entity\Vocabulary
*/
protected $vocabulary;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create a vocabulary for testing.
$this->vocabulary = Vocabulary::create([
'vid' => 'test_vocab',
'name' => 'Test Vocabulary',
]);
$this->vocabulary->save();
$this->accessHandler = $this->container->get('entity_type.manager')->getAccessControlHandler('taxonomy_vocabulary');
$administer_perm = 'administer taxonomy';
$this->grantPermissionsToTestRole([$administer_perm]);
}
/**
* Tests access to the vocabulary edit form.
*/
public function testVocabularyEditAccess(): void {
// 1. Grant permission to administer taxonomy, condition not set.
$this->assertTrue($this->vocabulary->access('update', $this->testUser), 'User should have update access to vocabulary with permission.');
// 2. Apply condition, set to met.
$this->clearConditionsFromEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id());
$this->applyConditionToEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id(), 'test_condition', ['condition_met' => TRUE]);
$this->vocabulary = Vocabulary::load($this->vocabulary->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->vocabulary->access('update', $this->testUser), 'User should have update access to vocabulary when condition is met.');
// 3. Set condition to not met.
$this->clearConditionsFromEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id());
$this->applyConditionToEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id(), 'test_condition', ['condition_met' => FALSE]);
$this->vocabulary = Vocabulary::load($this->vocabulary->id());
$this->accessHandler->resetCache();
$this->assertFalse($this->vocabulary->access('update', $this->testUser), 'User should NOT have update access to vocabulary when condition is not met.');
// 4. Clear conditions.
$this->clearConditionsFromEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id());
$this->vocabulary = Vocabulary::load($this->vocabulary->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->vocabulary->access('update', $this->testUser), 'User should have update access to vocabulary after conditions are cleared.');
}
/**
* Tests access to the vocabulary delete form.
*/
public function testVocabularyDeleteAccess(): void {
// 1. Grant permission to administer taxonomy, condition not set.
$this->assertTrue($this->vocabulary->access('delete', $this->testUser), 'User should have delete access to vocabulary with permission.');
// 2. Apply condition, set to met.
$this->clearConditionsFromEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id());
$this->applyConditionToEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id(), 'test_condition', ['condition_met' => TRUE]);
$this->vocabulary = Vocabulary::load($this->vocabulary->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->vocabulary->access('delete', $this->testUser), 'User should have delete access to vocabulary when condition is met.');
// 3. Set condition to not met.
$this->clearConditionsFromEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id());
$this->applyConditionToEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id(), 'test_condition', ['condition_met' => FALSE]);
$this->vocabulary = Vocabulary::load($this->vocabulary->id());
$this->accessHandler->resetCache();
$this->assertFalse($this->vocabulary->access('delete', $this->testUser), 'User should NOT have delete access to vocabulary when condition is not met.');
// 4. Clear conditions.
$this->clearConditionsFromEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id());
$this->vocabulary = Vocabulary::load($this->vocabulary->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->vocabulary->access('delete', $this->testUser), 'User should have delete access to vocabulary after conditions are cleared.');
}
/**
* Tests access to list terms for a vocabulary.
*/
public function testVocabularyListTermsAccess(): void {
$route_params = ['taxonomy_vocabulary' => $this->vocabulary->id()];
// 1. Grant permission to access taxonomy overview, condition not set.
$this->assertTrue($this->checkRouteAccess('entity.taxonomy_vocabulary.overview_form', $route_params)->isAllowed(), 'User should be able to list terms with permission.');
// 2. Apply condition to the vocabulary, set to met.
$this->clearConditionsFromEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id());
$this->applyConditionToEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id(), 'test_condition', ['condition_met' => TRUE]);
$this->vocabulary = Vocabulary::load($this->vocabulary->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->checkRouteAccess('entity.taxonomy_vocabulary.overview_form', $route_params)->isAllowed(), 'User should be able to list terms when condition is met.');
// 3. Set condition to not met.
$this->clearConditionsFromEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id());
$this->applyConditionToEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id(), 'test_condition', ['condition_met' => FALSE]);
$this->vocabulary = Vocabulary::load($this->vocabulary->id());
$this->accessHandler->resetCache();
$this->assertFalse($this->checkRouteAccess('entity.taxonomy_vocabulary.overview_form', $route_params)->isAllowed(), 'User should NOT be able to list terms when condition is not met.');
// 4. Clear conditions.
$this->clearConditionsFromEntityTypeConfig('taxonomy_vocabulary', $this->vocabulary->id());
$this->vocabulary = Vocabulary::load($this->vocabulary->id());
$this->accessHandler->resetCache();
$this->assertTrue($this->checkRouteAccess('entity.taxonomy_vocabulary.overview_form', $route_params)->isAllowed(), 'User should be able to list terms after conditions are cleared.');
}
}
