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