Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /** | |
3 | * File for the CiviUnitTestCase class | |
4 | * | |
5 | * (PHP 5) | |
6 | * | |
7 | * @copyright Copyright CiviCRM LLC (C) 2009 | |
8 | * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html | |
9 | * GNU Affero General Public License version 3 | |
10 | * @package CiviCRM | |
11 | * | |
12 | * This file is part of CiviCRM | |
13 | * | |
14 | * CiviCRM is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU Affero General Public License | |
16 | * as published by the Free Software Foundation; either version 3 of | |
17 | * the License, or (at your option) any later version. | |
18 | * | |
19 | * CiviCRM is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU Affero General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU Affero General Public | |
25 | * License along with this program. If not, see | |
26 | * <http://www.gnu.org/licenses/>. | |
27 | */ | |
28 | ||
29 | /** | |
30 | * Include configuration | |
31 | */ | |
32 | define('CIVICRM_SETTINGS_PATH', __DIR__ . '/civicrm.settings.dist.php'); | |
33 | define('CIVICRM_SETTINGS_LOCAL_PATH', __DIR__ . '/civicrm.settings.local.php'); | |
34 | ||
35 | if (file_exists(CIVICRM_SETTINGS_LOCAL_PATH)) { | |
36 | require_once CIVICRM_SETTINGS_LOCAL_PATH; | |
37 | } | |
38 | require_once CIVICRM_SETTINGS_PATH; | |
39 | /** | |
40 | * Include class definitions | |
41 | */ | |
42 | require_once 'PHPUnit/Extensions/Database/TestCase.php'; | |
43 | require_once 'PHPUnit/Framework/TestResult.php'; | |
44 | require_once 'PHPUnit/Extensions/Database/DataSet/FlatXmlDataSet.php'; | |
45 | require_once 'PHPUnit/Extensions/Database/DataSet/XmlDataSet.php'; | |
46 | require_once 'PHPUnit/Extensions/Database/DataSet/QueryDataSet.php'; | |
47 | require_once 'tests/phpunit/Utils.php'; | |
48 | require_once 'api/api.php'; | |
49 | require_once 'CRM/Financial/BAO/FinancialType.php'; | |
50 | define('API_LATEST_VERSION', 3); | |
51 | ||
52 | /** | |
53 | * Base class for CiviCRM unit tests | |
54 | * | |
55 | * Common functions for unit tests | |
56 | * @package CiviCRM | |
57 | */ | |
58 | class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { | |
59 | ||
60 | /** | |
61 | * Database has been initialized | |
62 | * | |
63 | * @var boolean | |
64 | */ | |
65 | private static $dbInit = FALSE; | |
66 | ||
67 | /** | |
68 | * Database connection | |
69 | * | |
70 | * @var PHPUnit_Extensions_Database_DB_IDatabaseConnection | |
71 | */ | |
72 | protected $_dbconn; | |
73 | ||
74 | /** | |
75 | * The database name | |
76 | * | |
77 | * @var string | |
78 | */ | |
79 | static protected $_dbName; | |
80 | ||
81 | /** | |
82 | * @var array of temporary directory names | |
83 | */ | |
84 | protected $tempDirs; | |
85 | ||
86 | /** | |
87 | * @var Utils instance | |
88 | */ | |
89 | public static $utils; | |
90 | ||
91 | /** | |
92 | * @var boolean populateOnce allows to skip db resets in setUp | |
93 | * | |
94 | * WARNING! USE WITH CAUTION - IT'LL RENDER DATA DEPENDENCIES | |
95 | * BETWEEN TESTS WHEN RUN IN SUITE. SUITABLE FOR LOCAL, LIMITED | |
96 | * "CHECK RUNS" ONLY! | |
97 | * | |
98 | * IF POSSIBLE, USE $this->DBResetRequired = FALSE IN YOUR TEST CASE! | |
99 | * | |
100 | * see also: http://forum.civicrm.org/index.php/topic,18065.0.html | |
101 | */ | |
102 | public static $populateOnce = FALSE; | |
103 | ||
104 | /** | |
105 | * Allow classes to state E-notice compliance | |
106 | */ | |
107 | public $_eNoticeCompliant = FALSE; | |
108 | ||
109 | /** | |
110 | * @var boolean DBResetRequired allows skipping DB reset | |
111 | * in specific test case. If you still need | |
112 | * to reset single test (method) of such case, call | |
113 | * $this->cleanDB() in the first line of this | |
114 | * test (method). | |
115 | */ | |
116 | public $DBResetRequired = TRUE; | |
117 | ||
118 | /** | |
119 | * Constructor | |
120 | * | |
121 | * Because we are overriding the parent class constructor, we | |
122 | * need to show the same arguments as exist in the constructor of | |
123 | * PHPUnit_Framework_TestCase, since | |
124 | * PHPUnit_Framework_TestSuite::createTest() creates a | |
125 | * ReflectionClass of the Test class and checks the constructor | |
126 | * of that class to decide how to set up the test. | |
127 | * | |
128 | * @param string $name | |
129 | * @param array $data | |
130 | * @param string $dataName | |
131 | */ | |
132 | function __construct($name = NULL, array$data = array(), $dataName = '') { | |
133 | parent::__construct($name, $data, $dataName); | |
134 | ||
135 | // we need full error reporting | |
136 | error_reporting(E_ALL & ~E_NOTICE); | |
137 | ||
138 | if (!empty($GLOBALS['mysql_db'])) { | |
139 | self::$_dbName = $GLOBALS['mysql_db']; | |
140 | } | |
141 | else { | |
142 | self::$_dbName = 'civicrm_tests_dev'; | |
143 | } | |
144 | ||
145 | // create test database | |
146 | self::$utils = new Utils($GLOBALS['mysql_host'], | |
147 | $GLOBALS['mysql_user'], | |
148 | $GLOBALS['mysql_pass'] | |
149 | ); | |
150 | ||
151 | // also load the class loader | |
152 | require_once 'CRM/Core/ClassLoader.php'; | |
153 | CRM_Core_ClassLoader::singleton()->register(); | |
154 | if (function_exists('_civix_phpunit_setUp')) { // FIXME: loosen coupling | |
155 | _civix_phpunit_setUp(); | |
156 | } | |
157 | } | |
158 | ||
159 | function requireDBReset() { | |
160 | return $this->DBResetRequired; | |
161 | } | |
162 | ||
163 | static function getDBName() { | |
164 | $dbName = !empty($GLOBALS['mysql_db']) ? $GLOBALS['mysql_db'] : 'civicrm_tests_dev'; | |
165 | return $dbName; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Create database connection for this instance | |
170 | * | |
171 | * Initialize the test database if it hasn't been initialized | |
172 | * | |
173 | * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection connection | |
174 | */ | |
175 | protected function getConnection() { | |
176 | $dbName = self::$_dbName; | |
177 | if (!self::$dbInit) { | |
178 | $dbName = self::getDBName(); | |
179 | ||
180 | // install test database | |
181 | echo PHP_EOL . "Installing {$dbName} database" . PHP_EOL; | |
182 | ||
183 | self::_populateDB(FALSE, $this); | |
184 | ||
185 | self::$dbInit = TRUE; | |
186 | } | |
187 | return $this->createDefaultDBConnection(self::$utils->pdo, $dbName); | |
188 | } | |
189 | ||
190 | /** | |
191 | * Required implementation of abstract method | |
192 | */ | |
193 | protected function getDataSet() { | |
194 | } | |
195 | ||
196 | private static function _populateDB($perClass = FALSE, &$object = NULL) { | |
197 | ||
198 | if ($perClass || $object == NULL) { | |
199 | $dbreset = TRUE; | |
200 | } | |
201 | else { | |
202 | $dbreset = $object->requireDBReset(); | |
203 | } | |
204 | ||
205 | if (self::$populateOnce || !$dbreset) { | |
206 | return; | |
207 | } | |
208 | self::$populateOnce = NULL; | |
209 | ||
210 | $dbName = self::getDBName(); | |
211 | $pdo = self::$utils->pdo; | |
212 | // only consider real tables and not views | |
213 | $tables = $pdo->query("SELECT table_name FROM INFORMATION_SCHEMA.TABLES | |
214 | WHERE TABLE_SCHEMA = '{$dbName}' AND TABLE_TYPE = 'BASE TABLE'"); | |
215 | ||
216 | $truncates = array(); | |
217 | $drops = array(); | |
218 | foreach ($tables as $table) { | |
219 | // skip log tables | |
220 | if (substr($table['table_name'], 0, 4) == 'log_') { | |
221 | continue; | |
222 | } | |
223 | ||
224 | // don't change list of installed extensions | |
225 | if ($table['table_name'] == 'civicrm_extension') { | |
226 | continue; | |
227 | } | |
228 | ||
229 | if (substr($table['table_name'], 0, 14) == 'civicrm_value_') { | |
230 | $drops[] = 'DROP TABLE ' . $table['table_name'] . ';'; | |
231 | } | |
232 | else { | |
233 | $truncates[] = 'TRUNCATE ' . $table['table_name'] . ';'; | |
234 | } | |
235 | } | |
236 | ||
237 | $queries = array( | |
238 | "USE {$dbName};", | |
239 | "SET foreign_key_checks = 0", | |
240 | // SQL mode needs to be strict, that's our standard | |
241 | "SET SQL_MODE='STRICT_ALL_TABLES';", | |
242 | "SET global innodb_flush_log_at_trx_commit = 2;", | |
243 | ); | |
244 | $queries = array_merge($queries, $truncates); | |
245 | $queries = array_merge($queries, $drops); | |
246 | foreach ($queries as $query) { | |
247 | if (self::$utils->do_query($query) === FALSE) { | |
248 | // failed to create test database | |
249 | echo "failed to create test db."; | |
250 | exit; | |
251 | } | |
252 | } | |
253 | ||
254 | // initialize test database | |
255 | $sql_file2 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/civicrm_data.mysql"; | |
256 | $sql_file3 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data.mysql"; | |
257 | $sql_file4 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data_second_domain.mysql"; | |
258 | ||
259 | $query2 = file_get_contents($sql_file2); | |
260 | $query3 = file_get_contents($sql_file3); | |
261 | $query4 = file_get_contents($sql_file4); | |
262 | if (self::$utils->do_query($query2) === FALSE) { | |
263 | echo "Cannot load civicrm_data.mysql. Aborting."; | |
264 | exit; | |
265 | } | |
266 | if (self::$utils->do_query($query3) === FALSE) { | |
267 | echo "Cannot load test_data.mysql. Aborting."; | |
268 | exit; | |
269 | } | |
270 | if (self::$utils->do_query($query4) === FALSE) { | |
271 | echo "Cannot load test_data.mysql. Aborting."; | |
272 | exit; | |
273 | } | |
274 | ||
275 | // done with all the loading, get transactions back | |
276 | if (self::$utils->do_query("set global innodb_flush_log_at_trx_commit = 1;") === FALSE) { | |
277 | echo "Cannot set global? Huh?"; | |
278 | exit; | |
279 | } | |
280 | ||
281 | if (self::$utils->do_query("SET foreign_key_checks = 1") === FALSE) { | |
282 | echo "Cannot get foreign keys back? Huh?"; | |
283 | exit; | |
284 | } | |
285 | ||
286 | unset($query, $query2, $query3); | |
287 | ||
288 | // Rebuild triggers | |
289 | civicrm_api('system', 'flush', array('version' => 3, 'triggers' => 1)); | |
290 | } | |
291 | ||
292 | public static function setUpBeforeClass() { | |
293 | self::_populateDB(TRUE); | |
294 | ||
295 | // also set this global hack | |
296 | $GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array(); | |
297 | } | |
298 | ||
299 | /** | |
300 | * Common setup functions for all unit tests | |
301 | */ | |
302 | protected function setUp() { | |
303 | CRM_Utils_Hook::singleton(TRUE); | |
304 | $this->errorScope = CRM_Core_TemporaryErrorScope::useException(); // REVERT | |
305 | // Use a temporary file for STDIN | |
306 | $GLOBALS['stdin'] = tmpfile(); | |
307 | if ($GLOBALS['stdin'] === FALSE) { | |
308 | echo "Couldn't open temporary file\n"; | |
309 | exit(1); | |
310 | } | |
311 | ||
312 | // Get and save a connection to the database | |
313 | $this->_dbconn = $this->getConnection(); | |
314 | ||
315 | // reload database before each test | |
316 | // $this->_populateDB(); | |
317 | ||
318 | // "initialize" CiviCRM to avoid problems when running single tests | |
319 | // FIXME: look at it closer in second stage | |
320 | ||
321 | // initialize the object once db is loaded | |
322 | require_once 'CRM/Core/Config.php'; | |
323 | $config = CRM_Core_Config::singleton(); | |
324 | ||
325 | // when running unit tests, use mockup user framework | |
326 | $config->setUserFramework('UnitTests'); | |
327 | ||
328 | // also fix the fatal error handler to throw exceptions, | |
329 | // rather than exit | |
330 | $config->fatalErrorHandler = 'CiviUnitTestCase_fatalErrorHandler'; | |
331 | ||
332 | // enable backtrace to get meaningful errors | |
333 | $config->backtrace = 1; | |
334 | ||
335 | // disable any left-over test extensions | |
336 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_extension WHERE full_name LIKE "test.%"'); | |
337 | ||
338 | // reset all the caches | |
339 | CRM_Utils_System::flushCache(); | |
340 | ||
341 | // clear permissions stub to not check permissions | |
342 | $config = CRM_Core_Config::singleton(); | |
343 | $config->userPermissionClass->permissions = NULL; | |
344 | ||
345 | //flush component settings | |
346 | CRM_Core_Component::getEnabledComponents(TRUE); | |
347 | ||
348 | if ($this->_eNoticeCompliant) { | |
349 | error_reporting(E_ALL); | |
350 | } | |
351 | else { | |
352 | error_reporting(E_ALL & ~E_NOTICE); | |
353 | } | |
354 | } | |
355 | ||
356 | /** | |
357 | * emulate a logged in user since certain functions use that | |
358 | * value to store a record in the DB (like activity) | |
359 | * CRM-8180 | |
360 | */ | |
361 | public function createLoggedInUser() { | |
362 | $params = array( | |
363 | 'first_name' => 'Logged In', | |
364 | 'last_name' => 'User ' . rand(), | |
365 | 'contact_type' => 'Individual', | |
366 | ); | |
367 | $contactID = $this->individualCreate($params); | |
368 | ||
369 | $session = CRM_Core_Session::singleton(); | |
370 | $session->set('userID', $contactID); | |
371 | } | |
372 | ||
373 | public function cleanDB() { | |
374 | self::$populateOnce = NULL; | |
375 | $this->DBResetRequired = TRUE; | |
376 | ||
377 | $this->_dbconn = $this->getConnection(); | |
378 | $this->_populateDB(); | |
379 | $this->tempDirs = array(); | |
380 | } | |
381 | ||
382 | /** | |
383 | * Common teardown functions for all unit tests | |
384 | */ | |
385 | protected function tearDown() { | |
386 | error_reporting(E_ALL & ~E_NOTICE); | |
387 | $tablesToTruncate = array('civicrm_contact'); | |
388 | $this->quickCleanup($tablesToTruncate); | |
389 | $this->cleanTempDirs(); | |
390 | $this->unsetExtensionSystem(); | |
391 | } | |
392 | ||
393 | /** | |
394 | * FIXME: Maybe a better way to do it | |
395 | */ | |
396 | function foreignKeyChecksOff() { | |
397 | self::$utils = new Utils($GLOBALS['mysql_host'], | |
398 | $GLOBALS['mysql_user'], | |
399 | $GLOBALS['mysql_pass'] | |
400 | ); | |
401 | $dbName = self::getDBName(); | |
402 | $query = "USE {$dbName};" . "SET foreign_key_checks = 1"; | |
403 | if (self::$utils->do_query($query) === FALSE) { | |
404 | // fail happens | |
405 | echo 'Cannot set foreign_key_checks = 0'; | |
406 | exit(1); | |
407 | } | |
408 | return TRUE; | |
409 | } | |
410 | ||
411 | function foreignKeyChecksOn() { | |
412 | // FIXME: might not be needed if previous fixme implemented | |
413 | } | |
414 | ||
415 | /** | |
416 | * Generic function to compare expected values after an api call to retrieved | |
417 | * DB values. | |
418 | * | |
419 | * @daoName string DAO Name of object we're evaluating. | |
420 | * @id int Id of object | |
421 | * @match array Associative array of field name => expected value. Empty if asserting | |
422 | * that a DELETE occurred | |
423 | * @delete boolean True if we're checking that a DELETE action occurred. | |
424 | */ | |
425 | function assertDBState($daoName, $id, $match, $delete = FALSE) { | |
426 | if (empty($id)) { | |
427 | // adding this here since developers forget to check for an id | |
428 | // and hence we get the first value in the db | |
429 | $this->fail('ID not populated. Please fix your assertDBState usage!!!'); | |
430 | } | |
431 | ||
432 | require_once (str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php"); | |
433 | eval('$object = new ' . $daoName . '( );'); | |
434 | $object->id = $id; | |
435 | $verifiedCount = 0; | |
436 | ||
437 | // If we're asserting successful record deletion, make sure object is NOT found. | |
438 | if ($delete) { | |
439 | if ($object->find(TRUE)) { | |
440 | $this->fail("Object not deleted by delete operation: $daoName, $id"); | |
441 | } | |
442 | return; | |
443 | } | |
444 | ||
445 | // Otherwise check matches of DAO field values against expected values in $match. | |
446 | if ($object->find(TRUE)) { | |
447 | $fields = & $object->fields(); | |
448 | foreach ($fields as $name => $value) { | |
449 | $dbName = $value['name']; | |
450 | if (isset($match[$name])) { | |
451 | $verifiedCount++; | |
452 | $this->assertEquals($object->$dbName, $match[$name]); | |
453 | } | |
454 | elseif (isset($match[$dbName])) { | |
455 | $verifiedCount++; | |
456 | $this->assertEquals($object->$dbName, $match[$dbName]); | |
457 | } | |
458 | } | |
459 | } | |
460 | else { | |
461 | $this->fail("Could not retrieve object: $daoName, $id"); | |
462 | } | |
463 | $object->free(); | |
464 | $matchSize = count($match); | |
465 | if ($verifiedCount != $matchSize) { | |
466 | $this->fail("Did not verify all fields in match array: $daoName, $id. Verified count = $verifiedCount. Match array size = $matchSize"); | |
467 | } | |
468 | } | |
469 | ||
470 | // Request a record from the DB by seachColumn+searchValue. Success if a record is found. | |
471 | function assertDBNotNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { | |
472 | if (empty($searchValue)) { | |
473 | $this->fail("empty value passed to assertDBNotNull"); | |
474 | } | |
475 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); | |
476 | $this->assertNotNull($value, $message); | |
477 | ||
478 | return $value; | |
479 | } | |
480 | ||
481 | // Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL. | |
482 | function assertDBNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { | |
483 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); | |
484 | $this->assertNull($value, $message); | |
485 | } | |
486 | ||
487 | // Request a record from the DB by id. Success if row not found. | |
488 | function assertDBRowNotExist($daoName, $id, $message = NULL) { | |
489 | $message = $message ? $message : "$daoName (#$id) should not exist"; | |
490 | $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE); | |
491 | $this->assertNull($value, $message); | |
492 | } | |
493 | ||
494 | // Request a record from the DB by id. Success if row not found. | |
495 | function assertDBRowExist($daoName, $id, $message = NULL) { | |
496 | $message = $message ? $message : "$daoName (#$id) should exist"; | |
497 | $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE); | |
498 | $this->assertEquals($id, $value, $message); | |
499 | } | |
500 | ||
501 | // Compare a single column value in a retrieved DB record to an expected value | |
502 | function assertDBCompareValue($daoName, $searchValue, $returnColumn, $searchColumn, | |
503 | $expectedValue, $message | |
504 | ) { | |
505 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); | |
506 | $this->assertEquals($value, $expectedValue, $message); | |
507 | } | |
508 | ||
509 | // Compare all values in a single retrieved DB record to an array of expected values | |
510 | function assertDBCompareValues($daoName, $searchParams, $expectedValues) { | |
511 | //get the values from db | |
512 | $dbValues = array(); | |
513 | CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues); | |
514 | ||
515 | // compare db values with expected values | |
516 | self::assertAttributesEquals($expectedValues, $dbValues); | |
517 | } | |
518 | ||
519 | /** | |
520 | * Assert that a SQL query returns a given value | |
521 | * | |
522 | * The first argument is an expected value. The remaining arguments are passed | |
523 | * to CRM_Core_DAO::singleValueQuery | |
524 | * | |
525 | * Example: $this->assertSql(2, 'select count(*) from foo where foo.bar like "%1"', | |
526 | * array(1 => array("Whiz", "String"))); | |
527 | */ | |
528 | function assertDBQuery($expected, $query, $params = array()) { | |
529 | $actual = CRM_Core_DAO::singleValueQuery($query, $params); | |
530 | $this->assertEquals($expected, $actual, | |
531 | sprintf('expected=[%s] actual=[%s] query=[%s]', | |
532 | $expected, $actual, CRM_Core_DAO::composeQuery($query, $params, FALSE) | |
533 | ) | |
534 | ); | |
535 | } | |
536 | ||
537 | /** | |
538 | * Assert that two array-trees are exactly equal, notwithstanding | |
539 | * the sorting of keys | |
540 | * | |
541 | * @param array $expected | |
542 | * @param array $actual | |
543 | */ | |
544 | function assertTreeEquals($expected, $actual) { | |
545 | $e = array(); | |
546 | $a = array(); | |
547 | CRM_Utils_Array::flatten($expected, $e, '', ':::'); | |
548 | CRM_Utils_Array::flatten($actual, $a, '', ':::'); | |
549 | ksort($e); | |
550 | ksort($a); | |
551 | ||
552 | $this->assertEquals($e, $a); | |
553 | } | |
554 | ||
555 | function assertAttributesEquals($expectedValues, $actualValues, $message = NULL) { | |
556 | foreach ($expectedValues as $paramName => $paramValue) { | |
557 | if (isset($actualValues[$paramName])) { | |
558 | $this->assertEquals($paramValue, $actualValues[$paramName], "Value Mismatch On $paramName - value 1 is $paramValue value 2 is {$actualValues[$paramName]}"); | |
559 | } | |
560 | else { | |
561 | $this->fail("Attribute '$paramName' not present in actual array."); | |
562 | } | |
563 | } | |
564 | } | |
565 | ||
566 | function assertArrayKeyExists($key, &$list) { | |
567 | $result = isset($list[$key]) ? TRUE : FALSE; | |
568 | $this->assertTrue($result, ts("%1 element exists?", | |
569 | array(1 => $key) | |
570 | )); | |
571 | } | |
572 | ||
573 | function assertArrayValueNotNull($key, &$list) { | |
574 | $this->assertArrayKeyExists($key, $list); | |
575 | ||
576 | $value = isset($list[$key]) ? $list[$key] : NULL; | |
577 | $this->assertTrue($value, | |
578 | ts("%1 element not null?", | |
579 | array(1 => $key) | |
580 | ) | |
581 | ); | |
582 | } | |
583 | ||
584 | function assertAPISuccess($apiResult, $prefix = '') { | |
585 | if (!empty($prefix)) { | |
586 | $prefix .= ': '; | |
587 | } | |
588 | $this->assertEquals(0, $apiResult['is_error'], $prefix . (empty($apiResult['error_message']) ? '' : $apiResult['error_message'])); | |
589 | } | |
590 | ||
591 | function assertType($expected, $actual, $message = '') { | |
592 | return $this->assertInternalType($expected, $actual, $message); | |
593 | } | |
594 | ||
595 | /** | |
596 | * Generic function to create Organisation, to be used in test cases | |
597 | * | |
598 | * @param array parameters for civicrm_contact_add api function call | |
599 | * | |
600 | * @return int id of Organisation created | |
601 | */ | |
602 | function organizationCreate($params = array()) { | |
603 | if (!$params) { | |
604 | $params = array(); | |
605 | } | |
606 | $orgParams = array( | |
607 | 'organization_name' => 'Unit Test Organization', | |
608 | 'contact_type' => 'Organization', | |
609 | 'version' => API_LATEST_VERSION, | |
610 | ); | |
611 | return $this->_contactCreate(array_merge($orgParams, $params)); | |
612 | } | |
613 | ||
614 | /** | |
615 | * Generic function to create Individual, to be used in test cases | |
616 | * | |
617 | * @param array parameters for civicrm_contact_add api function call | |
618 | * | |
619 | * @return int id of Individual created | |
620 | */ | |
621 | function individualCreate($params = NULL) { | |
622 | if ($params === NULL) { | |
623 | $params = array( | |
624 | 'first_name' => 'Anthony', | |
625 | 'middle_name' => 'J.', | |
626 | 'last_name' => 'Anderson', | |
627 | 'prefix_id' => 3, | |
628 | 'suffix_id' => 3, | |
629 | 'email' => 'anthony_anderson@civicrm.org', | |
630 | 'contact_type' => 'Individual', | |
631 | ); | |
632 | } | |
633 | $params['version'] = API_LATEST_VERSION; | |
634 | return $this->_contactCreate($params); | |
635 | } | |
636 | ||
637 | /** | |
638 | * Generic function to create Household, to be used in test cases | |
639 | * | |
640 | * @param array parameters for civicrm_contact_add api function call | |
641 | * | |
642 | * @return int id of Household created | |
643 | */ | |
644 | function householdCreate($params = NULL) { | |
645 | if ($params === NULL) { | |
646 | $params = array( | |
647 | 'household_name' => 'Unit Test household', | |
648 | 'contact_type' => 'Household', | |
649 | ); | |
650 | } | |
651 | $params['version'] = API_LATEST_VERSION; | |
652 | return $this->_contactCreate($params); | |
653 | } | |
654 | ||
655 | /** | |
656 | * Private helper function for calling civicrm_contact_add | |
657 | * | |
658 | * @param array parameters for civicrm_contact_add api function call | |
659 | * | |
660 | * @return int id of Household created | |
661 | */ | |
662 | private function _contactCreate($params) { | |
663 | $params['version'] = API_LATEST_VERSION; | |
664 | $result = civicrm_api('Contact', 'create', $params); | |
665 | if (CRM_Utils_Array::value('is_error', $result) || | |
666 | !CRM_Utils_Array::value('id', $result) | |
667 | ) { | |
668 | throw new Exception('Could not create test contact, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
669 | } | |
670 | return $result['id']; | |
671 | } | |
672 | ||
673 | function contactDelete($contactID) { | |
674 | $params['id'] = $contactID; | |
675 | $params['version'] = API_LATEST_VERSION; | |
676 | $params['skip_undelete'] = 1; | |
677 | $domain = new CRM_Core_BAO_Domain; | |
678 | $domain->contact_id = $contactID; | |
679 | if ($domain->find(TRUE)) { | |
680 | // we are finding tests trying to delete the domain contact in cleanup | |
681 | //since this is mainly for cleanup lets put a safeguard here | |
682 | return; | |
683 | } | |
684 | $result = civicrm_api('Contact', 'delete', $params); | |
685 | if (CRM_Utils_Array::value('is_error', $result)) { | |
686 | throw new Exception('Could not delete contact, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
687 | } | |
688 | return; | |
689 | } | |
690 | ||
691 | function contactTypeDelete($contactTypeId) { | |
692 | require_once 'CRM/Contact/BAO/ContactType.php'; | |
693 | $result = CRM_Contact_BAO_ContactType::del($contactTypeId); | |
694 | if (!$result) { | |
695 | throw new Exception('Could not delete contact type'); | |
696 | } | |
697 | } | |
698 | ||
699 | function membershipTypeCreate($contactID, $contributionTypeID = 1, $version = 3) { | |
700 | require_once 'CRM/Member/PseudoConstant.php'; | |
701 | CRM_Member_PseudoConstant::flush('membershipType'); | |
702 | CRM_Core_Config::clearDBCache(); | |
703 | $params = array( | |
704 | 'name' => 'General', | |
705 | 'duration_unit' => 'year', | |
706 | 'duration_interval' => 1, | |
707 | 'period_type' => 'rolling', | |
708 | 'member_of_contact_id' => $contactID, | |
709 | 'domain_id' => 1, | |
710 | // FIXME: I know it's 1, cause it was loaded directly to the db. | |
711 | // FIXME: when we load all the data, we'll need to address this to | |
712 | // FIXME: avoid hunting numbers around. | |
713 | 'financial_type_id' => $contributionTypeID, | |
714 | 'is_active' => 1, | |
715 | 'version' => $version, | |
716 | 'sequential' => 1, | |
717 | 'visibility' => 1, | |
718 | ); | |
719 | ||
720 | $result = civicrm_api('MembershipType', 'Create', $params); | |
721 | require_once 'CRM/Member/PseudoConstant.php'; | |
722 | CRM_Member_PseudoConstant::flush('membershipType'); | |
723 | CRM_Utils_Cache::singleton()->flush(); | |
724 | if (CRM_Utils_Array::value('is_error', $result) || | |
725 | (!CRM_Utils_Array::value('id', $result) && !CRM_Utils_Array::value('id', $result['values'][0])) | |
726 | ) { | |
727 | throw new Exception('Could not create membership type, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
728 | } | |
729 | ||
730 | return $result['id']; | |
731 | } | |
732 | ||
733 | function contactMembershipCreate($params) { | |
734 | $pre = array( | |
735 | 'join_date' => '2007-01-21', | |
736 | 'start_date' => '2007-01-21', | |
737 | 'end_date' => '2007-12-21', | |
738 | 'source' => 'Payment', | |
739 | 'version' => API_LATEST_VERSION, | |
740 | ); | |
741 | ||
742 | foreach ($pre as $key => $val) { | |
743 | if (!isset($params[$key])) { | |
744 | $params[$key] = $val; | |
745 | } | |
746 | } | |
747 | ||
748 | $result = civicrm_api('Membership', 'create', $params); | |
749 | ||
750 | if (CRM_Utils_Array::value('is_error', $result) || | |
751 | !CRM_Utils_Array::value('id', $result) | |
752 | ) { | |
753 | if (CRM_Utils_Array::value('error_message', $result)) { | |
754 | throw new Exception('Could not create membership, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
755 | } | |
756 | else { | |
757 | throw new Exception('Could not create membership' . ' - in line: ' . __LINE__); | |
758 | } | |
759 | } | |
760 | ||
761 | return $result['id']; | |
762 | } | |
763 | ||
764 | /** | |
765 | * Function to delete Membership Type | |
766 | * | |
767 | * @param int $membershipTypeID | |
768 | */ | |
769 | function membershipTypeDelete($params) { | |
770 | $params['version'] = API_LATEST_VERSION; | |
771 | ||
772 | $result = civicrm_api('MembershipType', 'Delete', $params); | |
773 | if (CRM_Utils_Array::value('is_error', $result)) { | |
774 | throw new Exception('Could not delete membership type' . $result['error_message']); | |
775 | } | |
776 | return; | |
777 | } | |
778 | ||
779 | function membershipDelete($membershipID) { | |
780 | $deleteParams = array('version' => 3, 'id' => $membershipID); | |
781 | $result = civicrm_api('Membership', 'Delete', $deleteParams); | |
782 | if (CRM_Utils_Array::value('is_error', $result)) { | |
783 | throw new Exception('Could not delete membership ' . $result['error_message'] . " params were " . print_r($deleteParams, TRUE)); | |
784 | } | |
785 | return; | |
786 | } | |
787 | ||
788 | function membershipStatusCreate($name = 'test member status') { | |
789 | $params['name'] = $name; | |
790 | $params['start_event'] = 'start_date'; | |
791 | $params['end_event'] = 'end_date'; | |
792 | $params['is_current_member'] = 1; | |
793 | $params['is_active'] = 1; | |
794 | $params['version'] = API_LATEST_VERSION; | |
795 | ||
796 | $result = civicrm_api('MembershipStatus', 'Create', $params); | |
797 | require_once 'CRM/Member/PseudoConstant.php'; | |
798 | CRM_Member_PseudoConstant::flush('membershipStatus'); | |
799 | if (CRM_Utils_Array::value('is_error', $result)) { | |
800 | throw new Exception("Could not create membership status: $name, Error message: " . $result['error_message']); | |
801 | exit(); | |
802 | } | |
803 | return $result['id']; | |
804 | } | |
805 | ||
806 | function membershipStatusDelete($membershipStatusID) { | |
807 | if (!$membershipStatusID) { | |
808 | return; | |
809 | } | |
810 | $result = civicrm_api('MembershipStatus', 'Delete', array('id' => $membershipStatusID, 'version' => 3)); | |
811 | if (CRM_Utils_Array::value('is_error', $result)) { | |
812 | throw new Exception('Could not delete membership status' . $result['error_message']); | |
813 | } | |
814 | return; | |
815 | } | |
816 | ||
817 | function relationshipTypeCreate($params = NULL) { | |
818 | if (is_null($params)) { | |
819 | $params = array( | |
820 | 'name_a_b' => 'Relation 1 for relationship type create', | |
821 | 'name_b_a' => 'Relation 2 for relationship type create', | |
822 | 'contact_type_a' => 'Individual', | |
823 | 'contact_type_b' => 'Organization', | |
824 | 'is_reserved' => 1, | |
825 | 'is_active' => 1, | |
826 | ); | |
827 | } | |
828 | ||
829 | $params['version'] = API_LATEST_VERSION; | |
830 | ||
831 | $result = civicrm_api('relationship_type', 'create', $params); | |
832 | ||
833 | if (civicrm_error($params) || CRM_Utils_Array::value('is_error', $result)) { | |
834 | throw new Exception('Could not create relationship type'); | |
835 | } | |
836 | ||
837 | require_once 'CRM/Core/PseudoConstant.php'; | |
838 | CRM_Core_PseudoConstant::flush('relationshipType'); | |
839 | ||
840 | return $result['id']; | |
841 | } | |
842 | ||
843 | /** | |
844 | * Function to delete Relatinship Type | |
845 | * | |
846 | * @param int $relationshipTypeID | |
847 | */ | |
848 | function relationshipTypeDelete($relationshipTypeID) { | |
849 | $params['id'] = $relationshipTypeID; | |
850 | $params['version'] = API_LATEST_VERSION; | |
851 | civicrm_api('relationship_type', 'delete', $params); | |
852 | ||
853 | if (civicrm_error($params)) { | |
854 | throw new Exception('Could not delete relationship type'); | |
855 | } | |
856 | ||
857 | return; | |
858 | } | |
859 | ||
860 | function paymentProcessorTypeCreate($params = NULL) { | |
861 | if (is_null($params)) { | |
862 | $params = array( | |
863 | 'name' => 'API_Test_PP', | |
864 | 'title' => 'API Test Payment Processor', | |
865 | 'class_name' => 'CRM_Core_Payment_APITest', | |
866 | 'billing_mode' => 'form', | |
867 | 'is_recur' => 0, | |
868 | 'is_reserved' => 1, | |
869 | 'is_active' => 1, | |
870 | ); | |
871 | } | |
872 | ||
873 | $params['version'] = API_LATEST_VERSION; | |
874 | ||
875 | $result = civicrm_api('payment_processor_type', 'create', $params); | |
876 | ||
877 | if (civicrm_error($params) || CRM_Utils_Array::value('is_error', $result)) { | |
878 | throw new Exception('Could not create payment processor type'); | |
879 | } | |
880 | ||
881 | require_once 'CRM/Core/PseudoConstant.php'; | |
882 | CRM_Core_PseudoConstant::flush('paymentProcessorType'); | |
883 | ||
884 | return $result['id']; | |
885 | } | |
886 | ||
887 | /** | |
888 | * Function to create Participant | |
889 | * | |
890 | * @param array $params array of contact id and event id values | |
891 | * | |
892 | * @return int $id of participant created | |
893 | */ | |
894 | function participantCreate($params) { | |
895 | $defaults = array( | |
896 | 'contact_id' => $params['contactID'], | |
897 | 'event_id' => $params['eventID'], | |
898 | 'status_id' => 2, | |
899 | 'role_id' => 1, | |
900 | 'register_date' => 20070219, | |
901 | 'source' => 'Wimbeldon', | |
902 | 'event_level' => 'Payment', | |
903 | 'version' => API_LATEST_VERSION, | |
904 | ); | |
905 | ||
906 | $params = array_merge($defaults, $params); | |
907 | $result = civicrm_api('Participant', 'create', $params); | |
908 | if (CRM_Utils_Array::value('is_error', $result)) { | |
909 | throw new Exception('Could not create participant ' . $result['error_message']); | |
910 | } | |
911 | return $result['id']; | |
912 | } | |
913 | ||
914 | /** | |
915 | * Function to create Payment Processor | |
916 | * | |
917 | * @return object of Payment Processsor | |
918 | */ | |
919 | function processorCreate() { | |
920 | $processorParams = array( | |
921 | 'domain_id' => 1, | |
922 | 'name' => 'Dummy', | |
923 | 'payment_processor_type_id' => 10, | |
924 | 'financial_account_id' => 12, | |
925 | 'is_active' => 1, | |
926 | 'user_name' => '', | |
927 | 'url_site' => 'http://dummy.com', | |
928 | 'url_recur' => 'http://dummy.com', | |
929 | 'billing_mode' => 1, | |
930 | ); | |
931 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($processorParams); | |
932 | return $paymentProcessor; | |
933 | } | |
934 | ||
935 | /** | |
936 | * Function to create contribution page | |
937 | * | |
938 | * @return object of contribution page | |
939 | */ | |
940 | function contributionPageCreate($params) { | |
941 | $this->_pageParams = array( | |
942 | 'version' => 3, | |
943 | 'title' => 'Test Contribution Page', | |
944 | 'financial_type_id' => 1, | |
945 | 'currency' => 'USD', | |
946 | 'financial_account_id' => 1, | |
947 | 'payment_processor' => $params['processor_id'], | |
948 | 'is_active' => 1, | |
949 | 'is_allow_other_amount' => 1, | |
950 | 'min_amount' => 10, | |
951 | 'max_amount' => 1000, | |
952 | ); | |
953 | $contributionPage = civicrm_api('contribution_page', 'create', $this->_pageParams); | |
954 | return $contributionPage; | |
955 | } | |
956 | ||
957 | /** | |
958 | * Function to create Financial Type | |
959 | * | |
960 | * @return int $id of financial account created | |
961 | */ | |
962 | function contributionTypeCreate() { | |
963 | ||
964 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
965 | $op->execute($this->_dbconn, | |
966 | new PHPUnit_Extensions_Database_DataSet_XMLDataSet( | |
967 | dirname(__FILE__) . '/../api/v' . API_LATEST_VERSION . '/dataset/financial_types.xml' | |
968 | ) | |
969 | ); | |
970 | ||
971 | require_once 'CRM/Contribute/PseudoConstant.php'; | |
972 | $financialType = CRM_Contribute_PseudoConstant::financialType(); | |
973 | CRM_Contribute_PseudoConstant::flush('financialType'); | |
974 | return key($financialType); | |
975 | } | |
976 | ||
977 | /** | |
978 | * Function to delete financial Types | |
979 | * * @param int $contributionTypeId | |
980 | */ | |
981 | function contributionTypeDelete($contributionTypeID = NULL) { | |
982 | if ($contributionTypeID === NULL) { | |
983 | civicrm_api('Contribution', 'get', | |
984 | array( | |
985 | 'version' => 3, | |
986 | 'financial_type_id' => 10, | |
987 | 'api.contribution.delete' => 1, | |
988 | ) | |
989 | ); | |
990 | civicrm_api('Contribution', 'get', | |
991 | array( | |
992 | 'version' => 3, | |
993 | 'financial_type_id' => 11, | |
994 | 'api.contribution.delete' => 1, | |
995 | ) | |
996 | ); | |
997 | ||
998 | // we know those were loaded from /dataset/financial_types.xml | |
999 | $del = CRM_Financial_BAO_FinancialType::del(10, 1); | |
1000 | $del = CRM_Financial_BAO_FinancialType::del(11, 1); | |
1001 | } | |
1002 | else { | |
1003 | civicrm_api('Contribution', 'get', array( | |
1004 | 'version' => 3, | |
1005 | 'financial_type_id' => $contributionTypeID, | |
1006 | 'api.contribution.delete' => 1 | |
1007 | )); | |
1008 | $del = CRM_Financial_BAO_FinancialType::del($contributionTypeID, 1); | |
1009 | } | |
1010 | if (is_array($del)) { | |
1011 | $this->assertEquals(0, CRM_Utils_Array::value('is_error', $del), $del['error_message']); | |
1012 | } | |
1013 | } | |
1014 | ||
1015 | /** | |
1016 | * Function to create Tag | |
1017 | * | |
1018 | * @return int tag_id of created tag | |
1019 | */ | |
1020 | function tagCreate($params = NULL) { | |
1021 | if ($params === NULL) { | |
1022 | $params = array( | |
1023 | 'name' => 'New Tag3' . rand(), | |
1024 | 'description' => 'This is description for New Tag ' . rand(), | |
1025 | 'domain_id' => '1', | |
1026 | 'version' => API_LATEST_VERSION, | |
1027 | ); | |
1028 | } | |
1029 | ||
1030 | $result = civicrm_api('Tag', 'create', $params); | |
1031 | return $result; | |
1032 | } | |
1033 | ||
1034 | /** | |
1035 | * Function to delete Tag | |
1036 | * | |
1037 | * @param int $tagId id of the tag to be deleted | |
1038 | */ | |
1039 | function tagDelete($tagId) { | |
1040 | require_once 'api/api.php'; | |
1041 | $params = array( | |
1042 | 'tag_id' => $tagId, | |
1043 | 'version' => API_LATEST_VERSION, | |
1044 | ); | |
1045 | $result = civicrm_api('Tag', 'delete', $params); | |
1046 | if (CRM_Utils_Array::value('is_error', $result)) { | |
1047 | throw new Exception('Could not delete tag'); | |
1048 | } | |
1049 | return $result['id']; | |
1050 | } | |
1051 | ||
1052 | /** | |
1053 | * Add entity(s) to the tag | |
1054 | * | |
1055 | * @param array $params | |
1056 | * | |
1057 | */ | |
1058 | function entityTagAdd($params) { | |
1059 | $params['version'] = API_LATEST_VERSION; | |
1060 | $result = civicrm_api('entity_tag', 'create', $params); | |
1061 | ||
1062 | if (CRM_Utils_Array::value('is_error', $result)) { | |
1063 | throw new Exception('Error while creating entity tag'); | |
1064 | } | |
1065 | return TRUE; | |
1066 | } | |
1067 | ||
1068 | /** | |
1069 | * Function to create contribution | |
1070 | * | |
1071 | * @param int $cID contact_id | |
1072 | * @param int $cTypeID id of financial type | |
1073 | * | |
1074 | * @return int id of created contribution | |
1075 | */ | |
1076 | function pledgeCreate($cID) { | |
1077 | $params = array( | |
1078 | 'contact_id' => $cID, | |
1079 | 'pledge_create_date' => date('Ymd'), | |
1080 | 'start_date' => date('Ymd'), | |
1081 | 'scheduled_date' => date('Ymd'), | |
1082 | 'amount' => 100.00, | |
1083 | 'pledge_status_id' => '2', | |
1084 | 'financial_type_id' => '1', | |
1085 | 'pledge_original_installment_amount' => 20, | |
1086 | 'frequency_interval' => 5, | |
1087 | 'frequency_unit' => 'year', | |
1088 | 'frequency_day' => 15, | |
1089 | 'installments' => 5, | |
1090 | 'version' => API_LATEST_VERSION, | |
1091 | ); | |
1092 | ||
1093 | $result = civicrm_api('Pledge', 'create', $params); | |
1094 | return $result['id']; | |
1095 | } | |
1096 | ||
1097 | /** | |
1098 | * Function to delete contribution | |
1099 | * | |
1100 | * @param int $contributionId | |
1101 | */ | |
1102 | function pledgeDelete($pledgeId) { | |
1103 | $params = array( | |
1104 | 'pledge_id' => $pledgeId, | |
1105 | 'version' => API_LATEST_VERSION, | |
1106 | ); | |
1107 | ||
1108 | civicrm_api('Pledge', 'delete', $params); | |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * Function to create contribution | |
1113 | * | |
1114 | * @param int $cID contact_id | |
1115 | * @param int $cTypeID id of financial type | |
1116 | * | |
1117 | * @return int id of created contribution | |
1118 | */ | |
1119 | function contributionCreate($cID, $cTypeID = 1, $invoiceID = 67890, $trxnID = 12345, $paymentInstrumentID = 1, $isFee = TRUE) { | |
1120 | $params = array( | |
1121 | 'domain_id' => 1, | |
1122 | 'contact_id' => $cID, | |
1123 | 'receive_date' => date('Ymd'), | |
1124 | 'total_amount' => 100.00, | |
1125 | 'financial_type_id' => empty($cTypeID) ? 1 : $cTypeID, | |
1126 | 'payment_instrument_id' => empty($paymentInstrumentID) ? 1 : $paymentInstrumentID, | |
1127 | 'non_deductible_amount' => 10.00, | |
1128 | 'trxn_id' => $trxnID, | |
1129 | 'invoice_id' => $invoiceID, | |
1130 | 'source' => 'SSF', | |
1131 | 'version' => API_LATEST_VERSION, | |
1132 | 'contribution_status_id' => 1, | |
1133 | // 'note' => 'Donating for Nobel Cause', *Fixme | |
1134 | ); | |
1135 | ||
1136 | if ($isFee) { | |
1137 | $params['fee_amount'] = 5.00; | |
1138 | $params['net_amount'] = 95.00; | |
1139 | } | |
1140 | ||
1141 | $result = civicrm_api('contribution', 'create', $params); | |
1142 | if (CRM_Utils_Array::value('is_error', $result) || | |
1143 | !CRM_Utils_Array::value('id', $result) | |
1144 | ) { | |
1145 | if (CRM_Utils_Array::value('error_message', $result)) { | |
1146 | throw new Exception('Could not create contribution, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
1147 | } | |
1148 | else { | |
1149 | throw new Exception('Could not create contribution in line: ' . __LINE__); | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | return $result['id']; | |
1154 | } | |
1155 | ||
1156 | /** | |
1157 | * Function to create online contribution | |
1158 | * | |
1159 | * @param int $financialType id of financial type | |
1160 | * | |
1161 | * @return int id of created contribution | |
1162 | */ | |
1163 | function onlineContributionCreate($params, $financialType, $invoiceID = 67890, $trxnID = 12345) { | |
1164 | $contribParams = array( | |
1165 | 'contact_id' => $params['contact_id'], | |
1166 | 'receive_date' => date('Ymd'), | |
1167 | 'total_amount' => 100.00, | |
1168 | 'financial_type_id' => $financialType, | |
1169 | 'contribution_page_id' => $params['contribution_page_id'], | |
1170 | 'trxn_id' => 12345, | |
1171 | 'invoice_id' => 67890, | |
1172 | 'source' => 'SSF', | |
1173 | 'version' => $this->_apiversion, | |
1174 | ); | |
1175 | ||
1176 | if (isset($params['contribution_status_id'])) { | |
1177 | $contribParams['contribution_status_id'] = $params['contribution_status_id']; | |
1178 | } | |
1179 | else { | |
1180 | $contribParams['contribution_status_id'] = 1; | |
1181 | } | |
1182 | if (isset($params['is_pay_later'])) { | |
1183 | $contribParams['is_pay_later'] = 1; | |
1184 | } | |
1185 | if (isset($params['payment_processor'])) { | |
1186 | $contribParams['payment_processor'] = $params['payment_processor']; | |
1187 | } | |
1188 | $result = civicrm_api('contribution', 'create', $contribParams); | |
1189 | if (CRM_Utils_Array::value('is_error', $result) || | |
1190 | !CRM_Utils_Array::value('id', $result) | |
1191 | ) { | |
1192 | if (CRM_Utils_Array::value('error_message', $result)) { | |
1193 | throw new Exception('Could not create contribution, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
1194 | } | |
1195 | else { | |
1196 | throw new Exception('Could not create contribution in line: ' . __LINE__); | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | return $result['id']; | |
1201 | } | |
1202 | ||
1203 | /** | |
1204 | * Function to delete contribution | |
1205 | * | |
1206 | * @param int $contributionId | |
1207 | */ | |
1208 | function contributionDelete($contributionId) { | |
1209 | $params = array( | |
1210 | 'contribution_id' => $contributionId, | |
1211 | 'version' => API_LATEST_VERSION, | |
1212 | ); | |
1213 | $result = civicrm_api('contribution', 'delete', $params); | |
1214 | ||
1215 | if (CRM_Utils_Array::value('is_error', $result) || | |
1216 | !CRM_Utils_Array::value('id', $result) | |
1217 | ) { | |
1218 | if (CRM_Utils_Array::value('error_message', $result)) { | |
1219 | throw new Exception('Could not delete contribution, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
1220 | } | |
1221 | else { | |
1222 | throw new Exception('Could not delete contribution - in line: ' . __LINE__); | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | return $result; | |
1227 | } | |
1228 | ||
1229 | /** | |
1230 | * Function to create an Event | |
1231 | * | |
1232 | * @param array $params name-value pair for an event | |
1233 | * | |
1234 | * @return array $event | |
1235 | */ | |
1236 | function eventCreate($params = array()) { | |
1237 | // if no contact was passed, make up a dummy event creator | |
1238 | if (!isset($params['contact_id'])) { | |
1239 | $params['contact_id'] = $this->_contactCreate(array( | |
1240 | 'contact_type' => 'Individual', | |
1241 | 'first_name' => 'Event', | |
1242 | 'last_name' => 'Creator', | |
1243 | 'version' => API_LATEST_VERSION | |
1244 | )); | |
1245 | } | |
1246 | ||
1247 | // set defaults for missing params | |
1248 | $params = array_merge(array( | |
1249 | 'title' => 'Annual CiviCRM meet', | |
1250 | 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now', | |
1251 | 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues', | |
1252 | 'event_type_id' => 1, | |
1253 | 'is_public' => 1, | |
1254 | 'start_date' => 20081021, | |
1255 | 'end_date' => 20081023, | |
1256 | 'is_online_registration' => 1, | |
1257 | 'registration_start_date' => 20080601, | |
1258 | 'registration_end_date' => 20081015, | |
1259 | 'max_participants' => 100, | |
1260 | 'event_full_text' => 'Sorry! We are already full', | |
1261 | 'is_monetory' => 0, | |
1262 | 'is_active' => 1, | |
1263 | 'version' => API_LATEST_VERSION, | |
1264 | 'is_show_location' => 0, | |
1265 | ), $params); | |
1266 | ||
1267 | $result = civicrm_api('Event', 'create', $params); | |
1268 | if ($result['is_error'] == 1) { | |
1269 | throw new Exception($result['error_message']); | |
1270 | } | |
1271 | return $result; | |
1272 | } | |
1273 | ||
1274 | /** | |
1275 | * Function to delete event | |
1276 | * | |
1277 | * @param int $id ID of the event | |
1278 | */ | |
1279 | function eventDelete($id) { | |
1280 | $params = array( | |
1281 | 'event_id' => $id, | |
1282 | 'version' => API_LATEST_VERSION, | |
1283 | ); | |
1284 | civicrm_api('event', 'delete', $params); | |
1285 | } | |
1286 | ||
1287 | /** | |
1288 | * Function to delete participant | |
1289 | * | |
1290 | * @param int $participantID | |
1291 | */ | |
1292 | function participantDelete($participantID) { | |
1293 | $params = array( | |
1294 | 'id' => $participantID, | |
1295 | 'version' => API_LATEST_VERSION, | |
1296 | ); | |
1297 | $result = civicrm_api('Participant', 'delete', $params); | |
1298 | ||
1299 | if (CRM_Utils_Array::value('is_error', $result)) { | |
1300 | throw new Exception('Could not delete participant'); | |
1301 | } | |
1302 | return; | |
1303 | } | |
1304 | ||
1305 | /** | |
1306 | * Function to create participant payment | |
1307 | * | |
1308 | * @return int $id of created payment | |
1309 | */ | |
1310 | function participantPaymentCreate($participantID, $contributionID = NULL) { | |
1311 | //Create Participant Payment record With Values | |
1312 | $params = array( | |
1313 | 'participant_id' => $participantID, | |
1314 | 'contribution_id' => $contributionID, | |
1315 | 'version' => API_LATEST_VERSION, | |
1316 | ); | |
1317 | ||
1318 | $result = civicrm_api('participant_payment', 'create', $params); | |
1319 | ||
1320 | if (CRM_Utils_Array::value('is_error', $result) || | |
1321 | !CRM_Utils_Array::value('id', $result) | |
1322 | ) { | |
1323 | if (CRM_Utils_Array::value('error_message', $result)) { | |
1324 | throw new Exception('Could not delete contribution, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
1325 | } | |
1326 | else { | |
1327 | throw new Exception('Could not delete contribution - in line: ' . __LINE__); | |
1328 | } | |
1329 | } | |
1330 | ||
1331 | return $result['id']; | |
1332 | } | |
1333 | ||
1334 | /** | |
1335 | * Function to delete participant payment | |
1336 | * | |
1337 | * @param int $paymentID | |
1338 | */ | |
1339 | function participantPaymentDelete($paymentID) { | |
1340 | $params = array( | |
1341 | 'id' => $paymentID, | |
1342 | 'version' => API_LATEST_VERSION, | |
1343 | ); | |
1344 | ||
1345 | $result = civicrm_api('participant_payment', 'delete', $params); | |
1346 | ||
1347 | if (CRM_Utils_Array::value('is_error', $result)) { | |
1348 | if (CRM_Utils_Array::value('error_message', $result)) { | |
1349 | throw new Exception('Could not delete contribution, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
1350 | } | |
1351 | else { | |
1352 | throw new Exception('Could not delete contribution - in line: ' . __LINE__); | |
1353 | } | |
1354 | } | |
1355 | ||
1356 | return; | |
1357 | } | |
1358 | ||
1359 | /** | |
1360 | * Function to add a Location | |
1361 | * | |
1362 | * @return int location id of created location | |
1363 | */ | |
1364 | function locationAdd($contactID) { | |
1365 | $address = array( | |
1366 | 1 => array( | |
1367 | 'location_type' => 'New Location Type', | |
1368 | 'is_primary' => 1, | |
1369 | 'name' => 'Saint Helier St', | |
1370 | 'county' => 'Marin', | |
1371 | 'country' => 'United States', | |
1372 | 'state_province' => 'Michigan', | |
1373 | 'supplemental_address_1' => 'Hallmark Ct', | |
1374 | 'supplemental_address_2' => 'Jersey Village', | |
1375 | ) | |
1376 | ); | |
1377 | ||
1378 | $params = array( | |
1379 | 'contact_id' => $contactID, | |
1380 | 'address' => $address, | |
1381 | 'version' => 2, | |
1382 | 'location_format' => '2.0', | |
1383 | 'location_type' => 'New Location Type', | |
1384 | ); | |
1385 | ||
1386 | $result = civicrm_api('Location', 'create', $params); | |
1387 | ||
1388 | if (civicrm_error($result)) { | |
1389 | throw new Exception("Could not create location: {$result['error_message']}"); | |
1390 | } | |
1391 | ||
1392 | return $result; | |
1393 | } | |
1394 | ||
1395 | /** | |
1396 | * Function to delete Locations of contact | |
1397 | * | |
1398 | * @params array $pamars parameters | |
1399 | */ | |
1400 | function locationDelete($params) { | |
1401 | $params['version'] = 2; | |
1402 | ||
1403 | $result = civicrm_api('Location', 'delete', $params); | |
1404 | if (civicrm_error($result)) { | |
1405 | throw new Exception("Could not delete location: {$result['error_message']}"); | |
1406 | } | |
1407 | ||
1408 | return; | |
1409 | } | |
1410 | ||
1411 | /** | |
1412 | * Function to add a Location Type | |
1413 | * | |
1414 | * @return int location id of created location | |
1415 | */ | |
1416 | function locationTypeCreate($params = NULL) { | |
1417 | if ($params === NULL) { | |
1418 | $params = array( | |
1419 | 'name' => 'New Location Type', | |
1420 | 'vcard_name' => 'New Location Type', | |
1421 | 'description' => 'Location Type for Delete', | |
1422 | 'is_active' => 1, | |
1423 | ); | |
1424 | } | |
1425 | ||
1426 | require_once 'CRM/Core/DAO/LocationType.php'; | |
1427 | $locationType = new CRM_Core_DAO_LocationType(); | |
1428 | $locationType->copyValues($params); | |
1429 | $locationType->save(); | |
1430 | // clear getfields cache | |
1431 | CRM_Core_PseudoConstant::flush('locationType'); | |
1432 | civicrm_api('phone', 'getfields', array('version' => 3, 'cache_clear' => 1)); | |
1433 | return $locationType; | |
1434 | } | |
1435 | ||
1436 | /** | |
1437 | * Function to delete a Location Type | |
1438 | * | |
1439 | * @param int location type id | |
1440 | */ | |
1441 | function locationTypeDelete($locationTypeId) { | |
1442 | require_once 'CRM/Core/DAO/LocationType.php'; | |
1443 | $locationType = new CRM_Core_DAO_LocationType(); | |
1444 | $locationType->id = $locationTypeId; | |
1445 | $locationType->delete(); | |
1446 | } | |
1447 | ||
1448 | /** | |
1449 | * Function to add a Group | |
1450 | * | |
1451 | * @params array to add group | |
1452 | * | |
1453 | * @return int groupId of created group | |
1454 | * | |
1455 | */ | |
1456 | function groupCreate($params = NULL) { | |
1457 | if ($params === NULL) { | |
1458 | $params = array( | |
1459 | 'name' => 'Test Group 1', | |
1460 | 'domain_id' => 1, | |
1461 | 'title' => 'New Test Group Created', | |
1462 | 'description' => 'New Test Group Created', | |
1463 | 'is_active' => 1, | |
1464 | 'visibility' => 'Public Pages', | |
1465 | 'group_type' => array( | |
1466 | '1' => 1, | |
1467 | '2' => 1, | |
1468 | ), | |
1469 | 'version' => API_LATEST_VERSION, | |
1470 | ); | |
1471 | } | |
1472 | ||
1473 | $result = civicrm_api('Group', 'create', $params); | |
1474 | if (CRM_Utils_Array::value('id', $result)) { | |
1475 | return $result['id']; | |
1476 | } | |
1477 | elseif (CRM_Utils_Array::value('result', $result)) { | |
1478 | return $result['result']->id; | |
1479 | } | |
1480 | else { | |
1481 | return NULL; | |
1482 | } | |
1483 | } | |
1484 | ||
1485 | /** | |
1486 | * Function to delete a Group | |
1487 | * | |
1488 | * @param int $id | |
1489 | */ | |
1490 | function groupDelete($gid) { | |
1491 | ||
1492 | $params = array( | |
1493 | 'id' => $gid, | |
1494 | 'version' => API_LATEST_VERSION, | |
1495 | ); | |
1496 | ||
1497 | civicrm_api('Group', 'delete', $params); | |
1498 | } | |
1499 | ||
1500 | /** | |
1501 | * Function to add a UF Join Entry | |
1502 | * | |
1503 | * @return int $id of created UF Join | |
1504 | */ | |
1505 | function ufjoinCreate($params = NULL) { | |
1506 | if ($params === NULL) { | |
1507 | $params = array( | |
1508 | 'is_active' => 1, | |
1509 | 'module' => 'CiviEvent', | |
1510 | 'entity_table' => 'civicrm_event', | |
1511 | 'entity_id' => 3, | |
1512 | 'weight' => 1, | |
1513 | 'uf_group_id' => 1, | |
1514 | ); | |
1515 | } | |
1516 | ||
1517 | $result = crm_add_uf_join($params); | |
1518 | ||
1519 | return $result; | |
1520 | } | |
1521 | ||
1522 | /** | |
1523 | * Function to delete a UF Join Entry | |
1524 | * | |
1525 | * @param array with missing uf_group_id | |
1526 | */ | |
1527 | function ufjoinDelete($params = NULL) { | |
1528 | if ($params === NULL) { | |
1529 | $params = array( | |
1530 | 'is_active' => 1, | |
1531 | 'module' => 'CiviEvent', | |
1532 | 'entity_table' => 'civicrm_event', | |
1533 | 'entity_id' => 3, | |
1534 | 'weight' => 1, | |
1535 | 'uf_group_id' => '', | |
1536 | ); | |
1537 | } | |
1538 | ||
1539 | crm_add_uf_join($params); | |
1540 | } | |
1541 | ||
1542 | /** | |
1543 | * Function to create Group for a contact | |
1544 | * | |
1545 | * @param int $contactId | |
1546 | */ | |
1547 | function contactGroupCreate($contactId) { | |
1548 | $params = array( | |
1549 | 'contact_id.1' => $contactId, | |
1550 | 'group_id' => 1, | |
1551 | ); | |
1552 | ||
1553 | civicrm_api('GroupContact', 'Create', $params); | |
1554 | } | |
1555 | ||
1556 | /** | |
1557 | * Function to delete Group for a contact | |
1558 | * | |
1559 | * @param array $params | |
1560 | */ | |
1561 | function contactGroupDelete($contactId) { | |
1562 | $params = array( | |
1563 | 'contact_id.1' => $contactId, | |
1564 | 'group_id' => 1, | |
1565 | ); | |
1566 | civicrm_api('GroupContact', 'Delete', $params); | |
1567 | } | |
1568 | ||
1569 | /** | |
1570 | * Function to create Activity | |
1571 | * | |
1572 | * @param int $contactId | |
1573 | */ | |
1574 | function activityCreate($params = NULL) { | |
1575 | ||
1576 | if ($params === NULL) { | |
1577 | $individualSourceID = $this->individualCreate(NULL); | |
1578 | ||
1579 | $contactParams = array( | |
1580 | 'first_name' => 'Julia', | |
1581 | 'Last_name' => 'Anderson', | |
1582 | 'prefix' => 'Ms.', | |
1583 | 'email' => 'julia_anderson@civicrm.org', | |
1584 | 'contact_type' => 'Individual', | |
1585 | 'version' => API_LATEST_VERSION, | |
1586 | ); | |
1587 | ||
1588 | $individualTargetID = $this->individualCreate($contactParams); | |
1589 | ||
1590 | $params = array( | |
1591 | 'source_contact_id' => $individualSourceID, | |
1592 | 'target_contact_id' => array($individualTargetID), | |
1593 | 'assignee_contact_id' => array($individualTargetID), | |
1594 | 'subject' => 'Discussion on warm beer', | |
1595 | 'activity_date_time' => date('Ymd'), | |
1596 | 'duration_hours' => 30, | |
1597 | 'duration_minutes' => 20, | |
1598 | 'location' => 'Baker Street', | |
1599 | 'details' => 'Lets schedule a meeting', | |
1600 | 'status_id' => 1, | |
1601 | 'activity_name' => 'Meeting', | |
1602 | 'version' => API_LATEST_VERSION, | |
1603 | ); | |
1604 | } | |
1605 | ||
1606 | ||
1607 | $result = civicrm_api('Activity', 'create', $params); | |
1608 | ||
1609 | $result['target_contact_id'] = $individualTargetID; | |
1610 | $result['assignee_contact_id'] = $individualTargetID; | |
1611 | return $result; | |
1612 | } | |
1613 | ||
1614 | /** | |
1615 | * Function to create an activity type | |
1616 | * | |
1617 | * @params array $params parameters | |
1618 | */ | |
1619 | function activityTypeCreate($params) { | |
1620 | $params['version'] = API_LATEST_VERSION; | |
1621 | $result = civicrm_api('ActivityType', 'create', $params); | |
1622 | if (CRM_Utils_Array::value('is_error', $result) || | |
1623 | !CRM_Utils_Array::value('id', $result) | |
1624 | ) { | |
1625 | throw new Exception('Could not create Activity type ' . $result['error_message']); | |
1626 | } | |
1627 | return $result; | |
1628 | } | |
1629 | ||
1630 | /** | |
1631 | * Function to delete activity type | |
1632 | * | |
1633 | * @params Integer $activityTypeId id of the activity type | |
1634 | */ | |
1635 | function activityTypeDelete($activityTypeId) { | |
1636 | $params['activity_type_id'] = $activityTypeId; | |
1637 | $params['version'] = API_LATEST_VERSION; | |
1638 | $result = civicrm_api('ActivityType', 'delete', $params); | |
1639 | if (!$result) { | |
1640 | throw new Exception('Could not delete activity type'); | |
1641 | } | |
1642 | return $result; | |
1643 | } | |
1644 | ||
1645 | /** | |
1646 | * Function to create custom group | |
1647 | * | |
1648 | * @param string $className | |
1649 | * @param string $title name of custom group | |
1650 | */ | |
1651 | function customGroupCreate($extends = 'Contact', $title = 'title') { | |
1652 | ||
1653 | if (CRM_Utils_Array::value('title', $extends)) { | |
1654 | $params = $extends; | |
1655 | $params['title'] = strlen($params['title']) > 13 ? substr($params['title'], 0, 13) : $params['title']; | |
1656 | } | |
1657 | else { | |
1658 | $params = array( | |
1659 | 'title' => strlen($title) > 13 ? substr($title, 0, 13) : $title, | |
1660 | 'extends' => $extends, | |
1661 | 'domain_id' => 1, | |
1662 | 'style' => 'Inline', | |
1663 | 'is_active' => 1, | |
1664 | 'version' => API_LATEST_VERSION, | |
1665 | ); | |
1666 | } | |
1667 | //have a crack @ deleting it first in the hope this will prevent derailing our tests | |
1668 | $check = civicrm_api('custom_group', 'get', array_merge($params, array('api.custom_group.delete' => 1))); | |
1669 | ||
1670 | $result = civicrm_api('custom_group', 'create', $params); | |
1671 | ||
1672 | if (CRM_Utils_Array::value('is_error', $result) || | |
1673 | !CRM_Utils_Array::value('id', $result) | |
1674 | ) { | |
1675 | throw new Exception('Could not create Custom Group ' . print_r($params, TRUE) . $result['error_message']); | |
1676 | } | |
1677 | return $result; | |
1678 | } | |
1679 | ||
1680 | /** | |
1681 | * existing function doesn't allow params to be over-ridden so need a new one | |
1682 | * this one allows you to only pass in the params you want to change | |
1683 | */ | |
1684 | function CustomGroupCreateByParams($params = array()) { | |
1685 | $defaults = array( | |
1686 | 'title' => "API Custom Group", | |
1687 | 'extends' => 'Contact', | |
1688 | 'domain_id' => 1, | |
1689 | 'style' => 'Inline', | |
1690 | 'is_active' => 1, | |
1691 | 'version' => API_LATEST_VERSION, | |
1692 | ); | |
1693 | $params = array_merge($defaults, $params); | |
1694 | $result = civicrm_api('custom_group', 'create', $params); | |
1695 | ||
1696 | if (CRM_Utils_Array::value('is_error', $result) || | |
1697 | !CRM_Utils_Array::value('id', $result) | |
1698 | ) { | |
1699 | throw new Exception('Could not create Custom Group ' . $result['error_message']); | |
1700 | } | |
1701 | return $result; | |
1702 | } | |
1703 | ||
1704 | /** | |
1705 | * Create custom group with multi fields | |
1706 | */ | |
1707 | function CustomGroupMultipleCreateByParams($params = array()) { | |
1708 | $defaults = array( | |
1709 | 'style' => 'Tab', | |
1710 | 'is_multiple' => 1, | |
1711 | ); | |
1712 | $params = array_merge($defaults, $params); | |
1713 | $result = $this->CustomGroupCreateByParams($params); | |
1714 | ||
1715 | if (CRM_Utils_Array::value('is_error', $result) || | |
1716 | !CRM_Utils_Array::value('id', $result) | |
1717 | ) { | |
1718 | throw new Exception('Could not create Custom Group ' . $result['error_message']); | |
1719 | } | |
1720 | return $result; | |
1721 | } | |
1722 | ||
1723 | /** | |
1724 | * Create custom group with multi fields | |
1725 | */ | |
1726 | function CustomGroupMultipleCreateWithFields($params = array()) { | |
1727 | // also need to pass on $params['custom_field'] if not set but not in place yet | |
1728 | $ids = array(); | |
1729 | $customGroup = $this->CustomGroupMultipleCreateByParams($params); | |
1730 | $ids['custom_group_id'] = $customGroup['id']; | |
1731 | if (CRM_Utils_Array::value('is_error', $ids['custom_group_id']) || | |
1732 | !CRM_Utils_Array::value('id', $customGroup) | |
1733 | ) { | |
1734 | throw new Exception('Could not create Custom Group from CustomGroupMultipleCreateWithFields' . $customGroup['error_message']); | |
1735 | } | |
1736 | ||
1737 | $customField = $this->customFieldCreate($ids['custom_group_id']); | |
1738 | ||
1739 | $ids['custom_field_id'][] = $customField['id']; | |
1740 | if (CRM_Utils_Array::value('is_error', $customField) || | |
1741 | !CRM_Utils_Array::value('id', $customField) | |
1742 | ) { | |
1743 | throw new Exception('Could not create Custom Field ' . $ids['custom_field']['error_message']); | |
1744 | } | |
1745 | $customField = $this->customFieldCreate($ids['custom_group_id'], 'field_2'); | |
1746 | $ids['custom_field_id'][] = $customField['id']; | |
1747 | if (CRM_Utils_Array::value('is_error', $customField) || | |
1748 | !CRM_Utils_Array::value('id', $customField) | |
1749 | ) { | |
1750 | throw new Exception('Could not create Custom Field ' . $ids['custom_field']['error_message']); | |
1751 | } | |
1752 | $customField = $this->customFieldCreate($ids['custom_group_id'], 'field_3'); | |
1753 | $ids['custom_field_id'][] = $customField['id']; | |
1754 | if (CRM_Utils_Array::value('is_error', $customField) || | |
1755 | !CRM_Utils_Array::value('id', $customField) | |
1756 | ) { | |
1757 | throw new Exception('Could not create Custom Field ' . $ids['custom_field']['error_message']); | |
1758 | } | |
1759 | return $ids; | |
1760 | } | |
1761 | ||
1762 | /** | |
1763 | * Create a custom group with a single text custom field. See | |
1764 | * participant:testCreateWithCustom for how to use this | |
1765 | * | |
1766 | * @param string $function __FUNCTION__ | |
1767 | * @param string $file __FILE__ | |
1768 | * | |
1769 | * @return array $ids ids of created objects | |
1770 | * | |
1771 | */ | |
1772 | function entityCustomGroupWithSingleFieldCreate($function, $filename) { | |
1773 | $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8); | |
1774 | if (empty($entity)) { | |
1775 | $entity = 'Contact'; | |
1776 | } | |
1777 | $customGroup = $this->CustomGroupCreate($entity, $function); | |
1778 | $customField = $this->customFieldCreate($customGroup['id'], $function); | |
1779 | CRM_Core_PseudoConstant::flush('customGroup'); | |
1780 | ||
1781 | return array('custom_group_id' => $customGroup['id'], 'custom_field_id' => $customField['id']); | |
1782 | } | |
1783 | ||
1784 | /** | |
1785 | * Function to delete custom group | |
1786 | * | |
1787 | * @param int $customGroupID | |
1788 | */ | |
1789 | function customGroupDelete($customGroupID) { | |
1790 | ||
1791 | $params['id'] = $customGroupID; | |
1792 | $params['version'] = API_LATEST_VERSION; | |
1793 | $result = civicrm_api('custom_group', 'delete', $params); | |
1794 | if (CRM_Utils_Array::value('is_error', $result)) { | |
1795 | print_r($params); | |
1796 | throw new Exception('Could not delete custom group' . $result['error_message']); | |
1797 | } | |
1798 | return; | |
1799 | } | |
1800 | ||
1801 | /** | |
1802 | * Function to create custom field | |
1803 | * | |
1804 | * @param int $customGroupID | |
1805 | * @param string $name name of custom field | |
1806 | * @param int $apiversion API version to use | |
1807 | */ | |
1808 | function customFieldCreate($customGroupID, $name = "Cust Field") { | |
1809 | ||
1810 | $params = array( | |
1811 | 'label' => $name, | |
1812 | 'name' => $name, | |
1813 | 'custom_group_id' => $customGroupID, | |
1814 | 'data_type' => 'String', | |
1815 | 'html_type' => 'Text', | |
1816 | 'is_searchable' => 1, | |
1817 | 'is_active' => 1, | |
1818 | 'version' => API_LATEST_VERSION, | |
1819 | ); | |
1820 | ||
1821 | $result = civicrm_api('custom_field', 'create', $params); | |
1822 | ||
1823 | if ($result['is_error'] == 0 && isset($result['id'])) { | |
1824 | CRM_Core_BAO_CustomField::getTableColumnGroup($result['id'], 1); | |
1825 | // force reset of enabled components to help grab custom fields | |
1826 | CRM_Core_Component::getEnabledComponents(1); | |
1827 | return $result; | |
1828 | } | |
1829 | ||
1830 | if (civicrm_error($result) | |
1831 | || !(CRM_Utils_Array::value('customFieldId', $result['result'])) | |
1832 | ) { | |
1833 | throw new Exception('Could not create Custom Field ' . $result['error_message']); | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | /** | |
1838 | * Function to delete custom field | |
1839 | * | |
1840 | * @param int $customFieldID | |
1841 | */ | |
1842 | function customFieldDelete($customFieldID) { | |
1843 | ||
1844 | $params['id'] = $customFieldID; | |
1845 | $params['version'] = API_LATEST_VERSION; | |
1846 | ||
1847 | $result = civicrm_api('custom_field', 'delete', $params); | |
1848 | ||
1849 | if (civicrm_error($result)) { | |
1850 | throw new Exception('Could not delete custom field'); | |
1851 | } | |
1852 | return; | |
1853 | } | |
1854 | ||
1855 | /** | |
1856 | * Function to create note | |
1857 | * | |
1858 | * @params array $params name-value pair for an event | |
1859 | * | |
1860 | * @return array $note | |
1861 | */ | |
1862 | function noteCreate($cId) { | |
1863 | $params = array( | |
1864 | 'entity_table' => 'civicrm_contact', | |
1865 | 'entity_id' => $cId, | |
1866 | 'note' => 'hello I am testing Note', | |
1867 | 'contact_id' => $cId, | |
1868 | 'modified_date' => date('Ymd'), | |
1869 | 'subject' => 'Test Note', | |
1870 | 'version' => API_LATEST_VERSION, | |
1871 | ); | |
1872 | ||
1873 | $result = civicrm_api('Note', 'create', $params); | |
1874 | ||
1875 | if (CRM_Utils_Array::value('is_error', $result)) { | |
1876 | if (CRM_Utils_Array::value('error_message', $result)) { | |
1877 | throw new Exception('Could not delete note, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
1878 | } | |
1879 | else { | |
1880 | throw new Exception('Could not delete note - in line: ' . __LINE__); | |
1881 | } | |
1882 | } | |
1883 | ||
1884 | return $result; | |
1885 | } | |
1886 | ||
1887 | /** | |
1888 | * Create test generated example in api/v3/examples. | |
1889 | * To turn this off (e.g. on the server) set | |
1890 | * define(DONT_DOCUMENT_TEST_CONFIG ,1); | |
1891 | * in your settings file | |
1892 | * @param array $params array as passed to civicrm_api function | |
1893 | * @param array $result array as received from the civicrm_api function | |
1894 | * @param string $function calling function - generally __FUNCTION__ | |
1895 | * @param string $filename called from file - generally __FILE__ | |
1896 | * @param string $description descriptive text for the example file | |
1897 | * @param string $subfile name for subfile - if this is completed the example will be put in a subfolder (named by the entity) | |
1898 | * @param string $action - optional action - otherwise taken from function name | |
1899 | */ | |
1900 | function documentMe($params, $result, $function, $filename, $description = "", $subfile = NULL, $action = NULL) { | |
1901 | if (defined('DONT_DOCUMENT_TEST_CONFIG')) { | |
1902 | return; | |
1903 | } | |
1904 | $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8); | |
1905 | //todo - this is a bit cludgey | |
1906 | if (empty($action)) { | |
1907 | if (strstr($function, 'Create')) { | |
1908 | $action = empty($action) ? 'create' : $action; | |
1909 | $entityAction = 'Create'; | |
1910 | } | |
1911 | elseif (strstr($function, 'GetSingle')) { | |
1912 | $action = empty($action) ? 'getsingle' : $action; | |
1913 | $entityAction = 'GetSingle'; | |
1914 | } | |
1915 | elseif (strstr($function, 'GetValue')) { | |
1916 | $action = empty($action) ? 'getvalue' : $action; | |
1917 | $entityAction = 'GetValue'; | |
1918 | } | |
1919 | elseif (strstr($function, 'GetCount')) { | |
1920 | $action = empty($action) ? 'getcount' : $action; | |
1921 | $entityAction = 'GetCount'; | |
1922 | } | |
1923 | elseif (strstr($function, 'Get')) { | |
1924 | $action = empty($action) ? 'get' : $action; | |
1925 | $entityAction = 'Get'; | |
1926 | } | |
1927 | elseif (strstr($function, 'Delete')) { | |
1928 | $action = empty($action) ? 'delete' : $action; | |
1929 | $entityAction = 'Delete'; | |
1930 | } | |
1931 | elseif (strstr($function, 'Update')) { | |
1932 | $action = empty($action) ? 'update' : $action; | |
1933 | $entityAction = 'Update'; | |
1934 | } | |
1935 | elseif (strstr($function, 'Subscribe')) { | |
1936 | $action = empty($action) ? 'subscribe' : $action; | |
1937 | $entityAction = 'Subscribe'; | |
1938 | } | |
1939 | elseif (strstr($function, 'Set')) { | |
1940 | $action = empty($action) ? 'set' : $action; | |
1941 | $entityAction = 'Set'; | |
1942 | } | |
1943 | elseif (strstr($function, 'Apply')) { | |
1944 | $action = empty($action) ? 'apply' : $action; | |
1945 | $entityAction = 'Apply'; | |
1946 | } | |
1947 | elseif (strstr($function, 'Replace')) { | |
1948 | $action = empty($action) ? 'replace' : $action; | |
1949 | $entityAction = 'Replace'; | |
1950 | } | |
1951 | } | |
1952 | else { | |
1953 | $entityAction = ucwords($action); | |
1954 | } | |
1955 | ||
1956 | $fieldsToChange = array( | |
1957 | 'hash' => '67eac7789eaee00', | |
1958 | 'modified_date' => '2012-11-14 16:02:35', | |
1959 | ); | |
1960 | //swap out keys that change too often | |
1961 | foreach ($fieldsToChange as $changeKey => $changeValue) { | |
1962 | if (isset($result['values']) && is_array($result['values'])) { | |
1963 | foreach ($result['values'] as $key => $value) { | |
1964 | if (is_array($value) && array_key_exists($changeKey, $value)) { | |
1965 | $result['values'][$key][$changeKey] = $changeValue; | |
1966 | } | |
1967 | } | |
1968 | } | |
1969 | } | |
1970 | ||
1971 | // a cleverer person than me would do it in a single regex | |
1972 | if (strstr($entity, 'UF')) { | |
1973 | $fnPrefix = strtolower(preg_replace('/(?<! )(?<!^)(?<=UF)[A-Z]/', '_$0', $entity)); | |
1974 | } | |
1975 | else { | |
1976 | $fnPrefix = strtolower(preg_replace('/(?<! )(?<!^)[A-Z]/', '_$0', $entity)); | |
1977 | } | |
1978 | $smarty = CRM_Core_Smarty::singleton(); | |
1979 | $smarty->assign('testfunction', $function); | |
1980 | $function = $fnPrefix . "_" . strtolower($action); | |
1981 | $smarty->assign('function', $function); | |
1982 | $smarty->assign('fnPrefix', $fnPrefix); | |
1983 | $smarty->assign('params', $params); | |
1984 | $smarty->assign('entity', $entity); | |
1985 | $smarty->assign('filename', basename($filename)); | |
1986 | $smarty->assign('description', $description); | |
1987 | $smarty->assign('result', $result); | |
1988 | ||
1989 | $smarty->assign('action', $action); | |
1990 | if (empty($subfile)) { | |
1991 | if (file_exists('../tests/templates/documentFunction.tpl')) { | |
1992 | $f = fopen("../api/v3/examples/$entity$entityAction.php", "w"); | |
1993 | fwrite($f, $smarty->fetch('../tests/templates/documentFunction.tpl')); | |
1994 | fclose($f); | |
1995 | } | |
1996 | } | |
1997 | else { | |
1998 | if (file_exists('../tests/templates/documentFunction.tpl')) { | |
1999 | if (!is_dir("../api/v3/examples/$entity")) { | |
2000 | mkdir("../api/v3/examples/$entity"); | |
2001 | } | |
2002 | $f = fopen("../api/v3/examples/$entity/$subfile.php", "w+b"); | |
2003 | fwrite($f, $smarty->fetch('../tests/templates/documentFunction.tpl')); | |
2004 | fclose($f); | |
2005 | } | |
2006 | } | |
2007 | } | |
2008 | ||
2009 | /** | |
2010 | * Function to delete note | |
2011 | * | |
2012 | * @params int $noteID | |
2013 | * | |
2014 | */ | |
2015 | function noteDelete($params) { | |
2016 | $params['version'] = API_LATEST_VERSION; | |
2017 | ||
2018 | $result = civicrm_api('Note', 'delete', $params); | |
2019 | ||
2020 | if (CRM_Utils_Array::value('is_error', $result)) { | |
2021 | if (CRM_Utils_Array::value('error_message', $result)) { | |
2022 | throw new Exception('Could not delete note, with message: ' . CRM_Utils_Array::value('error_message', $result)); | |
2023 | } | |
2024 | else { | |
2025 | throw new Exception('Could not delete note - in line: ' . __LINE__); | |
2026 | } | |
2027 | } | |
2028 | ||
2029 | return $result; | |
2030 | } | |
2031 | ||
2032 | /** | |
2033 | * Function to create custom field with Option Values | |
2034 | * | |
2035 | * @param array $customGroup | |
2036 | * @param string $name name of custom field | |
2037 | */ | |
2038 | function customFieldOptionValueCreate($customGroup, $name) { | |
2039 | $fieldParams = array( | |
2040 | 'custom_group_id' => $customGroup['id'], | |
2041 | 'name' => 'test_custom_group', | |
2042 | 'label' => 'Country', | |
2043 | 'html_type' => 'Select', | |
2044 | 'data_type' => 'String', | |
2045 | 'weight' => 4, | |
2046 | 'is_required' => 1, | |
2047 | 'is_searchable' => 0, | |
2048 | 'is_active' => 1, | |
2049 | 'version' => API_LATEST_VERSION, | |
2050 | ); | |
2051 | ||
2052 | $optionGroup = array( | |
2053 | 'domain_id' => 1, | |
2054 | 'name' => 'option_group1', | |
2055 | 'label' => 'option_group_label1', | |
2056 | ); | |
2057 | ||
2058 | $optionValue = array( | |
2059 | 'option_label' => array('Label1', 'Label2'), | |
2060 | 'option_value' => array('value1', 'value2'), | |
2061 | 'option_name' => array($name . '_1', $name . '_2'), | |
2062 | 'option_weight' => array(1, 2), | |
2063 | 'option_status' => 1, | |
2064 | ); | |
2065 | ||
2066 | $params = array_merge($fieldParams, $optionGroup, $optionValue); | |
2067 | ||
2068 | $result = civicrm_api('custom_field', 'create', $params); | |
2069 | ||
2070 | if ($result['is_error'] == 0 && isset($result['id'])) { | |
2071 | return $result; | |
2072 | } | |
2073 | if (civicrm_error($result) | |
2074 | || !(CRM_Utils_Array::value('customFieldId', $result['result'])) | |
2075 | ) { | |
2076 | throw new Exception('Could not create Custom Field'); | |
2077 | } | |
2078 | return $result; | |
2079 | } | |
2080 | ||
2081 | function confirmEntitiesDeleted($entities) { | |
2082 | foreach ($entities as $entity) { | |
2083 | ||
2084 | $result = civicrm_api($entity, 'Get', array( | |
2085 | 'version' => 3, | |
2086 | )); | |
2087 | if ($result['error'] == 1 || $result['count'] > 0) { | |
2088 | // > than $entity[0] to allow a value to be passed in? e.g. domain? | |
2089 | return TRUE; | |
2090 | } | |
2091 | } | |
2092 | } | |
2093 | ||
2094 | function quickCleanup($tablesToTruncate, $dropCustomValueTables = FALSE) { | |
2095 | if ($dropCustomValueTables) { | |
2096 | $tablesToTruncate[] = 'civicrm_custom_group'; | |
2097 | $tablesToTruncate[] = 'civicrm_custom_field'; | |
2098 | } | |
2099 | ||
2100 | CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;"); | |
2101 | foreach ($tablesToTruncate as $table) { | |
2102 | $sql = "TRUNCATE TABLE $table"; | |
2103 | CRM_Core_DAO::executeQuery($sql); | |
2104 | } | |
2105 | CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;"); | |
2106 | ||
2107 | if ($dropCustomValueTables) { | |
2108 | $dbName = self::getDBName(); | |
2109 | $query = " | |
2110 | SELECT TABLE_NAME as tableName | |
2111 | FROM INFORMATION_SCHEMA.TABLES | |
2112 | WHERE TABLE_SCHEMA = '{$dbName}' | |
2113 | AND ( TABLE_NAME LIKE 'civicrm_value_%' ) | |
2114 | "; | |
2115 | ||
2116 | $tableDAO = CRM_Core_DAO::executeQuery($query); | |
2117 | while ($tableDAO->fetch()) { | |
2118 | $sql = "DROP TABLE {$tableDAO->tableName}"; | |
2119 | CRM_Core_DAO::executeQuery($sql); | |
2120 | } | |
2121 | } | |
2122 | } | |
2123 | ||
2124 | /* | |
2125 | * Function does a 'Get' on the entity & compares the fields in the Params with those returned | |
2126 | * Default behaviour is to also delete the entity | |
2127 | * @param array $params params array to check agains | |
2128 | * @param int $id id of the entity concerned | |
2129 | * @param string $entity name of entity concerned (e.g. membership) | |
2130 | * @param bool $delete should the entity be deleted as part of this check | |
2131 | * @param string $errorText text to print on error | |
2132 | * | |
2133 | */ | |
2134 | function getAndCheck($params, $id, $entity, $delete = 1, $errorText = '') { | |
2135 | ||
2136 | $result = civicrm_api($entity, 'GetSingle', array( | |
2137 | 'id' => $id, | |
2138 | 'version' => $this->_apiversion, | |
2139 | )); | |
2140 | ||
2141 | if ($delete) { | |
2142 | civicrm_api($entity, 'Delete', array( | |
2143 | 'id' => $id, | |
2144 | 'version' => $this->_apiversion, | |
2145 | )); | |
2146 | } | |
2147 | $dateFields = $keys = array(); | |
2148 | $fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => 'get')); | |
2149 | foreach ($fields['values'] as $field => $settings) { | |
2150 | if (array_key_exists($field, $result)) { | |
2151 | $keys[CRM_Utils_Array::Value('name', $settings, $field)] = $field; | |
2152 | } | |
2153 | else { | |
2154 | $keys[CRM_Utils_Array::Value('name', $settings, $field)] = CRM_Utils_Array::value('name', $settings, $field); | |
2155 | } | |
2156 | ||
2157 | if (CRM_Utils_Array::value('type', $settings) == CRM_Utils_Type::T_DATE) { | |
2158 | $dateFields[] = $field; | |
2159 | } | |
2160 | } | |
2161 | ||
2162 | if (strtolower($entity) == 'contribution') { | |
2163 | $params['receive_date'] = date('Y-m-d', strtotime($params['receive_date'])); | |
2164 | // this is not returned in id format | |
2165 | unset($params['payment_instrument_id']); | |
2166 | $params['contribution_source'] = $params['source']; | |
2167 | unset($params['source']); | |
2168 | } | |
2169 | ||
2170 | foreach ($params as $key => $value) { | |
2171 | if ($key == 'version' || substr($key, 0, 3) == 'api') { | |
2172 | continue; | |
2173 | } | |
2174 | if (in_array($key, $dateFields)) { | |
2175 | $value = date('Y-m-d', strtotime($value)); | |
2176 | $result[$key] = date('Y-m-d', strtotime($result[$key])); | |
2177 | } | |
2178 | $this->assertEquals($value, $result[$keys[$key]], $key . " GetandCheck function determines that value: $value doesn't match " . print_r($result, TRUE) . $errorText); | |
2179 | } | |
2180 | } | |
2181 | ||
2182 | /** | |
2183 | * Function to get formatted values in the actual and expected result | |
2184 | * @param array $actual actual calculated values | |
2185 | * @param array $expected expected values | |
2186 | * | |
2187 | */ | |
2188 | function checkArrayEquals(&$actual, &$expected) { | |
2189 | self::unsetId($actual); | |
2190 | self::unsetId($expected); | |
2191 | $this->assertEquals($actual, $expected); | |
2192 | } | |
2193 | ||
2194 | /** | |
2195 | * Function to unset the key 'id' from the array | |
2196 | * @param array $unformattedArray The array from which the 'id' has to be unset | |
2197 | * | |
2198 | */ | |
2199 | static function unsetId(&$unformattedArray) { | |
2200 | $formattedArray = array(); | |
2201 | if (array_key_exists('id', $unformattedArray)) { | |
2202 | unset($unformattedArray['id']); | |
2203 | } | |
2204 | if (CRM_Utils_Array::value('values', $unformattedArray) && is_array($unformattedArray['values'])) { | |
2205 | foreach ($unformattedArray['values'] as $key => $value) { | |
2206 | if (is_Array($value)) { | |
2207 | foreach ($value as $k => $v) { | |
2208 | if ($k == 'id') { | |
2209 | unset($value[$k]); | |
2210 | } | |
2211 | } | |
2212 | } | |
2213 | elseif ($key == 'id') { | |
2214 | $unformattedArray[$key]; | |
2215 | } | |
2216 | $formattedArray = array($value); | |
2217 | } | |
2218 | $unformattedArray['values'] = $formattedArray; | |
2219 | } | |
2220 | } | |
2221 | ||
2222 | /** | |
2223 | * Helper to enable/disable custom directory support | |
2224 | * | |
2225 | * @param array $customDirs with members: | |
2226 | * 'php_path' Set to TRUE to use the default, FALSE or "" to disable support, or a string path to use another path | |
2227 | * 'template_path' Set to TRUE to use the default, FALSE or "" to disable support, or a string path to use another path | |
2228 | */ | |
2229 | function customDirectories($customDirs) { | |
2230 | require_once 'CRM/Core/Config.php'; | |
2231 | $config = CRM_Core_Config::singleton(); | |
2232 | ||
2233 | if (empty($customDirs['php_path']) || $customDirs['php_path'] === FALSE) { | |
2234 | unset($config->customPHPPathDir); | |
2235 | } | |
2236 | elseif ($customDirs['php_path'] === TRUE) { | |
2237 | $config->customPHPPathDir = dirname(dirname(__FILE__)) . '/custom_directories/php/'; | |
2238 | } | |
2239 | else { | |
2240 | $config->customPHPPathDir = $php_path; | |
2241 | } | |
2242 | ||
2243 | if (empty($customDirs['template_path']) || $customDirs['template_path'] === FALSE) { | |
2244 | unset($config->customTemplateDir); | |
2245 | } | |
2246 | elseif ($customDirs['template_path'] === TRUE) { | |
2247 | $config->customTemplateDir = dirname(dirname(__FILE__)) . '/custom_directories/templates/'; | |
2248 | } | |
2249 | else { | |
2250 | $config->customTemplateDir = $template_path; | |
2251 | } | |
2252 | } | |
2253 | ||
2254 | /** | |
2255 | * Generate a temporary folder | |
2256 | * | |
2257 | * @return $string | |
2258 | */ | |
2259 | function createTempDir($prefix = 'test-') { | |
2260 | $tempDir = CRM_Utils_File::tempdir($prefix); | |
2261 | $this->tempDirs[] = $tempDir; | |
2262 | return $tempDir; | |
2263 | } | |
2264 | ||
2265 | function cleanTempDirs() { | |
2266 | if (!is_array($this->tempDirs)) { | |
2267 | // fix test errors where this is not set | |
2268 | return; | |
2269 | } | |
2270 | foreach ($this->tempDirs as $tempDir) { | |
2271 | if (is_dir($tempDir)) { | |
2272 | CRM_Utils_File::cleanDir($tempDir, TRUE, FALSE); | |
2273 | } | |
2274 | } | |
2275 | } | |
2276 | ||
2277 | /** | |
2278 | * Temporarily replace the singleton extension with a different one | |
2279 | */ | |
2280 | function setExtensionSystem(CRM_Extension_System $system) { | |
2281 | if ($this->origExtensionSystem == NULL) { | |
2282 | $this->origExtensionSystem = CRM_Extension_System::singleton(); | |
2283 | } | |
2284 | CRM_Extension_System::setSingleton($this->origExtensionSystem); | |
2285 | } | |
2286 | ||
2287 | function unsetExtensionSystem() { | |
2288 | if ($this->origExtensionSystem !== NULL) { | |
2289 | CRM_Extension_System::setSingleton($this->origExtensionSystem); | |
2290 | $this->origExtensionSystem = NULL; | |
2291 | } | |
2292 | } | |
2293 | } | |
2294 | ||
2295 | function CiviUnitTestCase_fatalErrorHandler($message) { | |
2296 | throw new Exception("{$message['message']}: {$message['code']}"); | |
2297 | } |