You are here

public function LocalTasksTest::testPluginLocalTask in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Menu/LocalTasksTest.php \Drupal\system\Tests\Menu\LocalTasksTest::testPluginLocalTask()

Tests the plugin based local tasks.

File

core/modules/system/src/Tests/Menu/LocalTasksTest.php, line 106
Contains \Drupal\system\Tests\Menu\LocalTasksTest.

Class

LocalTasksTest
Tests local tasks derived from router and added/altered via hooks.

Namespace

Drupal\system\Tests\Menu

Code

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
    ->assertLocalTaskAppers($title);
  $title = Html::escape("<script>alert('Welcome to the derived jungle!')</script>");
  $this
    ->assertLocalTaskAppers($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
    ->assertLocalTaskAppers($title);

  // Ensure the view tab is active.
  $result = $this
    ->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]/a');
  $this
    ->assertEqual(1, count($result), 'There is just a single active tab.');
  $this
    ->assertEqual('View', (string) $result[0], 'The view tab is active.');

  // 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);
  $result = $this
    ->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]/a');
  $this
    ->assertEqual(1, count($result), 'There is just a single active tab.');
  $this
    ->assertEqual('Settings', (string) $result[0], 'The settings tab is active.');
  $this
    ->drupalGet(Url::fromRoute('menu_test.local_task_test_tasks_settings_sub1'));
  $this
    ->assertLocalTasks($sub_tasks, 1);
  $result = $this
    ->xpath('//ul[contains(@class, "tabs")]//a[contains(@class, "active")]');
  $this
    ->assertEqual(2, count($result), 'There are tabs active on both levels.');
  $this
    ->assertEqual('Settings', (string) $result[0], 'The settings tab is active.');
  $this
    ->assertEqual('Dynamic title for TestTasksSettingsSub1', (string) $result[1], 'The sub1 tab is active.');
  $this
    ->assertCacheTag('kittens:ragdoll');
  $this
    ->assertCacheTag('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
    ->assertEqual(2, count($result), 'There are tabs active on both levels.');
  $this
    ->assertEqual('Settings', (string) $result[0]->a, 'The settings tab is active.');
  $this
    ->assertEqual('Derive 1', (string) $result[1]->a, '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
    ->assertEqual($definitions['menu_test.local_task_test_tasks_view']['provider'], 'menu_test');
  $this
    ->assertEqual($definitions['menu_test.local_task_test_tasks_edit']['provider'], 'menu_test');
  $this
    ->assertEqual($definitions['menu_test.local_task_test_tasks_settings']['provider'], 'menu_test');
  $this
    ->assertEqual($definitions['menu_test.local_task_test_tasks_settings_sub1']['provider'], 'menu_test');
  $this
    ->assertEqual($definitions['menu_test.local_task_test_tasks_settings_sub2']['provider'], 'menu_test');
  $this
    ->assertEqual($definitions['menu_test.local_task_test_tasks_settings_sub3']['provider'], 'menu_test');

  // Test that we we correctly apply the active class to tabs where one of the
  // request attributes is upcast to an entity object.
  $entity = \Drupal::entityManager()
    ->getStorage('entity_test')
    ->create(array(
    '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);
  $result = $this
    ->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]');
  $this
    ->assertEqual(1, count($result), 'There is one active tab.');
  $this
    ->assertEqual('upcasting sub1', (string) $result[0]->a, 'The "upcasting sub1" tab is active.');
  $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);
  $result = $this
    ->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]');
  $this
    ->assertEqual(1, count($result), 'There is one active tab.');
  $this
    ->assertEqual('upcasting sub2', (string) $result[0]->a, 'The "upcasting sub2" tab is active.');
}