Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / tests / src / Kernel / QueryAccess / ConditionGroupTest.php
1 <?php
2
3 namespace Drupal\Tests\entity\Kernel\QueryAccess;
4
5 use Drupal\entity\QueryAccess\Condition;
6 use Drupal\entity\QueryAccess\ConditionGroup;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Tests the condition group class.
11  *
12  * ConditionGroup uses \Drupal\Core\Cache\Cache internally, which makes it
13  * impossible to use a unit test (due to Cache accessing the global container).
14  *
15  * @coversDefaultClass \Drupal\entity\QueryAccess\ConditionGroup
16  * @group entity
17  */
18 class ConditionGroupTest extends KernelTestBase {
19
20   /**
21    * ::covers getConjunction
22    * ::covers addCondition
23    * ::covers getConditions
24    * ::covers count.
25    */
26   public function testGetters() {
27     $condition_group = new ConditionGroup();
28     $condition_group->addCondition('uid', '2');
29     $this->assertEquals('AND', $condition_group->getConjunction());
30     $expected_conditions = [
31       new Condition('uid', '2'),
32     ];
33     $this->assertEquals($expected_conditions, $condition_group->getConditions());
34     $this->assertEquals(1, $condition_group->count());
35     $this->assertEquals("uid = '2'", $condition_group->__toString());
36
37     $condition_group = new ConditionGroup('OR');
38     $condition_group->addCondition('type', ['article', 'page']);
39     $condition_group->addCondition('status', '1', '<>');
40     $this->assertEquals('OR', $condition_group->getConjunction());
41     $expected_conditions = [
42       new Condition('type', ['article', 'page']),
43       new Condition('status', '1', '<>'),
44     ];
45     $expected_lines = [
46       "(",
47       "  type IN ['article', 'page']",
48       "    OR",
49       "  status <> '1'",
50       ")",
51     ];
52     $this->assertEquals($expected_conditions, $condition_group->getConditions());
53     $this->assertEquals(2, $condition_group->count());
54     $this->assertEquals(implode("\n", $expected_lines), $condition_group->__toString());
55
56     // Nested condition group with a single condition.
57     $condition_group = new ConditionGroup();
58     $condition_group->addCondition('type', ['article', 'page']);
59     $condition_group->addCondition((new ConditionGroup('AND'))
60       ->addCondition('status', '1')
61     );
62     $expected_conditions = [
63       new Condition('type', ['article', 'page']),
64       new Condition('status', '1'),
65     ];
66     $expected_lines = [
67       "(",
68       "  type IN ['article', 'page']",
69       "    AND",
70       "  status = '1'",
71       ")",
72     ];
73     $this->assertEquals($expected_conditions, $condition_group->getConditions());
74     $this->assertEquals('AND', $condition_group->getConjunction());
75     $this->assertEquals(2, $condition_group->count());
76     $this->assertEquals(implode("\n", $expected_lines), $condition_group->__toString());
77
78     // Nested condition group with multiple conditions.
79     $condition_group = new ConditionGroup();
80     $condition_group->addCondition('type', ['article', 'page']);
81     $nested_condition_group = new ConditionGroup('OR');
82     $nested_condition_group->addCondition('uid', '1');
83     $nested_condition_group->addCondition('status', '1');
84     $condition_group->addCondition($nested_condition_group);
85     $expected_conditions = [
86       new Condition('type', ['article', 'page']),
87       $nested_condition_group,
88     ];
89     $expected_lines = [
90       "(",
91       "  type IN ['article', 'page']",
92       "    AND",
93       "  (",
94       "    uid = '1'",
95       "      OR",
96       "    status = '1'",
97       "  )",
98       ")",
99     ];
100     $this->assertEquals($expected_conditions, $condition_group->getConditions());
101     $this->assertEquals('AND', $condition_group->getConjunction());
102     $this->assertEquals(2, $condition_group->count());
103     $this->assertEquals(implode("\n", $expected_lines), $condition_group->__toString());
104   }
105
106 }