Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 5 | | |
6 | +--------------------------------------------------------------------+ | |
f299f7db | 7 | | Copyright CiviCRM LLC (c) 2004-2020 | |
380f3545 TO |
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 | |
f299f7db | 32 | * @copyright CiviCRM LLC (c) 2004-2020 |
380f3545 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | ||
37 | ||
19b53e5b C |
38 | namespace api\v4; |
39 | ||
40 | use api\v4\Traits\TestDataLoaderTrait; | |
41 | use Civi\Test\HeadlessInterface; | |
42 | use Civi\Test\TransactionalInterface; | |
43 | ||
44 | /** | |
45 | * @group headless | |
46 | */ | |
1d10c3cf | 47 | class UnitTestCase extends \PHPUnit\Framework\TestCase implements HeadlessInterface, TransactionalInterface { |
19b53e5b C |
48 | |
49 | use TestDataLoaderTrait; | |
50 | ||
51 | /** | |
52 | * @see CiviUnitTestCase | |
53 | * | |
54 | * @param string $name | |
55 | * @param array $data | |
56 | * @param string $dataName | |
57 | */ | |
58 | public function __construct($name = NULL, array $data = [], $dataName = '') { | |
59 | parent::__construct($name, $data, $dataName); | |
60 | error_reporting(E_ALL & ~E_NOTICE); | |
61 | } | |
62 | ||
63 | public function setUpHeadless() { | |
e96f62cb | 64 | return \Civi\Test::headless()->apply(); |
19b53e5b C |
65 | } |
66 | ||
67 | /** | |
68 | * Tears down the fixture, for example, closes a network connection. | |
69 | * | |
70 | * This method is called after a test is executed. | |
71 | */ | |
72 | public function tearDown() { | |
73 | parent::tearDown(); | |
74 | } | |
75 | ||
76 | /** | |
77 | * Quick clean by emptying tables created for the test. | |
78 | * | |
79 | * @param array $params | |
80 | */ | |
81 | public function cleanup($params) { | |
82 | $params += [ | |
83 | 'tablesToTruncate' => [], | |
84 | ]; | |
85 | \CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;"); | |
86 | foreach ($params['tablesToTruncate'] as $table) { | |
87 | \Civi::log()->info('truncating: ' . $table); | |
88 | $sql = "TRUNCATE TABLE $table"; | |
89 | \CRM_Core_DAO::executeQuery($sql); | |
90 | } | |
91 | \CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;"); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Quick record counter | |
96 | * | |
97 | * @param string $table_name | |
98 | * @returns int record count | |
99 | */ | |
100 | public function getRowCount($table_name) { | |
101 | $sql = "SELECT count(id) FROM $table_name"; | |
102 | return (int) \CRM_Core_DAO::singleValueQuery($sql); | |
103 | } | |
104 | ||
105 | /** | |
106 | * Create sample entities (using V3 for now). | |
107 | * | |
108 | * @param array $params | |
109 | * (type, seq, overrides, count) | |
110 | * @return array | |
111 | * (either single, or array of array if count >1) | |
112 | * @throws \CiviCRM_API3_Exception | |
113 | * @throws \Exception | |
114 | */ | |
115 | public static function createEntity($params) { | |
116 | $params += [ | |
117 | 'count' => 1, | |
118 | 'seq' => 0, | |
119 | ]; | |
120 | $entities = []; | |
121 | $entity = NULL; | |
122 | for ($i = 0; $i < $params['count']; $i++) { | |
123 | $params['seq']++; | |
124 | $data = self::sample($params); | |
125 | $api_params = ['sequential' => 1] + $data['sample_params']; | |
126 | $result = civicrm_api3($data['entity'], 'create', $api_params); | |
127 | if ($result['is_error']) { | |
128 | throw new \Exception("creating $data[entity] failed"); | |
129 | } | |
130 | $entity = $result['values'][0]; | |
131 | if (!($entity['id'] > 0)) { | |
132 | throw new \Exception("created entity is malformed"); | |
133 | } | |
134 | $entities[] = $entity; | |
135 | } | |
136 | return $params['count'] == 1 ? $entity : $entities; | |
137 | } | |
138 | ||
139 | /** | |
140 | * Helper function for creating sample entities. | |
141 | * | |
142 | * Depending on the supplied sequence integer, plucks values from the dummy data. | |
143 | * Constructs a foreign entity when an ID is required but isn't supplied in the overrides. | |
144 | * | |
145 | * Inspired by CiviUnitTestCase:: | |
146 | * @todo - extract this function to own class and share with CiviUnitTestCase? | |
147 | * @param array $params | |
148 | * - type: string roughly matching entity type | |
149 | * - seq: (optional) int sequence number for the values of this type | |
150 | * - overrides: (optional) array of fill in parameters | |
151 | * | |
152 | * @return array | |
153 | * - entity: string API entity type (usually the type supplied except for contact subtypes) | |
154 | * - sample_params: array API sample_params properties of sample entity | |
155 | */ | |
156 | public static function sample($params) { | |
157 | $params += [ | |
158 | 'seq' => 0, | |
159 | 'overrides' => [], | |
160 | ]; | |
161 | $type = $params['type']; | |
162 | // sample data - if field is array then chosed based on `seq` | |
163 | $sample_params = []; | |
164 | if (in_array($type, ['Individual', 'Organization', 'Household'])) { | |
165 | $sample_params['contact_type'] = $type; | |
166 | $entity = 'Contact'; | |
167 | } | |
168 | else { | |
169 | $entity = $type; | |
170 | } | |
171 | // use the seq to pluck a set of params out | |
172 | foreach (self::sampleData($type) as $key => $value) { | |
173 | if (is_array($value)) { | |
174 | $sample_params[$key] = $value[$params['seq'] % count($value)]; | |
175 | } | |
176 | else { | |
177 | $sample_params[$key] = $value; | |
178 | } | |
179 | } | |
180 | if ($type == 'Individual') { | |
181 | $sample_params['email'] = strtolower( | |
182 | $sample_params['first_name'] . '_' . $sample_params['last_name'] . '@civicrm.org' | |
183 | ); | |
184 | $sample_params['prefix_id'] = 3; | |
185 | $sample_params['suffix_id'] = 3; | |
186 | } | |
187 | if (!count($sample_params)) { | |
188 | throw new \Exception("unknown sample type: $type"); | |
189 | } | |
190 | $sample_params = $params['overrides'] + $sample_params; | |
191 | // make foreign enitiies if they haven't been supplied | |
192 | foreach ($sample_params as $key => $value) { | |
193 | if (substr($value, 0, 6) === 'dummy.') { | |
194 | $foreign_entity = self::createEntity([ | |
195 | 'type' => substr($value, 6), | |
196 | 'seq' => $params['seq'], | |
197 | ]); | |
198 | $sample_params[$key] = $foreign_entity['id']; | |
199 | } | |
200 | } | |
201 | return compact("entity", "sample_params"); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Provider of sample data. | |
206 | * | |
207 | * @return array | |
208 | * Array values represent a set of allowable items. | |
209 | * Strings in the form "dummy.Entity" require creating a foreign entity first. | |
210 | */ | |
211 | public static function sampleData($type) { | |
212 | $data = [ | |
213 | 'Individual' => [ | |
214 | // The number of values in each list need to be coprime numbers to not have duplicates | |
215 | 'first_name' => ['Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'], | |
216 | 'middle_name' => ['J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'], | |
217 | 'last_name' => ['Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'], | |
218 | 'contact_type' => 'Individual', | |
219 | ], | |
220 | 'Organization' => [ | |
221 | 'organization_name' => [ | |
222 | 'Unit Test Organization', | |
223 | 'Acme', | |
224 | 'Roberts and Sons', | |
225 | 'Cryo Space Labs', | |
226 | 'Sharper Pens', | |
227 | ], | |
228 | ], | |
229 | 'Household' => [ | |
230 | 'household_name' => ['Unit Test household'], | |
231 | ], | |
232 | 'Event' => [ | |
233 | 'title' => 'Annual CiviCRM meet', | |
234 | 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now', | |
235 | 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues', | |
236 | 'event_type_id' => 1, | |
237 | 'is_public' => 1, | |
238 | 'start_date' => 20081021, | |
239 | 'end_date' => 20081023, | |
240 | 'is_online_registration' => 1, | |
241 | 'registration_start_date' => 20080601, | |
242 | 'registration_end_date' => 20081015, | |
243 | 'max_participants' => 100, | |
244 | 'event_full_text' => 'Sorry! We are already full', | |
245 | 'is_monetary' => 0, | |
246 | 'is_active' => 1, | |
247 | 'is_show_location' => 0, | |
248 | ], | |
249 | 'Participant' => [ | |
250 | 'event_id' => 'dummy.Event', | |
251 | 'contact_id' => 'dummy.Individual', | |
252 | 'status_id' => 2, | |
253 | 'role_id' => 1, | |
254 | 'register_date' => 20070219, | |
255 | 'source' => 'Wimbeldon', | |
256 | 'event_level' => 'Payment', | |
257 | ], | |
258 | 'Contribution' => [ | |
259 | 'contact_id' => 'dummy.Individual', | |
260 | // donation, 2 = member, 3 = campaign contribution, 4=event | |
261 | 'financial_type_id' => 1, | |
262 | 'total_amount' => 7.3, | |
263 | ], | |
264 | 'Activity' => [ | |
265 | //'activity_type_id' => 1, | |
266 | 'subject' => 'unit testing', | |
267 | 'source_contact_id' => 'dummy.Individual', | |
268 | ], | |
269 | ]; | |
270 | if ($type == 'Contact') { | |
271 | $type = 'Individual'; | |
272 | } | |
273 | return $data[$type]; | |
274 | } | |
275 | ||
276 | } |