View source
<?php
namespace Drupal\Tests\eck\Functional;
use Drupal\Core\Url;
class LocalTaskEntityTest extends FunctionalTestBase {
protected static $modules = [
'node',
'block',
];
protected $entityTypeInfo;
protected $bundleInfo;
public function setUp() {
parent::setUp();
$this->entityTypeInfo = $this
->createEntityType();
$this->bundleInfo = $this
->createEntityBundle($this->entityTypeInfo['id']);
$this
->drupalPlaceBlock('local_tasks_block');
}
public function testLocalTask() {
$edit['title[0][value]'] = $this
->randomMachineName();
$route_args = [
'eck_entity_type' => $this->entityTypeInfo['id'],
'eck_entity_bundle' => $this->bundleInfo['type'],
];
$this
->drupalGet(Url::fromRoute("eck.entity.add", $route_args));
$this
->submitForm($edit, 'Save');
$route_args = [
$this->entityTypeInfo['id'] => 1,
];
$this
->assertLocalTasksFor("entity.{$this->entityTypeInfo['id']}.canonical", $route_args);
$this
->assertLocalTasksFor("entity.{$this->entityTypeInfo['id']}.edit_form", $route_args);
$this
->assertLocalTasksFor("entity.{$this->entityTypeInfo['id']}.delete_form", $route_args);
}
protected function assertLocalTasksFor($route, array $routeArguments) {
$this
->drupalGet(Url::fromRoute($route, $routeArguments));
$this
->assertLocalTaskLinkRoute("entity.{$this->entityTypeInfo['id']}.canonical", $routeArguments, 'View');
$this
->assertLocalTaskLinkRoute("entity.{$this->entityTypeInfo['id']}.edit_form", $routeArguments, 'Edit');
$this
->assertLocalTaskLinkRoute("entity.{$this->entityTypeInfo['id']}.delete_form", $routeArguments, 'Delete');
}
protected function assertLocalTaskLinkRoute($route, array $route_args, $label) {
$url = Url::fromRoute($route, $route_args);
$links = $this
->xpath('//ul/li/a[contains(@href, :href) and normalize-space(text())=:label]', [
':href' => $url
->toString(),
':label' => $label,
]);
$this
->assertEquals(1, \count($links), $this
->t('Link with label %label found and its route is :route', [
':route' => $route,
'%label' => $label,
]));
}
}