diff --git a/core/lib/Drupal/Component/Plugin/Context/Context.php b/core/lib/Drupal/Component/Plugin/Context/Context.php index b3f431e..9c6b93c 100644 --- a/core/lib/Drupal/Component/Plugin/Context/Context.php +++ b/core/lib/Drupal/Component/Plugin/Context/Context.php @@ -37,7 +37,7 @@ class Context implements ContextInterface { * The context definition. */ public function __construct(ContextDefinitionInterface $context_definition) { - $this->setContextDefinition($context_definition); + $this->contextDefinition = $context_definition; } /** diff --git a/core/lib/Drupal/Component/Plugin/Context/ContextDefinitionInterface.php b/core/lib/Drupal/Component/Plugin/Context/ContextDefinitionInterface.php index 07288f6..25a3dfe 100644 --- a/core/lib/Drupal/Component/Plugin/Context/ContextDefinitionInterface.php +++ b/core/lib/Drupal/Component/Plugin/Context/ContextDefinitionInterface.php @@ -155,6 +155,7 @@ public function addConstraint($constraint_name, $options = NULL); * @return array * A validation constraint definition which can be used for instantiating a * \Symfony\Component\Validator\Constraint object. + * * @throws Drupal\Component\Plugin\Exception\ContextException * If $constraint_name is not available, this method throws an exception. */ diff --git a/core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php b/core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php index 0e996c1..c673607 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php @@ -87,6 +87,43 @@ public function testGetContextValue($expected, $context_value, $is_required, $da } /** + * Helper to build a mocked Drupal\Component\Plugin\Context\Context object. + * + * @param int $expected_count + * The number of Constraint objects we expect. + * @param string $class_constraint + * Value to mock as returned from + * ContextDefinitionInterface::getConstraint('class'). + * + * @return Drupal\Component\Plugin\Context\Context|mock + * A mocked Drupal\Component\Plugin\Context\Context with all expectations + * set. + */ + protected function mockGetConstraintsContext($expected_count, $class_constraint) { + // Mock a ContextDefintionInterface object. + $context_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface') + ->setMethods(array('getConstraint')) + ->getMockForAbstractClass(); + // Set expecations for the context definition object. + $context_definition->expects($this->once()) + ->method('getConstraint') + ->with($this->equalTo('class')) + ->willReturn($class_constraint); + + // Mock a Context object. + $context = $this->getMockBuilder('Drupal\Component\Plugin\Context\Context') + ->disableOriginalConstructor() + ->setMethods(array('getContextDefinition')) + ->getMock(); + // Set expectations for getContextDefinition(). + $context->expects($this->once()) + ->method('getContextDefinition') + ->willReturn($context_definition); + + return $context; + } + + /** * @return array * - Number of elements expected in array returned from getConstraints(). * - Value to mock as returned from @@ -95,6 +132,18 @@ public function testGetContextValue($expected, $context_value, $is_required, $da public function providerGetConstraints() { return [ [1, '\stdClass'], + ]; + } + + /** + * @return array + * - Number of elements expected in array returned from getConstraints(). + * - Value to mock as returned from + * ContextDefinitionInterface::getConstraint('class'). NULL here, which + * should trigger an exception. + */ + public function providerGetConstraintsException() { + return [ [0, NULL], ]; } @@ -109,32 +158,32 @@ public function testGetConstraints($expected_count, $class_constraint) { $this->setExpectedException('Drupal\Component\Plugin\Exception\ContextException'); } - // Mock a ContextDefintionInterface object. - $context_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface') - ->setMethods(array('getConstraint')) - ->getMockForAbstractClass(); - // Set expecations for the context definition object. - $context_definition->expects($this->once()) - ->method('getConstraint') - ->with($this->equalTo('class')) - ->willReturn($class_constraint); - - // Mock a Context object. - $context = $this->getMockBuilder('Drupal\Component\Plugin\Context\Context') - ->disableOriginalConstructor() - ->setMethods(array('getContextDefinition')) - ->getMock(); - // Set expectations for getContextDefinition(). - $context->expects($this->once()) - ->method('getContextDefinition') - ->willReturn($context_definition); + $mock_context = $this->mockGetConstraintsContext($expected_count, $class_constraint); // Exercise getConstraints(). - $constraints = $context->getConstraints(); + $constraints = $mock_context->getConstraints(); // Check that getConstraints() returned an array. $this->assertInternalType('array', $constraints); // Check that getConstraints() returned an array of the correct size. $this->assertEquals($expected_count, count($constraints)); + // Check that all the constraints returned are Context objects as per the + // interface documentation. + foreach ($constraints as $constraint) { + $this->assertInstanceOf('Drupal\Component\Plugin\Context\Context', $constraint); + } + } + + + /** + * @covers ::getConstraints + * @expectedException Drupal\Component\Plugin\Exception\ContextException + * @dataProvider providerGetConstraintsException + */ + public function testGetConstraintsException($expected_count, $class_constraint) { + $mock_context = $this->mockGetConstraintsContext($expected_count, $class_constraint); + + // Exercise getConstraints(). + $constraints = $mock_context->getConstraints(); } }