4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace api\v
4\Action
;
24 use api\v
4\UnitTestCase
;
25 use Civi\Api4\MockBasicEntity
;
30 class BasicActionsTest
extends UnitTestCase
{
32 public function testCrud() {
33 MockBasicEntity
::delete()->addWhere('id', '>', 0)->execute();
35 $id1 = MockBasicEntity
::create()->addValue('foo', 'one')->execute()->first()['id'];
37 $result = MockBasicEntity
::get()->execute();
38 $this->assertCount(1, $result);
40 $id2 = MockBasicEntity
::create()->addValue('foo', 'two')->execute()->first()['id'];
42 $result = MockBasicEntity
::get()->selectRowCount()->execute();
43 $this->assertEquals(2, $result->count());
45 MockBasicEntity
::update()->addWhere('id', '=', $id2)->addValue('foo', 'new')->execute();
47 $result = MockBasicEntity
::get()->addOrderBy('id', 'DESC')->setLimit(1)->execute();
48 $this->assertCount(1, $result);
49 $this->assertEquals('new', $result->first()['foo']);
51 $result = MockBasicEntity
::save()
52 ->addRecord(['id' => $id1, 'foo' => 'one updated'])
53 ->addRecord(['id' => $id2])
54 ->addRecord(['foo' => 'three'])
55 ->addDefault('color', 'pink')
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']);
67 $this->assertEquals('one updated', MockBasicEntity
::get()->addWhere('id', '=', $id1)->execute()->first()['foo']);
69 MockBasicEntity
::delete()->addWhere('id', '=', $id2);
70 $result = MockBasicEntity
::get()->execute();
71 $this->assertEquals('one updated', $result->first()['foo']);
74 public function testReplace() {
75 MockBasicEntity
::delete()->addWhere('id', '>', 0)->execute();
78 ['group' => 'one', 'color' => 'red'],
79 ['group' => 'one', 'color' => 'blue'],
80 ['group' => 'one', 'color' => 'green'],
81 ['group' => 'two', 'color' => 'orange'],
84 foreach ($objects as &$object) {
85 $object['id'] = MockBasicEntity
::create()->setValues($object)->execute()->first()['id'];
88 // Keep red, change blue, delete green, and add yellow
90 ['color' => 'red', 'id' => $objects[0]['id']],
91 ['color' => 'not blue', 'id' => $objects[1]['id']],
92 ['color' => 'yellow'],
95 MockBasicEntity
::replace()->addWhere('group', '=', 'one')->setRecords($replacements)->execute();
97 $newObjects = MockBasicEntity
::get()->addOrderBy('id', 'DESC')->execute()->indexBy('id');
99 $this->assertCount(4, $newObjects);
101 $this->assertEquals('yellow', $newObjects->first()['color']);
103 $this->assertEquals('not blue', $newObjects[$objects[1]['id']]['color']);
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']);
110 public function testBatchFrobnicate() {
111 MockBasicEntity
::delete()->addWhere('id', '>', 0)->execute();
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],
119 foreach ($objects as &$object) {
120 $object['id'] = MockBasicEntity
::create()->setValues($object)->execute()->first()['id'];
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));
128 public function testGetFields() {
129 $getFields = MockBasicEntity
::getFields()->execute()->indexBy('name');
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']);
136 // Getfields should default to loadOptions = false and reduce them to bool
137 $this->assertTrue($getFields['group']['options']);
138 $this->assertFalse($getFields['id']['options']);
141 $getFields = MockBasicEntity
::getFields()
142 ->addWhere('name', '=', 'group')
143 ->setLoadOptions(TRUE)
144 ->execute()->indexBy('name');
146 $this->assertCount(1, $getFields);
147 $this->assertArrayHasKey('one', $getFields['group']['options']);
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');
159 $itemsToGet = new \
ReflectionMethod($get, '_itemsToGet');
160 $itemsToGet->setAccessible(TRUE);
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'));
169 public function testFieldsToGet() {
170 $get = MockBasicEntity
::get()
171 ->addWhere('color', '!=', 'green');
173 $isFieldSelected = new \
ReflectionMethod($get, '_isFieldSelected');
174 $isFieldSelected->setAccessible(TRUE);
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'));
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'));
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'));
196 $get->addOrderBy('group');
197 $this->assertTrue($isFieldSelected->invoke($get, 'group'));
200 public function testWildcardSelect() {
201 MockBasicEntity
::delete()->addWhere('id', '>', 0)->execute();
204 ['group' => 'one', 'color' => 'red', 'shape' => 'round', 'size' => 'med', 'weight' => 10],
205 ['group' => 'two', 'color' => 'blue', 'shape' => 'round', 'size' => 'med', 'weight' => 20],
207 MockBasicEntity
::save()->setRecords($records)->execute();
209 foreach (MockBasicEntity
::get()->addSelect('*')->execute() as $result) {
211 $this->assertEquals(['color', 'group', 'id', 'shape', 'size', 'weight'], array_keys($result));
214 $result = MockBasicEntity
::get()
215 ->addSelect('*e', 'weig*ht')
218 $this->assertEquals(['shape', 'size', 'weight'], array_keys($result));