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