function ShortcutLinksTest::testShortcutLinkAdd

Same name in other branches
  1. 8.9.x core/modules/shortcut/tests/src/Functional/ShortcutLinksTest.php \Drupal\Tests\shortcut\Functional\ShortcutLinksTest::testShortcutLinkAdd()
  2. 10 core/modules/shortcut/tests/src/Functional/ShortcutLinksTest.php \Drupal\Tests\shortcut\Functional\ShortcutLinksTest::testShortcutLinkAdd()
  3. 11.x 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->assertSession()
        ->pageTextContains('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->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link');
        $this->submitForm($form_data, 'Save');
        $this->assertSession()
            ->statusCodeEquals(200);
        $this->assertSession()
            ->pageTextContains('Added a shortcut for ' . $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->assertEquals($entity->link->uri, $loaded->link->uri);
        $this->assertEquals($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->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link');
    $this->submitForm($form_data, 'Save');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains("The path '/admin' is inaccessible.");
    $form_data = [
        'title[0][value]' => $title,
        'link[0][uri]' => '/node',
    ];
    $this->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link');
    $this->submitForm($form_data, '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->drupalGet('admin/config/user-interface/shortcut/add-set');
    $this->submitForm($edit, 'Save');
    $title = $this->randomMachineName();
    $form_data = [
        'title[0][value]' => $title,
        'link[0][uri]' => '/admin',
    ];
    $this->drupalGet('admin/config/user-interface/shortcut/manage/' . $edit['id'] . '/add-link');
    $this->submitForm($form_data, 'Save');
    $this->assertSession()
        ->statusCodeEquals(200);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.