function ShortcutLinksTest::testShortcutLinkAdd

Tests that creating a shortcut works properly.

File

drupal/core/modules/shortcut/lib/Drupal/shortcut/Tests/ShortcutLinksTest.php, line 26
Definition of Drupal\shortcut\Tests\ShortcutLinksTest.

Class

ShortcutLinksTest
Defines shortcut links test cases.

Namespace

Drupal\shortcut\Tests

Code

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

  // Create an alias for the node so we can test aliases.
  $path = array(
    'source' => 'node/' . $this->node->nid,
    'alias' => $this
      ->randomName(8),
  );
  drupal_container()
    ->get('path.crud')
    ->save($path['source'], $path['alias']);

  // Create some paths to test.
  $test_cases = array(
    array(
      'path' => '',
    ),
    array(
      'path' => 'admin',
    ),
    array(
      'path' => 'admin/config/system/site-information',
    ),
    array(
      'path' => "node/{$this->node->nid}/edit",
    ),
    array(
      'path' => $path['alias'],
    ),
  );

  // Check that each new shortcut links where it should.
  foreach ($test_cases as $test) {
    $title = $this
      ->randomName();
    $form_data = array(
      'shortcut_link[link_title]' => $title,
      'shortcut_link[link_path]' => $test['path'],
    );
    $this
      ->drupalPost('admin/config/user-interface/shortcut/manage/' . $set
      ->id() . '/add-link', $form_data, t('Save'));
    $this
      ->assertResponse(200);
    $saved_set = shortcut_set_load($set
      ->id());
    $paths = $this
      ->getShortcutInformation($saved_set, 'link_path');
    $test_path = empty($test['path']) ? '<front>' : $test['path'];
    $this
      ->assertTrue(in_array(drupal_container()
      ->get('path.alias_manager')
      ->getSystemPath($test_path), $paths), 'Shortcut created: ' . $test['path']);
    $this
      ->assertLink($title, 0, 'Shortcut link found on the page.');
  }
}