(NFC) (dev/core#878) Simplify '@copyright' annotation
[civicrm-core.git] / tests / phpunit / api / v4 / Action / BasicActionsTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2020 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC https://civicrm.org/licensing
33 * $Id$
34 *
35 */
36
37
38 namespace api\v4\Action;
39
40 use api\v4\UnitTestCase;
41 use Civi\Api4\MockBasicEntity;
42
43 /**
44 * @group headless
45 */
46 class BasicActionsTest extends UnitTestCase {
47
48 public function testCrud() {
49 MockBasicEntity::delete()->addWhere('id', '>', 0)->execute();
50
51 $id1 = MockBasicEntity::create()->addValue('foo', 'one')->execute()->first()['id'];
52
53 $result = MockBasicEntity::get()->execute();
54 $this->assertCount(1, $result);
55
56 $id2 = MockBasicEntity::create()->addValue('foo', 'two')->execute()->first()['id'];
57
58 $result = MockBasicEntity::get()->selectRowCount()->execute();
59 $this->assertEquals(2, $result->count());
60
61 MockBasicEntity::update()->addWhere('id', '=', $id2)->addValue('foo', 'new')->execute();
62
63 $result = MockBasicEntity::get()->addOrderBy('id', 'DESC')->setLimit(1)->execute();
64 $this->assertCount(1, $result);
65 $this->assertEquals('new', $result->first()['foo']);
66
67 $result = MockBasicEntity::save()
68 ->addRecord(['id' => $id1, 'foo' => 'one updated'])
69 ->addRecord(['id' => $id2])
70 ->addRecord(['foo' => 'three'])
71 ->addDefault('color', 'pink')
72 ->setReload(TRUE)
73 ->execute()
74 ->indexBy('id');
75
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']);
81 }
82
83 $this->assertEquals('one updated', MockBasicEntity::get()->addWhere('id', '=', $id1)->execute()->first()['foo']);
84
85 MockBasicEntity::delete()->addWhere('id', '=', $id2);
86 $result = MockBasicEntity::get()->execute();
87 $this->assertEquals('one updated', $result->first()['foo']);
88 }
89
90 public function testReplace() {
91 MockBasicEntity::delete()->addWhere('id', '>', 0)->execute();
92
93 $objects = [
94 ['group' => 'one', 'color' => 'red'],
95 ['group' => 'one', 'color' => 'blue'],
96 ['group' => 'one', 'color' => 'green'],
97 ['group' => 'two', 'color' => 'orange'],
98 ];
99
100 foreach ($objects as &$object) {
101 $object['id'] = MockBasicEntity::create()->setValues($object)->execute()->first()['id'];
102 }
103
104 // Keep red, change blue, delete green, and add yellow
105 $replacements = [
106 ['color' => 'red', 'id' => $objects[0]['id']],
107 ['color' => 'not blue', 'id' => $objects[1]['id']],
108 ['color' => 'yellow'],
109 ];
110
111 MockBasicEntity::replace()->addWhere('group', '=', 'one')->setRecords($replacements)->execute();
112
113 $newObjects = MockBasicEntity::get()->addOrderBy('id', 'DESC')->execute()->indexBy('id');
114
115 $this->assertCount(4, $newObjects);
116
117 $this->assertEquals('yellow', $newObjects->first()['color']);
118
119 $this->assertEquals('not blue', $newObjects[$objects[1]['id']]['color']);
120
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']);
124 }
125
126 public function testBatchFrobnicate() {
127 MockBasicEntity::delete()->addWhere('id', '>', 0)->execute();
128
129 $objects = [
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],
134 ];
135 foreach ($objects as &$object) {
136 $object['id'] = MockBasicEntity::create()->setValues($object)->execute()->first()['id'];
137 }
138
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));
142 }
143
144 public function testGetFields() {
145 $getFields = MockBasicEntity::getFields()->execute()->indexBy('name');
146
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']);
151
152 // Getfields should default to loadOptions = false and reduce them to bool
153 $this->assertTrue($getFields['group']['options']);
154 $this->assertFalse($getFields['id']['options']);
155
156 // Now load options
157 $getFields = MockBasicEntity::getFields()
158 ->addWhere('name', '=', 'group')
159 ->setLoadOptions(TRUE)
160 ->execute()->indexBy('name');
161
162 $this->assertCount(1, $getFields);
163 $this->assertArrayHasKey('one', $getFields['group']['options']);
164 }
165
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');
174
175 $itemsToGet = new \ReflectionMethod($get, '_itemsToGet');
176 $itemsToGet->setAccessible(TRUE);
177
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'));
183 }
184
185 public function testFieldsToGet() {
186 $get = MockBasicEntity::get()
187 ->addWhere('color', '!=', 'green');
188
189 $isFieldSelected = new \ReflectionMethod($get, '_isFieldSelected');
190 $isFieldSelected->setAccessible(TRUE);
191
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'));
196
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'));
205
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'));
213
214 $get->addOrderBy('group');
215 $this->assertTrue($isFieldSelected->invoke($get, 'group'));
216 }
217
218 }