You are here

public function ShortcutLinksTest::testShortcutLinkAdd in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/shortcut/src/Tests/ShortcutLinksTest.php \Drupal\shortcut\Tests\ShortcutLinksTest::testShortcutLinkAdd()

Tests that creating a shortcut works properly.

File

core/modules/shortcut/src/Tests/ShortcutLinksTest.php, line 41
Contains \Drupal\shortcut\Tests\ShortcutLinksTest.

Class

ShortcutLinksTest
Create, view, edit, delete, and change shortcut links.

Namespace

Drupal\shortcut\Tests

Code

public function testShortcutLinkAdd() {
  $set = $this->set;

  // Create an alias for the node so we can test aliases.
  $path = array(
    'source' => '/node/' . $this->node
      ->id(),
    'alias' => '/' . $this
      ->randomMachineName(8),
  );
  $this->container
    ->get('path.alias_storage')
    ->save($path['source'], $path['alias']);

  // Create some paths to test.
  $test_cases = [
    '/',
    '/admin',
    '/admin/config/system/site-information',
    '/node/' . $this->node
      ->id() . '/edit',
    '/' . $path['alias'],
    '/router_test/test2',
    '/router_test/test3/value',
  ];
  $test_cases_non_access = [
    '/admin',
    '/admin/config/system/site-information',
  ];

  // Check that each new shortcut links where it should.
  foreach ($test_cases as $test_path) {
    $title = $this
      ->randomMachineName();
    $form_data = array(
      'title[0][value]' => $title,
      'link[0][uri]' => $test_path,
    );
    $this
      ->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set
      ->id() . '/add-link', $form_data, t('Save'));
    $this
      ->assertResponse(200);
    $this
      ->assertText(t('Added a shortcut for @title.', array(
      '@title' => $title,
    )));
    $saved_set = ShortcutSet::load($set
      ->id());
    $paths = $this
      ->getShortcutInformation($saved_set, 'link');
    $this
      ->assertTrue(in_array('internal:' . $test_path, $paths), 'Shortcut created: ' . $test_path);
    if (in_array($test_path, $test_cases_non_access)) {
      $this
        ->assertNoLink($title, SafeMarkup::format('Shortcut link %url not accessible on the page.', [
        '%url' => $test_path,
      ]));
    }
    else {
      $this
        ->assertLink($title, 0, SafeMarkup::format('Shortcut link %url found on the page.', [
        '%url' => $test_path,
      ]));
    }
  }
  $saved_set = ShortcutSet::load($set
    ->id());

  // Test that saving and re-loading a shortcut preserves its values.
  $shortcuts = $saved_set
    ->getShortcuts();
  foreach ($shortcuts as $entity) {

    // Test the node routes with parameters.
    $entity
      ->save();
    $loaded = Shortcut::load($entity
      ->id());
    $this
      ->assertEqual($entity->link->uri, $loaded->link->uri);
    $this
      ->assertEqual($entity->link->options, $loaded->link->options);
  }

  // Login as non admin user, to check that access is checked when creating
  // shortcuts.
  $this
    ->drupalLogin($this->shortcutUser);
  $title = $this
    ->randomMachineName();
  $form_data = [
    'title[0][value]' => $title,
    'link[0][uri]' => '/admin',
  ];
  $this
    ->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set
    ->id() . '/add-link', $form_data, t('Save'));
  $this
    ->assertResponse(200);
  $this
    ->assertRaw(t("The path '@link_path' is inaccessible.", [
    '@link_path' => '/admin',
  ]));
  $form_data = [
    'title[0][value]' => $title,
    'link[0][uri]' => '/node',
  ];
  $this
    ->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set
    ->id() . '/add-link', $form_data, t('Save'));
  $this
    ->assertLink($title, 0, 'Shortcut link found on the page.');

  // Create a new shortcut set and add a link to it.
  $this
    ->drupalLogin($this->adminUser);
  $edit = array(
    'label' => $this
      ->randomMachineName(),
    'id' => strtolower($this
      ->randomMachineName()),
  );
  $this
    ->drupalPostForm('admin/config/user-interface/shortcut/add-set', $edit, t('Save'));
  $title = $this
    ->randomMachineName();
  $form_data = [
    'title[0][value]' => $title,
    'link[0][uri]' => '/admin',
  ];
  $this
    ->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $edit['id'] . '/add-link', $form_data, t('Save'));
  $this
    ->assertResponse(200);
}