View source
<?php
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class LocalActionTest extends BrowserTestBase {
public static $modules = [
'block',
'menu_test',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_actions_block');
}
public function testLocalAction() {
$this
->drupalGet('menu-test-local-action');
$this
->assertLocalAction([
[
Url::fromRoute('menu_test.local_action4'),
'My dynamic-title action',
],
[
Url::fromRoute('menu_test.local_action4'),
Html::escape("<script>alert('Welcome to the jungle!')</script>"),
],
[
Url::fromRoute('menu_test.local_action4'),
Html::escape("<script>alert('Welcome to the derived jungle!')</script>"),
],
[
Url::fromRoute('menu_test.local_action2'),
'My hook_menu action',
],
[
Url::fromRoute('menu_test.local_action3'),
'My YAML discovery action',
],
[
Url::fromRoute('menu_test.local_action5'),
'Title override',
],
]);
$this
->drupalGet(Url::fromRoute('menu_test.local_action6'));
$this
->assertLocalAction([
[
Url::fromRoute('menu_test.local_action5'),
'Original title',
],
]);
$header_values = explode(' ', $this
->drupalGetHeader('x-drupal-cache-tags'));
$this
->assertContains('config:menu_test.links.action', $header_values, "Found 'config:menu_test.links.action' cache tag in header");
$config = $this->container
->get('config.factory')
->getEditable('menu_test.links.action');
$config
->set('title', 'New title');
$config
->save();
$this
->drupalGet(Url::fromRoute('menu_test.local_action6'));
$this
->assertLocalAction([
[
Url::fromRoute('menu_test.local_action5'),
'New title',
],
]);
}
protected function assertLocalAction(array $actions) {
$elements = $this
->xpath('//a[contains(@class, :class)]', [
':class' => 'button-action',
]);
$index = 0;
foreach ($actions as $action) {
list($url, $title) = $action;
$this
->assertPattern('@<a [^>]*class="[^"]*button-action[^"]*"[^>]*>' . preg_quote($title, '@') . '</@');
$this
->assertEqual($elements[$index]
->getAttribute('href'), $url
->toString());
$index++;
}
}
}