View source
<?php
namespace Drupal\Tests\workbench_access\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Drupal\Tests\workbench_access\Traits\WorkbenchAccessTestTrait;
class TaxonomySchemeUITest extends BrowserTestBase {
use WorkbenchAccessTestTrait;
protected $defaultTheme = 'stable';
protected $admin;
protected $vocabulary;
public static $modules = [
'workbench_access',
'node',
'taxonomy',
'options',
'user',
'block',
'entity_test',
'system',
];
protected function setUp() {
parent::setUp();
$this
->createContentType([
'type' => 'page',
]);
$this
->createContentType([
'type' => 'article',
]);
$this->vocabulary = $this
->setUpVocabulary();
$this
->setUpTaxonomyFieldForEntityType('node', 'page', $this->vocabulary
->id());
$this
->setUpTaxonomyFieldForEntityType('taxonomy_term', $this->vocabulary
->id(), $this->vocabulary
->id(), 'recursive', 'Recursive Field');
$vocab = Vocabulary::create([
'vid' => 'selected',
'name' => 'Selected Vocabulary',
]);
$vocab
->save();
$this
->setUpTaxonomyFieldForEntityType('taxonomy_term', $vocab
->id(), $this->vocabulary
->id(), 'non_recursive', 'Allowed Field');
entity_test_create_bundle('access_controlled');
entity_test_create_bundle('notaccess_controlled');
$this
->setUpTaxonomyFieldForEntityType('entity_test', 'access_controlled', $this->vocabulary
->id());
$this->admin = $this
->setUpAdminUser([
'administer workbench access',
]);
$this
->placeBlock('local_actions_block');
}
public function testSchemeUi() {
$this
->assertThatUnprivilegedUsersCannotAccessAdminPages();
$scheme = $this
->assertCreatingAnAccessSchemeAsAdmin();
$this
->assertAdminCanSelectVocabularies($scheme);
$this
->assertAdminCanAddPageNodeTypeToScheme($scheme);
$this
->assertAdminCannotAddArticleNodeTypeToScheme($scheme);
$this
->assertAdminCanAddEntityTestAccessControlledBundleToScheme($scheme);
$this
->assertAdminCannotAddEntityTestAccessAccessControlledBundleToScheme($scheme);
$this
->assertAdminCannotAddUnselectedVocabulary($scheme);
$this
->assertAdminCannotAddRecursiveTaxonomy($scheme);
}
public function assertAdminCanSelectVocabularies(AccessSchemeInterface $scheme) {
$this
->drupalGet($scheme
->toUrl('edit-form'));
$this
->submitForm([
'scheme_settings[vocabularies][workbench_access]' => 1,
], 'Save');
$updated = $this
->loadUnchangedScheme($scheme
->id());
$this
->assertEquals([
'workbench_access',
], $updated
->getAccessScheme()
->getConfiguration()['vocabularies']);
}
protected function assertAdminCanAddPageNodeTypeToScheme(AccessSchemeInterface $scheme) {
$this
->drupalGet($scheme
->toUrl('edit-form'));
$this
->submitForm([
'scheme_settings[fields][node:page:field_workbench_access]' => 1,
], 'Save');
$updated = $this
->loadUnchangedScheme($scheme
->id());
$this
->assertTrue($updated
->getAccessScheme()
->applies('node', 'page'));
}
protected function assertAdminCannotAddArticleNodeTypeToScheme(AccessSchemeInterface $scheme) {
$this
->drupalGet($scheme
->toUrl('edit-form'));
$this
->assertSession()
->fieldNotExists('scheme_settings[fields][node:article:field_workbench_access]');
$this
->assertFalse($scheme
->getAccessScheme()
->applies('node', 'article'));
}
protected function assertAdminCanAddEntityTestAccessControlledBundleToScheme(AccessSchemeInterface $scheme) {
$this
->drupalGet($scheme
->toUrl('edit-form'));
$this
->submitForm([
'scheme_settings[fields][entity_test:access_controlled:field_workbench_access]' => 1,
], 'Save');
$updated = $this
->loadUnchangedScheme($scheme
->id());
$this
->assertTrue($updated
->getAccessScheme()
->applies('entity_test', 'access_controlled'));
}
protected function assertAdminCannotAddEntityTestAccessAccessControlledBundleToScheme(AccessSchemeInterface $scheme) {
$this
->drupalGet($scheme
->toUrl('edit-form'));
$this
->assertSession()
->fieldNotExists('scheme_settings[fields][entity_test:not_access_controlled:field_workbench_access]');
$this
->assertFalse($scheme
->getAccessScheme()
->applies('entity_test', 'not_access_controlled'));
}
protected function assertAdminCannotAddUnselectedVocabulary(AccessSchemeInterface $scheme) {
$this
->drupalGet($scheme
->toUrl('edit-form'));
$this
->submitForm([
'scheme_settings[vocabularies][workbench_access]' => 0,
'scheme_settings[vocabularies][selected]' => 1,
'scheme_settings[fields][entity_test:access_controlled:field_workbench_access]' => 1,
'scheme_settings[fields][node:page:field_workbench_access]' => 0,
], 'Save');
$this
->assertSession()
->pageTextContains('The field Section on entity_test entities of type access_controlled is not in the selected vocabularies.');
$this
->assertSession()
->pageTextNotContains('The field Section on node entities of type page is not in the selected vocabularies.');
}
protected function assertAdminCannotAddRecursiveTaxonomy(AccessSchemeInterface $scheme) {
$this
->drupalGet($scheme
->toUrl('edit-form'));
$this
->assertSession()
->pageTextContains('Allowed Field');
$this
->assertSession()
->pageTextNotContains('Recursive Field');
$this
->assertSession()
->pageTextNotContains('Term Parents');
}
}