3 namespace Drupal\Tests\system\Functional\Menu;
5 use Drupal\Component\Utility\Html;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests local actions derived from router and added/altered via hooks.
14 class LocalActionTest extends BrowserTestBase {
21 public static $modules = ['block', 'menu_test'];
26 protected function setUp() {
29 $this->drupalPlaceBlock('local_actions_block');
33 * Tests appearance of local actions.
35 public function testLocalAction() {
36 $this->drupalGet('menu-test-local-action');
37 // Ensure that both menu and route based actions are shown.
38 $this->assertLocalAction([
39 [Url::fromRoute('menu_test.local_action4'), 'My dynamic-title action'],
40 [Url::fromRoute('menu_test.local_action4'), Html::escape("<script>alert('Welcome to the jungle!')</script>")],
41 [Url::fromRoute('menu_test.local_action4'), Html::escape("<script>alert('Welcome to the derived jungle!')</script>")],
42 [Url::fromRoute('menu_test.local_action2'), 'My hook_menu action'],
43 [Url::fromRoute('menu_test.local_action3'), 'My YAML discovery action'],
44 [Url::fromRoute('menu_test.local_action5'), 'Title override'],
46 // Test a local action title that changes based on a config value.
47 $this->drupalGet(Url::fromRoute('menu_test.local_action6'));
48 $this->assertLocalAction([
49 [Url::fromRoute('menu_test.local_action5'), 'Original title'],
51 // Verify the expected cache tag in the response headers.
52 $header_values = explode(' ', $this->drupalGetHeader('x-drupal-cache-tags'));
53 $this->assertTrue(in_array('config:menu_test.links.action', $header_values), "Found 'config:menu_test.links.action' cache tag in header");
54 /** @var \Drupal\Core\Config\Config $config */
55 $config = $this->container->get('config.factory')->getEditable('menu_test.links.action');
56 $config->set('title', 'New title');
58 $this->drupalGet(Url::fromRoute('menu_test.local_action6'));
59 $this->assertLocalAction([
60 [Url::fromRoute('menu_test.local_action5'), 'New title'],
65 * Asserts local actions in the page output.
67 * @param array $actions
68 * A list of expected action link titles, keyed by the hrefs.
70 protected function assertLocalAction(array $actions) {
71 $elements = $this->xpath('//a[contains(@class, :class)]', [
72 ':class' => 'button-action',
75 foreach ($actions as $action) {
76 /** @var \Drupal\Core\Url $url */
77 list($url, $title) = $action;
78 // SimpleXML gives us the unescaped text, not the actual escaped markup,
79 // so use a pattern instead to check the raw content.
80 // This behaviour is a bug in libxml, see
81 // https://bugs.php.net/bug.php?id=49437.
82 $this->assertPattern('@<a [^>]*class="[^"]*button-action[^"]*"[^>]*>' . preg_quote($title, '@') . '</@');
83 $this->assertEqual($elements[$index]->getAttribute('href'), $url->toString());