PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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