View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\Condition;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase;
class ListContainsTest extends RulesIntegrationTestBase {
protected $condition;
protected function setUp() : void {
parent::setUp();
$this->condition = $this->conditionManager
->createInstance('rules_list_contains');
}
public function testConditionEvaluation() {
$list = [
'One',
'Two',
'Three',
];
$this->condition
->setContextValue('list', $list)
->setContextValue('item', 'Zero');
$this
->assertFalse($this->condition
->evaluate());
$this->condition
->setContextValue('list', $list)
->setContextValue('item', 'One');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('list', $list)
->setContextValue('item', 'Two');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('list', $list)
->setContextValue('item', 'Three');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('list', $list)
->setContextValue('item', 'Four');
$this
->assertFalse($this->condition
->evaluate());
$entity_zero = $this
->prophesizeEntity(EntityInterface::class);
$entity_zero
->id()
->willReturn('entity_zero_id');
$entity_one = $this
->prophesizeEntity(EntityInterface::class);
$entity_one
->id()
->willReturn('entity_one_id');
$entity_two = $this
->prophesizeEntity(EntityInterface::class);
$entity_two
->id()
->willReturn('entity_two_id');
$entity_three = $this
->prophesizeEntity(EntityInterface::class);
$entity_three
->id()
->willReturn('entity_three_id');
$entity_four = $this
->prophesizeEntity(EntityInterface::class);
$entity_four
->id()
->willReturn('entity_four_id');
$entity_list = [
$entity_one
->reveal(),
$entity_two
->reveal(),
$entity_three
->reveal(),
];
$this->condition
->setContextValue('list', $entity_list)
->setContextValue('item', $entity_zero
->reveal());
$this
->assertFalse($this->condition
->evaluate());
$this->condition
->setContextValue('list', $entity_list)
->setContextValue('item', $entity_one
->reveal());
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('list', $entity_list)
->setContextValue('item', $entity_two
->reveal());
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('list', $entity_list)
->setContextValue('item', $entity_three
->reveal());
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('list', $entity_list)
->setContextValue('item', $entity_four
->reveal());
$this
->assertFalse($this->condition
->evaluate());
}
}