You are here

public function EntityHierarchyWorkbenchAccessTest::testWorkbenchAccessIntegration in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 modules/entity_hierarchy_workbench_access/tests/src/Kernel/EntityHierarchyWorkbenchAccessTest.php \Drupal\Tests\entity_hierarchy_workbench_access\Kernel\EntityHierarchyWorkbenchAccessTest::testWorkbenchAccessIntegration()

Tests integration.

File

modules/entity_hierarchy_workbench_access/tests/src/Kernel/EntityHierarchyWorkbenchAccessTest.php, line 150

Class

EntityHierarchyWorkbenchAccessTest
Tests interaction between entity_hierarchy and workbench_access.

Namespace

Drupal\Tests\entity_hierarchy_workbench_access\Kernel

Code

public function testWorkbenchAccessIntegration() {

  // Get UID 1 out of the way.
  $root = $this
    ->createUser();
  $this->container
    ->get('account_switcher')
    ->switchTo($root);

  // Create a section.
  $section1 = Node::create([
    'type' => $this->parentNodeType
      ->id(),
    'title' => 'Section',
    self::BOOLEAN_FIELD => TRUE,
    'status' => TRUE,
  ]);
  $section1
    ->save();

  // With some children.
  $children_of_section1 = $this
    ->createChildEntities($section1
    ->id());

  // Make the last child also a section.
  $last_child = end($children_of_section1);
  $last_child->{self::BOOLEAN_FIELD} = TRUE;
  $last_child
    ->save();
  $grandchildren = $this
    ->createChildEntities($last_child
    ->id(), 1);

  // Check the tree labels.
  $tree = $this->scheme
    ->getAccessScheme()
    ->getTree();
  $this
    ->assertSame([
    1 => 'Section',
    6 => 'Child 5 (Section)',
  ], array_map(function ($item) {
    return $item['label'];
  }, $tree[self::BOOLEAN_FIELD . '_value']));

  // Create a different section.
  $section2 = Node::create([
    'type' => $this->parentNodeType
      ->id(),
    'title' => 'Section',
    self::BOOLEAN_FIELD => TRUE,
    'status' => TRUE,
  ]);
  $section2
    ->save();

  // With some children.
  $children_of_section2 = $this
    ->createChildEntities($section2
    ->id());

  // Create an editor.
  $editor1 = $this
    ->createUser([], [
    sprintf('create %s content', $this->childNodeType
      ->id()),
    sprintf('delete any %s content', $this->childNodeType
      ->id()),
    sprintf('edit any %s content', $this->childNodeType
      ->id()),
    sprintf('create %s content', $this->parentNodeType
      ->id()),
    sprintf('delete any %s content', $this->parentNodeType
      ->id()),
    sprintf('edit any %s content', $this->parentNodeType
      ->id()),
    'access content',
  ]);

  // Assign them to first section.
  $userSectionStorage = $this->container
    ->get('workbench_access.user_section_storage');
  $userSectionStorage
    ->addUser($this->scheme, $editor1, [
    $section1
      ->id(),
  ]);

  // They should be able to edit/delete from first section and children.
  // But not from different section and children.
  $allowed = array_merge([
    $section1,
  ], $children_of_section1, $grandchildren);
  $disallowed = array_merge([
    $section2,
  ], $children_of_section2);
  $this
    ->checkAccess($allowed, $disallowed, $editor1);

  // Now create a user with rights to a sub-section.
  $editor2 = $this
    ->createUser([], [
    sprintf('create %s content', $this->childNodeType
      ->id()),
    sprintf('delete any %s content', $this->childNodeType
      ->id()),
    sprintf('edit any %s content', $this->childNodeType
      ->id()),
    sprintf('create %s content', $this->parentNodeType
      ->id()),
    sprintf('delete any %s content', $this->parentNodeType
      ->id()),
    sprintf('edit any %s content', $this->parentNodeType
      ->id()),
    'access content',
  ]);

  // Assign them to child section.
  $userSectionStorage
    ->addUser($this->scheme, $editor2, [
    $last_child
      ->id(),
  ]);
  $allowed = [
    $last_child,
    reset($grandchildren),
  ];
  array_pop($children_of_section1);
  $disallowed = array_merge($disallowed, $children_of_section1, [
    $section1,
  ]);
  $this
    ->checkAccess($allowed, $disallowed, $editor2);

  // Try to create a node with no section.
  $node = Node::create([
    'type' => $this->childNodeType
      ->id(),
    'title' => 'A new child',
  ]);
  $this
    ->assertEmpty($node
    ->validate());
  $config = $this->container
    ->get('config.factory')
    ->getEditable('workbench_access.settings');
  $config
    ->set('deny_on_empty', TRUE);
  $config
    ->save();
  $this
    ->assertNotEmpty($node
    ->validate());
}