You are here

public function ShortcutLinksTest::testShortcutLinkAdd in Drupal 8

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

Tests that creating a shortcut works properly.

File

core/modules/shortcut/tests/src/Functional/ShortcutLinksTest.php, line 48

Class

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

Namespace

Drupal\Tests\shortcut\Functional

Code

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

  // Create an alias for the node so we can test aliases.
  $path_alias = $this
    ->createPathAlias('/node/' . $this->node
    ->id(), '/' . $this
    ->randomMachineName(8));

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

  // Test the add shortcut form UI. Test that the base field description is
  // there.
  $this
    ->drupalGet('admin/config/user-interface/shortcut/manage/' . $set
    ->id() . '/add-link');
  $this
    ->assertRaw('The location this shortcut points to.');

  // Check that each new shortcut links where it should.
  foreach ($test_cases as $test_path) {
    $title = $this
      ->randomMachineName();
    $form_data = [
      '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
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertText(t('Added a shortcut for @title.', [
      '@title' => $title,
    ]));
    $saved_set = ShortcutSet::load($set
      ->id());
    $paths = $this
      ->getShortcutInformation($saved_set, 'link');
    $this
      ->assertContains('internal:' . $test_path, $paths, 'Shortcut created: ' . $test_path);
    if (in_array($test_path, $test_cases_non_access)) {
      $this
        ->assertSession()
        ->linkNotExists($title, new FormattableMarkup('Shortcut link %url not accessible on the page.', [
        '%url' => $test_path,
      ]));
    }
    else {
      $this
        ->assertSession()
        ->linkExists($title, 0, new FormattableMarkup('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);
  }

  // Log in 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
    ->assertSession()
    ->statusCodeEquals(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
    ->assertSession()
    ->linkExists($title, 0, 'Shortcut link found on the page.');

  // Create a new shortcut set and add a link to it.
  $this
    ->drupalLogin($this->adminUser);
  $edit = [
    '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
    ->assertSession()
    ->statusCodeEquals(200);
}