PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php

http://github.com/drupal/drupal
PHP | 172 lines | 92 code | 30 blank | 50 comment | 0 complexity | 341eabc9e4fc7cad50a553a55b2d26d0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\Plugin\Context\ContextTest.
  5. */
  6. namespace Drupal\Tests\Core\Plugin\Context;
  7. use Drupal\Core\Cache\CacheableDependencyInterface;
  8. use Drupal\Core\Plugin\Context\Context;
  9. use Drupal\Core\TypedData\TypedDataInterface;
  10. use Drupal\Core\TypedData\TypedDataManagerInterface;
  11. use Drupal\Tests\UnitTestCase;
  12. use Symfony\Component\DependencyInjection\Container;
  13. /**
  14. * @coversDefaultClass \Drupal\Core\Plugin\Context\Context
  15. * @group Plugin
  16. */
  17. class ContextTest extends UnitTestCase {
  18. /**
  19. * The mocked context definition object.
  20. *
  21. * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface|\PHPUnit\Framework\MockObject\MockObject
  22. */
  23. protected $contextDefinition;
  24. /**
  25. * The mocked Typed Data manager.
  26. *
  27. * @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit\Framework\MockObject\MockObject
  28. */
  29. protected $typedDataManager;
  30. /**
  31. * The mocked Typed Data object.
  32. *
  33. * @var \Drupal\Core\TypedData\TypedDataInterface|\PHPUnit\Framework\MockObject\MockObject
  34. */
  35. protected $typedData;
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function setUp() {
  40. parent::setUp();
  41. $this->typedDataManager = $this->createMock(TypedDataManagerInterface::class);
  42. }
  43. /**
  44. * @covers ::getContextValue
  45. */
  46. public function testDefaultValue() {
  47. $this->setUpDefaultValue('test');
  48. $context = new Context($this->contextDefinition);
  49. $context->setTypedDataManager($this->typedDataManager);
  50. $this->assertEquals('test', $context->getContextValue());
  51. }
  52. /**
  53. * @covers ::getContextData
  54. */
  55. public function testDefaultDataValue() {
  56. $this->setUpDefaultValue('test');
  57. $context = new Context($this->contextDefinition);
  58. $context->setTypedDataManager($this->typedDataManager);
  59. $this->assertEquals($this->typedData, $context->getContextData());
  60. }
  61. /**
  62. * @covers ::getContextData
  63. */
  64. public function testNullDataValue() {
  65. $this->setUpDefaultValue(NULL);
  66. $context = new Context($this->contextDefinition);
  67. $context->setTypedDataManager($this->typedDataManager);
  68. $this->assertEquals($this->typedData, $context->getContextData());
  69. }
  70. /**
  71. * @covers ::setContextValue
  72. */
  73. public function testSetContextValueTypedData() {
  74. $this->contextDefinition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinitionInterface')
  75. ->setMethods(['getDefaultValue', 'getDataDefinition'])
  76. ->getMockForAbstractClass();
  77. $typed_data = $this->createMock('Drupal\Core\TypedData\TypedDataInterface');
  78. $context = new Context($this->contextDefinition, $typed_data);
  79. $this->assertSame($typed_data, $context->getContextData());
  80. }
  81. /**
  82. * @covers ::setContextValue
  83. */
  84. public function testSetContextValueCacheableDependency() {
  85. $container = new Container();
  86. $cache_context_manager = $this->getMockBuilder('Drupal\Core\Cache\CacheContextsManager')
  87. ->disableOriginalConstructor()
  88. ->setMethods(['validateTokens'])
  89. ->getMock();
  90. $container->set('cache_contexts_manager', $cache_context_manager);
  91. $cache_context_manager->expects($this->any())
  92. ->method('validateTokens')
  93. ->with(['route'])
  94. ->willReturn(['route']);
  95. \Drupal::setContainer($container);
  96. $this->contextDefinition = $this->createMock('Drupal\Core\Plugin\Context\ContextDefinitionInterface');
  97. $context = new Context($this->contextDefinition);
  98. $context->setTypedDataManager($this->typedDataManager);
  99. $cacheable_dependency = $this->createMock('Drupal\Tests\Core\Plugin\Context\TypedDataCacheableDependencyInterface');
  100. $cacheable_dependency->expects($this->once())
  101. ->method('getCacheTags')
  102. ->willReturn(['node:1']);
  103. $cacheable_dependency->expects($this->once())
  104. ->method('getCacheContexts')
  105. ->willReturn(['route']);
  106. $cacheable_dependency->expects($this->once())
  107. ->method('getCacheMaxAge')
  108. ->willReturn(60);
  109. $context = Context::createFromContext($context, $cacheable_dependency);
  110. $this->assertSame($cacheable_dependency, $context->getContextData());
  111. $this->assertEquals(['node:1'], $context->getCacheTags());
  112. $this->assertEquals(['route'], $context->getCacheContexts());
  113. $this->assertEquals(60, $context->getCacheMaxAge());
  114. }
  115. /**
  116. * Set up mocks for the getDefaultValue() method call.
  117. *
  118. * @param mixed $default_value
  119. * The default value to assign to the mock context definition.
  120. */
  121. protected function setUpDefaultValue($default_value = NULL) {
  122. $mock_data_definition = $this->createMock('Drupal\Core\TypedData\DataDefinitionInterface');
  123. $this->contextDefinition = $this->getMockBuilder('Drupal\Core\Plugin\Context\ContextDefinitionInterface')
  124. ->setMethods(['getDefaultValue', 'getDataDefinition'])
  125. ->getMockForAbstractClass();
  126. $this->contextDefinition->expects($this->once())
  127. ->method('getDefaultValue')
  128. ->willReturn($default_value);
  129. $this->contextDefinition->expects($this->once())
  130. ->method('getDataDefinition')
  131. ->willReturn($mock_data_definition);
  132. $this->typedData = $this->createMock('Drupal\Core\TypedData\TypedDataInterface');
  133. $this->typedDataManager->expects($this->once())
  134. ->method('create')
  135. ->with($mock_data_definition, $default_value)
  136. ->willReturn($this->typedData);
  137. }
  138. }
  139. /**
  140. * Test interface used for mocking.
  141. */
  142. interface TypedDataCacheableDependencyInterface extends CacheableDependencyInterface, TypedDataInterface {}