View source
<?php
namespace Drupal\Tests\shortcut\Functional;
use Drupal\shortcut\Entity\Shortcut;
use Drupal\shortcut\Entity\ShortcutSet;
use Drupal\shortcut\ShortcutSetInterface;
use Drupal\Tests\BrowserTestBase;
abstract class ShortcutTestBase extends BrowserTestBase {
protected static $modules = [
'node',
'toolbar',
'shortcut',
];
protected $adminUser;
protected $shortcutUser;
protected $node;
protected $set;
protected function setUp() : void {
parent::setUp();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$shortcut = Shortcut::create([
'shortcut_set' => 'default',
'title' => t('Add content'),
'weight' => -20,
'link' => [
'uri' => 'internal:/node/add',
],
]);
$shortcut
->save();
$shortcut = Shortcut::create([
'shortcut_set' => 'default',
'title' => t('All content'),
'weight' => -19,
'link' => [
'uri' => 'internal:/admin/content',
],
]);
$shortcut
->save();
}
$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',
]);
$this->shortcutUser = $this
->drupalCreateUser([
'customize shortcut links',
'switch shortcut sets',
'access shortcuts',
'access content',
]);
$this->node = $this
->drupalCreateNode([
'type' => 'article',
]);
$this
->drupalLogin($this->adminUser);
$this->set = ShortcutSet::load('default');
\Drupal::entityTypeManager()
->getStorage('shortcut_set')
->assignUser($this->set, $this->adminUser);
}
public function generateShortcutSet($label = '', $id = NULL) {
$set = ShortcutSet::create([
'id' => $id ?? strtolower($this
->randomMachineName()),
'label' => empty($label) ? $this
->randomString() : $label,
]);
$set
->save();
return $set;
}
public function getShortcutInformation(ShortcutSetInterface $set, $key) {
$info = [];
\Drupal::entityTypeManager()
->getStorage('shortcut')
->resetCache();
foreach ($set
->getShortcuts() as $shortcut) {
if ($key == 'link') {
$info[] = $shortcut->link->uri;
}
else {
$info[] = $shortcut->{$key}->value;
}
}
return $info;
}
}