View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
class TermAccessTest extends TaxonomyTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected $defaultTheme = 'stark';
public function testTermAccess() {
$assert_session = $this
->assertSession();
$vocabulary = $this
->createVocabulary();
$published_term = Term::create([
'vid' => $vocabulary
->id(),
'name' => 'Published term',
'status' => 1,
]);
$published_term
->save();
$unpublished_term = Term::create([
'vid' => $vocabulary
->id(),
'name' => 'Unpublished term',
'status' => 0,
]);
$unpublished_term
->save();
$this
->drupalLogin($this
->drupalCreateUser([
'administer taxonomy',
]));
$this
->drupalGet('taxonomy/term/' . $published_term
->id());
$assert_session
->statusCodeEquals(200);
$this
->assertTermAccess($published_term, 'view', TRUE);
$this
->drupalGet('taxonomy/term/' . $unpublished_term
->id());
$assert_session
->statusCodeEquals(200);
$this
->assertTermAccess($unpublished_term, 'view', TRUE);
$this
->drupalGet('taxonomy/term/' . $published_term
->id() . '/edit');
$assert_session
->statusCodeEquals(200);
$this
->assertTermAccess($published_term, 'update', TRUE);
$this
->drupalGet('taxonomy/term/' . $unpublished_term
->id() . '/edit');
$assert_session
->statusCodeEquals(200);
$this
->assertTermAccess($unpublished_term, 'update', TRUE);
$this
->drupalGet('taxonomy/term/' . $published_term
->id() . '/delete');
$assert_session
->statusCodeEquals(200);
$this
->assertTermAccess($published_term, 'delete', TRUE);
$this
->drupalGet('taxonomy/term/' . $unpublished_term
->id() . '/delete');
$assert_session
->statusCodeEquals(200);
$this
->assertTermAccess($unpublished_term, 'delete', TRUE);
$this
->drupalLogin($this
->drupalCreateUser([
'access content',
]));
$this
->drupalGet('taxonomy/term/' . $published_term
->id());
$assert_session
->statusCodeEquals(200);
$this
->assertTermAccess($published_term, 'view', TRUE);
$this
->drupalGet('taxonomy/term/' . $unpublished_term
->id());
$assert_session
->statusCodeEquals(403);
$this
->assertTermAccess($unpublished_term, 'view', FALSE, "The 'access content' permission is required and the taxonomy term must be published.");
$this
->drupalGet('taxonomy/term/' . $published_term
->id() . '/edit');
$assert_session
->statusCodeEquals(403);
$this
->assertTermAccess($published_term, 'update', FALSE, "The following permissions are required: 'edit terms in {$vocabulary->id()}' OR 'administer taxonomy'.");
$this
->drupalGet('taxonomy/term/' . $unpublished_term
->id() . '/edit');
$assert_session
->statusCodeEquals(403);
$this
->assertTermAccess($unpublished_term, 'update', FALSE, "The following permissions are required: 'edit terms in {$vocabulary->id()}' OR 'administer taxonomy'.");
$this
->drupalGet('taxonomy/term/' . $published_term
->id() . '/delete');
$assert_session
->statusCodeEquals(403);
$this
->assertTermAccess($published_term, 'delete', FALSE, "The following permissions are required: 'delete terms in {$vocabulary->id()}' OR 'administer taxonomy'.");
$this
->drupalGet('taxonomy/term/' . $unpublished_term
->id() . '/delete');
$assert_session
->statusCodeEquals(403);
$this
->assertTermAccess($unpublished_term, 'delete', FALSE, "The following permissions are required: 'delete terms in {$vocabulary->id()}' OR 'administer taxonomy'.");
\Drupal::service('module_installer')
->install([
'views',
], TRUE);
$this
->rebuildContainer();
$this
->drupalGet('taxonomy/term/' . $published_term
->id());
$assert_session
->statusCodeEquals(200);
$this
->drupalGet('taxonomy/term/' . $unpublished_term
->id());
$assert_session
->statusCodeEquals(404);
}
protected function assertTermAccess(TermInterface $term, string $access_operation, bool $access_allowed, string $access_reason = '') : void {
$access_result = $term
->access($access_operation, NULL, TRUE);
$this
->assertSame($access_allowed, $access_result
->isAllowed());
if ($access_reason) {
$this
->assertSame($access_reason, $access_result
->getReason());
}
}
}