View source
<?php
namespace Drupal\Tests\workbench_access\Kernel;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\KernelTests\KernelTestBase;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\workbench_access\Traits\WorkbenchAccessTestTrait;
use Drupal\workbench_access\Entity\AccessScheme;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
class SectionTokenTest extends KernelTestBase {
use ContentTypeCreationTrait;
use NodeCreationTrait;
use UserCreationTrait;
use WorkbenchAccessTestTrait;
protected $menu;
protected $vocabulary;
protected $taxonomyScheme;
protected $menuScheme;
protected $userStorage;
protected static $modules = [
'node',
'link',
'menu_link_content',
'menu_ui',
'text',
'system',
'user',
'workbench_access',
'field',
'filter',
'taxonomy',
'options',
];
protected function setUp() {
parent::setUp();
$this
->installConfig([
'filter',
'node',
'workbench_access',
'system',
]);
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installEntitySchema('menu_link_content');
$this
->installEntitySchema('section_association');
$this
->installSchema('system', [
'key_value',
'sequences',
]);
$this->vocabulary = $this
->setUpVocabulary();
$node_type = $this
->setUpContentType();
$this
->setUpTaxonomyFieldForEntityType('node', $node_type
->id(), $this->vocabulary
->id(), WorkbenchAccessManagerInterface::FIELD_NAME, 'Section', $cardinality = 3);
$this->taxonomyScheme = $this
->setupTaxonomyScheme($node_type, $this->vocabulary);
$this->menu = Menu::load('main');
$node_type
->setThirdPartySetting('menu_ui', 'available_menus', [
'main',
]);
$node_type
->save();
$this->menuScheme = $this
->setupMenuScheme([
$node_type
->id(),
], [
'main',
], 'menu_section');
$this->userStorage = \Drupal::service('workbench_access.user_section_storage');
}
public function testUserSectionTokens() {
$user = $this
->createUser();
$link = MenuLinkContent::create([
'title' => 'Test menu link',
'link' => [
[
'uri' => 'route:<front>',
],
],
'menu_name' => $this->menu
->id(),
]);
$link
->save();
$this->userStorage
->addUser($this->menuScheme, $user, [
$link
->getPluginId(),
]);
$tokens = [
'workbench-access-sections' => 'Test menu link',
];
$bubbleable_metadata = new BubbleableMetadata();
$this
->assertTokens('user', [
'user' => $user,
], $tokens, $bubbleable_metadata);
$this
->assertContains($this->menuScheme
->getCacheTags()[0], $bubbleable_metadata
->getCacheTags());
$term = Term::create([
'name' => 'Test term',
'vid' => $this->vocabulary
->id(),
]);
$term
->save();
$this->userStorage
->addUser($this->taxonomyScheme, $user, [
$term
->id(),
]);
$tokens = [
'workbench-access-sections' => 'Test term, Test menu link',
];
$this
->assertTokens('user', [
'user' => $user,
], $tokens, $bubbleable_metadata);
$this
->assertContains($this->taxonomyScheme
->getCacheTags()[0], $bubbleable_metadata
->getCacheTags());
$term = Term::create([
'name' => 'Test term 2',
'vid' => $this->vocabulary
->id(),
]);
$term
->save();
$this->userStorage
->addUser($this->taxonomyScheme, $user, [
$term
->id(),
]);
$tokens = [
'workbench-access-sections' => 'Test term, Test term 2, Test menu link',
];
$this
->assertTokens('user', [
'user' => $user,
], $tokens, $bubbleable_metadata);
$this
->setCurrentUser($user);
$this
->assertTokens('current-user', [], $tokens, $bubbleable_metadata);
}
public function testNodeSectionTokens() {
$node1 = $this
->createNode([
'type' => 'page',
'title' => 'foo',
]);
$tokens = [];
$bubbleable_metadata = new BubbleableMetadata();
$this
->assertTokens('node', [
'node' => $node1,
], $tokens, $bubbleable_metadata);
$term = Term::create([
'name' => 'Test term',
'vid' => $this->vocabulary
->id(),
]);
$term
->save();
$node2 = $this
->createNode([
'type' => 'page',
'title' => 'bar',
WorkbenchAccessManagerInterface::FIELD_NAME => $term
->id(),
]);
$tokens = [
'workbench-access-sections' => 'Test term',
];
$bubbleable_metadata = new BubbleableMetadata();
$this
->assertTokens('node', [
'node' => $node2,
], $tokens, $bubbleable_metadata);
$this
->assertContains($this->taxonomyScheme
->getCacheTags()[0], $bubbleable_metadata
->getCacheTags());
$term2 = Term::create([
'name' => 'Test term two',
'vid' => $this->vocabulary
->id(),
]);
$term2
->save();
$node3 = $this
->createNode([
'type' => 'page',
'title' => 'bar',
WorkbenchAccessManagerInterface::FIELD_NAME => [
$term
->id(),
$term2
->id(),
],
]);
$tokens = [
'workbench-access-sections' => 'Test term, Test term two',
];
$bubbleable_metadata = new BubbleableMetadata();
$this
->assertTokens('node', [
'node' => $node3,
], $tokens, $bubbleable_metadata);
$this
->assertContains($this->taxonomyScheme
->getCacheTags()[0], $bubbleable_metadata
->getCacheTags());
$link = MenuLinkContent::create([
'title' => 'Test menu link',
'link' => [
[
'uri' => 'route:<front>',
],
],
'menu_name' => $this->menu
->id(),
]);
$link
->save();
$node4 = $this
->createNode([
'type' => 'page',
'title' => 'bar',
]);
_menu_ui_node_save($node4, [
'title' => 'Menu test',
'menu_name' => 'main',
'description' => 'view bar',
'parent' => $link
->getPluginId(),
]);
$tokens = [
'workbench-access-sections' => 'Menu test',
];
$bubbleable_metadata = new BubbleableMetadata();
$this
->assertTokens('node', [
'node' => $node4,
], $tokens, $bubbleable_metadata);
$this
->assertContains($this->menuScheme
->getCacheTags()[0], $bubbleable_metadata
->getCacheTags());
$node5 = $this
->createNode([
'type' => 'page',
'title' => 'bar',
'field_workbench_access' => $term
->id(),
]);
_menu_ui_node_save($node5, [
'title' => 'Test another menu link',
'menu_name' => 'main',
'description' => 'view bar',
'parent' => $link
->getPluginId(),
]);
$tokens = [
'workbench-access-sections' => 'Test term, Test another menu link',
];
$this
->assertTokens('node', [
'node' => $node5,
], $tokens, $bubbleable_metadata);
$this
->assertContains($this->taxonomyScheme
->getCacheTags()[0], $bubbleable_metadata
->getCacheTags());
$this
->assertContains($this->menuScheme
->getCacheTags()[0], $bubbleable_metadata
->getCacheTags());
}
protected function assertTokens($type, array $data, array $tokens, BubbleableMetadata $bubbleable_metadata) {
$input = array_reduce(array_keys($tokens), function ($carry, $token) use ($type) {
$carry[$token] = "[{$type}:{$token}]";
return $carry;
}, []);
$replacements = \Drupal::token()
->generate($type, $input, $data, [], $bubbleable_metadata);
foreach ($tokens as $name => $expected) {
$token = $input[$name];
$this
->assertEquals($replacements[$token], $expected);
}
return $replacements;
}
}