4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2020 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC https://civicrm.org/licensing
38 namespace api\v
4\Action
;
40 use api\v
4\UnitTestCase
;
41 use Civi\Api4\MockBasicEntity
;
46 class BasicActionsTest
extends UnitTestCase
{
48 public function testCrud() {
49 MockBasicEntity
::delete()->addWhere('id', '>', 0)->execute();
51 $id1 = MockBasicEntity
::create()->addValue('foo', 'one')->execute()->first()['id'];
53 $result = MockBasicEntity
::get()->execute();
54 $this->assertCount(1, $result);
56 $id2 = MockBasicEntity
::create()->addValue('foo', 'two')->execute()->first()['id'];
58 $result = MockBasicEntity
::get()->selectRowCount()->execute();
59 $this->assertEquals(2, $result->count());
61 MockBasicEntity
::update()->addWhere('id', '=', $id2)->addValue('foo', 'new')->execute();
63 $result = MockBasicEntity
::get()->addOrderBy('id', 'DESC')->setLimit(1)->execute();
64 $this->assertCount(1, $result);
65 $this->assertEquals('new', $result->first()['foo']);
67 $result = MockBasicEntity
::save()
68 ->addRecord(['id' => $id1, 'foo' => 'one updated'])
69 ->addRecord(['id' => $id2])
70 ->addRecord(['foo' => 'three'])
71 ->addDefault('color', 'pink')
76 $this->assertEquals('new', $result[$id2]['foo']);
77 $this->assertEquals('three', $result->last()['foo']);
78 $this->assertCount(3, $result);
79 foreach ($result as $item) {
80 $this->assertEquals('pink', $item['color']);
83 $this->assertEquals('one updated', MockBasicEntity
::get()->addWhere('id', '=', $id1)->execute()->first()['foo']);
85 MockBasicEntity
::delete()->addWhere('id', '=', $id2);
86 $result = MockBasicEntity
::get()->execute();
87 $this->assertEquals('one updated', $result->first()['foo']);
90 public function testReplace() {
91 MockBasicEntity
::delete()->addWhere('id', '>', 0)->execute();
94 ['group' => 'one', 'color' => 'red'],
95 ['group' => 'one', 'color' => 'blue'],
96 ['group' => 'one', 'color' => 'green'],
97 ['group' => 'two', 'color' => 'orange'],
100 foreach ($objects as &$object) {
101 $object['id'] = MockBasicEntity
::create()->setValues($object)->execute()->first()['id'];
104 // Keep red, change blue, delete green, and add yellow
106 ['color' => 'red', 'id' => $objects[0]['id']],
107 ['color' => 'not blue', 'id' => $objects[1]['id']],
108 ['color' => 'yellow'],
111 MockBasicEntity
::replace()->addWhere('group', '=', 'one')->setRecords($replacements)->execute();
113 $newObjects = MockBasicEntity
::get()->addOrderBy('id', 'DESC')->execute()->indexBy('id');
115 $this->assertCount(4, $newObjects);
117 $this->assertEquals('yellow', $newObjects->first()['color']);
119 $this->assertEquals('not blue', $newObjects[$objects[1]['id']]['color']);
121 // Ensure group two hasn't been altered
122 $this->assertEquals('orange', $newObjects[$objects[3]['id']]['color']);
123 $this->assertEquals('two', $newObjects[$objects[3]['id']]['group']);
126 public function testBatchFrobnicate() {
127 MockBasicEntity
::delete()->addWhere('id', '>', 0)->execute();
130 ['group' => 'one', 'color' => 'red', 'number' => 10],
131 ['group' => 'one', 'color' => 'blue', 'number' => 20],
132 ['group' => 'one', 'color' => 'green', 'number' => 30],
133 ['group' => 'two', 'color' => 'blue', 'number' => 40],
135 foreach ($objects as &$object) {
136 $object['id'] = MockBasicEntity
::create()->setValues($object)->execute()->first()['id'];
139 $result = MockBasicEntity
::batchFrobnicate()->addWhere('color', '=', 'blue')->execute();
140 $this->assertEquals(2, count($result));
141 $this->assertEquals([400, 1600], \CRM_Utils_Array
::collect('frobnication', (array) $result));
144 public function testGetFields() {
145 $getFields = MockBasicEntity
::getFields()->execute()->indexBy('name');
147 $this->assertCount(6, $getFields);
148 $this->assertEquals('Id', $getFields['id']['title']);
149 // Ensure default data type is "String" when not specified
150 $this->assertEquals('String', $getFields['color']['data_type']);
152 // Getfields should default to loadOptions = false and reduce them to bool
153 $this->assertTrue($getFields['group']['options']);
154 $this->assertFalse($getFields['id']['options']);
157 $getFields = MockBasicEntity
::getFields()
158 ->addWhere('name', '=', 'group')
159 ->setLoadOptions(TRUE)
160 ->execute()->indexBy('name');
162 $this->assertCount(1, $getFields);
163 $this->assertArrayHasKey('one', $getFields['group']['options']);
166 public function testItemsToGet() {
167 $get = MockBasicEntity
::get()
168 ->addWhere('color', 'NOT IN', ['yellow'])
169 ->addWhere('color', 'IN', ['red', 'blue'])
170 ->addWhere('color', '!=', 'green')
171 ->addWhere('group', '=', 'one')
172 ->addWhere('size', 'LIKE', 'big')
173 ->addWhere('shape', 'LIKE', '%a');
175 $itemsToGet = new \
ReflectionMethod($get, '_itemsToGet');
176 $itemsToGet->setAccessible(TRUE);
178 $this->assertEquals(['red', 'blue'], $itemsToGet->invoke($get, 'color'));
179 $this->assertEquals(['one'], $itemsToGet->invoke($get, 'group'));
180 $this->assertEquals(['big'], $itemsToGet->invoke($get, 'size'));
181 $this->assertEmpty($itemsToGet->invoke($get, 'shape'));
182 $this->assertEmpty($itemsToGet->invoke($get, 'weight'));
185 public function testFieldsToGet() {
186 $get = MockBasicEntity
::get()
187 ->addWhere('color', '!=', 'green');
189 $isFieldSelected = new \
ReflectionMethod($get, '_isFieldSelected');
190 $isFieldSelected->setAccessible(TRUE);
192 // If no "select" is set, should always return true
193 $this->assertTrue($isFieldSelected->invoke($get, 'color'));
194 $this->assertTrue($isFieldSelected->invoke($get, 'shape'));
195 $this->assertTrue($isFieldSelected->invoke($get, 'size'));
197 // With a non-empty "select" fieldsToSelect() will return fields needed to evaluate each clause.
198 $get->addSelect('id');
199 $this->assertTrue($isFieldSelected->invoke($get, 'color'));
200 $this->assertTrue($isFieldSelected->invoke($get, 'id'));
201 $this->assertFalse($isFieldSelected->invoke($get, 'shape'));
202 $this->assertFalse($isFieldSelected->invoke($get, 'size'));
203 $this->assertFalse($isFieldSelected->invoke($get, 'weight'));
204 $this->assertFalse($isFieldSelected->invoke($get, 'group'));
206 $get->addClause('OR', ['shape', '=', 'round'], ['AND', [['size', '=', 'big'], ['weight', '!=', 'small']]]);
207 $this->assertTrue($isFieldSelected->invoke($get, 'color'));
208 $this->assertTrue($isFieldSelected->invoke($get, 'id'));
209 $this->assertTrue($isFieldSelected->invoke($get, 'shape'));
210 $this->assertTrue($isFieldSelected->invoke($get, 'size'));
211 $this->assertTrue($isFieldSelected->invoke($get, 'weight'));
212 $this->assertFalse($isFieldSelected->invoke($get, 'group'));
214 $get->addOrderBy('group');
215 $this->assertTrue($isFieldSelected->invoke($get, 'group'));