3 namespace Drupal\shortcut\Tests;
5 use Drupal\shortcut\Entity\Shortcut;
6 use Drupal\shortcut\Entity\ShortcutSet;
7 use Drupal\shortcut\ShortcutSetInterface;
8 use Drupal\simpletest\WebTestBase;
11 * Defines base class for shortcut test cases.
13 abstract class ShortcutTestBase extends WebTestBase {
20 public static $modules = ['node', 'toolbar', 'shortcut'];
23 * User with permission to administer shortcuts.
25 * @var \Drupal\user\UserInterface
30 * User with permission to use shortcuts, but not administer them.
32 * @var \Drupal\user\UserInterface
34 protected $shortcutUser;
37 * Generic node used for testing.
39 * @var \Drupal\node\NodeInterface
44 * Site-wide default shortcut set.
46 * @var \Drupal\shortcut\ShortcutSetInterface
50 protected function setUp() {
53 if ($this->profile != 'standard') {
54 // Create Basic page and Article node types.
55 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
56 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
58 // Populate the default shortcut set.
59 $shortcut = Shortcut::create([
60 'shortcut_set' => 'default',
61 'title' => t('Add content'),
64 'uri' => 'internal:/node/add',
69 $shortcut = Shortcut::create([
70 'shortcut_set' => 'default',
71 'title' => t('All content'),
74 'uri' => 'internal:/admin/content',
81 $this->adminUser = $this->drupalCreateUser(['access toolbar', 'administer shortcuts', 'view the administration theme', 'create article content', 'create page content', 'access content overview', 'administer users', 'link to any page', 'edit any article content']);
82 $this->shortcutUser = $this->drupalCreateUser(['customize shortcut links', 'switch shortcut sets', 'access shortcuts', 'access content']);
85 $this->node = $this->drupalCreateNode(['type' => 'article']);
87 // Log in as admin and grab the default shortcut set.
88 $this->drupalLogin($this->adminUser);
89 $this->set = ShortcutSet::load('default');
90 \Drupal::entityManager()->getStorage('shortcut_set')->assignUser($this->set, $this->adminUser);
94 * Creates a generic shortcut set.
96 public function generateShortcutSet($label = '', $id = NULL) {
97 $set = ShortcutSet::create([
98 'id' => isset($id) ? $id : strtolower($this->randomMachineName()),
99 'label' => empty($label) ? $this->randomString() : $label,
106 * Extracts information from shortcut set links.
108 * @param \Drupal\shortcut\ShortcutSetInterface $set
109 * The shortcut set object to extract information from.
111 * The array key indicating what information to extract from each link:
112 * - 'title': Extract shortcut titles.
113 * - 'link': Extract shortcut paths.
114 * - 'id': Extract the shortcut ID.
117 * Array of the requested information from each link.
119 public function getShortcutInformation(ShortcutSetInterface $set, $key) {
121 \Drupal::entityManager()->getStorage('shortcut')->resetCache();
122 foreach ($set->getShortcuts() as $shortcut) {
123 if ($key == 'link') {
124 $info[] = $shortcut->link->uri;
127 $info[] = $shortcut->{$key}->value;