db backup prior to drupal security update
[yaffs-website] / web / core / modules / system / src / Tests / Menu / LocalActionTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Menu;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Url;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10  * Tests local actions derived from router and added/altered via hooks.
11  *
12  * @group Menu
13  */
14 class LocalActionTest extends WebTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var string[]
20    */
21   public static $modules = ['block', 'menu_test'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     $this->drupalPlaceBlock('local_actions_block');
30   }
31
32   /**
33    * Tests appearance of local actions.
34    */
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'],
45     ]);
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'],
50     ]);
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');
57     $config->save();
58     $this->drupalGet(Url::fromRoute('menu_test.local_action6'));
59     $this->assertLocalAction([
60       [Url::fromRoute('menu_test.local_action5'), 'New title'],
61     ]);
62   }
63
64   /**
65    * Asserts local actions in the page output.
66    *
67    * @param array $actions
68    *   A list of expected action link titles, keyed by the hrefs.
69    */
70   protected function assertLocalAction(array $actions) {
71     $elements = $this->xpath('//a[contains(@class, :class)]', [
72       ':class' => 'button-action',
73     ]);
74     $index = 0;
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]['href'], $url->toString());
84       $index++;
85     }
86   }
87
88 }