View source
<?php
namespace Drupal\Tests\workbench_access\Functional;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\menu_link_content\MenuLinkContentInterface;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
class ViewsFieldMenuTest extends BrowserTestBase {
protected $defaultTheme = 'stable';
protected $links = [];
protected $nodes = [];
protected $user;
protected $user2;
protected static $modules = [
'workbench_access',
'views',
'node',
'taxonomy',
'menu_link_content',
'menu_ui',
'system',
'user',
'filter',
'workbench_access_test',
];
protected function setUp() {
parent::setUp();
$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(),
]);
}
}
$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);
$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);
$expected = sort($ids);
$existing = $user_storage
->getUserSections($scheme, $this->user2);
$this
->assertEquals($expected, sort($existing));
}
public function testFieldAndFilter() {
$this
->drupalLogin($this->user);
$this
->drupalGet('admin/content/sections/menu');
$assert = $this
->assertSession();
foreach ($this->links as $section => $link) {
$row = $assert
->elementExists('css', '.views-row:contains("' . $link
->label() . '")');
$assert
->pageTextContains($section . ' node 1');
}
$this
->drupalGet('admin/content/sections/menu', [
'query' => [
'workbench_access_section' => $this->links['Some section']
->getPluginId(),
],
]);
$assert
->pageTextContains('Some section node 1');
$assert
->pageTextContains('Some section node 2');
$assert
->elementNotExists('css', '.views-row:contains("Another section")');
$assert
->elementNotExists('css', '.views-row:contains("More sections")');
$this
->drupalGet('admin/people/sections/menu');
$row = $assert
->elementExists('css', '.views-row:contains("' . $this->user
->label() . '")');
foreach ($this->links as $section => $link) {
$assert
->elementExists('css', '.views-row:contains("' . $section . '")', $row);
}
$row = $assert
->elementExists('css', '.views-row:contains("' . $this->user2
->label() . '")');
$assert
->elementExists('css', '.views-row:contains("Some section")', $row);
$this
->drupalGet('admin/people/sections/menu', [
'query' => [
'section' => $this->links['Some section']
->getPluginId(),
],
]);
$assert
->elementExists('css', '.views-row:contains("' . $this->user
->label() . '")');
$assert
->elementExists('css', '.views-row:contains("' . $this->user2
->label() . '")');
$this
->drupalGet('admin/people/sections/menu', [
'query' => [
'section' => $this->links['Another section']
->getPluginId(),
],
]);
$assert
->elementExists('css', '.views-row:contains("' . $this->user
->label() . '")');
$assert
->elementNotExists('css', '.views-row:contains("' . $this->user2
->label() . '")');
$this
->drupalLogin($this->user2);
$this
->drupalGet('admin/content/sections/menu');
$assert
->pageTextContains('Some section node 1');
$assert
->pageTextContains('Some section node 2');
$assert
->elementNotExists('css', '.views-row:contains("Another section")');
$assert
->elementNotExists('css', '.views-row:contains("More sections")');
}
}