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