Example #1
0
 /**
  * Tests that creating a shortcut works properly.
  */
 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->randomName(8));
     $this->container->get('path.alias_storage')->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->id() . '/edit'), array('path' => $path['alias']), array('path' => 'router_test/test2'), array('path' => 'router_test/test3/value'));
     // Check that each new shortcut links where it should.
     foreach ($test_cases as $test) {
         $title = $this->randomName();
         $form_data = array('title[0][value]' => $title, 'path' => $test['path']);
         $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
         $this->assertResponse(200);
         $saved_set = ShortcutSet::load($set->id());
         $paths = $this->getShortcutInformation($saved_set, 'path');
         $this->assertTrue(in_array($this->container->get('path.alias_manager')->getPathByAlias($test['path']), $paths), 'Shortcut created: ' . $test['path']);
         $this->assertLink($title, 0, 'Shortcut link found on the page.');
     }
     $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.
         if (strpos($entity->route_name->value, 'node.') === 0) {
             $entity->save();
             $loaded = Shortcut::load($entity->id());
             $this->assertEqual($entity->route_name->value, $loaded->route_name->value);
             $this->assertEqual($entity->get('route_parameters')->first()->getValue(), $loaded->get('route_parameters')->first()->getValue());
         }
     }
 }
 /**
  * Asserts various aspects of a shortcut entity.
  *
  * @param int $id
  *   The shortcut ID.
  * @param string $title
  *   The expected title of the shortcut.
  * @param int $weight
  *   The expected weight of the shortcut.
  * @param string $url
  *   The expected URL of the shortcut.
  */
 protected function assertEntity($id, $title, $weight, $url) {
   $shortcut = Shortcut::load($id);
   $this->assertTrue($shortcut instanceof ShortcutInterface);
   /** @var \Drupal\shortcut\ShortcutInterface $shortcut */
   $this->assertIdentical($title, $shortcut->getTitle());
   $this->assertIdentical($weight, $shortcut->getWeight());
   $this->assertIdentical($url, $shortcut->getUrl()->toString());
 }
 /**
  * Tests that creating a shortcut works properly.
  */
 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);
     }
     // 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->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);
 }
 /**
  * Tests that creating a shortcut works properly.
  */
 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 = array(array('path' => '', 'route_name' => '<front>'), array('path' => '<front>', 'route_name' => '<front>'), array('path' => 'admin', 'route_name' => 'system.admin'), array('path' => 'admin/config/system/site-information', 'route_name' => 'system.site_information_settings'), array('path' => 'node/' . $this->node->id() . '/edit', 'route_name' => 'entity.node.edit_form'), array('path' => $path['alias'], 'route_name' => 'entity.node.canonical'), array('path' => 'router_test/test2', 'route_name' => 'router_test.2'), array('path' => 'router_test/test3/value', 'route_name' => 'router_test.3'));
     // Check that each new shortcut links where it should.
     foreach ($test_cases as $test) {
         $title = $this->randomMachineName();
         $form_data = array('title[0][value]' => $title, 'path' => $test['path']);
         $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
         $this->assertResponse(200);
         $saved_set = ShortcutSet::load($set->id());
         $routes = $this->getShortcutInformation($saved_set, 'route_name');
         $this->assertTrue(in_array($test['route_name'], $routes), 'Shortcut created: ' . $test['path']);
         $this->assertLink($title, 0, 'Shortcut link found on the page.');
     }
     $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.
         if (strpos($entity->route_name->value, 'node.') === 0) {
             $entity->save();
             $loaded = Shortcut::load($entity->id());
             $this->assertEqual($entity->route_name->value, $loaded->route_name->value);
             $this->assertEqual($entity->get('route_parameters')->first()->getValue(), $loaded->get('route_parameters')->first()->getValue());
         }
     }
     // Login as non admin user, to check that access is checked when creating
     // shortcuts.
     $this->drupalLogin($this->shortcut_user);
     $title = $this->randomMachineName();
     $form_data = ['title[0][value]' => $title, 'path' => 'admin'];
     $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
     $this->assertResponse(200);
     $this->assertRaw(t('The shortcut must correspond to a valid path on the site.'));
     $form_data = ['title[0][value]' => $title, 'path' => '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.');
 }