function LocalTasksTest::testPluginLocalTask
Same name in other branches
- 8.9.x core/modules/system/tests/src/Functional/Menu/LocalTasksTest.php \Drupal\Tests\system\Functional\Menu\LocalTasksTest::testPluginLocalTask()
- 10 core/modules/system/tests/src/Functional/Menu/LocalTasksTest.php \Drupal\Tests\system\Functional\Menu\LocalTasksTest::testPluginLocalTask()
- 11.x core/modules/system/tests/src/Functional/Menu/LocalTasksTest.php \Drupal\Tests\system\Functional\Menu\LocalTasksTest::testPluginLocalTask()
Tests the plugin based local tasks.
File
-
core/
modules/ system/ tests/ src/ Functional/ Menu/ LocalTasksTest.php, line 102
Class
- LocalTasksTest
- Tests local tasks derived from router and added/altered via hooks.
Namespace
Drupal\Tests\system\Functional\MenuCode
public function testPluginLocalTask() {
// Verify local tasks defined in the hook.
$this->drupalGet(Url::fromRoute('menu_test.tasks_default'));
$this->assertLocalTasks([
[
'menu_test.tasks_default',
[],
],
[
'menu_test.router_test1',
[
'bar' => 'unsafe',
],
],
[
'menu_test.router_test1',
[
'bar' => '1',
],
],
[
'menu_test.router_test2',
[
'bar' => '2',
],
],
]);
// Verify that script tags are escaped on output.
$title = Html::escape("Task 1 <script>alert('Welcome to the jungle!')</script>");
$this->assertLocalTaskAppears($title);
$title = Html::escape("<script>alert('Welcome to the derived jungle!')</script>");
$this->assertLocalTaskAppears($title);
// Verify that local tasks appear as defined in the router.
$this->drupalGet(Url::fromRoute('menu_test.local_task_test_tasks_view'));
$this->assertLocalTasks([
[
'menu_test.local_task_test_tasks_view',
[],
],
[
'menu_test.local_task_test_tasks_edit',
[],
],
[
'menu_test.local_task_test_tasks_settings',
[],
],
[
'menu_test.local_task_test_tasks_settings_dynamic',
[],
],
]);
$title = Html::escape("<script>alert('Welcome to the jungle!')</script>");
$this->assertLocalTaskAppears($title);
// Ensure the view tab is active.
$this->assertSession()
->elementsCount('xpath', '//ul[contains(@class, "tabs")]//li[contains(@class, "active")]/a', 1);
$this->assertSession()
->elementTextEquals('xpath', '//ul[contains(@class, "tabs")]//li[contains(@class, "active")]/a', 'View(active tab)');
// Verify that local tasks in the second level appear.
$sub_tasks = [
[
'menu_test.local_task_test_tasks_settings_sub1',
[],
],
[
'menu_test.local_task_test_tasks_settings_sub2',
[],
],
[
'menu_test.local_task_test_tasks_settings_sub3',
[],
],
[
'menu_test.local_task_test_tasks_settings_derived',
[
'placeholder' => 'derive1',
],
],
[
'menu_test.local_task_test_tasks_settings_derived',
[
'placeholder' => 'derive2',
],
],
];
$this->drupalGet(Url::fromRoute('menu_test.local_task_test_tasks_settings'));
$this->assertLocalTasks($sub_tasks, 1);
$this->assertSession()
->elementsCount('xpath', '//ul[contains(@class, "tabs")]//li[contains(@class, "active")]/a', 1);
$this->assertSession()
->elementTextEquals('xpath', '//ul[contains(@class, "tabs")]//li[contains(@class, "active")]/a', 'Settings(active tab)');
$this->drupalGet(Url::fromRoute('menu_test.local_task_test_tasks_settings_sub1'));
$this->assertLocalTasks($sub_tasks, 1);
$xpath = '//ul[contains(@class, "tabs")]//a[contains(@class, "active")]';
$this->assertSession()
->elementsCount('xpath', $xpath, 2);
$links = $this->xpath($xpath);
$this->assertEquals('Settings(active tab)', $links[0]->getText(), 'The settings tab is active.');
$this->assertEquals('Dynamic title for TestTasksSettingsSub1(active tab)', $links[1]->getText(), 'The sub1 tab is active.');
$this->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'kittens:ragdoll');
$this->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'kittens:dwarf-cat');
$this->drupalGet(Url::fromRoute('menu_test.local_task_test_tasks_settings_derived', [
'placeholder' => 'derive1',
]));
$this->assertLocalTasks($sub_tasks, 1);
$result = $this->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]');
$this->assertCount(2, $result, 'There are tabs active on both levels.');
$this->assertEquals('Settings(active tab)', $result[0]->getText(), 'The settings tab is active.');
$this->assertEquals('Derive 1(active tab)', $result[1]->getText(), 'The derive1 tab is active.');
// Ensures that the local tasks contains the proper 'provider key'
$definitions = $this->container
->get('plugin.manager.menu.local_task')
->getDefinitions();
$this->assertEquals('menu_test', $definitions['menu_test.local_task_test_tasks_view']['provider']);
$this->assertEquals('menu_test', $definitions['menu_test.local_task_test_tasks_edit']['provider']);
$this->assertEquals('menu_test', $definitions['menu_test.local_task_test_tasks_settings']['provider']);
$this->assertEquals('menu_test', $definitions['menu_test.local_task_test_tasks_settings_sub1']['provider']);
$this->assertEquals('menu_test', $definitions['menu_test.local_task_test_tasks_settings_sub2']['provider']);
$this->assertEquals('menu_test', $definitions['menu_test.local_task_test_tasks_settings_sub3']['provider']);
// Test that we correctly apply the active class to tabs where one of the
// request attributes is upcast to an entity object.
$entity = \Drupal::entityTypeManager()->getStorage('entity_test')
->create([
'bundle' => 'test',
]);
$entity->save();
$this->drupalGet(Url::fromRoute('menu_test.local_task_test_upcasting_sub1', [
'entity_test' => '1',
]));
$tasks = [
[
'menu_test.local_task_test_upcasting_sub1',
[
'entity_test' => '1',
],
],
[
'menu_test.local_task_test_upcasting_sub2',
[
'entity_test' => '1',
],
],
];
$this->assertLocalTasks($tasks, 0);
$this->assertSession()
->elementsCount('xpath', '//ul[contains(@class, "tabs")]//li[contains(@class, "active")]', 1);
$this->assertSession()
->elementTextEquals('xpath', '//ul[contains(@class, "tabs")]//li[contains(@class, "active")]', 'upcasting sub1(active tab)');
$this->drupalGet(Url::fromRoute('menu_test.local_task_test_upcasting_sub2', [
'entity_test' => '1',
]));
$tasks = [
[
'menu_test.local_task_test_upcasting_sub1',
[
'entity_test' => '1',
],
],
[
'menu_test.local_task_test_upcasting_sub2',
[
'entity_test' => '1',
],
],
];
$this->assertLocalTasks($tasks, 0);
$this->assertSession()
->elementsCount('xpath', '//ul[contains(@class, "tabs")]//li[contains(@class, "active")]', 1);
$this->assertSession()
->elementTextEquals('xpath', '//ul[contains(@class, "tabs")]//li[contains(@class, "active")]', 'upcasting sub2(active tab)');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.