diff --git a/src/Plugin/Condition/DataListContains.php b/src/Plugin/Condition/DataListContains.php index e41c57ff..4c83a1a5 100644 --- a/src/Plugin/Condition/DataListContains.php +++ b/src/Plugin/Condition/DataListContains.php @@ -36,6 +36,16 @@ */ class DataListContains extends RulesConditionBase { + /** + * {@inheritdoc} + */ + public function refineContextDefinitions() { + + // Refine the item context. + $typed_data_item = $this->getContext('item')->getContextData(); + $this->pluginDefinition['context']['item']->setDataType($typed_data_item->getDataDefinition()->getDataType()); + } + /** * {@inheritdoc} */ diff --git a/tests/src/Integration/Condition/ListContainsTest.php b/tests/src/Integration/Condition/ListContainsTest.php index 992a2d25..910c4b71 100644 --- a/tests/src/Integration/Condition/ListContainsTest.php +++ b/tests/src/Integration/Condition/ListContainsTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\rules\Integration\Condition; use Drupal\Tests\rules\Integration\RulesIntegrationTestBase; +use Drupal\Core\TypedData\TypedDataInterface; /** * @coversDefaultClass \Drupal\rules\Plugin\Condition\DataListContains @@ -22,6 +23,27 @@ class ListContainsTest extends RulesIntegrationTestBase { */ protected $condition; + /** + * 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} */ @@ -29,6 +51,32 @@ public function setUp() { parent::setUp(); $this->condition = $this->conditionManager->createInstance('rules_list_contains'); + + $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->any()) + ->method('getDefaultValue') + ->willReturn('test'); + + $this->contextDefinition->expects($this->any()) + ->method('getDataDefinition') + ->willReturn($mock_data_definition); + + $this->typedData = $this->getMock('Drupal\Core\TypedData\TypedDataInterface'); + + $this->typedDataManager = $this->getMockBuilder('Drupal\Core\TypedData\TypedDataManager') + ->disableOriginalConstructor() + ->setMethods(array('create')) + ->getMock(); + + $this->typedDataManager->expects($this->any()) + ->method('create') + ->with($mock_data_definition, 'test') + ->willReturn($this->typedData); } /** @@ -47,8 +95,8 @@ public function testSummary() { */ public function testConditionEvaluation() { - // Test array of string values - $list = ['One','Two','Three']; + // Test array of string values. + $list = ['One', 'Two', 'Three']; // Test that the list doesn't contain 'Zero'. $this->condition @@ -80,7 +128,7 @@ public function testConditionEvaluation() { ->setContextValue('item', 'Four'); $this->assertFalse($this->condition->evaluate()); - // Create array of mock entities + // Create array of mock entities. $entity_zero = $this->getMock('Drupal\Core\Entity\EntityInterface'); $entity_zero->expects($this->any()) ->method('id') @@ -106,8 +154,8 @@ public function testConditionEvaluation() { ->method('id') ->will($this->returnValue('entity_four_id')); - // Test array of entities - $entity_list = [$entity_one,$entity_two,$entity_three]; + // Test array of entities. + $entity_list = [$entity_one, $entity_two, $entity_three]; // Test that the list of entities doesn't contain entity 'entity_zero'. $this->condition @@ -139,4 +187,30 @@ public function testConditionEvaluation() { ->setContextValue('item', $entity_four); $this->assertFalse($this->condition->evaluate()); } + + /** + * Test refining the context definitions. + * + * @covers ::refineContextDefinitions + */ + public function testRefiningContextDefinitions() { + // Create array of mock entities. + $entity_zero = $this->getMock('Drupal\Core\Entity\EntityInterface'); + $entity_one = $this->getMock('Drupal\Core\Entity\EntityInterface'); + + // Test array of entities. + $entity_list = [$entity_zero, $entity_one]; + + $this->condition + ->setContextValue('list', $entity_list) + ->setContextValue('item', $entity_zero); + $this->condition->refineContextdefinitions(); + + // Get the context definition for the item + // and make sure that the type is now the entity type. + $typed_data_item = $this->condition->getContext('item')->getContextData(); + $item_context_definition = $typed_data_item->getDataDefinition()->getDataType(); + $this->assertEquals('entity_zero_type', $item_context_definition); + } + }