Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Plugin/Condition/DataListContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
*/
class DataListContains extends RulesConditionBase {

/**
* {@inheritdoc}
*/
public function refineContextDefinitions() {

// Refine the item context.
if ($item = $this->getContextValue('item')) {
if ($item instanceof EntityInterface && $type = $item->getEntityTypeId()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why EntityInterface here? I think this should just be "$typed_data_item = $this->getContextData('item')" and then "$this->pluginDefinition['context']['item']->setDataType($typed_data_item->getDataDefinition()->getDataType());"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"$typed_data_item = $this->getContextData('item')" created this error:

PHP Fatal error: Call to undefined method Drupal\rules\Plugin\Condition\DataListContains::getContextData() in /Users/steve/Sites/d8dev/modules/rules/src/Plugin/Condition/DataListContains.php on line 45

...so I copied the code from DataComparison.php:

$item = $this->condition->getContextDefinition('item');

Your suggested code failed my test though:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'any'
+'entity:entity_zero_type_id'

$this->pluginDefinition['context']['item']->setDataType("entity:$type");
}
};

}

/**
* {@inheritdoc}
*/
Expand Down
48 changes: 43 additions & 5 deletions tests/src/Integration/Condition/ListContainsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,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
Expand Down Expand Up @@ -80,7 +80,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')
Expand All @@ -106,8 +106,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
Expand Down Expand Up @@ -139,4 +139,42 @@ 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_zero->expects($this->any())
->method('id')
->willReturn('entity_zero_id');
$entity_zero->expects($this->any())
->method('getEntityTypeId')
->willReturn('entity_zero_type_id');

$entity_one = $this->getMock('Drupal\Core\Entity\EntityInterface');
$entity_one->expects($this->any())
->method('id')
->willReturn('entity_one_id');
$entity_one->expects($this->any())
->method('getEntityTypeId')
->willReturn('entity_one_type_id');

// 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.
$item = $this->condition->getContextDefinition('item');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

item is a bad variable name here, since this is not the item but the context definition of item.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, well made ;)

$this->assertEquals($item->getDataType(), 'entity:entity_zero_type_id');
}

}