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