Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
3 | namespace api\v4\Action; | |
4 | ||
5 | use api\v4\UnitTestCase; | |
6 | use Civi\Api4\MockBasicEntity; | |
7 | ||
8 | /** | |
9 | * @group headless | |
10 | */ | |
11 | class BasicActionsTest extends UnitTestCase { | |
12 | ||
13 | public function testCrud() { | |
14 | MockBasicEntity::delete()->addWhere('id', '>', 0)->execute(); | |
15 | ||
16 | $id1 = MockBasicEntity::create()->addValue('foo', 'one')->execute()->first()['id']; | |
17 | ||
18 | $result = MockBasicEntity::get()->execute(); | |
19 | $this->assertCount(1, $result); | |
20 | ||
21 | $id2 = MockBasicEntity::create()->addValue('foo', 'two')->execute()->first()['id']; | |
22 | ||
23 | $result = MockBasicEntity::get()->selectRowCount()->execute(); | |
24 | $this->assertEquals(2, $result->count()); | |
25 | ||
26 | MockBasicEntity::update()->addWhere('id', '=', $id2)->addValue('foo', 'new')->execute(); | |
27 | ||
28 | $result = MockBasicEntity::get()->addOrderBy('id', 'DESC')->setLimit(1)->execute(); | |
29 | $this->assertCount(1, $result); | |
30 | $this->assertEquals('new', $result->first()['foo']); | |
31 | ||
32 | $result = MockBasicEntity::save() | |
33 | ->addRecord(['id' => $id1, 'foo' => 'one updated']) | |
34 | ->addRecord(['id' => $id2]) | |
35 | ->addRecord(['foo' => 'three']) | |
36 | ->addDefault('color', 'pink') | |
37 | ->setReload(TRUE) | |
38 | ->execute() | |
39 | ->indexBy('id'); | |
40 | ||
41 | $this->assertEquals('new', $result[$id2]['foo']); | |
42 | $this->assertEquals('three', $result->last()['foo']); | |
43 | $this->assertCount(3, $result); | |
44 | foreach ($result as $item) { | |
45 | $this->assertEquals('pink', $item['color']); | |
46 | } | |
47 | ||
48 | $this->assertEquals('one updated', MockBasicEntity::get()->addWhere('id', '=', $id1)->execute()->first()['foo']); | |
49 | ||
50 | MockBasicEntity::delete()->addWhere('id', '=', $id2); | |
51 | $result = MockBasicEntity::get()->execute(); | |
52 | $this->assertEquals('one updated', $result->first()['foo']); | |
53 | } | |
54 | ||
55 | public function testReplace() { | |
56 | MockBasicEntity::delete()->addWhere('id', '>', 0)->execute(); | |
57 | ||
58 | $objects = [ | |
59 | ['group' => 'one', 'color' => 'red'], | |
60 | ['group' => 'one', 'color' => 'blue'], | |
61 | ['group' => 'one', 'color' => 'green'], | |
62 | ['group' => 'two', 'color' => 'orange'], | |
63 | ]; | |
64 | ||
65 | foreach ($objects as &$object) { | |
66 | $object['id'] = MockBasicEntity::create()->setValues($object)->execute()->first()['id']; | |
67 | } | |
68 | ||
69 | // Keep red, change blue, delete green, and add yellow | |
70 | $replacements = [ | |
71 | ['color' => 'red', 'id' => $objects[0]['id']], | |
72 | ['color' => 'not blue', 'id' => $objects[1]['id']], | |
73 | ['color' => 'yellow'], | |
74 | ]; | |
75 | ||
76 | MockBasicEntity::replace()->addWhere('group', '=', 'one')->setRecords($replacements)->execute(); | |
77 | ||
78 | $newObjects = MockBasicEntity::get()->addOrderBy('id', 'DESC')->execute()->indexBy('id'); | |
79 | ||
80 | $this->assertCount(4, $newObjects); | |
81 | ||
82 | $this->assertEquals('yellow', $newObjects->first()['color']); | |
83 | ||
84 | $this->assertEquals('not blue', $newObjects[$objects[1]['id']]['color']); | |
85 | ||
86 | // Ensure group two hasn't been altered | |
87 | $this->assertEquals('orange', $newObjects[$objects[3]['id']]['color']); | |
88 | $this->assertEquals('two', $newObjects[$objects[3]['id']]['group']); | |
89 | } | |
90 | ||
91 | public function testBatchFrobnicate() { | |
92 | MockBasicEntity::delete()->addWhere('id', '>', 0)->execute(); | |
93 | ||
94 | $objects = [ | |
95 | ['group' => 'one', 'color' => 'red', 'number' => 10], | |
96 | ['group' => 'one', 'color' => 'blue', 'number' => 20], | |
97 | ['group' => 'one', 'color' => 'green', 'number' => 30], | |
98 | ['group' => 'two', 'color' => 'blue', 'number' => 40], | |
99 | ]; | |
100 | foreach ($objects as &$object) { | |
101 | $object['id'] = MockBasicEntity::create()->setValues($object)->execute()->first()['id']; | |
102 | } | |
103 | ||
104 | $result = MockBasicEntity::batchFrobnicate()->addWhere('color', '=', 'blue')->execute(); | |
105 | $this->assertEquals(2, count($result)); | |
106 | $this->assertEquals([400, 1600], \CRM_Utils_Array::collect('frobnication', (array) $result)); | |
107 | } | |
108 | ||
109 | public function testGetFields() { | |
110 | $getFields = MockBasicEntity::getFields()->execute()->indexBy('name'); | |
111 | ||
112 | $this->assertCount(6, $getFields); | |
113 | $this->assertEquals('Id', $getFields['id']['title']); | |
114 | // Ensure default data type is "String" when not specified | |
115 | $this->assertEquals('String', $getFields['color']['data_type']); | |
116 | ||
117 | // Getfields should default to loadOptions = false and reduce them to bool | |
118 | $this->assertTrue($getFields['group']['options']); | |
119 | $this->assertFalse($getFields['id']['options']); | |
120 | ||
121 | // Now load options | |
122 | $getFields = MockBasicEntity::getFields() | |
123 | ->addWhere('name', '=', 'group') | |
124 | ->setLoadOptions(TRUE) | |
125 | ->execute()->indexBy('name'); | |
126 | ||
127 | $this->assertCount(1, $getFields); | |
128 | $this->assertArrayHasKey('one', $getFields['group']['options']); | |
129 | } | |
130 | ||
131 | public function testItemsToGet() { | |
132 | $get = MockBasicEntity::get() | |
133 | ->addWhere('color', 'NOT IN', ['yellow']) | |
134 | ->addWhere('color', 'IN', ['red', 'blue']) | |
135 | ->addWhere('color', '!=', 'green') | |
136 | ->addWhere('group', '=', 'one') | |
137 | ->addWhere('size', 'LIKE', 'big') | |
138 | ->addWhere('shape', 'LIKE', '%a'); | |
139 | ||
140 | $itemsToGet = new \ReflectionMethod($get, '_itemsToGet'); | |
141 | $itemsToGet->setAccessible(TRUE); | |
142 | ||
143 | $this->assertEquals(['red', 'blue'], $itemsToGet->invoke($get, 'color')); | |
144 | $this->assertEquals(['one'], $itemsToGet->invoke($get, 'group')); | |
145 | $this->assertEquals(['big'], $itemsToGet->invoke($get, 'size')); | |
146 | $this->assertEmpty($itemsToGet->invoke($get, 'shape')); | |
147 | $this->assertEmpty($itemsToGet->invoke($get, 'weight')); | |
148 | } | |
149 | ||
150 | public function testFieldsToGet() { | |
151 | $get = MockBasicEntity::get() | |
152 | ->addWhere('color', '!=', 'green'); | |
153 | ||
154 | $isFieldSelected = new \ReflectionMethod($get, '_isFieldSelected'); | |
155 | $isFieldSelected->setAccessible(TRUE); | |
156 | ||
157 | // If no "select" is set, should always return true | |
158 | $this->assertTrue($isFieldSelected->invoke($get, 'color')); | |
159 | $this->assertTrue($isFieldSelected->invoke($get, 'shape')); | |
160 | $this->assertTrue($isFieldSelected->invoke($get, 'size')); | |
161 | ||
162 | // With a non-empty "select" fieldsToSelect() will return fields needed to evaluate each clause. | |
163 | $get->addSelect('id'); | |
164 | $this->assertTrue($isFieldSelected->invoke($get, 'color')); | |
165 | $this->assertTrue($isFieldSelected->invoke($get, 'id')); | |
166 | $this->assertFalse($isFieldSelected->invoke($get, 'shape')); | |
167 | $this->assertFalse($isFieldSelected->invoke($get, 'size')); | |
168 | $this->assertFalse($isFieldSelected->invoke($get, 'weight')); | |
169 | $this->assertFalse($isFieldSelected->invoke($get, 'group')); | |
170 | ||
171 | $get->addClause('OR', ['shape', '=', 'round'], ['AND', [['size', '=', 'big'], ['weight', '!=', 'small']]]); | |
172 | $this->assertTrue($isFieldSelected->invoke($get, 'color')); | |
173 | $this->assertTrue($isFieldSelected->invoke($get, 'id')); | |
174 | $this->assertTrue($isFieldSelected->invoke($get, 'shape')); | |
175 | $this->assertTrue($isFieldSelected->invoke($get, 'size')); | |
176 | $this->assertTrue($isFieldSelected->invoke($get, 'weight')); | |
177 | $this->assertFalse($isFieldSelected->invoke($get, 'group')); | |
178 | ||
179 | $get->addOrderBy('group'); | |
180 | $this->assertTrue($isFieldSelected->invoke($get, 'group')); | |
181 | } | |
182 | ||
183 | } |