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 all 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
84 changes: 79 additions & 5 deletions tests/src/Integration/Condition/ListContainsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,13 +23,60 @@ 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}
*/
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);
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

}