ContextTest.php in Zircon Profile 8
File
core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php
View source
<?php
namespace Drupal\Tests\Core\Plugin\Context;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Container;
class ContextTest extends UnitTestCase {
protected $contextDefinition;
protected $typedDataManager;
protected $typedData;
public function setUp() {
parent::setUp();
$this->typedDataManager = $this
->getMockBuilder('Drupal\\Core\\TypedData\\TypedDataManager')
->disableOriginalConstructor()
->setMethods(array(
'create',
))
->getMock();
}
public function testDefaultValue() {
$this
->setUpDefaultValue('test');
$context = new Context($this->contextDefinition);
$context
->setTypedDataManager($this->typedDataManager);
$this
->assertEquals('test', $context
->getContextValue());
}
public function testDefaultDataValue() {
$this
->setUpDefaultValue('test');
$context = new Context($this->contextDefinition);
$context
->setTypedDataManager($this->typedDataManager);
$this
->assertEquals($this->typedData, $context
->getContextData());
}
public function testNullDataValue() {
$this
->setUpDefaultValue(NULL);
$context = new Context($this->contextDefinition);
$context
->setTypedDataManager($this->typedDataManager);
$this
->assertEquals($this->typedData, $context
->getContextData());
}
public function testSetContextValueTypedData() {
$this->contextDefinition = $this
->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')
->setMethods(array(
'getDefaultValue',
'getDataDefinition',
))
->getMockForAbstractClass();
$typed_data = $this
->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
$context = new Context($this->contextDefinition, $typed_data);
$this
->assertSame($typed_data, $context
->getContextData());
}
public function testSetContextValueCacheableDependency() {
$container = new Container();
$cache_context_manager = $this
->getMockBuilder('Drupal\\Core\\Cache\\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$container
->set('cache_contexts_manager', $cache_context_manager);
$cache_context_manager
->expects($this
->any())
->method('validateTokens')
->with([
'route',
])
->willReturn([
'route',
]);
\Drupal::setContainer($container);
$this->contextDefinition = $this
->getMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface');
$context = new Context($this->contextDefinition);
$context
->setTypedDataManager($this->typedDataManager);
$cacheable_dependency = $this
->getMock('Drupal\\Tests\\Core\\Plugin\\Context\\TypedDataCacheableDependencyInterface');
$cacheable_dependency
->expects($this
->once())
->method('getCacheTags')
->willReturn([
'node:1',
]);
$cacheable_dependency
->expects($this
->once())
->method('getCacheContexts')
->willReturn([
'route',
]);
$cacheable_dependency
->expects($this
->once())
->method('getCacheMaxAge')
->willReturn(60);
$context = Context::createFromContext($context, $cacheable_dependency);
$this
->assertSame($cacheable_dependency, $context
->getContextData());
$this
->assertEquals([
'node:1',
], $context
->getCacheTags());
$this
->assertEquals([
'route',
], $context
->getCacheContexts());
$this
->assertEquals(60, $context
->getCacheMaxAge());
}
protected function setUpDefaultValue($default_value = NULL) {
$mock_data_definition = $this
->getMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
$this->contextDefinition = $this
->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')
->setMethods(array(
'getDefaultValue',
'getDataDefinition',
))
->getMockForAbstractClass();
$this->contextDefinition
->expects($this
->once())
->method('getDefaultValue')
->willReturn($default_value);
$this->contextDefinition
->expects($this
->once())
->method('getDataDefinition')
->willReturn($mock_data_definition);
$this->typedData = $this
->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
$this->typedDataManager
->expects($this
->once())
->method('create')
->with($mock_data_definition, $default_value)
->willReturn($this->typedData);
}
}
interface TypedDataCacheableDependencyInterface extends CacheableDependencyInterface, TypedDataInterface {
}