ShortcutSetsTest.php

Definition of Drupal\shortcut\Tests\ShortcutSetsTest.

Namespace

Drupal\shortcut\Tests

File

drupal/core/modules/shortcut/lib/Drupal/shortcut/Tests/ShortcutSetsTest.php
View source
<?php

/**
 * @file
 * Definition of Drupal\shortcut\Tests\ShortcutSetsTest.
 */
namespace Drupal\shortcut\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Defines shortcut set test cases.
 */
class ShortcutSetsTest extends ShortcutTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Shortcut set functionality',
      'description' => 'Create, view, edit, delete, and change shortcut sets.',
      'group' => 'Shortcut',
    );
  }

  /**
   * Tests creating a shortcut set.
   */
  function testShortcutSetAdd() {
    $new_set = $this
      ->generateShortcutSet($this
      ->randomName());
    $sets = entity_load_multiple('shortcut');
    $this
      ->assertTrue(isset($sets[$new_set
      ->id()]), 'Successfully created a shortcut set.');
    $this
      ->drupalGet('user/' . $this->admin_user->uid . '/shortcuts');
    $this
      ->assertText($new_set
      ->label(), 'Generated shortcut set was listed as a choice on the user account page.');
  }

  /**
   * Tests switching a user's own shortcut set.
   */
  function testShortcutSetSwitchOwn() {
    $new_set = $this
      ->generateShortcutSet($this
      ->randomName());

    // Attempt to switch the default shortcut set to the newly created shortcut
    // set.
    $this
      ->drupalPost('user/' . $this->admin_user->uid . '/shortcuts', array(
      'set' => $new_set
        ->id(),
    ), t('Change set'));
    $this
      ->assertResponse(200);
    $current_set = shortcut_current_displayed_set($this->admin_user);
    $this
      ->assertTrue($new_set
      ->id() == $current_set
      ->id(), 'Successfully switched own shortcut set.');
  }

  /**
   * Tests switching another user's shortcut set.
   */
  function testShortcutSetAssign() {
    $new_set = $this
      ->generateShortcutSet($this
      ->randomName());
    shortcut_set_assign_user($new_set, $this->shortcut_user);
    $current_set = shortcut_current_displayed_set($this->shortcut_user);
    $this
      ->assertTrue($new_set
      ->id() == $current_set
      ->id(), "Successfully switched another user's shortcut set.");
  }

  /**
   * Tests switching a user's shortcut set and creating one at the same time.
   */
  function testShortcutSetSwitchCreate() {
    $edit = array(
      'set' => 'new',
      'id' => strtolower($this
        ->randomName()),
      'label' => $this
        ->randomString(),
    );
    $this
      ->drupalPost('user/' . $this->admin_user->uid . '/shortcuts', $edit, t('Change set'));
    $current_set = shortcut_current_displayed_set($this->admin_user);
    $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.');
  }

  /**
   * Tests switching a user's shortcut set without providing a new set name.
   */
  function testShortcutSetSwitchNoSetName() {
    $edit = array(
      'set' => 'new',
    );
    $this
      ->drupalPost('user/' . $this->admin_user->uid . '/shortcuts', $edit, t('Change set'));
    $this
      ->assertText(t('The new set label is required.'));
    $current_set = shortcut_current_displayed_set($this->admin_user);
    $this
      ->assertEqual($current_set
      ->id(), $this->set
      ->id(), 'Attempting to switch to a new shortcut set without providing a set name does not succeed.');
  }

  /**
   * Tests that save() correctly updates existing links.
   */
  function testShortcutSetSave() {
    $set = $this->set;
    $old_mlids = $this
      ->getShortcutInformation($set, 'mlid');
    $menu_link = $this
      ->generateShortcutLink('admin', $this
      ->randomName());
    $menu_link
      ->save();
    $set->links[$menu_link
      ->uuid()] = $menu_link;
    $set
      ->save();
    $saved_set = shortcut_set_load($set
      ->id());
    $new_mlids = $this
      ->getShortcutInformation($saved_set, 'mlid');
    $this
      ->assertTrue(count(array_intersect($old_mlids, $new_mlids)) == count($old_mlids), 'Shortcut::save() did not inadvertently change existing mlids.');
  }

  /**
   * Tests renaming a shortcut set.
   */
  function testShortcutSetRename() {
    $set = $this->set;
    $new_label = $this
      ->randomName();
    $this
      ->drupalPost('admin/config/user-interface/shortcut/manage/' . $set
      ->id() . '/edit', array(
      'label' => $new_label,
    ), t('Save'));
    $set = shortcut_set_load($set
      ->id());
    $this
      ->assertTrue($set
      ->label() == $new_label, 'Shortcut set has been successfully renamed.');
  }

  /**
   * Tests renaming a shortcut set to the same name as another set.
   */
  function testShortcutSetRenameAlreadyExists() {
    $set = $this
      ->generateShortcutSet($this
      ->randomName());
    $existing_label = $this->set
      ->label();
    $this
      ->drupalPost('admin/config/user-interface/shortcut/manage/' . $set
      ->id() . '/edit', array(
      'label' => $existing_label,
    ), t('Save'));
    $this
      ->assertRaw(t('The shortcut set %name already exists. Choose another name.', array(
      '%name' => $existing_label,
    )));
    $set = shortcut_set_load($set
      ->id());
    $this
      ->assertNotEqual($set
      ->label(), $existing_label, format_string('The shortcut set %title cannot be renamed to %new-title because a shortcut set with that title already exists.', array(
      '%title' => $set
        ->label(),
      '%new-title' => $existing_label,
    )));
  }

  /**
   * Tests unassigning a shortcut set.
   */
  function testShortcutSetUnassign() {
    $new_set = $this
      ->generateShortcutSet($this
      ->randomName());
    shortcut_set_assign_user($new_set, $this->shortcut_user);
    shortcut_set_unassign_user($this->shortcut_user);
    $current_set = shortcut_current_displayed_set($this->shortcut_user);
    $default_set = shortcut_default_set($this->shortcut_user);
    $this
      ->assertTrue($current_set
      ->id() == $default_set
      ->id(), "Successfully unassigned another user's shortcut set.");
  }

  /**
   * Tests deleting a shortcut set.
   */
  function testShortcutSetDelete() {
    $new_set = $this
      ->generateShortcutSet($this
      ->randomName());
    $this
      ->drupalPost('admin/config/user-interface/shortcut/manage/' . $new_set
      ->id() . '/delete', array(), t('Delete'));
    $sets = entity_load_multiple('shortcut');
    $this
      ->assertFalse(isset($sets[$new_set
      ->id()]), 'Successfully deleted a shortcut set.');
  }

  /**
   * Tests deleting the default shortcut set.
   */
  function testShortcutSetDeleteDefault() {
    $this
      ->drupalGet('admin/config/user-interface/shortcut/manage/default/delete');
    $this
      ->assertResponse(403);
  }

  /**
   * Tests creating a new shortcut set with a defined set name.
   */
  function testShortcutSetCreateWithSetName() {
    $random_name = $this
      ->randomName();
    $new_set = $this
      ->generateShortcutSet($random_name, $random_name, TRUE);
    $sets = entity_load_multiple('shortcut');
    $this
      ->assertTrue(isset($sets[$random_name]), 'Successfully created a shortcut set with a defined set name.');
    $this
      ->drupalGet('user/' . $this->admin_user->uid . '/shortcuts');
    $this
      ->assertText($new_set
      ->label(), 'Generated shortcut set was listed as a choice on the user account page.');
  }

}

Classes

Namesort descending Description
ShortcutSetsTest Defines shortcut set test cases.