View source
<?php
namespace Drupal\Tests\workbench_access\Functional;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\BrowserTestBase;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
use Drupal\Tests\workbench_access\Traits\WorkbenchAccessTestTrait;
class NodeFormTest extends BrowserTestBase {
use WorkbenchAccessTestTrait;
protected $defaultTheme = 'stable';
public static $modules = [
'workbench_access',
'node',
'taxonomy',
'options',
'user',
'system',
];
public function testNodeForm() {
$node_type = $this
->createContentType([
'type' => 'page',
]);
$vocab = $this
->setUpVocabulary();
$field = $this
->setUpTaxonomyFieldForEntityType('node', $node_type
->id(), $vocab
->id());
$this
->assertEqual($field
->getDefaultValueLiteral(), []);
$scheme = $this
->setUpTaxonomyScheme($node_type, $vocab);
$user_storage = \Drupal::service('workbench_access.user_section_storage');
$role_storage = \Drupal::service('workbench_access.role_section_storage');
$editor = $this
->setUpEditorUser();
$this
->drupalLogin($editor);
$staff_term = Term::create([
'vid' => $vocab
->id(),
'name' => 'Staff',
]);
$staff_term
->save();
$super_staff_term = Term::create([
'vid' => $vocab
->id(),
'name' => 'Super staff',
]);
$super_staff_term
->save();
$base_term = Term::create([
'vid' => $vocab
->id(),
'name' => 'Editor',
]);
$base_term
->save();
$user_storage
->addUser($scheme, $editor, [
$base_term
->id(),
]);
$expected = [
$editor
->id(),
];
$existing_users = $user_storage
->getEditors($scheme, $base_term
->id());
$this
->assertEquals($expected, array_keys($existing_users));
$staff_rid = $this
->createRole([], 'staff');
$super_staff_rid = $this
->createRole([], 'super_staff');
\Drupal::service('workbench_access.role_section_storage')
->addRole($scheme, $staff_rid, [
$staff_term
->id(),
]);
\Drupal::service('workbench_access.role_section_storage')
->addRole($scheme, $super_staff_rid, [
$super_staff_term
->id(),
]);
$web_assert = $this
->assertSession();
$this
->drupalGet('node/add/page');
$web_assert
->optionNotExists(WorkbenchAccessManagerInterface::FIELD_NAME, $staff_term
->getName());
$web_assert
->optionNotExists(WorkbenchAccessManagerInterface::FIELD_NAME, $super_staff_term
->getName());
$editor
->addRole($staff_rid);
$editor
->save();
$this
->drupalGet('node/add/page');
$web_assert
->optionExists(WorkbenchAccessManagerInterface::FIELD_NAME, $staff_term
->getName());
$this
->setFieldType('node', 'page', 'entity_reference_autocomplete');
$this
->drupalGet('node/add/page');
$field_name = WorkbenchAccessManagerInterface::FIELD_NAME . "[0][target_id]";
$this
->submitForm([
$field_name => 'garbage',
'title[0][value]' => 'Foo',
], 'Save');
$web_assert
->pageTextContains('There are no entities matching "garbage".');
$this
->submitForm([
$field_name => $super_staff_term
->label() . ' (' . $super_staff_term
->id() . ')',
'title[0][value]' => 'Foo',
], 'Save');
$web_assert
->pageTextContains('The referenced entity (taxonomy_term: 2) does not exist.');
$editor
->addRole($super_staff_rid);
$editor
->save();
$this
->submitForm([
$field_name => $super_staff_term
->label() . ' (' . $super_staff_term
->id() . ')',
'title[0][value]' => 'Foo',
], 'Save');
$web_assert
->pageTextNotContains('The referenced entity (taxonomy_term: 2) does not exist.');
$this
->setFieldType('node', 'page');
$this
->drupalGet('node/add/page');
$web_assert
->optionExists(WorkbenchAccessManagerInterface::FIELD_NAME, $staff_term
->getName());
$web_assert
->optionExists(WorkbenchAccessManagerInterface::FIELD_NAME, $super_staff_term
->getName());
$field
->setDefaultValue($staff_term
->id())
->save();
$this
->drupalGet('node/add/page');
$web_assert
->optionExists(WorkbenchAccessManagerInterface::FIELD_NAME, $staff_term
->getName());
$web_assert
->optionExists(WorkbenchAccessManagerInterface::FIELD_NAME, $super_staff_term
->getName());
}
}