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