Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code for refining item context #184

Open
wants to merge 6 commits into
base: 8.x-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
10 changes: 10 additions & 0 deletions src/Plugin/Condition/DataListContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
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');
}

}