Merge pull request #16901 from eileenmcnaughton/settings
[civicrm-core.git] / tests / phpunit / api / v4 / Action / BasicActionsTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace api\v4\Action;
23
24 use api\v4\UnitTestCase;
25 use Civi\Api4\MockBasicEntity;
26
27 /**
28 * @group headless
29 */
30 class BasicActionsTest extends UnitTestCase {
31
32 public function testCrud() {
33 MockBasicEntity::delete()->addWhere('id', '>', 0)->execute();
34
35 $id1 = MockBasicEntity::create()->addValue('foo', 'one')->execute()->first()['id'];
36
37 $result = MockBasicEntity::get()->execute();
38 $this->assertCount(1, $result);
39
40 $id2 = MockBasicEntity::create()->addValue('foo', 'two')->execute()->first()['id'];
41
42 $result = MockBasicEntity::get()->selectRowCount()->execute();
43 $this->assertEquals(2, $result->count());
44
45 MockBasicEntity::update()->addWhere('id', '=', $id2)->addValue('foo', 'new')->execute();
46
47 $result = MockBasicEntity::get()->addOrderBy('id', 'DESC')->setLimit(1)->execute();
48 $this->assertCount(1, $result);
49 $this->assertEquals('new', $result->first()['foo']);
50
51 $result = MockBasicEntity::save()
52 ->addRecord(['id' => $id1, 'foo' => 'one updated'])
53 ->addRecord(['id' => $id2])
54 ->addRecord(['foo' => 'three'])
55 ->addDefault('color', 'pink')
56 ->setReload(TRUE)
57 ->execute()
58 ->indexBy('id');
59
60 $this->assertEquals('new', $result[$id2]['foo']);
61 $this->assertEquals('three', $result->last()['foo']);
62 $this->assertCount(3, $result);
63 foreach ($result as $item) {
64 $this->assertEquals('pink', $item['color']);
65 }
66
67 $this->assertEquals('one updated', MockBasicEntity::get()->addWhere('id', '=', $id1)->execute()->first()['foo']);
68
69 MockBasicEntity::delete()->addWhere('id', '=', $id2);
70 $result = MockBasicEntity::get()->execute();
71 $this->assertEquals('one updated', $result->first()['foo']);
72 }
73
74 public function testReplace() {
75 MockBasicEntity::delete()->addWhere('id', '>', 0)->execute();
76
77 $objects = [
78 ['group' => 'one', 'color' => 'red'],
79 ['group' => 'one', 'color' => 'blue'],
80 ['group' => 'one', 'color' => 'green'],
81 ['group' => 'two', 'color' => 'orange'],
82 ];
83
84 foreach ($objects as &$object) {
85 $object['id'] = MockBasicEntity::create()->setValues($object)->execute()->first()['id'];
86 }
87
88 // Keep red, change blue, delete green, and add yellow
89 $replacements = [
90 ['color' => 'red', 'id' => $objects[0]['id']],
91 ['color' => 'not blue', 'id' => $objects[1]['id']],
92 ['color' => 'yellow'],
93 ];
94
95 MockBasicEntity::replace()->addWhere('group', '=', 'one')->setRecords($replacements)->execute();
96
97 $newObjects = MockBasicEntity::get()->addOrderBy('id', 'DESC')->execute()->indexBy('id');
98
99 $this->assertCount(4, $newObjects);
100
101 $this->assertEquals('yellow', $newObjects->first()['color']);
102
103 $this->assertEquals('not blue', $newObjects[$objects[1]['id']]['color']);
104
105 // Ensure group two hasn't been altered
106 $this->assertEquals('orange', $newObjects[$objects[3]['id']]['color']);
107 $this->assertEquals('two', $newObjects[$objects[3]['id']]['group']);
108 }
109
110 public function testBatchFrobnicate() {
111 MockBasicEntity::delete()->addWhere('id', '>', 0)->execute();
112
113 $objects = [
114 ['group' => 'one', 'color' => 'red', 'number' => 10],
115 ['group' => 'one', 'color' => 'blue', 'number' => 20],
116 ['group' => 'one', 'color' => 'green', 'number' => 30],
117 ['group' => 'two', 'color' => 'blue', 'number' => 40],
118 ];
119 foreach ($objects as &$object) {
120 $object['id'] = MockBasicEntity::create()->setValues($object)->execute()->first()['id'];
121 }
122
123 $result = MockBasicEntity::batchFrobnicate()->addWhere('color', '=', 'blue')->execute();
124 $this->assertEquals(2, count($result));
125 $this->assertEquals([400, 1600], \CRM_Utils_Array::collect('frobnication', (array) $result));
126 }
127
128 public function testGetFields() {
129 $getFields = MockBasicEntity::getFields()->execute()->indexBy('name');
130
131 $this->assertCount(6, $getFields);
132 $this->assertEquals('Id', $getFields['id']['title']);
133 // Ensure default data type is "String" when not specified
134 $this->assertEquals('String', $getFields['color']['data_type']);
135
136 // Getfields should default to loadOptions = false and reduce them to bool
137 $this->assertTrue($getFields['group']['options']);
138 $this->assertFalse($getFields['id']['options']);
139
140 // Now load options
141 $getFields = MockBasicEntity::getFields()
142 ->addWhere('name', '=', 'group')
143 ->setLoadOptions(TRUE)
144 ->execute()->indexBy('name');
145
146 $this->assertCount(1, $getFields);
147 $this->assertArrayHasKey('one', $getFields['group']['options']);
148 }
149
150 public function testItemsToGet() {
151 $get = MockBasicEntity::get()
152 ->addWhere('color', 'NOT IN', ['yellow'])
153 ->addWhere('color', 'IN', ['red', 'blue'])
154 ->addWhere('color', '!=', 'green')
155 ->addWhere('group', '=', 'one')
156 ->addWhere('size', 'LIKE', 'big')
157 ->addWhere('shape', 'LIKE', '%a');
158
159 $itemsToGet = new \ReflectionMethod($get, '_itemsToGet');
160 $itemsToGet->setAccessible(TRUE);
161
162 $this->assertEquals(['red', 'blue'], $itemsToGet->invoke($get, 'color'));
163 $this->assertEquals(['one'], $itemsToGet->invoke($get, 'group'));
164 $this->assertEquals(['big'], $itemsToGet->invoke($get, 'size'));
165 $this->assertEmpty($itemsToGet->invoke($get, 'shape'));
166 $this->assertEmpty($itemsToGet->invoke($get, 'weight'));
167 }
168
169 public function testFieldsToGet() {
170 $get = MockBasicEntity::get()
171 ->addWhere('color', '!=', 'green');
172
173 $isFieldSelected = new \ReflectionMethod($get, '_isFieldSelected');
174 $isFieldSelected->setAccessible(TRUE);
175
176 // If no "select" is set, should always return true
177 $this->assertTrue($isFieldSelected->invoke($get, 'color'));
178 $this->assertTrue($isFieldSelected->invoke($get, 'shape'));
179 $this->assertTrue($isFieldSelected->invoke($get, 'size', 'color', 'shape'));
180
181 // With a non-empty "select" fieldsToSelect() will return fields needed to evaluate each clause.
182 $get->addSelect('id');
183 $this->assertTrue($isFieldSelected->invoke($get, 'color', 'shape', 'size'));
184 $this->assertTrue($isFieldSelected->invoke($get, 'id'));
185 $this->assertFalse($isFieldSelected->invoke($get, 'shape', 'size', 'weight'));
186 $this->assertFalse($isFieldSelected->invoke($get, 'group'));
187
188 $get->addClause('OR', ['shape', '=', 'round'], ['AND', [['size', '=', 'big'], ['weight', '!=', 'small']]]);
189 $this->assertTrue($isFieldSelected->invoke($get, 'color'));
190 $this->assertTrue($isFieldSelected->invoke($get, 'id'));
191 $this->assertTrue($isFieldSelected->invoke($get, 'shape'));
192 $this->assertTrue($isFieldSelected->invoke($get, 'size'));
193 $this->assertTrue($isFieldSelected->invoke($get, 'group', 'weight'));
194 $this->assertFalse($isFieldSelected->invoke($get, 'group'));
195
196 $get->addOrderBy('group');
197 $this->assertTrue($isFieldSelected->invoke($get, 'group'));
198 }
199
200 public function testWildcardSelect() {
201 MockBasicEntity::delete()->addWhere('id', '>', 0)->execute();
202
203 $records = [
204 ['group' => 'one', 'color' => 'red', 'shape' => 'round', 'size' => 'med', 'weight' => 10],
205 ['group' => 'two', 'color' => 'blue', 'shape' => 'round', 'size' => 'med', 'weight' => 20],
206 ];
207 MockBasicEntity::save()->setRecords($records)->execute();
208
209 foreach (MockBasicEntity::get()->addSelect('*')->execute() as $result) {
210 ksort($result);
211 $this->assertEquals(['color', 'group', 'id', 'shape', 'size', 'weight'], array_keys($result));
212 }
213
214 $result = MockBasicEntity::get()
215 ->addSelect('*e', 'weig*ht')
216 ->execute()
217 ->first();
218 $this->assertEquals(['shape', 'size', 'weight'], array_keys($result));
219 }
220
221 }