View source
<?php
namespace Drupal\Tests\Core\Menu;
use Drupal\Core\Menu\LocalTaskDefault;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;
class LocalTaskDefaultTest extends UnitTestCase {
protected $localTaskBase;
protected $config = array();
protected $pluginId = 'local_task_default';
protected $pluginDefinition = array(
'id' => 'local_task_default',
);
protected $stringTranslation;
protected $routeProvider;
protected function setUp() {
parent::setUp();
$this->stringTranslation = $this
->getMock('Drupal\\Core\\StringTranslation\\TranslationInterface');
$this->routeProvider = $this
->getMock('Drupal\\Core\\Routing\\RouteProviderInterface');
}
protected function setupLocalTaskDefault() {
$this->localTaskBase = new TestLocalTaskDefault($this->config, $this->pluginId, $this->pluginDefinition);
$this->localTaskBase
->setRouteProvider($this->routeProvider);
}
public function testGetRouteParametersForStaticRoute() {
$this->pluginDefinition = array(
'route_name' => 'test_route',
);
$this->routeProvider
->expects($this
->once())
->method('getRouteByName')
->with('test_route')
->will($this
->returnValue(new Route('/test-route')));
$this
->setupLocalTaskDefault();
$route_match = new RouteMatch('', new Route('/'));
$this
->assertEquals(array(), $this->localTaskBase
->getRouteParameters($route_match));
}
public function testGetRouteParametersInPluginDefinitions() {
$this->pluginDefinition = array(
'route_name' => 'test_route',
'route_parameters' => array(
'parameter' => 'example',
),
);
$this->routeProvider
->expects($this
->once())
->method('getRouteByName')
->with('test_route')
->will($this
->returnValue(new Route('/test-route/{parameter}')));
$this
->setupLocalTaskDefault();
$route_match = new RouteMatch('', new Route('/'));
$this
->assertEquals(array(
'parameter' => 'example',
), $this->localTaskBase
->getRouteParameters($route_match));
}
public function testGetRouteParametersForDynamicRouteWithNonUpcastedParameters() {
$this->pluginDefinition = array(
'route_name' => 'test_route',
);
$route = new Route('/test-route/{parameter}');
$this->routeProvider
->expects($this
->once())
->method('getRouteByName')
->with('test_route')
->will($this
->returnValue($route));
$this
->setupLocalTaskDefault();
$route_match = new RouteMatch('', $route, array(), array(
'parameter' => 'example',
));
$this
->assertEquals(array(
'parameter' => 'example',
), $this->localTaskBase
->getRouteParameters($route_match));
}
public function testGetRouteParametersForDynamicRouteWithUpcastedParameters() {
$this->pluginDefinition = array(
'route_name' => 'test_route',
);
$route = new Route('/test-route/{parameter}');
$this->routeProvider
->expects($this
->once())
->method('getRouteByName')
->with('test_route')
->will($this
->returnValue($route));
$this
->setupLocalTaskDefault();
$route_match = new RouteMatch('', $route, array(
'parameter' => (object) 'example2',
), array(
'parameter' => 'example',
));
$this
->assertEquals(array(
'parameter' => 'example',
), $this->localTaskBase
->getRouteParameters($route_match));
}
public function providerTestGetWeight() {
return array(
array(
array(
'weight' => 314,
),
'test_id',
314,
),
array(
array(
'base_route' => 'local_task_default',
'route_name' => 'local_task_default',
'id' => 'local_task_default',
),
'local_task_default',
-10,
),
array(
array(
'base_route' => 'local_task_example',
'route_name' => 'local_task_other',
'id' => 'local_task_default',
),
'local_task_default',
0,
),
array(
array(
'base_route' => 'local_task_example',
'id' => 'local_task_derivative_default:example_id',
'route_name' => 'local_task_example',
),
'local_task_derivative_default:example_id',
-10,
),
);
}
public function testGetWeight($plugin_definition, $plugin_id, $expected_weight) {
$this->pluginDefinition = $plugin_definition;
$this->pluginId = $plugin_id;
$this
->setupLocalTaskDefault();
$this
->assertEquals($expected_weight, $this->localTaskBase
->getWeight());
}
public function testActive() {
$this
->setupLocalTaskDefault();
$this
->assertFalse($this->localTaskBase
->getActive());
$this->localTaskBase
->setActive();
$this
->assertTrue($this->localTaskBase
->getActive());
}
public function testGetTitle() {
$this->pluginDefinition['title'] = new TranslatableMarkup('Example', [], [], $this->stringTranslation);
$this->stringTranslation
->expects($this
->once())
->method('translateString')
->with($this->pluginDefinition['title'])
->will($this
->returnValue('Example translated'));
$this
->setupLocalTaskDefault();
$this
->assertEquals('Example translated', $this->localTaskBase
->getTitle());
}
public function testGetTitleWithContext() {
$title = 'Example';
$this->pluginDefinition['title'] = new TranslatableMarkup($title, array(), array(
'context' => 'context',
), $this->stringTranslation);
$this->stringTranslation
->expects($this
->once())
->method('translateString')
->with($this->pluginDefinition['title'])
->will($this
->returnValue('Example translated with context'));
$this
->setupLocalTaskDefault();
$this
->assertEquals('Example translated with context', $this->localTaskBase
->getTitle());
}
public function testGetTitleWithTitleArguments() {
$this->pluginDefinition['title'] = new TranslatableMarkup('Example @test', array(
'@test' => 'value',
), [], $this->stringTranslation);
$this->stringTranslation
->expects($this
->once())
->method('translateString')
->with($this->pluginDefinition['title'])
->will($this
->returnValue('Example value'));
$this
->setupLocalTaskDefault();
$this
->assertEquals('Example value', $this->localTaskBase
->getTitle());
}
public function testGetOptions() {
$this->pluginDefinition['options'] = array(
'attributes' => array(
'class' => array(
'example',
),
),
);
$this
->setupLocalTaskDefault();
$route_match = new RouteMatch('', new Route('/'));
$this
->assertEquals($this->pluginDefinition['options'], $this->localTaskBase
->getOptions($route_match));
$this->localTaskBase
->setActive(TRUE);
$this
->assertEquals(array(
'attributes' => array(
'class' => array(
'example',
'is-active',
),
),
), $this->localTaskBase
->getOptions($route_match));
}
public function testCacheabilityMetadata() {
$this->pluginDefinition['cache_contexts'] = [
'route',
];
$this->pluginDefinition['cache_tags'] = [
'kitten',
];
$this->pluginDefinition['cache_max_age'] = 3600;
$this
->setupLocalTaskDefault();
$this
->assertEquals([
'route',
], $this->localTaskBase
->getCacheContexts());
$this
->assertEquals([
'kitten',
], $this->localTaskBase
->getCacheTags());
$this
->assertEquals(3600, $this->localTaskBase
->getCacheMaxAge());
}
}
class TestLocalTaskDefault extends LocalTaskDefault {
public function setRouteProvider(RouteProviderInterface $route_provider) {
$this->routeProvider = $route_provider;
return $this;
}
}