View source
<?php
namespace Drupal\Tests\shortcut\Functional;
use Drupal\shortcut\Entity\ShortcutSet;
class ShortcutSetsTest extends ShortcutTestBase {
public static $modules = [
'block',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_actions_block');
}
public function testShortcutSetAdd() {
$this
->drupalGet('admin/config/user-interface/shortcut');
$this
->clickLink(t('Add shortcut set'));
$edit = [
'label' => $this
->randomMachineName(),
'id' => strtolower($this
->randomMachineName()),
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$new_set = $this->container
->get('entity_type.manager')
->getStorage('shortcut_set')
->load($edit['id']);
$this
->assertIdentical($new_set
->id(), $edit['id'], 'Successfully created a shortcut set.');
$this
->drupalGet('user/' . $this->adminUser
->id() . '/shortcuts');
$this
->assertText($new_set
->label(), 'Generated shortcut set was listed as a choice on the user account page.');
}
public function testShortcutSetEdit() {
$set = $this->set;
$shortcuts = $set
->getShortcuts();
$this
->drupalGet('admin/config/user-interface/shortcut/manage/' . $set
->id() . '/customize');
$this
->assertTitle('List links | Drupal');
$element = $this
->xpath('//div[@class="layout-content"]//table');
$this
->assertNotEmpty($element, 'Shortcut entity list table found.');
$elements = $this
->xpath('//div[@class="layout-content"]//table/thead/tr/th');
$this
->assertCount(3, $elements, 'Correct number of table header cells found.');
$expected_items = [
t('Name'),
t('Weight'),
t('Operations'),
];
foreach ($elements as $key => $element) {
$this
->assertEqual($element
->getText(), $expected_items[$key]);
}
$weight = count($shortcuts);
$edit = [];
foreach ($shortcuts as $shortcut) {
$title = $shortcut
->getTitle();
$this
->assertSession()
->linkExists($title);
$this
->assertFieldByName('shortcuts[links][' . $shortcut
->id() . '][weight]');
$edit['shortcuts[links][' . $shortcut
->id() . '][weight]'] = $weight;
$weight--;
}
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertRaw(t('The shortcut set has been updated.'));
\Drupal::entityTypeManager()
->getStorage('shortcut')
->resetCache();
$this
->assertIdentical(array_reverse(array_keys($shortcuts)), array_keys($set
->getShortcuts()));
}
public function testShortcutSetSwitchOwn() {
$new_set = $this
->generateShortcutSet($this
->randomMachineName());
$this
->drupalPostForm('user/' . $this->adminUser
->id() . '/shortcuts', [
'set' => $new_set
->id(),
], t('Change set'));
$this
->assertSession()
->statusCodeEquals(200);
$current_set = shortcut_current_displayed_set($this->adminUser);
$this
->assertTrue($new_set
->id() == $current_set
->id(), 'Successfully switched own shortcut set.');
}
public function testShortcutSetAssign() {
$new_set = $this
->generateShortcutSet($this
->randomMachineName());
\Drupal::entityTypeManager()
->getStorage('shortcut_set')
->assignUser($new_set, $this->shortcutUser);
$current_set = shortcut_current_displayed_set($this->shortcutUser);
$this
->assertTrue($new_set
->id() == $current_set
->id(), "Successfully switched another user's shortcut set.");
}
public function testShortcutSetSwitchCreate() {
$edit = [
'set' => 'new',
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
];
$this
->drupalPostForm('user/' . $this->adminUser
->id() . '/shortcuts', $edit, t('Change set'));
$current_set = shortcut_current_displayed_set($this->adminUser);
$this
->assertNotEqual($current_set
->id(), $this->set
->id(), 'A shortcut set can be switched to at the same time as it is created.');
$this
->assertEqual($current_set
->label(), $edit['label'], 'The new set is correctly assigned to the user.');
}
public function testShortcutSetSwitchNoSetName() {
$edit = [
'set' => 'new',
];
$this
->drupalPostForm('user/' . $this->adminUser
->id() . '/shortcuts', $edit, t('Change set'));
$this
->assertText(t('The new set label is required.'));
$current_set = shortcut_current_displayed_set($this->adminUser);
$this
->assertEqual($current_set
->id(), $this->set
->id(), 'Attempting to switch to a new shortcut set without providing a set name does not succeed.');
$this
->assertFieldByXPath("//input[@name='label' and contains(concat(' ', normalize-space(@class), ' '), ' error ')]", NULL, 'The new set label field has the error class');
}
public function testShortcutSetRename() {
$set = $this->set;
$new_label = $this
->randomMachineName();
$this
->drupalGet('admin/config/user-interface/shortcut');
$this
->clickLink(t('Edit shortcut set'));
$this
->drupalPostForm(NULL, [
'label' => $new_label,
], t('Save'));
$set = ShortcutSet::load($set
->id());
$this
->assertTrue($set
->label() == $new_label, 'Shortcut set has been successfully renamed.');
}
public function testShortcutSetUnassign() {
$new_set = $this
->generateShortcutSet($this
->randomMachineName());
$shortcut_set_storage = \Drupal::entityTypeManager()
->getStorage('shortcut_set');
$shortcut_set_storage
->assignUser($new_set, $this->shortcutUser);
$shortcut_set_storage
->unassignUser($this->shortcutUser);
$current_set = shortcut_current_displayed_set($this->shortcutUser);
$default_set = shortcut_default_set($this->shortcutUser);
$this
->assertTrue($current_set
->id() == $default_set
->id(), "Successfully unassigned another user's shortcut set.");
}
public function testShortcutSetDelete() {
$new_set = $this
->generateShortcutSet($this
->randomMachineName());
$this
->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $new_set
->id() . '/delete', [], t('Delete'));
$sets = ShortcutSet::loadMultiple();
$this
->assertFalse(isset($sets[$new_set
->id()]), 'Successfully deleted a shortcut set.');
}
public function testShortcutSetDeleteDefault() {
$this
->drupalGet('admin/config/user-interface/shortcut/manage/default/delete');
$this
->assertSession()
->statusCodeEquals(403);
}
public function testShortcutSetCreateWithSetName() {
$random_name = $this
->randomMachineName();
$new_set = $this
->generateShortcutSet($random_name, $random_name);
$sets = ShortcutSet::loadMultiple();
$this
->assertTrue(isset($sets[$random_name]), 'Successfully created a shortcut set with a defined set name.');
$this
->drupalGet('user/' . $this->adminUser
->id() . '/shortcuts');
$this
->assertText($new_set
->label(), 'Generated shortcut set was listed as a choice on the user account page.');
}
}