diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 066afcc953b3f42d55ff86d5a5cb9a46283e42c6..b7294ad65ffe2d811d408bd69f71c97bebc588ab 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -311,7 +311,8 @@ function shortcut_preprocess_page_title(&$variables) { // Replicate template_preprocess_html()'s processing to get the title in // string form, so we can set the default name for the shortcut. - $name = render($variables['title']); + // Strip HTML tags from the title. + $name = trim(strip_tags(render($variables['title']))); $query = array( 'link' => $link, 'name' => $name, diff --git a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php index c6efe4134f047731f91290f9856b58cdf5649f0c..679f89bbecdc338ce2e9bb2ce63bb1318ef325c1 100644 --- a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php @@ -2,7 +2,9 @@ namespace Drupal\shortcut\Tests; +use Drupal\block_content\Entity\BlockContentType; use Drupal\Component\Utility\SafeMarkup; +use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Url; use Drupal\shortcut\Entity\Shortcut; use Drupal\shortcut\Entity\ShortcutSet; @@ -161,18 +163,12 @@ public function testShortcutQuickLink() { // Test the "Add to shortcuts" link for a page generated by views. $this->clickLink('Add to Default shortcuts'); $this->assertText('Added a shortcut for People.'); - // Due to the structure of the markup in the link ::assertLink() doesn't - // works here. - $link = $this->xpath('//a[normalize-space()=:label]', array(':label' => 'Remove from Default shortcuts')); - $this->assertTrue(!empty($link), 'Link Remove from Default shortcuts found.'); + $this->assertShortcutQuickLink('Remove from Default shortcuts'); // Test the "Remove from shortcuts" link for a page generated by views. $this->clickLink('Remove from Default shortcuts'); $this->assertText('The shortcut People has been deleted.'); - // Due to the structure of the markup in the link ::assertLink() doesn't - // works here. - $link = $this->xpath('//a[normalize-space()=:label]', array(':label' => 'Add to Default shortcuts')); - $this->assertTrue(!empty($link), 'Link Add to Default shortcuts found.'); + $this->assertShortcutQuickLink('Add to Default shortcuts'); // Test two pages which use same route name but different route parameters. $this->drupalGet('node/add/page'); @@ -186,6 +182,37 @@ public function testShortcutQuickLink() { // Add Shortcut for Article. $this->clickLink('Add to Default shortcuts'); $this->assertText('Added a shortcut for Create Article.'); + + $this->config('system.theme')->set('default', 'seven')->save(); + $this->drupalGet('node/' . $this->node->id()); + $title = $this->node->getTitle(); + + // Test the "Add to shortcuts" link for node view route. + $this->clickLink('Add to Default shortcuts'); + $this->assertText(new FormattableMarkup('Added a shortcut for @title.', ['@title' => $title])); + $this->assertShortcutQuickLink('Remove from Default shortcuts'); + + // Test the "Remove from shortcuts" link for node view route. + $this->clickLink('Remove from Default shortcuts'); + $this->assertText(new FormattableMarkup('The shortcut @title has been deleted.', ['@title' => $title])); + $this->assertShortcutQuickLink('Add to Default shortcuts'); + + \Drupal::service('module_installer')->install(['block_content']); + BlockContentType::create(array( + 'id' => 'basic', + 'label' => 'Basic block', + 'revision' => FALSE, + ))->save(); + // Test page with HTML tags in title. + $this->drupalGet('admin/structure/block/block-content/manage/basic'); + $page_title = new FormattableMarkup('Edit %label custom block type', ['%label' => 'Basic block']); + $this->assertRaw($page_title); + // Add shortcut to this page. + $this->clickLink('Add to Default shortcuts'); + $this->assertRaw(new FormattableMarkup('Added a shortcut for %title.', [ + '%title' => trim(strip_tags($page_title)), + ])); + } /** @@ -404,4 +431,32 @@ public function testShortcutBlockAccess() { $this->assertNoBlockAppears($block); } + /** + * Passes if a shortcut quick link with the specified label is found. + * + * An optional link index may be passed. + * + * @param string $label + * Text between the anchor tags. + * @param int $index + * Link position counting from zero. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages: use format_string() to embed variables in the message text, not + * t(). If left blank, a default message will be displayed. + * @param string $group + * (optional) The group this message is in, which is displayed in a column + * in test output. Use 'Debug' to indicate this is debugging output. Do not + * translate this string. Defaults to 'Other'; most tests do not override + * this default. + * + * @return bool + * TRUE if the assertion succeeded, FALSE otherwise. + */ + protected function assertShortcutQuickLink($label, $index = 0, $message = '', $group = 'Other') { + $links = $this->xpath('//a[normalize-space()=:label]', array(':label' => $label)); + $message = ($message ? $message : SafeMarkup::format('Shortcut quick link with label %label found.', array('%label' => $label))); + return $this->assert(isset($links[$index]), $message, $group); + } + }