1e3196ceb4d1fe113606082e5fb37e8b715d5d92
[yaffs-website] / web / core / modules / shortcut / src / Tests / ShortcutTestBase.php
1 <?php
2
3 namespace Drupal\shortcut\Tests;
4
5 use Drupal\shortcut\Entity\Shortcut;
6 use Drupal\shortcut\Entity\ShortcutSet;
7 use Drupal\shortcut\ShortcutSetInterface;
8 use Drupal\simpletest\WebTestBase;
9
10 /**
11  * Defines base class for shortcut test cases.
12  */
13 abstract class ShortcutTestBase extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['node', 'toolbar', 'shortcut'];
21
22   /**
23    * User with permission to administer shortcuts.
24    *
25    * @var \Drupal\user\UserInterface
26    */
27   protected $adminUser;
28
29   /**
30    * User with permission to use shortcuts, but not administer them.
31    *
32    * @var \Drupal\user\UserInterface
33    */
34   protected $shortcutUser;
35
36   /**
37    * Generic node used for testing.
38    *
39    * @var \Drupal\node\NodeInterface
40    */
41   protected $node;
42
43   /**
44    * Site-wide default shortcut set.
45    *
46    * @var \Drupal\shortcut\ShortcutSetInterface
47    */
48   protected $set;
49
50   protected function setUp() {
51     parent::setUp();
52
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']);
57
58       // Populate the default shortcut set.
59       $shortcut = Shortcut::create([
60         'shortcut_set' => 'default',
61         'title' => t('Add content'),
62         'weight' => -20,
63         'link' => [
64           'uri' => 'internal:/node/add',
65         ],
66       ]);
67       $shortcut->save();
68
69       $shortcut = Shortcut::create([
70         'shortcut_set' => 'default',
71         'title' => t('All content'),
72         'weight' => -19,
73         'link' => [
74           'uri' => 'internal:/admin/content',
75         ],
76       ]);
77       $shortcut->save();
78     }
79
80     // Create users.
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']);
83
84     // Create a node.
85     $this->node = $this->drupalCreateNode(['type' => 'article']);
86
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);
91   }
92
93   /**
94    * Creates a generic shortcut set.
95    */
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,
100     ]);
101     $set->save();
102     return $set;
103   }
104
105   /**
106    * Extracts information from shortcut set links.
107    *
108    * @param \Drupal\shortcut\ShortcutSetInterface $set
109    *   The shortcut set object to extract information from.
110    * @param string $key
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.
115    *
116    * @return array
117    *   Array of the requested information from each link.
118    */
119   public function getShortcutInformation(ShortcutSetInterface $set, $key) {
120     $info = [];
121     \Drupal::entityManager()->getStorage('shortcut')->resetCache();
122     foreach ($set->getShortcuts() as $shortcut) {
123       if ($key == 'link') {
124         $info[] = $shortcut->link->uri;
125       }
126       else {
127         $info[] = $shortcut->{$key}->value;
128       }
129     }
130     return $info;
131   }
132
133 }