You are here

protected function ViewsFieldMenuTest::setUp in Workbench Access 8

Overrides BrowserTestBase::setUp

File

tests/src/Functional/ViewsFieldMenuTest.php, line 71

Class

ViewsFieldMenuTest
Defines a class for testing workbench access views.

Namespace

Drupal\Tests\workbench_access\Functional

Code

protected function setUp() {
  parent::setUp();

  // Create some sections and some nodes in them.
  $sections = [
    'Some section',
    'Another section',
    'More sections',
  ];
  foreach ($sections as $section) {
    $this->links[$section] = MenuLinkContent::create([
      'title' => $section,
      'link' => [
        [
          'uri' => 'route:<front>',
        ],
      ],
      'menu_name' => 'main',
    ]);
    $this->links[$section]
      ->save();
    foreach ([
      ' node 1',
      ' node 2',
    ] as $stub) {
      $title = $section . $stub;
      $this->nodes[$title] = Node::create([
        'type' => 'article',
        'title' => $title,
        'status' => 1,
      ]);
      $this->nodes[$title]
        ->save();
      _menu_ui_node_save($this->nodes[$title], [
        'title' => $title,
        'menu_name' => 'main',
        'description' => 'view bar',
        'parent' => $this->links[$section]
          ->getPluginId(),
      ]);
    }
  }

  // Create a user who can access content etc.
  $permissions = [
    'create article content',
    'edit any article content',
    'access content',
    'delete any article content',
    'administer nodes',
    'use workbench access',
    'access user profiles',
  ];
  $this->user = $this
    ->createUser($permissions);
  $user_storage = \Drupal::service('workbench_access.user_section_storage');
  $scheme_storage = \Drupal::service('entity_type.manager')
    ->getStorage('access_scheme');
  $scheme = $scheme_storage
    ->load('menu');
  $ids = array_values(array_map(function (MenuLinkContentInterface $link) {
    return $link
      ->getPluginId();
  }, $this->links));
  $user_storage
    ->addUser($scheme, $this->user, $ids);

  // Check data loading.
  $expected = sort($ids);
  $existing = $user_storage
    ->getUserSections($scheme, $this->user);
  $this
    ->assertEquals($expected, sort($existing));
  $this->user2 = $this
    ->createUser($permissions);
  $ids = [
    reset($this->links)
      ->getPluginId(),
  ];
  $user_storage
    ->addUser($scheme, $this->user2, $ids);

  // Check data loading.
  $expected = sort($ids);
  $existing = $user_storage
    ->getUserSections($scheme, $this->user2);
  $this
    ->assertEquals($expected, sort($existing));
}