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\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\workbench_access\Entity\AccessScheme;
class ViewsUserOutputTest extends BrowserTestBase {
protected $defaultTheme = 'stable';
protected $terms = [];
protected $links = [];
protected $nodes = [];
protected $user;
protected $user2;
protected $userStorage;
protected static $modules = [
'workbench_access',
'views',
'node',
'taxonomy',
'menu_link_content',
'menu_ui',
'system',
'user',
'filter',
'workbench_access_test',
];
protected $scheme;
protected function setUp() {
parent::setUp();
$sections = [
'Some section',
'Another section',
'More sections',
];
foreach ($sections as $section) {
$this->terms[$section] = Term::create([
'vid' => 'editorial_section',
'name' => $section . ' term',
]);
$this->terms[$section]
->save();
foreach ([
' node 1',
' node 2',
] as $stub) {
$title = $section . $stub;
$this->nodes[$title] = Node::create([
'type' => 'article',
'title' => $title,
'status' => 1,
'field_workbench_access' => $this->terms[$section],
]);
$this->nodes[$title]
->save();
}
}
$permissions = [
'create article content',
'edit any article content',
'access content',
'delete any article content',
'administer nodes',
'access user profiles',
'use workbench access',
];
$this->user = $this
->createUser($permissions);
$this->user
->save();
$this->userStorage = \Drupal::service('workbench_access.user_section_storage');
$this->scheme = AccessScheme::load('editorial_section');
$values = array_values(array_map(function (TermInterface $term) {
return $term
->id();
}, $this->terms));
$this->userStorage
->addUser($this->scheme, $this->user, $values);
$this->user2 = $this
->createUser($permissions);
$this->user2
->save();
$this->userStorage
->addUser($this->scheme, $this->user2, $values);
}
public function testFieldOutput() {
$this
->drupalLogin($this->user);
$this
->drupalGet('user-sections');
$assert = $this
->assertSession();
foreach ($this->terms as $section => $term) {
$row = $assert
->elementExists('css', '.views-row:contains("' . $this->user
->label() . '")');
$assert
->elementExists('css', '.views-row:contains("' . $section . '")', $row);
$this
->assertNoLinkByHref('/taxonomy/term/' . $term
->id());
}
$this
->drupalGet('user-sections-2');
foreach ($this->terms as $section => $term) {
$row = $assert
->elementExists('css', '.views-row:contains("' . $this->user
->label() . '")');
$assert
->elementExists('css', '.views-row:contains("' . $section . '")', $row);
$this
->assertLinkByHref('/taxonomy/term/' . $term
->id());
}
}
}