class ContextTest

Same name in this branch
  1. 11.x core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest
Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
  2. 9 core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest
  3. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
  4. 8.9.x core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest
  5. 10 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
  6. 10 core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest

@coversDefaultClass \Drupal\Core\Plugin\Context\Context @group Plugin

Hierarchy

Expanded class hierarchy of ContextTest

File

core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php, line 18

Namespace

Drupal\Tests\Core\Plugin\Context
View source
class ContextTest extends UnitTestCase {
    
    /**
     * The mocked context definition object.
     *
     * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $contextDefinition;
    
    /**
     * The mocked Typed Data manager.
     *
     * @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $typedDataManager;
    
    /**
     * The mocked Typed Data object.
     *
     * @var \Drupal\Core\TypedData\TypedDataInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $typedData;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->typedDataManager = $this->createMock(TypedDataManagerInterface::class);
    }
    
    /**
     * @covers ::getContextValue
     */
    public function testDefaultValue() : void {
        $this->setUpDefaultValue('test');
        $context = new Context($this->contextDefinition);
        $context->setTypedDataManager($this->typedDataManager);
        $this->assertEquals('test', $context->getContextValue());
    }
    
    /**
     * @covers ::getContextData
     */
    public function testDefaultDataValue() : void {
        $this->setUpDefaultValue('test');
        $context = new Context($this->contextDefinition);
        $context->setTypedDataManager($this->typedDataManager);
        $this->assertEquals($this->typedData, $context->getContextData());
    }
    
    /**
     * @covers ::getContextData
     */
    public function testNullDataValue() : void {
        $this->setUpDefaultValue(NULL);
        $context = new Context($this->contextDefinition);
        $context->setTypedDataManager($this->typedDataManager);
        $this->assertEquals($this->typedData, $context->getContextData());
    }
    
    /**
     * @covers ::setContextValue
     */
    public function testSetContextValueTypedData() : void {
        $this->contextDefinition = $this->createMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface');
        $typed_data = $this->createMock('Drupal\\Core\\TypedData\\TypedDataInterface');
        $context = new Context($this->contextDefinition, $typed_data);
        $this->assertSame($typed_data, $context->getContextData());
    }
    
    /**
     * @covers ::setContextValue
     */
    public function testSetContextValueCacheableDependency() : void {
        $container = new Container();
        $cache_context_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\Context\\CacheContextsManager')
            ->disableOriginalConstructor()
            ->onlyMethods([
            'validateTokens',
        ])
            ->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->createMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface');
        $context = new Context($this->contextDefinition);
        $context->setTypedDataManager($this->typedDataManager);
        $cacheable_dependency = $this->createMock('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());
    }
    
    /**
     * Set up mocks for the getDefaultValue() method call.
     *
     * @param mixed $default_value
     *   The default value to assign to the mock context definition.
     */
    protected function setUpDefaultValue($default_value = NULL) {
        $mock_data_definition = $this->createMock('Drupal\\Core\\TypedData\\DataDefinitionInterface');
        $this->contextDefinition = $this->createMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface');
        $this->contextDefinition
            ->expects($this->once())
            ->method('getDefaultValue')
            ->willReturn($default_value);
        $this->contextDefinition
            ->expects($this->once())
            ->method('getDataDefinition')
            ->willReturn($mock_data_definition);
        $this->typedData = $this->createMock('Drupal\\Core\\TypedData\\TypedDataInterface');
        $this->typedDataManager
            ->expects($this->once())
            ->method('create')
            ->with($mock_data_definition, $default_value)
            ->willReturn($this->typedData);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ContextTest::$contextDefinition protected property The mocked context definition object.
ContextTest::$typedData protected property The mocked Typed Data object.
ContextTest::$typedDataManager protected property The mocked Typed Data manager.
ContextTest::setUp protected function Overrides UnitTestCase::setUp
ContextTest::setUpDefaultValue protected function Set up mocks for the getDefaultValue() method call.
ContextTest::testDefaultDataValue public function @covers ::getContextData
ContextTest::testDefaultValue public function @covers ::getContextValue
ContextTest::testNullDataValue public function @covers ::getContextData
ContextTest::testSetContextValueCacheableDependency public function @covers ::setContextValue
ContextTest::testSetContextValueTypedData public function @covers ::setContextValue
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.