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 | */ | |
6a488035 TO |
42 | require_once 'tests/phpunit/Utils.php'; |
43 | require_once 'api/api.php'; | |
44 | require_once 'CRM/Financial/BAO/FinancialType.php'; | |
45 | define('API_LATEST_VERSION', 3); | |
46 | ||
47 | /** | |
48 | * Base class for CiviCRM unit tests | |
49 | * | |
d67f1f28 TO |
50 | * This class supports two (mutually-exclusive) techniques for cleaning up test data. Subclasses |
51 | * may opt for one or neither: | |
52 | * | |
53 | * 1. quickCleanup() is a helper which truncates a series of tables. Call quickCleanup() | |
54 | * as part of setUp() and/or tearDown(). quickCleanup() is thorough - but it can | |
55 | * be cumbersome to use (b/c you must identify the tables to cleanup) and slow to execute. | |
56 | * 2. useTransaction() executes the test inside a transaction. It's easier to use | |
57 | * (because you don't need to identify specific tables), but it doesn't work for tests | |
58 | * which manipulate schema or truncate data -- and could behave inconsistently | |
59 | * for tests which specifically examine DB transactions. | |
60 | * | |
6a488035 TO |
61 | * Common functions for unit tests |
62 | * @package CiviCRM | |
63 | */ | |
64 | class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { | |
65 | ||
f6722559 | 66 | /** |
100fef9d | 67 | * Api version - easier to override than just a define |
f6722559 | 68 | */ |
69 | protected $_apiversion = API_LATEST_VERSION; | |
6a488035 TO |
70 | /** |
71 | * Database has been initialized | |
72 | * | |
73 | * @var boolean | |
74 | */ | |
75 | private static $dbInit = FALSE; | |
76 | ||
77 | /** | |
78 | * Database connection | |
79 | * | |
80 | * @var PHPUnit_Extensions_Database_DB_IDatabaseConnection | |
81 | */ | |
82 | protected $_dbconn; | |
83 | ||
84 | /** | |
85 | * The database name | |
86 | * | |
87 | * @var string | |
88 | */ | |
89 | static protected $_dbName; | |
90 | ||
0b6f58fa ARW |
91 | /** |
92 | * Track tables we have modified during a test | |
93 | */ | |
94 | protected $_tablesToTruncate = array(); | |
95 | ||
6a488035 TO |
96 | /** |
97 | * @var array of temporary directory names | |
98 | */ | |
99 | protected $tempDirs; | |
100 | ||
101 | /** | |
102 | * @var Utils instance | |
103 | */ | |
104 | public static $utils; | |
105 | ||
106 | /** | |
107 | * @var boolean populateOnce allows to skip db resets in setUp | |
108 | * | |
109 | * WARNING! USE WITH CAUTION - IT'LL RENDER DATA DEPENDENCIES | |
110 | * BETWEEN TESTS WHEN RUN IN SUITE. SUITABLE FOR LOCAL, LIMITED | |
111 | * "CHECK RUNS" ONLY! | |
112 | * | |
113 | * IF POSSIBLE, USE $this->DBResetRequired = FALSE IN YOUR TEST CASE! | |
114 | * | |
115 | * see also: http://forum.civicrm.org/index.php/topic,18065.0.html | |
116 | */ | |
117 | public static $populateOnce = FALSE; | |
118 | ||
119 | /** | |
120 | * Allow classes to state E-notice compliance | |
121 | */ | |
eafce897 | 122 | public $_eNoticeCompliant = TRUE; |
6a488035 TO |
123 | |
124 | /** | |
125 | * @var boolean DBResetRequired allows skipping DB reset | |
126 | * in specific test case. If you still need | |
127 | * to reset single test (method) of such case, call | |
128 | * $this->cleanDB() in the first line of this | |
129 | * test (method). | |
130 | */ | |
131 | public $DBResetRequired = TRUE; | |
132 | ||
d67f1f28 TO |
133 | /** |
134 | * @var CRM_Core_Transaction|NULL | |
135 | */ | |
136 | private $tx = NULL; | |
137 | ||
6c466b51 EM |
138 | /** |
139 | * @var CRM_Utils_Hook_UnitTests hookClass | |
140 | * example of setting a method for a hook | |
141 | * $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults')); | |
142 | */ | |
143 | public $hookClass = NULL; | |
144 | ||
145 | /** | |
146 | * @var array common values to be re-used multiple times within a class - usually to create the relevant entity | |
147 | */ | |
148 | protected $_params = array(); | |
149 | ||
150 | /** | |
151 | * @var CRM_Extension_System | |
152 | */ | |
153 | protected $origExtensionSystem; | |
154 | ||
6a488035 TO |
155 | /** |
156 | * Constructor | |
157 | * | |
158 | * Because we are overriding the parent class constructor, we | |
159 | * need to show the same arguments as exist in the constructor of | |
160 | * PHPUnit_Framework_TestCase, since | |
161 | * PHPUnit_Framework_TestSuite::createTest() creates a | |
162 | * ReflectionClass of the Test class and checks the constructor | |
163 | * of that class to decide how to set up the test. | |
164 | * | |
e16033b4 TO |
165 | * @param string $name |
166 | * @param array $data | |
167 | * @param string $dataName | |
6a488035 | 168 | */ |
00be9182 | 169 | public function __construct($name = NULL, array$data = array(), $dataName = '') { |
6a488035 TO |
170 | parent::__construct($name, $data, $dataName); |
171 | ||
172 | // we need full error reporting | |
173 | error_reporting(E_ALL & ~E_NOTICE); | |
174 | ||
175 | if (!empty($GLOBALS['mysql_db'])) { | |
176 | self::$_dbName = $GLOBALS['mysql_db']; | |
177 | } | |
178 | else { | |
179 | self::$_dbName = 'civicrm_tests_dev'; | |
180 | } | |
181 | ||
182 | // create test database | |
183 | self::$utils = new Utils($GLOBALS['mysql_host'], | |
fc3c781d | 184 | $GLOBALS['mysql_port'], |
6a488035 TO |
185 | $GLOBALS['mysql_user'], |
186 | $GLOBALS['mysql_pass'] | |
187 | ); | |
188 | ||
189 | // also load the class loader | |
190 | require_once 'CRM/Core/ClassLoader.php'; | |
191 | CRM_Core_ClassLoader::singleton()->register(); | |
192 | if (function_exists('_civix_phpunit_setUp')) { // FIXME: loosen coupling | |
193 | _civix_phpunit_setUp(); | |
194 | } | |
195 | } | |
196 | ||
2e90bf37 TO |
197 | protected function runTest() { |
198 | try { | |
199 | return parent::runTest(); | |
5896d037 TO |
200 | } |
201 | catch (PEAR_Exception $e) { | |
2e90bf37 TO |
202 | // PEAR_Exception has metadata in funny places, and PHPUnit won't log it nicely |
203 | throw new Exception(\CRM_Core_Error::formatTextException($e), $e->getCode()); | |
204 | } | |
205 | } | |
206 | ||
4cbe18b8 EM |
207 | /** |
208 | * @return bool | |
209 | */ | |
00be9182 | 210 | public function requireDBReset() { |
6a488035 TO |
211 | return $this->DBResetRequired; |
212 | } | |
213 | ||
4cbe18b8 EM |
214 | /** |
215 | * @return string | |
216 | */ | |
00be9182 | 217 | public static function getDBName() { |
6a488035 TO |
218 | $dbName = !empty($GLOBALS['mysql_db']) ? $GLOBALS['mysql_db'] : 'civicrm_tests_dev'; |
219 | return $dbName; | |
220 | } | |
221 | ||
222 | /** | |
223 | * Create database connection for this instance | |
224 | * | |
225 | * Initialize the test database if it hasn't been initialized | |
226 | * | |
227 | * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection connection | |
228 | */ | |
229 | protected function getConnection() { | |
230 | $dbName = self::$_dbName; | |
231 | if (!self::$dbInit) { | |
232 | $dbName = self::getDBName(); | |
233 | ||
234 | // install test database | |
235 | echo PHP_EOL . "Installing {$dbName} database" . PHP_EOL; | |
236 | ||
99117745 | 237 | static::_populateDB(FALSE, $this); |
6a488035 TO |
238 | |
239 | self::$dbInit = TRUE; | |
240 | } | |
241 | return $this->createDefaultDBConnection(self::$utils->pdo, $dbName); | |
242 | } | |
243 | ||
244 | /** | |
245 | * Required implementation of abstract method | |
246 | */ | |
247 | protected function getDataSet() { | |
248 | } | |
249 | ||
99117745 TO |
250 | /** |
251 | * @param bool $perClass | |
252 | * @param null $object | |
253 | * @return bool TRUE if the populate logic runs; FALSE if it is skipped | |
254 | */ | |
255 | protected static function _populateDB($perClass = FALSE, &$object = NULL) { | |
6a488035 TO |
256 | |
257 | if ($perClass || $object == NULL) { | |
258 | $dbreset = TRUE; | |
259 | } | |
260 | else { | |
261 | $dbreset = $object->requireDBReset(); | |
262 | } | |
263 | ||
264 | if (self::$populateOnce || !$dbreset) { | |
99117745 | 265 | return FALSE; |
6a488035 TO |
266 | } |
267 | self::$populateOnce = NULL; | |
268 | ||
269 | $dbName = self::getDBName(); | |
270 | $pdo = self::$utils->pdo; | |
271 | // only consider real tables and not views | |
272 | $tables = $pdo->query("SELECT table_name FROM INFORMATION_SCHEMA.TABLES | |
273 | WHERE TABLE_SCHEMA = '{$dbName}' AND TABLE_TYPE = 'BASE TABLE'"); | |
274 | ||
275 | $truncates = array(); | |
276 | $drops = array(); | |
277 | foreach ($tables as $table) { | |
278 | // skip log tables | |
279 | if (substr($table['table_name'], 0, 4) == 'log_') { | |
280 | continue; | |
281 | } | |
282 | ||
283 | // don't change list of installed extensions | |
284 | if ($table['table_name'] == 'civicrm_extension') { | |
285 | continue; | |
286 | } | |
287 | ||
288 | if (substr($table['table_name'], 0, 14) == 'civicrm_value_') { | |
289 | $drops[] = 'DROP TABLE ' . $table['table_name'] . ';'; | |
290 | } | |
291 | else { | |
292 | $truncates[] = 'TRUNCATE ' . $table['table_name'] . ';'; | |
293 | } | |
294 | } | |
295 | ||
296 | $queries = array( | |
297 | "USE {$dbName};", | |
298 | "SET foreign_key_checks = 0", | |
299 | // SQL mode needs to be strict, that's our standard | |
300 | "SET SQL_MODE='STRICT_ALL_TABLES';", | |
301 | "SET global innodb_flush_log_at_trx_commit = 2;", | |
302 | ); | |
303 | $queries = array_merge($queries, $truncates); | |
304 | $queries = array_merge($queries, $drops); | |
305 | foreach ($queries as $query) { | |
306 | if (self::$utils->do_query($query) === FALSE) { | |
307 | // failed to create test database | |
308 | echo "failed to create test db."; | |
309 | exit; | |
310 | } | |
311 | } | |
312 | ||
313 | // initialize test database | |
314 | $sql_file2 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/civicrm_data.mysql"; | |
315 | $sql_file3 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data.mysql"; | |
316 | $sql_file4 = dirname(dirname(dirname(dirname(__FILE__)))) . "/sql/test_data_second_domain.mysql"; | |
317 | ||
318 | $query2 = file_get_contents($sql_file2); | |
319 | $query3 = file_get_contents($sql_file3); | |
320 | $query4 = file_get_contents($sql_file4); | |
321 | if (self::$utils->do_query($query2) === FALSE) { | |
322 | echo "Cannot load civicrm_data.mysql. Aborting."; | |
323 | exit; | |
324 | } | |
325 | if (self::$utils->do_query($query3) === FALSE) { | |
326 | echo "Cannot load test_data.mysql. Aborting."; | |
327 | exit; | |
328 | } | |
329 | if (self::$utils->do_query($query4) === FALSE) { | |
330 | echo "Cannot load test_data.mysql. Aborting."; | |
331 | exit; | |
332 | } | |
333 | ||
334 | // done with all the loading, get transactions back | |
335 | if (self::$utils->do_query("set global innodb_flush_log_at_trx_commit = 1;") === FALSE) { | |
336 | echo "Cannot set global? Huh?"; | |
337 | exit; | |
338 | } | |
339 | ||
340 | if (self::$utils->do_query("SET foreign_key_checks = 1") === FALSE) { | |
341 | echo "Cannot get foreign keys back? Huh?"; | |
342 | exit; | |
343 | } | |
344 | ||
345 | unset($query, $query2, $query3); | |
346 | ||
347 | // Rebuild triggers | |
348 | civicrm_api('system', 'flush', array('version' => 3, 'triggers' => 1)); | |
99117745 | 349 | |
229a603f | 350 | CRM_Core_BAO_ConfigSetting::setEnabledComponents(array( |
5896d037 TO |
351 | 'CiviEvent', |
352 | 'CiviContribute', | |
353 | 'CiviMember', | |
354 | 'CiviMail', | |
355 | 'CiviReport', | |
21dfd5f5 | 356 | 'CiviPledge', |
229a603f TO |
357 | )); |
358 | ||
99117745 | 359 | return TRUE; |
6a488035 TO |
360 | } |
361 | ||
362 | public static function setUpBeforeClass() { | |
99117745 | 363 | static::_populateDB(TRUE); |
6a488035 TO |
364 | |
365 | // also set this global hack | |
366 | $GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array(); | |
03a1f367 TO |
367 | |
368 | $env = new CRM_Utils_Check_Env(); | |
7342d7c4 | 369 | CRM_Utils_Check::singleton()->assertValid($env->checkMysqlTime()); |
6a488035 TO |
370 | } |
371 | ||
372 | /** | |
373 | * Common setup functions for all unit tests | |
374 | */ | |
375 | protected function setUp() { | |
4db17da0 TO |
376 | $session = CRM_Core_Session::singleton(); |
377 | $session->set('userID', NULL); | |
378 | ||
6a488035 TO |
379 | $this->errorScope = CRM_Core_TemporaryErrorScope::useException(); // REVERT |
380 | // Use a temporary file for STDIN | |
381 | $GLOBALS['stdin'] = tmpfile(); | |
382 | if ($GLOBALS['stdin'] === FALSE) { | |
383 | echo "Couldn't open temporary file\n"; | |
384 | exit(1); | |
385 | } | |
386 | ||
387 | // Get and save a connection to the database | |
388 | $this->_dbconn = $this->getConnection(); | |
389 | ||
390 | // reload database before each test | |
391 | // $this->_populateDB(); | |
392 | ||
393 | // "initialize" CiviCRM to avoid problems when running single tests | |
394 | // FIXME: look at it closer in second stage | |
395 | ||
396 | // initialize the object once db is loaded | |
72ad6c1b | 397 | CRM_Core_Config::$_mail = NULL; |
6a488035 TO |
398 | $config = CRM_Core_Config::singleton(); |
399 | ||
400 | // when running unit tests, use mockup user framework | |
401 | $config->setUserFramework('UnitTests'); | |
6c466b51 | 402 | $this->hookClass = CRM_Utils_Hook::singleton(TRUE); |
6a488035 TO |
403 | // also fix the fatal error handler to throw exceptions, |
404 | // rather than exit | |
405 | $config->fatalErrorHandler = 'CiviUnitTestCase_fatalErrorHandler'; | |
406 | ||
407 | // enable backtrace to get meaningful errors | |
408 | $config->backtrace = 1; | |
409 | ||
410 | // disable any left-over test extensions | |
411 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_extension WHERE full_name LIKE "test.%"'); | |
412 | ||
413 | // reset all the caches | |
414 | CRM_Utils_System::flushCache(); | |
415 | ||
416 | // clear permissions stub to not check permissions | |
417 | $config = CRM_Core_Config::singleton(); | |
418 | $config->userPermissionClass->permissions = NULL; | |
419 | ||
420 | //flush component settings | |
421 | CRM_Core_Component::getEnabledComponents(TRUE); | |
422 | ||
423 | if ($this->_eNoticeCompliant) { | |
424 | error_reporting(E_ALL); | |
425 | } | |
426 | else { | |
427 | error_reporting(E_ALL & ~E_NOTICE); | |
428 | } | |
d58b453e | 429 | $this->_sethtmlGlobals(); |
6a488035 TO |
430 | } |
431 | ||
0b6f58fa ARW |
432 | /** |
433 | * Read everything from the datasets directory and insert into the db | |
434 | */ | |
435 | public function loadAllFixtures() { | |
436 | $fixturesDir = __DIR__ . '/../../fixtures'; | |
437 | ||
438 | $this->getConnection()->getConnection()->query("SET FOREIGN_KEY_CHECKS = 0;"); | |
439 | ||
440 | $xmlFiles = glob($fixturesDir . '/*.xml'); | |
441 | foreach ($xmlFiles as $xmlFixture) { | |
442 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
bbfd46a5 | 443 | $dataset = $this->createXMLDataSet($xmlFixture); |
0b6f58fa ARW |
444 | $this->_tablesToTruncate = array_merge($this->_tablesToTruncate, $dataset->getTableNames()); |
445 | $op->execute($this->_dbconn, $dataset); | |
446 | } | |
447 | ||
448 | $yamlFiles = glob($fixturesDir . '/*.yaml'); | |
449 | foreach ($yamlFiles as $yamlFixture) { | |
450 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
451 | $dataset = new PHPUnit_Extensions_Database_DataSet_YamlDataSet($yamlFixture); | |
452 | $this->_tablesToTruncate = array_merge($this->_tablesToTruncate, $dataset->getTableNames()); | |
453 | $op->execute($this->_dbconn, $dataset); | |
454 | } | |
455 | ||
456 | $this->getConnection()->getConnection()->query("SET FOREIGN_KEY_CHECKS = 1;"); | |
457 | } | |
458 | ||
6a488035 | 459 | /** |
100fef9d | 460 | * Emulate a logged in user since certain functions use that |
6a488035 TO |
461 | * value to store a record in the DB (like activity) |
462 | * CRM-8180 | |
463 | */ | |
464 | public function createLoggedInUser() { | |
465 | $params = array( | |
466 | 'first_name' => 'Logged In', | |
467 | 'last_name' => 'User ' . rand(), | |
468 | 'contact_type' => 'Individual', | |
469 | ); | |
470 | $contactID = $this->individualCreate($params); | |
471 | ||
472 | $session = CRM_Core_Session::singleton(); | |
473 | $session->set('userID', $contactID); | |
474 | } | |
475 | ||
476 | public function cleanDB() { | |
477 | self::$populateOnce = NULL; | |
478 | $this->DBResetRequired = TRUE; | |
479 | ||
480 | $this->_dbconn = $this->getConnection(); | |
99117745 | 481 | static::_populateDB(); |
6a488035 TO |
482 | $this->tempDirs = array(); |
483 | } | |
484 | ||
5767aa07 AN |
485 | /** |
486 | * Create default domain contacts for the two domains added during test class | |
487 | * database population. | |
488 | */ | |
489 | public function createDomainContacts() { | |
490 | $default_domain_contact = $this->organizationCreate(); | |
491 | $second_domain_contact = $this->organizationCreate(); | |
492 | } | |
493 | ||
6a488035 TO |
494 | /** |
495 | * Common teardown functions for all unit tests | |
496 | */ | |
497 | protected function tearDown() { | |
498 | error_reporting(E_ALL & ~E_NOTICE); | |
6c466b51 EM |
499 | CRM_Utils_Hook::singleton()->reset(); |
500 | $this->hookClass->reset(); | |
9ce07588 TO |
501 | $session = CRM_Core_Session::singleton(); |
502 | $session->set('userID', NULL); | |
d67f1f28 TO |
503 | |
504 | if ($this->tx) { | |
505 | $this->tx->rollback()->commit(); | |
506 | $this->tx = NULL; | |
507 | ||
508 | CRM_Core_Transaction::forceRollbackIfEnabled(); | |
509 | \Civi\Core\Transaction\Manager::singleton(TRUE); | |
5896d037 TO |
510 | } |
511 | else { | |
d67f1f28 TO |
512 | CRM_Core_Transaction::forceRollbackIfEnabled(); |
513 | \Civi\Core\Transaction\Manager::singleton(TRUE); | |
514 | ||
515 | $tablesToTruncate = array('civicrm_contact'); | |
516 | $this->quickCleanup($tablesToTruncate); | |
a335f6b2 | 517 | $this->createDomainContacts(); |
d67f1f28 TO |
518 | } |
519 | ||
6a488035 TO |
520 | $this->cleanTempDirs(); |
521 | $this->unsetExtensionSystem(); | |
73252974 | 522 | $this->clearOutputBuffer(); |
6a488035 TO |
523 | } |
524 | ||
525 | /** | |
526 | * FIXME: Maybe a better way to do it | |
527 | */ | |
00be9182 | 528 | public function foreignKeyChecksOff() { |
6a488035 | 529 | self::$utils = new Utils($GLOBALS['mysql_host'], |
fc3c781d | 530 | $GLOBALS['mysql_port'], |
6a488035 TO |
531 | $GLOBALS['mysql_user'], |
532 | $GLOBALS['mysql_pass'] | |
533 | ); | |
534 | $dbName = self::getDBName(); | |
535 | $query = "USE {$dbName};" . "SET foreign_key_checks = 1"; | |
536 | if (self::$utils->do_query($query) === FALSE) { | |
537 | // fail happens | |
538 | echo 'Cannot set foreign_key_checks = 0'; | |
539 | exit(1); | |
540 | } | |
541 | return TRUE; | |
542 | } | |
543 | ||
00be9182 | 544 | public function foreignKeyChecksOn() { |
6a488035 TO |
545 | // FIXME: might not be needed if previous fixme implemented |
546 | } | |
547 | ||
548 | /** | |
549 | * Generic function to compare expected values after an api call to retrieved | |
550 | * DB values. | |
551 | * | |
552 | * @daoName string DAO Name of object we're evaluating. | |
553 | * @id int Id of object | |
554 | * @match array Associative array of field name => expected value. Empty if asserting | |
555 | * that a DELETE occurred | |
556 | * @delete boolean True if we're checking that a DELETE action occurred. | |
557 | */ | |
00be9182 | 558 | public function assertDBState($daoName, $id, $match, $delete = FALSE) { |
6a488035 TO |
559 | if (empty($id)) { |
560 | // adding this here since developers forget to check for an id | |
561 | // and hence we get the first value in the db | |
562 | $this->fail('ID not populated. Please fix your assertDBState usage!!!'); | |
563 | } | |
564 | ||
4d5c2eb5 | 565 | $object = new $daoName(); |
6a488035 TO |
566 | $object->id = $id; |
567 | $verifiedCount = 0; | |
568 | ||
569 | // If we're asserting successful record deletion, make sure object is NOT found. | |
570 | if ($delete) { | |
571 | if ($object->find(TRUE)) { | |
572 | $this->fail("Object not deleted by delete operation: $daoName, $id"); | |
573 | } | |
574 | return; | |
575 | } | |
576 | ||
577 | // Otherwise check matches of DAO field values against expected values in $match. | |
578 | if ($object->find(TRUE)) { | |
5896d037 | 579 | $fields = &$object->fields(); |
6a488035 TO |
580 | foreach ($fields as $name => $value) { |
581 | $dbName = $value['name']; | |
582 | if (isset($match[$name])) { | |
583 | $verifiedCount++; | |
584 | $this->assertEquals($object->$dbName, $match[$name]); | |
585 | } | |
586 | elseif (isset($match[$dbName])) { | |
587 | $verifiedCount++; | |
588 | $this->assertEquals($object->$dbName, $match[$dbName]); | |
589 | } | |
590 | } | |
591 | } | |
592 | else { | |
593 | $this->fail("Could not retrieve object: $daoName, $id"); | |
594 | } | |
595 | $object->free(); | |
596 | $matchSize = count($match); | |
597 | if ($verifiedCount != $matchSize) { | |
598 | $this->fail("Did not verify all fields in match array: $daoName, $id. Verified count = $verifiedCount. Match array size = $matchSize"); | |
599 | } | |
600 | } | |
601 | ||
602 | // Request a record from the DB by seachColumn+searchValue. Success if a record is found. | |
4cbe18b8 | 603 | /** |
c490a46a | 604 | * @param string $daoName |
4cbe18b8 EM |
605 | * @param $searchValue |
606 | * @param $returnColumn | |
607 | * @param $searchColumn | |
608 | * @param $message | |
609 | * | |
610 | * @return null|string | |
611 | * @throws PHPUnit_Framework_AssertionFailedError | |
612 | */ | |
00be9182 | 613 | public function assertDBNotNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { |
6a488035 TO |
614 | if (empty($searchValue)) { |
615 | $this->fail("empty value passed to assertDBNotNull"); | |
616 | } | |
617 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); | |
618 | $this->assertNotNull($value, $message); | |
619 | ||
620 | return $value; | |
621 | } | |
622 | ||
623 | // Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL. | |
4cbe18b8 | 624 | /** |
c490a46a | 625 | * @param string $daoName |
4cbe18b8 EM |
626 | * @param $searchValue |
627 | * @param $returnColumn | |
628 | * @param $searchColumn | |
629 | * @param $message | |
630 | */ | |
00be9182 | 631 | public function assertDBNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { |
6a488035 TO |
632 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); |
633 | $this->assertNull($value, $message); | |
634 | } | |
635 | ||
636 | // Request a record from the DB by id. Success if row not found. | |
4cbe18b8 | 637 | /** |
c490a46a | 638 | * @param string $daoName |
100fef9d | 639 | * @param int $id |
4cbe18b8 EM |
640 | * @param null $message |
641 | */ | |
00be9182 | 642 | public function assertDBRowNotExist($daoName, $id, $message = NULL) { |
6a488035 TO |
643 | $message = $message ? $message : "$daoName (#$id) should not exist"; |
644 | $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE); | |
645 | $this->assertNull($value, $message); | |
646 | } | |
647 | ||
648 | // Request a record from the DB by id. Success if row not found. | |
4cbe18b8 | 649 | /** |
c490a46a | 650 | * @param string $daoName |
100fef9d | 651 | * @param int $id |
4cbe18b8 EM |
652 | * @param null $message |
653 | */ | |
00be9182 | 654 | public function assertDBRowExist($daoName, $id, $message = NULL) { |
6a488035 TO |
655 | $message = $message ? $message : "$daoName (#$id) should exist"; |
656 | $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE); | |
657 | $this->assertEquals($id, $value, $message); | |
658 | } | |
659 | ||
660 | // Compare a single column value in a retrieved DB record to an expected value | |
4cbe18b8 | 661 | /** |
c490a46a | 662 | * @param string $daoName |
4cbe18b8 EM |
663 | * @param $searchValue |
664 | * @param $returnColumn | |
665 | * @param $searchColumn | |
666 | * @param $expectedValue | |
667 | * @param $message | |
668 | */ | |
5896d037 TO |
669 | function assertDBCompareValue( |
670 | $daoName, $searchValue, $returnColumn, $searchColumn, | |
671 | $expectedValue, $message | |
6a488035 TO |
672 | ) { |
673 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); | |
674 | $this->assertEquals($value, $expectedValue, $message); | |
675 | } | |
676 | ||
677 | // Compare all values in a single retrieved DB record to an array of expected values | |
4cbe18b8 | 678 | /** |
c490a46a | 679 | * @param string $daoName |
100fef9d | 680 | * @param array $searchParams |
4cbe18b8 EM |
681 | * @param $expectedValues |
682 | */ | |
00be9182 | 683 | public function assertDBCompareValues($daoName, $searchParams, $expectedValues) { |
6a488035 TO |
684 | //get the values from db |
685 | $dbValues = array(); | |
686 | CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues); | |
687 | ||
688 | // compare db values with expected values | |
689 | self::assertAttributesEquals($expectedValues, $dbValues); | |
690 | } | |
691 | ||
692 | /** | |
693 | * Assert that a SQL query returns a given value | |
694 | * | |
695 | * The first argument is an expected value. The remaining arguments are passed | |
696 | * to CRM_Core_DAO::singleValueQuery | |
697 | * | |
698 | * Example: $this->assertSql(2, 'select count(*) from foo where foo.bar like "%1"', | |
699 | * array(1 => array("Whiz", "String"))); | |
700 | */ | |
00be9182 | 701 | public function assertDBQuery($expected, $query, $params = array(), $message = '') { |
5896d037 TO |
702 | if ($message) { |
703 | $message .= ': '; | |
6c6e6187 | 704 | } |
6a488035 TO |
705 | $actual = CRM_Core_DAO::singleValueQuery($query, $params); |
706 | $this->assertEquals($expected, $actual, | |
5bb8417a TO |
707 | sprintf('%sexpected=[%s] actual=[%s] query=[%s]', |
708 | $message, $expected, $actual, CRM_Core_DAO::composeQuery($query, $params, FALSE) | |
6a488035 TO |
709 | ) |
710 | ); | |
711 | } | |
712 | ||
713 | /** | |
714 | * Assert that two array-trees are exactly equal, notwithstanding | |
715 | * the sorting of keys | |
716 | * | |
717 | * @param array $expected | |
718 | * @param array $actual | |
719 | */ | |
00be9182 | 720 | public function assertTreeEquals($expected, $actual) { |
6a488035 TO |
721 | $e = array(); |
722 | $a = array(); | |
723 | CRM_Utils_Array::flatten($expected, $e, '', ':::'); | |
724 | CRM_Utils_Array::flatten($actual, $a, '', ':::'); | |
725 | ksort($e); | |
726 | ksort($a); | |
727 | ||
728 | $this->assertEquals($e, $a); | |
729 | } | |
730 | ||
dc92f2f8 TO |
731 | /** |
732 | * Assert that two numbers are approximately equal | |
733 | * | |
734 | * @param int|float $expected | |
735 | * @param int|float $actual | |
736 | * @param int|float $tolerance | |
737 | * @param string $message | |
738 | */ | |
00be9182 | 739 | public function assertApproxEquals($expected, $actual, $tolerance, $message = NULL) { |
dc92f2f8 TO |
740 | if ($message === NULL) { |
741 | $message = sprintf("approx-equals: expected=[%.3f] actual=[%.3f] tolerance=[%.3f]", $expected, $actual, $tolerance); | |
742 | } | |
743 | $this->assertTrue(abs($actual - $expected) < $tolerance, $message); | |
744 | } | |
745 | ||
4cbe18b8 EM |
746 | /** |
747 | * @param $expectedValues | |
748 | * @param $actualValues | |
749 | * @param null $message | |
750 | * | |
751 | * @throws PHPUnit_Framework_AssertionFailedError | |
752 | */ | |
00be9182 | 753 | public function assertAttributesEquals($expectedValues, $actualValues, $message = NULL) { |
6a488035 TO |
754 | foreach ($expectedValues as $paramName => $paramValue) { |
755 | if (isset($actualValues[$paramName])) { | |
5896d037 | 756 | $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 |
757 | } |
758 | else { | |
759 | $this->fail("Attribute '$paramName' not present in actual array."); | |
760 | } | |
761 | } | |
762 | } | |
763 | ||
4cbe18b8 EM |
764 | /** |
765 | * @param $key | |
766 | * @param $list | |
767 | */ | |
00be9182 | 768 | public function assertArrayKeyExists($key, &$list) { |
6a488035 TO |
769 | $result = isset($list[$key]) ? TRUE : FALSE; |
770 | $this->assertTrue($result, ts("%1 element exists?", | |
771 | array(1 => $key) | |
772 | )); | |
773 | } | |
774 | ||
4cbe18b8 EM |
775 | /** |
776 | * @param $key | |
777 | * @param $list | |
778 | */ | |
00be9182 | 779 | public function assertArrayValueNotNull($key, &$list) { |
6a488035 TO |
780 | $this->assertArrayKeyExists($key, $list); |
781 | ||
782 | $value = isset($list[$key]) ? $list[$key] : NULL; | |
783 | $this->assertTrue($value, | |
784 | ts("%1 element not null?", | |
785 | array(1 => $key) | |
786 | ) | |
787 | ); | |
788 | } | |
789 | ||
c8950569 | 790 | /** |
100fef9d | 791 | * Check that api returned 'is_error' => 0 |
c8950569 | 792 | * else provide full message |
e16033b4 TO |
793 | * @param array $apiResult |
794 | * Api result. | |
795 | * @param string $prefix | |
796 | * Extra test to add to message. | |
c8950569 | 797 | */ |
00be9182 | 798 | public function assertAPISuccess($apiResult, $prefix = '') { |
6a488035 TO |
799 | if (!empty($prefix)) { |
800 | $prefix .= ': '; | |
801 | } | |
7681ca91 | 802 | $errorMessage = empty($apiResult['error_message']) ? '' : " " . $apiResult['error_message']; |
f6722559 | 803 | |
5896d037 | 804 | if (!empty($apiResult['debug_information'])) { |
f6722559 | 805 | $errorMessage .= "\n " . print_r($apiResult['debug_information'], TRUE); |
806 | } | |
5896d037 | 807 | if (!empty($apiResult['trace'])) { |
7681ca91 | 808 | $errorMessage .= "\n" . print_r($apiResult['trace'], TRUE); |
809 | } | |
810 | $this->assertEquals(0, $apiResult['is_error'], $prefix . $errorMessage); | |
6a488035 TO |
811 | } |
812 | ||
feb2a730 | 813 | /** |
100fef9d | 814 | * Check that api returned 'is_error' => 1 |
feb2a730 | 815 | * else provide full message |
2a6da8d7 | 816 | * |
e16033b4 TO |
817 | * @param array $apiResult |
818 | * Api result. | |
819 | * @param string $prefix | |
820 | * Extra test to add to message. | |
2a6da8d7 | 821 | * @param null $expectedError |
feb2a730 | 822 | */ |
00be9182 | 823 | public function assertAPIFailure($apiResult, $prefix = '', $expectedError = NULL) { |
feb2a730 | 824 | if (!empty($prefix)) { |
825 | $prefix .= ': '; | |
826 | } | |
5896d037 TO |
827 | if ($expectedError && !empty($apiResult['is_error'])) { |
828 | $this->assertEquals($expectedError, $apiResult['error_message'], 'api error message not as expected' . $prefix); | |
64fe97da | 829 | } |
feb2a730 | 830 | $this->assertEquals(1, $apiResult['is_error'], "api call should have failed but it succeeded " . $prefix . (print_r($apiResult, TRUE))); |
f6722559 | 831 | $this->assertNotEmpty($apiResult['error_message']); |
feb2a730 | 832 | } |
833 | ||
4cbe18b8 EM |
834 | /** |
835 | * @param $expected | |
836 | * @param $actual | |
837 | * @param string $message | |
838 | */ | |
00be9182 | 839 | public function assertType($expected, $actual, $message = '') { |
6a488035 TO |
840 | return $this->assertInternalType($expected, $actual, $message); |
841 | } | |
54bd1003 | 842 | |
c43d01f3 | 843 | /** |
100fef9d | 844 | * Check that a deleted item has been deleted |
c43d01f3 | 845 | */ |
00be9182 | 846 | public function assertAPIDeleted($entity, $id) { |
c43d01f3 | 847 | $this->callAPISuccess($entity, 'getcount', array('id' => $id), 0); |
848 | } | |
849 | ||
850 | ||
f6722559 | 851 | /** |
100fef9d | 852 | * Check that api returned 'is_error' => 1 |
f6722559 | 853 | * else provide full message |
c490a46a | 854 | * @param array $result |
2a6da8d7 EM |
855 | * @param $expected |
856 | * @param array $valuesToExclude | |
e16033b4 TO |
857 | * @param string $prefix |
858 | * Extra test to add to message. | |
f6722559 | 859 | */ |
00be9182 | 860 | public function assertAPIArrayComparison($result, $expected, $valuesToExclude = array(), $prefix = '') { |
f6722559 | 861 | $valuesToExclude = array_merge($valuesToExclude, array('debug', 'xdebug', 'sequential')); |
862 | foreach ($valuesToExclude as $value) { | |
5896d037 | 863 | if (isset($result[$value])) { |
f6722559 | 864 | unset($result[$value]); |
865 | } | |
5896d037 | 866 | if (isset($expected[$value])) { |
f6722559 | 867 | unset($expected[$value]); |
868 | } | |
869 | } | |
870 | $this->assertEquals($result, $expected, "api result array comparison failed " . $prefix . print_r($result, TRUE) . ' was compared to ' . print_r($expected, TRUE)); | |
871 | } | |
872 | ||
0f643fb2 TO |
873 | /** |
874 | * A stub for the API interface. This can be overriden by subclasses to change how the API is called. | |
875 | * | |
876 | * @param $entity | |
877 | * @param $action | |
c490a46a | 878 | * @param array $params |
0f643fb2 TO |
879 | * @return array|int |
880 | */ | |
00be9182 | 881 | public function civicrm_api($entity, $action, $params) { |
0f643fb2 TO |
882 | return civicrm_api($entity, $action, $params); |
883 | } | |
884 | ||
54bd1003 | 885 | /** |
886 | * This function exists to wrap api functions | |
791c263c | 887 | * so we can ensure they succeed & throw exceptions without litterering the test with checks |
77b97be7 | 888 | * |
791c263c | 889 | * @param string $entity |
890 | * @param string $action | |
891 | * @param array $params | |
e16033b4 TO |
892 | * @param mixed $checkAgainst |
893 | * Optional value to check result against, implemented for getvalue,. | |
a86d27fc | 894 | * getcount, getsingle. Note that for getvalue the type is checked rather than the value |
895 | * for getsingle the array is compared against an array passed in - the id is not compared (for | |
896 | * better or worse ) | |
77b97be7 EM |
897 | * |
898 | * @return array|int | |
791c263c | 899 | */ |
00be9182 | 900 | public function callAPISuccess($entity, $action, $params, $checkAgainst = NULL) { |
fbda92d3 | 901 | $params = array_merge(array( |
f6722559 | 902 | 'version' => $this->_apiversion, |
fbda92d3 | 903 | 'debug' => 1, |
904 | ), | |
905 | $params | |
9443c775 | 906 | ); |
f6722559 | 907 | switch (strtolower($action)) { |
6c6e6187 | 908 | case 'getvalue': |
f6722559 | 909 | return $this->callAPISuccessGetValue($entity, $params, $checkAgainst); |
6c6e6187 TO |
910 | |
911 | case 'getsingle': | |
f6722559 | 912 | return $this->callAPISuccessGetSingle($entity, $params, $checkAgainst); |
6c6e6187 TO |
913 | |
914 | case 'getcount': | |
915 | return $this->callAPISuccessGetCount($entity, $params, $checkAgainst); | |
f6722559 | 916 | } |
0f643fb2 | 917 | $result = $this->civicrm_api($entity, $action, $params); |
54bd1003 | 918 | $this->assertAPISuccess($result, "Failure in api call for $entity $action"); |
919 | return $result; | |
920 | } | |
921 | ||
fbda92d3 | 922 | /** |
923 | * This function exists to wrap api getValue function & check the result | |
924 | * so we can ensure they succeed & throw exceptions without litterering the test with checks | |
925 | * There is a type check in this | |
77b97be7 | 926 | * |
fbda92d3 | 927 | * @param string $entity |
928 | * @param array $params | |
e16033b4 TO |
929 | * @param string $type |
930 | * Per http://php.net/manual/en/function.gettype.php possible types. | |
c8950569 KJ |
931 | * - boolean |
932 | * - integer | |
933 | * - double | |
934 | * - string | |
935 | * - array | |
936 | * - object | |
77b97be7 EM |
937 | * |
938 | * @return array|int | |
fbda92d3 | 939 | */ |
00be9182 | 940 | public function callAPISuccessGetValue($entity, $params, $type = NULL) { |
fbda92d3 | 941 | $params += array( |
f6722559 | 942 | 'version' => $this->_apiversion, |
fbda92d3 | 943 | 'debug' => 1, |
944 | ); | |
0f643fb2 | 945 | $result = $this->civicrm_api($entity, 'getvalue', $params); |
5896d037 TO |
946 | if ($type) { |
947 | if ($type == 'integer') { | |
fbda92d3 | 948 | // api seems to return integers as strings |
949 | $this->assertTrue(is_numeric($result), "expected a numeric value but got " . print_r($result, 1)); | |
950 | } | |
5896d037 TO |
951 | else { |
952 | $this->assertType($type, $result, "returned result should have been of type $type but was "); | |
fbda92d3 | 953 | } |
954 | } | |
955 | return $result; | |
956 | } | |
c8950569 | 957 | |
f6722559 | 958 | /** |
959 | * This function exists to wrap api getsingle function & check the result | |
960 | * so we can ensure they succeed & throw exceptions without litterering the test with checks | |
8deefe64 | 961 | * |
f6722559 | 962 | * @param string $entity |
963 | * @param array $params | |
e16033b4 TO |
964 | * @param array $checkAgainst |
965 | * Array to compare result against. | |
f6722559 | 966 | * - boolean |
967 | * - integer | |
968 | * - double | |
969 | * - string | |
970 | * - array | |
971 | * - object | |
8deefe64 | 972 | * |
cbdcc634 | 973 | * @throws Exception |
8deefe64 | 974 | * @return array|int |
f6722559 | 975 | */ |
00be9182 | 976 | public function callAPISuccessGetSingle($entity, $params, $checkAgainst = NULL) { |
f6722559 | 977 | $params += array( |
978 | 'version' => $this->_apiversion, | |
979 | 'debug' => 1, | |
980 | ); | |
0f643fb2 | 981 | $result = $this->civicrm_api($entity, 'getsingle', $params); |
5896d037 | 982 | if (!is_array($result) || !empty($result['is_error']) || isset($result['values'])) { |
f6722559 | 983 | throw new Exception('Invalid getsingle result' . print_r($result, TRUE)); |
984 | } | |
5896d037 | 985 | if ($checkAgainst) { |
f6722559 | 986 | // @todo - have gone with the fn that unsets id? should we check id? |
987 | $this->checkArrayEquals($result, $checkAgainst); | |
988 | } | |
989 | return $result; | |
990 | } | |
2a6da8d7 | 991 | |
f6722559 | 992 | /** |
993 | * This function exists to wrap api getValue function & check the result | |
994 | * so we can ensure they succeed & throw exceptions without litterering the test with checks | |
995 | * There is a type check in this | |
996 | * @param string $entity | |
997 | * @param array $params | |
2a6da8d7 EM |
998 | * @param null $count |
999 | * @throws Exception | |
1000 | * @return array|int | |
f6722559 | 1001 | */ |
00be9182 | 1002 | public function callAPISuccessGetCount($entity, $params, $count = NULL) { |
f6722559 | 1003 | $params += array( |
1004 | 'version' => $this->_apiversion, | |
1005 | 'debug' => 1, | |
1006 | ); | |
0f643fb2 | 1007 | $result = $this->civicrm_api($entity, 'getcount', $params); |
5896d037 | 1008 | if (!is_integer($result) || !empty($result['is_error']) || isset($result['values'])) { |
f6722559 | 1009 | throw new Exception('Invalid getcount result : ' . print_r($result, TRUE) . " type :" . gettype($result)); |
1010 | } | |
5896d037 | 1011 | if (is_int($count)) { |
fd843187 | 1012 | $this->assertEquals($count, $result, "incorrect count returned from $entity getcount"); |
f6722559 | 1013 | } |
1014 | return $result; | |
1015 | } | |
69f028bc | 1016 | |
54bd1003 | 1017 | /** |
1018 | * This function exists to wrap api functions | |
1019 | * so we can ensure they succeed, generate and example & throw exceptions without litterering the test with checks | |
1020 | * | |
1021 | * @param string $entity | |
1022 | * @param string $action | |
1023 | * @param array $params | |
e16033b4 TO |
1024 | * @param string $function |
1025 | * Pass this in to create a generated example. | |
1026 | * @param string $file | |
1027 | * Pass this in to create a generated example. | |
69f028bc CW |
1028 | * @param string $description |
1029 | * @param string|null $subfile | |
1030 | * @param string|null $actionName | |
1031 | * @return array|int | |
54bd1003 | 1032 | */ |
5896d037 | 1033 | public function callAPIAndDocument($entity, $action, $params, $function, $file, $description = "", $subfile = NULL, $actionName = NULL) { |
f6722559 | 1034 | $params['version'] = $this->_apiversion; |
17400ace | 1035 | $result = $this->callAPISuccess($entity, $action, $params); |
9443c775 | 1036 | $this->documentMe($params, $result, $function, $file, $description, $subfile, $actionName); |
54bd1003 | 1037 | return $result; |
1038 | } | |
1039 | ||
1040 | /** | |
1041 | * This function exists to wrap api functions | |
1042 | * so we can ensure they fail where expected & throw exceptions without litterering the test with checks | |
1043 | * @param string $entity | |
1044 | * @param string $action | |
1045 | * @param array $params | |
e16033b4 TO |
1046 | * @param string $expectedErrorMessage |
1047 | * Error. | |
2a6da8d7 EM |
1048 | * @param null $extraOutput |
1049 | * @return array|int | |
54bd1003 | 1050 | */ |
00be9182 | 1051 | public function callAPIFailure($entity, $action, $params, $expectedErrorMessage = NULL, $extraOutput = NULL) { |
9443c775 CW |
1052 | if (is_array($params)) { |
1053 | $params += array( | |
f6722559 | 1054 | 'version' => $this->_apiversion, |
9443c775 CW |
1055 | ); |
1056 | } | |
0f643fb2 | 1057 | $result = $this->civicrm_api($entity, $action, $params); |
54bd1003 | 1058 | $this->assertAPIFailure($result, "We expected a failure for $entity $action but got a success"); |
791c263c | 1059 | return $result; |
1060 | } | |
9443c775 | 1061 | |
fbda92d3 | 1062 | /** |
1063 | * Create required data based on $this->entity & $this->params | |
1064 | * This is just a way to set up the test data for delete & get functions | |
1065 | * so the distinction between set | |
1066 | * up & tested functions is clearer | |
1067 | * | |
6c6e6187 | 1068 | * @return array api Result |
fbda92d3 | 1069 | */ |
5896d037 | 1070 | public function createTestEntity() { |
fbda92d3 | 1071 | return $entity = $this->callAPISuccess($this->entity, 'create', $this->params); |
1072 | } | |
1073 | ||
6a488035 TO |
1074 | /** |
1075 | * Generic function to create Organisation, to be used in test cases | |
1076 | * | |
e16033b4 TO |
1077 | * @param array parameters for civicrm_contact_add api function call |
1078 | * @param int sequence number if creating multiple organizations | |
6a488035 TO |
1079 | * |
1080 | * @return int id of Organisation created | |
1081 | */ | |
00be9182 | 1082 | public function organizationCreate($params = array(), $seq = 0) { |
6a488035 TO |
1083 | if (!$params) { |
1084 | $params = array(); | |
1085 | } | |
4cc99d00 | 1086 | $params = array_merge($this->sampleContact('Organization', $seq), $params); |
1087 | return $this->_contactCreate($params); | |
6a488035 TO |
1088 | } |
1089 | ||
1090 | /** | |
1091 | * Generic function to create Individual, to be used in test cases | |
1092 | * | |
e16033b4 TO |
1093 | * @param array parameters for civicrm_contact_add api function call |
1094 | * @param int sequence number if creating multiple individuals | |
6a488035 TO |
1095 | * |
1096 | * @return int id of Individual created | |
1097 | */ | |
00be9182 | 1098 | public function individualCreate($params = array(), $seq = 0) { |
4cc99d00 | 1099 | $params = array_merge($this->sampleContact('Individual', $seq), $params); |
6a488035 TO |
1100 | return $this->_contactCreate($params); |
1101 | } | |
1102 | ||
1103 | /** | |
1104 | * Generic function to create Household, to be used in test cases | |
1105 | * | |
e16033b4 TO |
1106 | * @param array parameters for civicrm_contact_add api function call |
1107 | * @param int sequence number if creating multiple households | |
6a488035 TO |
1108 | * |
1109 | * @return int id of Household created | |
1110 | */ | |
00be9182 | 1111 | public function householdCreate($params = array(), $seq = 0) { |
dc8624c6 | 1112 | $params = array_merge($this->sampleContact('Household', $seq), $params); |
6a488035 TO |
1113 | return $this->_contactCreate($params); |
1114 | } | |
1115 | ||
4cc99d00 | 1116 | /** |
1117 | * Helper function for getting sample contact properties | |
1118 | * | |
e16033b4 TO |
1119 | * @param enum contact type: Individual, Organization |
1120 | * @param int sequence number for the values of this type | |
4cc99d00 | 1121 | * |
1122 | * @return array properties of sample contact (ie. $params for API call) | |
1123 | */ | |
00be9182 | 1124 | public function sampleContact($contact_type, $seq = 0) { |
4cc99d00 | 1125 | $samples = array( |
1126 | 'Individual' => array( | |
1127 | // The number of values in each list need to be coprime numbers to not have duplicates | |
1128 | 'first_name' => array('Anthony', 'Joe', 'Terrence', 'Lucie', 'Albert', 'Bill', 'Kim'), | |
1129 | 'middle_name' => array('J.', 'M.', 'P', 'L.', 'K.', 'A.', 'B.', 'C.', 'D', 'E.', 'Z.'), | |
1130 | 'last_name' => array('Anderson', 'Miller', 'Smith', 'Collins', 'Peterson'), | |
1131 | ), | |
1132 | 'Organization' => array( | |
5896d037 TO |
1133 | 'organization_name' => array( |
1134 | 'Unit Test Organization', | |
1135 | 'Acme', | |
1136 | 'Roberts and Sons', | |
1137 | 'Cryo Space Labs', | |
21dfd5f5 | 1138 | 'Sharper Pens', |
5896d037 | 1139 | ), |
4cc99d00 | 1140 | ), |
1141 | 'Household' => array( | |
1142 | 'household_name' => array('Unit Test household'), | |
1143 | ), | |
1144 | ); | |
1145 | $params = array('contact_type' => $contact_type); | |
1146 | foreach ($samples[$contact_type] as $key => $values) { | |
1147 | $params[$key] = $values[$seq % sizeof($values)]; | |
1148 | } | |
5896d037 | 1149 | if ($contact_type == 'Individual') { |
6c6e6187 | 1150 | $params['email'] = strtolower( |
5896d037 TO |
1151 | $params['first_name'] . '_' . $params['last_name'] . '@civicrm.org' |
1152 | ); | |
6c6e6187 TO |
1153 | $params['prefix_id'] = 3; |
1154 | $params['suffix_id'] = 3; | |
4cc99d00 | 1155 | } |
1156 | return $params; | |
1157 | } | |
1158 | ||
6a488035 TO |
1159 | /** |
1160 | * Private helper function for calling civicrm_contact_add | |
1161 | * | |
e16033b4 TO |
1162 | * @param array $params |
1163 | * For civicrm_contact_add api function call. | |
9da2e77c E |
1164 | * |
1165 | * @throws Exception | |
6a488035 TO |
1166 | * |
1167 | * @return int id of Household created | |
1168 | */ | |
1169 | private function _contactCreate($params) { | |
4732bb2f | 1170 | $result = $this->callAPISuccess('contact', 'create', $params); |
8cc574cf | 1171 | if (!empty($result['is_error']) || empty($result['id'])) { |
ace01fda | 1172 | 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 |
1173 | } |
1174 | return $result['id']; | |
1175 | } | |
1176 | ||
4cbe18b8 | 1177 | /** |
100fef9d | 1178 | * @param int $contactID |
4cbe18b8 EM |
1179 | * |
1180 | * @return array|int | |
1181 | */ | |
00be9182 | 1182 | public function contactDelete($contactID) { |
ace01fda CW |
1183 | $params = array( |
1184 | 'id' => $contactID, | |
ace01fda CW |
1185 | 'skip_undelete' => 1, |
1186 | 'debug' => 1, | |
1187 | ); | |
6a488035 TO |
1188 | $domain = new CRM_Core_BAO_Domain; |
1189 | $domain->contact_id = $contactID; | |
1190 | if ($domain->find(TRUE)) { | |
1191 | // we are finding tests trying to delete the domain contact in cleanup | |
1192 | //since this is mainly for cleanup lets put a safeguard here | |
1193 | return; | |
1194 | } | |
f6722559 | 1195 | $result = $this->callAPISuccess('contact', 'delete', $params); |
6c6e6187 | 1196 | return $result; |
6a488035 TO |
1197 | } |
1198 | ||
4cbe18b8 | 1199 | /** |
100fef9d | 1200 | * @param int $contactTypeId |
4cbe18b8 EM |
1201 | * |
1202 | * @throws Exception | |
1203 | */ | |
00be9182 | 1204 | public function contactTypeDelete($contactTypeId) { |
6a488035 TO |
1205 | require_once 'CRM/Contact/BAO/ContactType.php'; |
1206 | $result = CRM_Contact_BAO_ContactType::del($contactTypeId); | |
1207 | if (!$result) { | |
1208 | throw new Exception('Could not delete contact type'); | |
1209 | } | |
1210 | } | |
1211 | ||
4cbe18b8 EM |
1212 | /** |
1213 | * @param array $params | |
1214 | * | |
1215 | * @return mixed | |
1216 | */ | |
00be9182 | 1217 | public function membershipTypeCreate($params = array()) { |
6a488035 TO |
1218 | CRM_Member_PseudoConstant::flush('membershipType'); |
1219 | CRM_Core_Config::clearDBCache(); | |
0090e3d2 | 1220 | $memberOfOrganization = $this->organizationCreate(); |
75638074 | 1221 | $params = array_merge(array( |
6a488035 TO |
1222 | 'name' => 'General', |
1223 | 'duration_unit' => 'year', | |
1224 | 'duration_interval' => 1, | |
1225 | 'period_type' => 'rolling', | |
0090e3d2 | 1226 | 'member_of_contact_id' => $memberOfOrganization, |
6a488035 | 1227 | 'domain_id' => 1, |
75638074 | 1228 | 'financial_type_id' => 1, |
6a488035 | 1229 | 'is_active' => 1, |
6a488035 | 1230 | 'sequential' => 1, |
eacf220c | 1231 | 'visibility' => 'Public', |
75638074 | 1232 | ), $params); |
1233 | ||
1234 | $result = $this->callAPISuccess('MembershipType', 'Create', $params); | |
6a488035 | 1235 | |
6a488035 TO |
1236 | CRM_Member_PseudoConstant::flush('membershipType'); |
1237 | CRM_Utils_Cache::singleton()->flush(); | |
6a488035 TO |
1238 | |
1239 | return $result['id']; | |
1240 | } | |
1241 | ||
4cbe18b8 | 1242 | /** |
c490a46a | 1243 | * @param array $params |
4cbe18b8 EM |
1244 | * |
1245 | * @return mixed | |
1246 | */ | |
00be9182 | 1247 | public function contactMembershipCreate($params) { |
6a488035 TO |
1248 | $pre = array( |
1249 | 'join_date' => '2007-01-21', | |
1250 | 'start_date' => '2007-01-21', | |
1251 | 'end_date' => '2007-12-21', | |
1252 | 'source' => 'Payment', | |
6a488035 TO |
1253 | ); |
1254 | ||
1255 | foreach ($pre as $key => $val) { | |
1256 | if (!isset($params[$key])) { | |
1257 | $params[$key] = $val; | |
1258 | } | |
1259 | } | |
1260 | ||
f6722559 | 1261 | $result = $this->callAPISuccess('Membership', 'create', $params); |
6a488035 TO |
1262 | return $result['id']; |
1263 | } | |
1264 | ||
1265 | /** | |
100fef9d | 1266 | * Delete Membership Type |
6a488035 | 1267 | * |
c490a46a | 1268 | * @param array $params |
6a488035 | 1269 | */ |
00be9182 | 1270 | public function membershipTypeDelete($params) { |
cab024d4 | 1271 | $this->callAPISuccess('MembershipType', 'Delete', $params); |
6a488035 TO |
1272 | } |
1273 | ||
4cbe18b8 | 1274 | /** |
100fef9d | 1275 | * @param int $membershipID |
4cbe18b8 | 1276 | */ |
00be9182 | 1277 | public function membershipDelete($membershipID) { |
f6722559 | 1278 | $deleteParams = array('id' => $membershipID); |
1279 | $result = $this->callAPISuccess('Membership', 'Delete', $deleteParams); | |
6a488035 TO |
1280 | return; |
1281 | } | |
1282 | ||
4cbe18b8 EM |
1283 | /** |
1284 | * @param string $name | |
1285 | * | |
1286 | * @return mixed | |
1287 | */ | |
00be9182 | 1288 | public function membershipStatusCreate($name = 'test member status') { |
6a488035 TO |
1289 | $params['name'] = $name; |
1290 | $params['start_event'] = 'start_date'; | |
1291 | $params['end_event'] = 'end_date'; | |
1292 | $params['is_current_member'] = 1; | |
1293 | $params['is_active'] = 1; | |
6a488035 | 1294 | |
f6722559 | 1295 | $result = $this->callAPISuccess('MembershipStatus', 'Create', $params); |
6a488035 | 1296 | CRM_Member_PseudoConstant::flush('membershipStatus'); |
6a488035 TO |
1297 | return $result['id']; |
1298 | } | |
1299 | ||
4cbe18b8 | 1300 | /** |
100fef9d | 1301 | * @param int $membershipStatusID |
4cbe18b8 | 1302 | */ |
00be9182 | 1303 | public function membershipStatusDelete($membershipStatusID) { |
6a488035 TO |
1304 | if (!$membershipStatusID) { |
1305 | return; | |
1306 | } | |
4732bb2f | 1307 | $result = $this->callAPISuccess('MembershipStatus', 'Delete', array('id' => $membershipStatusID)); |
6a488035 TO |
1308 | return; |
1309 | } | |
1310 | ||
4cbe18b8 EM |
1311 | /** |
1312 | * @param array $params | |
1313 | * | |
1314 | * @return mixed | |
1315 | */ | |
00be9182 | 1316 | public function relationshipTypeCreate($params = array()) { |
75638074 | 1317 | $params = array_merge(array( |
6a488035 TO |
1318 | 'name_a_b' => 'Relation 1 for relationship type create', |
1319 | 'name_b_a' => 'Relation 2 for relationship type create', | |
1320 | 'contact_type_a' => 'Individual', | |
1321 | 'contact_type_b' => 'Organization', | |
1322 | 'is_reserved' => 1, | |
1323 | 'is_active' => 1, | |
75638074 | 1324 | ), |
5896d037 | 1325 | $params |
75638074 | 1326 | ); |
6a488035 | 1327 | |
f6722559 | 1328 | $result = $this->callAPISuccess('relationship_type', 'create', $params); |
6a488035 TO |
1329 | CRM_Core_PseudoConstant::flush('relationshipType'); |
1330 | ||
1331 | return $result['id']; | |
1332 | } | |
1333 | ||
1334 | /** | |
100fef9d | 1335 | * Delete Relatinship Type |
6a488035 TO |
1336 | * |
1337 | * @param int $relationshipTypeID | |
1338 | */ | |
00be9182 | 1339 | public function relationshipTypeDelete($relationshipTypeID) { |
6a488035 | 1340 | $params['id'] = $relationshipTypeID; |
f6722559 | 1341 | $this->callAPISuccess('relationship_type', 'delete', $params); |
6a488035 TO |
1342 | } |
1343 | ||
4cbe18b8 | 1344 | /** |
100fef9d | 1345 | * @param array $params |
4cbe18b8 EM |
1346 | * |
1347 | * @return mixed | |
1348 | */ | |
00be9182 | 1349 | public function paymentProcessorTypeCreate($params = NULL) { |
6a488035 TO |
1350 | if (is_null($params)) { |
1351 | $params = array( | |
1352 | 'name' => 'API_Test_PP', | |
1353 | 'title' => 'API Test Payment Processor', | |
1354 | 'class_name' => 'CRM_Core_Payment_APITest', | |
1355 | 'billing_mode' => 'form', | |
1356 | 'is_recur' => 0, | |
1357 | 'is_reserved' => 1, | |
1358 | 'is_active' => 1, | |
1359 | ); | |
1360 | } | |
f6722559 | 1361 | $result = $this->callAPISuccess('payment_processor_type', 'create', $params); |
6a488035 | 1362 | |
6a488035 TO |
1363 | CRM_Core_PseudoConstant::flush('paymentProcessorType'); |
1364 | ||
1365 | return $result['id']; | |
1366 | } | |
1367 | ||
1368 | /** | |
100fef9d | 1369 | * Create Participant |
6a488035 | 1370 | * |
e16033b4 TO |
1371 | * @param array $params |
1372 | * Array of contact id and event id values. | |
6a488035 TO |
1373 | * |
1374 | * @return int $id of participant created | |
1375 | */ | |
00be9182 | 1376 | public function participantCreate($params) { |
5896d037 | 1377 | if (empty($params['contact_id'])) { |
94692f2d | 1378 | $params['contact_id'] = $this->individualCreate(); |
1379 | } | |
5896d037 | 1380 | if (empty($params['event_id'])) { |
94692f2d | 1381 | $event = $this->eventCreate(); |
1382 | $params['event_id'] = $event['id']; | |
1383 | } | |
6a488035 | 1384 | $defaults = array( |
6a488035 TO |
1385 | 'status_id' => 2, |
1386 | 'role_id' => 1, | |
1387 | 'register_date' => 20070219, | |
1388 | 'source' => 'Wimbeldon', | |
1389 | 'event_level' => 'Payment', | |
94692f2d | 1390 | 'debug' => 1, |
6a488035 TO |
1391 | ); |
1392 | ||
1393 | $params = array_merge($defaults, $params); | |
f6722559 | 1394 | $result = $this->callAPISuccess('Participant', 'create', $params); |
6a488035 TO |
1395 | return $result['id']; |
1396 | } | |
1397 | ||
1398 | /** | |
100fef9d | 1399 | * Create Payment Processor |
6a488035 TO |
1400 | * |
1401 | * @return object of Payment Processsor | |
1402 | */ | |
00be9182 | 1403 | public function processorCreate() { |
6a488035 TO |
1404 | $processorParams = array( |
1405 | 'domain_id' => 1, | |
1406 | 'name' => 'Dummy', | |
1407 | 'payment_processor_type_id' => 10, | |
1408 | 'financial_account_id' => 12, | |
1409 | 'is_active' => 1, | |
1410 | 'user_name' => '', | |
1411 | 'url_site' => 'http://dummy.com', | |
1412 | 'url_recur' => 'http://dummy.com', | |
1413 | 'billing_mode' => 1, | |
1414 | ); | |
1415 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($processorParams); | |
1416 | return $paymentProcessor; | |
1417 | } | |
1418 | ||
1419 | /** | |
100fef9d | 1420 | * Create contribution page |
6a488035 | 1421 | * |
c490a46a | 1422 | * @param array $params |
6a488035 TO |
1423 | * @return object of contribution page |
1424 | */ | |
00be9182 | 1425 | public function contributionPageCreate($params) { |
6a488035 | 1426 | $this->_pageParams = array( |
6a488035 TO |
1427 | 'title' => 'Test Contribution Page', |
1428 | 'financial_type_id' => 1, | |
1429 | 'currency' => 'USD', | |
1430 | 'financial_account_id' => 1, | |
1431 | 'payment_processor' => $params['processor_id'], | |
1432 | 'is_active' => 1, | |
1433 | 'is_allow_other_amount' => 1, | |
1434 | 'min_amount' => 10, | |
1435 | 'max_amount' => 1000, | |
1436 | ); | |
f6722559 | 1437 | $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams); |
6a488035 TO |
1438 | return $contributionPage; |
1439 | } | |
1440 | ||
6a488035 | 1441 | /** |
100fef9d | 1442 | * Create Tag |
6a488035 | 1443 | * |
2a6da8d7 | 1444 | * @param array $params |
fb32de45 | 1445 | * @return array result of created tag |
6a488035 | 1446 | */ |
00be9182 | 1447 | public function tagCreate($params = array()) { |
fb32de45 | 1448 | $defaults = array( |
1449 | 'name' => 'New Tag3', | |
1450 | 'description' => 'This is description for Our New Tag ', | |
1451 | 'domain_id' => '1', | |
1452 | ); | |
1453 | $params = array_merge($defaults, $params); | |
6c6e6187 | 1454 | $result = $this->callAPISuccess('Tag', 'create', $params); |
fb32de45 | 1455 | return $result['values'][$result['id']]; |
6a488035 TO |
1456 | } |
1457 | ||
1458 | /** | |
100fef9d | 1459 | * Delete Tag |
6a488035 | 1460 | * |
e16033b4 TO |
1461 | * @param int $tagId |
1462 | * Id of the tag to be deleted. | |
6a488035 | 1463 | */ |
00be9182 | 1464 | public function tagDelete($tagId) { |
6a488035 TO |
1465 | require_once 'api/api.php'; |
1466 | $params = array( | |
1467 | 'tag_id' => $tagId, | |
6a488035 | 1468 | ); |
f6722559 | 1469 | $result = $this->callAPISuccess('Tag', 'delete', $params); |
6a488035 TO |
1470 | return $result['id']; |
1471 | } | |
1472 | ||
1473 | /** | |
1474 | * Add entity(s) to the tag | |
1475 | * | |
e16033b4 | 1476 | * @param array $params |
77b97be7 EM |
1477 | * |
1478 | * @return bool | |
6a488035 | 1479 | */ |
00be9182 | 1480 | public function entityTagAdd($params) { |
f6722559 | 1481 | $result = $this->callAPISuccess('entity_tag', 'create', $params); |
6a488035 TO |
1482 | return TRUE; |
1483 | } | |
1484 | ||
1485 | /** | |
100fef9d | 1486 | * Create contribution |
6a488035 | 1487 | * |
e16033b4 TO |
1488 | * @param int $cID |
1489 | * Contact_id. | |
77b97be7 | 1490 | * |
6a488035 TO |
1491 | * @return int id of created contribution |
1492 | */ | |
00be9182 | 1493 | public function pledgeCreate($cID) { |
6a488035 TO |
1494 | $params = array( |
1495 | 'contact_id' => $cID, | |
1496 | 'pledge_create_date' => date('Ymd'), | |
1497 | 'start_date' => date('Ymd'), | |
1498 | 'scheduled_date' => date('Ymd'), | |
1499 | 'amount' => 100.00, | |
1500 | 'pledge_status_id' => '2', | |
1501 | 'financial_type_id' => '1', | |
1502 | 'pledge_original_installment_amount' => 20, | |
1503 | 'frequency_interval' => 5, | |
1504 | 'frequency_unit' => 'year', | |
1505 | 'frequency_day' => 15, | |
1506 | 'installments' => 5, | |
6a488035 TO |
1507 | ); |
1508 | ||
f6722559 | 1509 | $result = $this->callAPISuccess('Pledge', 'create', $params); |
6a488035 TO |
1510 | return $result['id']; |
1511 | } | |
1512 | ||
1513 | /** | |
100fef9d | 1514 | * Delete contribution |
6a488035 | 1515 | * |
c490a46a | 1516 | * @param int $pledgeId |
6a488035 | 1517 | */ |
00be9182 | 1518 | public function pledgeDelete($pledgeId) { |
6a488035 TO |
1519 | $params = array( |
1520 | 'pledge_id' => $pledgeId, | |
6a488035 | 1521 | ); |
f6722559 | 1522 | $this->callAPISuccess('Pledge', 'delete', $params); |
6a488035 TO |
1523 | } |
1524 | ||
1525 | /** | |
100fef9d | 1526 | * Create contribution |
6a488035 | 1527 | * |
e16033b4 TO |
1528 | * @param int $cID |
1529 | * Contact_id. | |
1530 | * @param int $cTypeID | |
1531 | * Id of financial type. | |
6a488035 | 1532 | * |
2a6da8d7 EM |
1533 | * @param int $invoiceID |
1534 | * @param int $trxnID | |
1535 | * @param int $paymentInstrumentID | |
1536 | * @param bool $isFee | |
6a488035 TO |
1537 | * @return int id of created contribution |
1538 | */ | |
00be9182 | 1539 | public function contributionCreate($cID, $cTypeID = 1, $invoiceID = 67890, $trxnID = 12345, $paymentInstrumentID = 1, $isFee = TRUE) { |
6a488035 TO |
1540 | $params = array( |
1541 | 'domain_id' => 1, | |
1542 | 'contact_id' => $cID, | |
1543 | 'receive_date' => date('Ymd'), | |
1544 | 'total_amount' => 100.00, | |
1545 | 'financial_type_id' => empty($cTypeID) ? 1 : $cTypeID, | |
1546 | 'payment_instrument_id' => empty($paymentInstrumentID) ? 1 : $paymentInstrumentID, | |
1547 | 'non_deductible_amount' => 10.00, | |
1548 | 'trxn_id' => $trxnID, | |
1549 | 'invoice_id' => $invoiceID, | |
1550 | 'source' => 'SSF', | |
6a488035 TO |
1551 | 'contribution_status_id' => 1, |
1552 | // 'note' => 'Donating for Nobel Cause', *Fixme | |
1553 | ); | |
1554 | ||
1555 | if ($isFee) { | |
1556 | $params['fee_amount'] = 5.00; | |
1557 | $params['net_amount'] = 95.00; | |
1558 | } | |
1559 | ||
f6722559 | 1560 | $result = $this->callAPISuccess('contribution', 'create', $params); |
6a488035 TO |
1561 | return $result['id']; |
1562 | } | |
1563 | ||
1564 | /** | |
100fef9d | 1565 | * Create online contribution |
6a488035 | 1566 | * |
c490a46a | 1567 | * @param array $params |
e16033b4 TO |
1568 | * @param int $financialType |
1569 | * Id of financial type. | |
6a488035 | 1570 | * |
2a6da8d7 EM |
1571 | * @param int $invoiceID |
1572 | * @param int $trxnID | |
6a488035 TO |
1573 | * @return int id of created contribution |
1574 | */ | |
00be9182 | 1575 | public function onlineContributionCreate($params, $financialType, $invoiceID = 67890, $trxnID = 12345) { |
6a488035 TO |
1576 | $contribParams = array( |
1577 | 'contact_id' => $params['contact_id'], | |
1578 | 'receive_date' => date('Ymd'), | |
1579 | 'total_amount' => 100.00, | |
1580 | 'financial_type_id' => $financialType, | |
1581 | 'contribution_page_id' => $params['contribution_page_id'], | |
1582 | 'trxn_id' => 12345, | |
1583 | 'invoice_id' => 67890, | |
1584 | 'source' => 'SSF', | |
6a488035 | 1585 | ); |
f6722559 | 1586 | $contribParams = array_merge($contribParams, $params); |
1587 | $result = $this->callAPISuccess('contribution', 'create', $contribParams); | |
6a488035 TO |
1588 | |
1589 | return $result['id']; | |
1590 | } | |
1591 | ||
1592 | /** | |
100fef9d | 1593 | * Delete contribution |
6a488035 TO |
1594 | * |
1595 | * @param int $contributionId | |
8deefe64 EM |
1596 | * |
1597 | * @return array|int | |
6a488035 | 1598 | */ |
00be9182 | 1599 | public function contributionDelete($contributionId) { |
6a488035 TO |
1600 | $params = array( |
1601 | 'contribution_id' => $contributionId, | |
6a488035 | 1602 | ); |
f6722559 | 1603 | $result = $this->callAPISuccess('contribution', 'delete', $params); |
6a488035 TO |
1604 | return $result; |
1605 | } | |
1606 | ||
1607 | /** | |
100fef9d | 1608 | * Create an Event |
6a488035 | 1609 | * |
e16033b4 TO |
1610 | * @param array $params |
1611 | * Name-value pair for an event. | |
6a488035 TO |
1612 | * |
1613 | * @return array $event | |
1614 | */ | |
00be9182 | 1615 | public function eventCreate($params = array()) { |
6a488035 TO |
1616 | // if no contact was passed, make up a dummy event creator |
1617 | if (!isset($params['contact_id'])) { | |
1618 | $params['contact_id'] = $this->_contactCreate(array( | |
1619 | 'contact_type' => 'Individual', | |
1620 | 'first_name' => 'Event', | |
1621 | 'last_name' => 'Creator', | |
6a488035 TO |
1622 | )); |
1623 | } | |
1624 | ||
1625 | // set defaults for missing params | |
1626 | $params = array_merge(array( | |
1627 | 'title' => 'Annual CiviCRM meet', | |
1628 | 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now', | |
1629 | 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues', | |
1630 | 'event_type_id' => 1, | |
1631 | 'is_public' => 1, | |
1632 | 'start_date' => 20081021, | |
1633 | 'end_date' => 20081023, | |
1634 | 'is_online_registration' => 1, | |
1635 | 'registration_start_date' => 20080601, | |
1636 | 'registration_end_date' => 20081015, | |
1637 | 'max_participants' => 100, | |
1638 | 'event_full_text' => 'Sorry! We are already full', | |
1639 | 'is_monetory' => 0, | |
1640 | 'is_active' => 1, | |
6a488035 TO |
1641 | 'is_show_location' => 0, |
1642 | ), $params); | |
1643 | ||
8cba5814 | 1644 | return $this->callAPISuccess('Event', 'create', $params); |
6a488035 TO |
1645 | } |
1646 | ||
1647 | /** | |
100fef9d | 1648 | * Delete event |
6a488035 | 1649 | * |
e16033b4 TO |
1650 | * @param int $id |
1651 | * ID of the event. | |
8deefe64 EM |
1652 | * |
1653 | * @return array|int | |
6a488035 | 1654 | */ |
00be9182 | 1655 | public function eventDelete($id) { |
6a488035 TO |
1656 | $params = array( |
1657 | 'event_id' => $id, | |
6a488035 | 1658 | ); |
8cba5814 | 1659 | return $this->callAPISuccess('event', 'delete', $params); |
6a488035 TO |
1660 | } |
1661 | ||
1662 | /** | |
100fef9d | 1663 | * Delete participant |
6a488035 TO |
1664 | * |
1665 | * @param int $participantID | |
8deefe64 EM |
1666 | * |
1667 | * @return array|int | |
6a488035 | 1668 | */ |
00be9182 | 1669 | public function participantDelete($participantID) { |
6a488035 TO |
1670 | $params = array( |
1671 | 'id' => $participantID, | |
6a488035 | 1672 | ); |
8cba5814 | 1673 | return $this->callAPISuccess('Participant', 'delete', $params); |
6a488035 TO |
1674 | } |
1675 | ||
1676 | /** | |
100fef9d | 1677 | * Create participant payment |
6a488035 | 1678 | * |
100fef9d CW |
1679 | * @param int $participantID |
1680 | * @param int $contributionID | |
6a488035 TO |
1681 | * @return int $id of created payment |
1682 | */ | |
00be9182 | 1683 | public function participantPaymentCreate($participantID, $contributionID = NULL) { |
6a488035 TO |
1684 | //Create Participant Payment record With Values |
1685 | $params = array( | |
1686 | 'participant_id' => $participantID, | |
1687 | 'contribution_id' => $contributionID, | |
6a488035 TO |
1688 | ); |
1689 | ||
f6722559 | 1690 | $result = $this->callAPISuccess('participant_payment', 'create', $params); |
6a488035 TO |
1691 | return $result['id']; |
1692 | } | |
1693 | ||
1694 | /** | |
100fef9d | 1695 | * Delete participant payment |
6a488035 TO |
1696 | * |
1697 | * @param int $paymentID | |
1698 | */ | |
00be9182 | 1699 | public function participantPaymentDelete($paymentID) { |
6a488035 TO |
1700 | $params = array( |
1701 | 'id' => $paymentID, | |
6a488035 | 1702 | ); |
f6722559 | 1703 | $result = $this->callAPISuccess('participant_payment', 'delete', $params); |
6a488035 TO |
1704 | } |
1705 | ||
1706 | /** | |
100fef9d | 1707 | * Add a Location |
6a488035 | 1708 | * |
100fef9d | 1709 | * @param int $contactID |
6a488035 TO |
1710 | * @return int location id of created location |
1711 | */ | |
00be9182 | 1712 | public function locationAdd($contactID) { |
6a488035 TO |
1713 | $address = array( |
1714 | 1 => array( | |
1715 | 'location_type' => 'New Location Type', | |
1716 | 'is_primary' => 1, | |
1717 | 'name' => 'Saint Helier St', | |
1718 | 'county' => 'Marin', | |
1719 | 'country' => 'United States', | |
1720 | 'state_province' => 'Michigan', | |
1721 | 'supplemental_address_1' => 'Hallmark Ct', | |
1722 | 'supplemental_address_2' => 'Jersey Village', | |
21dfd5f5 | 1723 | ), |
6a488035 TO |
1724 | ); |
1725 | ||
1726 | $params = array( | |
1727 | 'contact_id' => $contactID, | |
1728 | 'address' => $address, | |
6a488035 TO |
1729 | 'location_format' => '2.0', |
1730 | 'location_type' => 'New Location Type', | |
1731 | ); | |
1732 | ||
f6722559 | 1733 | $result = $this->callAPISuccess('Location', 'create', $params); |
6a488035 TO |
1734 | return $result; |
1735 | } | |
1736 | ||
1737 | /** | |
100fef9d | 1738 | * Delete Locations of contact |
6a488035 | 1739 | * |
e16033b4 TO |
1740 | * @param array $params |
1741 | * Parameters. | |
6a488035 | 1742 | */ |
00be9182 | 1743 | public function locationDelete($params) { |
c490a46a | 1744 | $this->callAPISuccess('Location', 'delete', $params); |
6a488035 TO |
1745 | } |
1746 | ||
1747 | /** | |
100fef9d | 1748 | * Add a Location Type |
6a488035 | 1749 | * |
100fef9d | 1750 | * @param array $params |
a41f119c | 1751 | * @return CRM_Core_DAO_LocationType location id of created location |
6a488035 | 1752 | */ |
00be9182 | 1753 | public function locationTypeCreate($params = NULL) { |
6a488035 TO |
1754 | if ($params === NULL) { |
1755 | $params = array( | |
1756 | 'name' => 'New Location Type', | |
1757 | 'vcard_name' => 'New Location Type', | |
1758 | 'description' => 'Location Type for Delete', | |
1759 | 'is_active' => 1, | |
1760 | ); | |
1761 | } | |
1762 | ||
6a488035 TO |
1763 | $locationType = new CRM_Core_DAO_LocationType(); |
1764 | $locationType->copyValues($params); | |
1765 | $locationType->save(); | |
1766 | // clear getfields cache | |
2683ce94 | 1767 | CRM_Core_PseudoConstant::flush(); |
f6722559 | 1768 | $this->callAPISuccess('phone', 'getfields', array('version' => 3, 'cache_clear' => 1)); |
6a488035 TO |
1769 | return $locationType; |
1770 | } | |
1771 | ||
1772 | /** | |
100fef9d | 1773 | * Delete a Location Type |
6a488035 TO |
1774 | * |
1775 | * @param int location type id | |
1776 | */ | |
00be9182 | 1777 | public function locationTypeDelete($locationTypeId) { |
6a488035 TO |
1778 | $locationType = new CRM_Core_DAO_LocationType(); |
1779 | $locationType->id = $locationTypeId; | |
1780 | $locationType->delete(); | |
1781 | } | |
1782 | ||
1783 | /** | |
100fef9d | 1784 | * Add a Group |
6a488035 | 1785 | * |
2a6da8d7 | 1786 | * @param array $params |
6a488035 | 1787 | * @return int groupId of created group |
6a488035 | 1788 | */ |
00be9182 | 1789 | public function groupCreate($params = array()) { |
fadb804f | 1790 | $params = array_merge(array( |
5896d037 TO |
1791 | 'name' => 'Test Group 1', |
1792 | 'domain_id' => 1, | |
1793 | 'title' => 'New Test Group Created', | |
1794 | 'description' => 'New Test Group Created', | |
1795 | 'is_active' => 1, | |
1796 | 'visibility' => 'Public Pages', | |
1797 | 'group_type' => array( | |
1798 | '1' => 1, | |
1799 | '2' => 1, | |
1800 | ), | |
1801 | ), $params); | |
6a488035 | 1802 | |
f6722559 | 1803 | $result = $this->callAPISuccess('Group', 'create', $params); |
1804 | return $result['id']; | |
6a488035 TO |
1805 | } |
1806 | ||
7811a84b | 1807 | |
6c6e6187 | 1808 | /** |
7811a84b | 1809 | * Function to add a Group |
ef643544 | 1810 | * |
7811a84b | 1811 | * @params array to add group |
ef643544 | 1812 | * |
7811a84b | 1813 | * @param array $params |
1814 | * @return int groupId of created group | |
ef643544 | 1815 | */ |
00be9182 | 1816 | public function groupContactCreate($groupID, $totalCount = 10) { |
ef643544 | 1817 | $params = array('group_id' => $groupID); |
6c6e6187 | 1818 | for ($i = 1; $i <= $totalCount; $i++) { |
ef643544 | 1819 | $contactID = $this->individualCreate(); |
1820 | if ($i == 1) { | |
1821 | $params += array('contact_id' => $contactID); | |
1822 | } | |
1823 | else { | |
1824 | $params += array("contact_id.$i" => $contactID); | |
1825 | } | |
1826 | } | |
1827 | $result = $this->callAPISuccess('GroupContact', 'create', $params); | |
7811a84b | 1828 | |
ef643544 | 1829 | return $result; |
1830 | } | |
1831 | ||
6a488035 | 1832 | /** |
100fef9d | 1833 | * Delete a Group |
6a488035 | 1834 | * |
c490a46a | 1835 | * @param int $gid |
6a488035 | 1836 | */ |
00be9182 | 1837 | public function groupDelete($gid) { |
6a488035 TO |
1838 | |
1839 | $params = array( | |
1840 | 'id' => $gid, | |
6a488035 TO |
1841 | ); |
1842 | ||
f6722559 | 1843 | $this->callAPISuccess('Group', 'delete', $params); |
6a488035 TO |
1844 | } |
1845 | ||
5c496c74 | 1846 | /** |
1847 | * Create a UFField | |
1848 | * @param array $params | |
1849 | */ | |
00be9182 | 1850 | public function uFFieldCreate($params = array()) { |
5c496c74 | 1851 | $params = array_merge(array( |
1852 | 'uf_group_id' => 1, | |
1853 | 'field_name' => 'first_name', | |
1854 | 'is_active' => 1, | |
1855 | 'is_required' => 1, | |
1856 | 'visibility' => 'Public Pages and Listings', | |
1857 | 'is_searchable' => '1', | |
1858 | 'label' => 'first_name', | |
1859 | 'field_type' => 'Individual', | |
1860 | 'weight' => 1, | |
1861 | ), $params); | |
1862 | $this->callAPISuccess('uf_field', 'create', $params); | |
1863 | } | |
2a6da8d7 | 1864 | |
6a488035 | 1865 | /** |
100fef9d | 1866 | * Add a UF Join Entry |
6a488035 | 1867 | * |
100fef9d | 1868 | * @param array $params |
6a488035 TO |
1869 | * @return int $id of created UF Join |
1870 | */ | |
00be9182 | 1871 | public function ufjoinCreate($params = NULL) { |
6a488035 TO |
1872 | if ($params === NULL) { |
1873 | $params = array( | |
1874 | 'is_active' => 1, | |
1875 | 'module' => 'CiviEvent', | |
1876 | 'entity_table' => 'civicrm_event', | |
1877 | 'entity_id' => 3, | |
1878 | 'weight' => 1, | |
1879 | 'uf_group_id' => 1, | |
1880 | ); | |
1881 | } | |
f6722559 | 1882 | $result = $this->callAPISuccess('uf_join', 'create', $params); |
6a488035 TO |
1883 | return $result; |
1884 | } | |
1885 | ||
1886 | /** | |
100fef9d | 1887 | * Delete a UF Join Entry |
6a488035 TO |
1888 | * |
1889 | * @param array with missing uf_group_id | |
1890 | */ | |
00be9182 | 1891 | public function ufjoinDelete($params = NULL) { |
6a488035 TO |
1892 | if ($params === NULL) { |
1893 | $params = array( | |
1894 | 'is_active' => 1, | |
1895 | 'module' => 'CiviEvent', | |
1896 | 'entity_table' => 'civicrm_event', | |
1897 | 'entity_id' => 3, | |
1898 | 'weight' => 1, | |
1899 | 'uf_group_id' => '', | |
1900 | ); | |
1901 | } | |
1902 | ||
1903 | crm_add_uf_join($params); | |
1904 | } | |
1905 | ||
1906 | /** | |
100fef9d | 1907 | * Create Group for a contact |
6a488035 TO |
1908 | * |
1909 | * @param int $contactId | |
1910 | */ | |
00be9182 | 1911 | public function contactGroupCreate($contactId) { |
6a488035 TO |
1912 | $params = array( |
1913 | 'contact_id.1' => $contactId, | |
1914 | 'group_id' => 1, | |
1915 | ); | |
1916 | ||
f6722559 | 1917 | $this->callAPISuccess('GroupContact', 'Create', $params); |
6a488035 TO |
1918 | } |
1919 | ||
1920 | /** | |
100fef9d | 1921 | * Delete Group for a contact |
6a488035 | 1922 | * |
100fef9d | 1923 | * @param int $contactId |
6a488035 | 1924 | */ |
00be9182 | 1925 | public function contactGroupDelete($contactId) { |
6a488035 TO |
1926 | $params = array( |
1927 | 'contact_id.1' => $contactId, | |
1928 | 'group_id' => 1, | |
1929 | ); | |
0f643fb2 | 1930 | $this->civicrm_api('GroupContact', 'Delete', $params); |
6a488035 TO |
1931 | } |
1932 | ||
1933 | /** | |
100fef9d | 1934 | * Create Activity |
6a488035 | 1935 | * |
c490a46a | 1936 | * @param array $params |
2a6da8d7 | 1937 | * @return array|int |
6a488035 | 1938 | */ |
00be9182 | 1939 | public function activityCreate($params = NULL) { |
6a488035 TO |
1940 | |
1941 | if ($params === NULL) { | |
e4d5f1e2 | 1942 | $individualSourceID = $this->individualCreate(); |
6a488035 TO |
1943 | |
1944 | $contactParams = array( | |
1945 | 'first_name' => 'Julia', | |
1946 | 'Last_name' => 'Anderson', | |
1947 | 'prefix' => 'Ms.', | |
1948 | 'email' => 'julia_anderson@civicrm.org', | |
1949 | 'contact_type' => 'Individual', | |
6a488035 TO |
1950 | ); |
1951 | ||
1952 | $individualTargetID = $this->individualCreate($contactParams); | |
1953 | ||
1954 | $params = array( | |
1955 | 'source_contact_id' => $individualSourceID, | |
1956 | 'target_contact_id' => array($individualTargetID), | |
1957 | 'assignee_contact_id' => array($individualTargetID), | |
1958 | 'subject' => 'Discussion on warm beer', | |
1959 | 'activity_date_time' => date('Ymd'), | |
1960 | 'duration_hours' => 30, | |
1961 | 'duration_minutes' => 20, | |
1962 | 'location' => 'Baker Street', | |
1963 | 'details' => 'Lets schedule a meeting', | |
1964 | 'status_id' => 1, | |
1965 | 'activity_name' => 'Meeting', | |
6a488035 TO |
1966 | ); |
1967 | } | |
1968 | ||
f6722559 | 1969 | $result = $this->callAPISuccess('Activity', 'create', $params); |
6a488035 TO |
1970 | |
1971 | $result['target_contact_id'] = $individualTargetID; | |
1972 | $result['assignee_contact_id'] = $individualTargetID; | |
1973 | return $result; | |
1974 | } | |
1975 | ||
1976 | /** | |
100fef9d | 1977 | * Create an activity type |
6a488035 | 1978 | * |
e16033b4 TO |
1979 | * @param array $params |
1980 | * Parameters. | |
6a488035 | 1981 | */ |
00be9182 | 1982 | public function activityTypeCreate($params) { |
f6722559 | 1983 | $result = $this->callAPISuccess('ActivityType', 'create', $params); |
6a488035 TO |
1984 | return $result; |
1985 | } | |
1986 | ||
1987 | /** | |
100fef9d | 1988 | * Delete activity type |
6a488035 | 1989 | * |
e16033b4 TO |
1990 | * @param int $activityTypeId |
1991 | * Id of the activity type. | |
6a488035 | 1992 | */ |
00be9182 | 1993 | public function activityTypeDelete($activityTypeId) { |
6a488035 | 1994 | $params['activity_type_id'] = $activityTypeId; |
f6722559 | 1995 | $result = $this->callAPISuccess('ActivityType', 'delete', $params); |
6a488035 TO |
1996 | return $result; |
1997 | } | |
1998 | ||
1999 | /** | |
100fef9d | 2000 | * Create custom group |
6a488035 | 2001 | * |
2a6da8d7 EM |
2002 | * @param array $params |
2003 | * @return array|int | |
6a488035 | 2004 | */ |
00be9182 | 2005 | public function customGroupCreate($params = array()) { |
f6722559 | 2006 | $defaults = array( |
2007 | 'title' => 'new custom group', | |
2008 | 'extends' => 'Contact', | |
2009 | 'domain_id' => 1, | |
2010 | 'style' => 'Inline', | |
2011 | 'is_active' => 1, | |
2012 | ); | |
6a488035 | 2013 | |
f6722559 | 2014 | $params = array_merge($defaults, $params); |
2015 | ||
2016 | if (strlen($params['title']) > 13) { | |
6c6e6187 | 2017 | $params['title'] = substr($params['title'], 0, 13); |
6a488035 | 2018 | } |
6a488035 | 2019 | |
f6722559 | 2020 | //have a crack @ deleting it first in the hope this will prevent derailing our tests |
5896d037 TO |
2021 | $this->callAPISuccess('custom_group', 'get', array( |
2022 | 'title' => $params['title'], | |
21dfd5f5 | 2023 | array('api.custom_group.delete' => 1), |
5896d037 | 2024 | )); |
6a488035 | 2025 | |
f6722559 | 2026 | return $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
2027 | } |
2028 | ||
2029 | /** | |
100fef9d | 2030 | * Existing function doesn't allow params to be over-ridden so need a new one |
6a488035 TO |
2031 | * this one allows you to only pass in the params you want to change |
2032 | */ | |
00be9182 | 2033 | public function CustomGroupCreateByParams($params = array()) { |
6a488035 TO |
2034 | $defaults = array( |
2035 | 'title' => "API Custom Group", | |
2036 | 'extends' => 'Contact', | |
2037 | 'domain_id' => 1, | |
2038 | 'style' => 'Inline', | |
2039 | 'is_active' => 1, | |
6a488035 TO |
2040 | ); |
2041 | $params = array_merge($defaults, $params); | |
f6722559 | 2042 | return $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
2043 | } |
2044 | ||
2045 | /** | |
2046 | * Create custom group with multi fields | |
2047 | */ | |
00be9182 | 2048 | public function CustomGroupMultipleCreateByParams($params = array()) { |
6a488035 TO |
2049 | $defaults = array( |
2050 | 'style' => 'Tab', | |
2051 | 'is_multiple' => 1, | |
2052 | ); | |
2053 | $params = array_merge($defaults, $params); | |
f6722559 | 2054 | return $this->CustomGroupCreateByParams($params); |
6a488035 TO |
2055 | } |
2056 | ||
2057 | /** | |
2058 | * Create custom group with multi fields | |
2059 | */ | |
00be9182 | 2060 | public function CustomGroupMultipleCreateWithFields($params = array()) { |
6a488035 TO |
2061 | // also need to pass on $params['custom_field'] if not set but not in place yet |
2062 | $ids = array(); | |
2063 | $customGroup = $this->CustomGroupMultipleCreateByParams($params); | |
2064 | $ids['custom_group_id'] = $customGroup['id']; | |
6a488035 | 2065 | |
5896d037 TO |
2066 | $customField = $this->customFieldCreate(array( |
2067 | 'custom_group_id' => $ids['custom_group_id'], | |
21dfd5f5 | 2068 | 'label' => 'field_1' . $ids['custom_group_id'], |
5896d037 | 2069 | )); |
6a488035 TO |
2070 | |
2071 | $ids['custom_field_id'][] = $customField['id']; | |
f6722559 | 2072 | |
5896d037 TO |
2073 | $customField = $this->customFieldCreate(array( |
2074 | 'custom_group_id' => $ids['custom_group_id'], | |
2075 | 'default_value' => '', | |
21dfd5f5 | 2076 | 'label' => 'field_2' . $ids['custom_group_id'], |
5896d037 | 2077 | )); |
6a488035 | 2078 | $ids['custom_field_id'][] = $customField['id']; |
f6722559 | 2079 | |
5896d037 TO |
2080 | $customField = $this->customFieldCreate(array( |
2081 | 'custom_group_id' => $ids['custom_group_id'], | |
2082 | 'default_value' => '', | |
21dfd5f5 | 2083 | 'label' => 'field_3' . $ids['custom_group_id'], |
5896d037 | 2084 | )); |
6a488035 | 2085 | $ids['custom_field_id'][] = $customField['id']; |
f6722559 | 2086 | |
6a488035 TO |
2087 | return $ids; |
2088 | } | |
2089 | ||
2090 | /** | |
2091 | * Create a custom group with a single text custom field. See | |
2092 | * participant:testCreateWithCustom for how to use this | |
2093 | * | |
e16033b4 TO |
2094 | * @param string $function |
2095 | * __FUNCTION__. | |
5a4f6742 CW |
2096 | * @param string $filename |
2097 | * $file __FILE__. | |
6a488035 TO |
2098 | * |
2099 | * @return array $ids ids of created objects | |
6a488035 | 2100 | */ |
00be9182 | 2101 | public function entityCustomGroupWithSingleFieldCreate($function, $filename) { |
f6722559 | 2102 | $params = array('title' => $function); |
6a488035 | 2103 | $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8); |
6c6e6187 | 2104 | $params['extends'] = $entity ? $entity : 'Contact'; |
f6722559 | 2105 | $customGroup = $this->CustomGroupCreate($params); |
b422b715 | 2106 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'], 'label' => $function)); |
80085473 | 2107 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
2108 | |
2109 | return array('custom_group_id' => $customGroup['id'], 'custom_field_id' => $customField['id']); | |
2110 | } | |
2111 | ||
2112 | /** | |
100fef9d | 2113 | * Delete custom group |
6a488035 | 2114 | * |
8deefe64 EM |
2115 | * @param int $customGroupID |
2116 | * | |
2117 | * @return array|int | |
6a488035 | 2118 | */ |
00be9182 | 2119 | public function customGroupDelete($customGroupID) { |
6a488035 | 2120 | $params['id'] = $customGroupID; |
f6722559 | 2121 | return $this->callAPISuccess('custom_group', 'delete', $params); |
6a488035 TO |
2122 | } |
2123 | ||
2124 | /** | |
100fef9d | 2125 | * Create custom field |
6a488035 | 2126 | * |
e16033b4 TO |
2127 | * @param array $params |
2128 | * (custom_group_id) is required. | |
2a6da8d7 | 2129 | * @return array|int |
6a488035 | 2130 | */ |
00be9182 | 2131 | public function customFieldCreate($params) { |
b422b715 | 2132 | $params = array_merge(array( |
2133 | 'label' => 'Custom Field', | |
6a488035 TO |
2134 | 'data_type' => 'String', |
2135 | 'html_type' => 'Text', | |
2136 | 'is_searchable' => 1, | |
2137 | 'is_active' => 1, | |
5c496c74 | 2138 | 'default_value' => 'defaultValue', |
b422b715 | 2139 | ), $params); |
6a488035 | 2140 | |
5c496c74 | 2141 | $result = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
2142 | |
2143 | if ($result['is_error'] == 0 && isset($result['id'])) { | |
2144 | CRM_Core_BAO_CustomField::getTableColumnGroup($result['id'], 1); | |
2145 | // force reset of enabled components to help grab custom fields | |
2146 | CRM_Core_Component::getEnabledComponents(1); | |
2147 | return $result; | |
2148 | } | |
6a488035 TO |
2149 | } |
2150 | ||
2151 | /** | |
100fef9d | 2152 | * Delete custom field |
6a488035 TO |
2153 | * |
2154 | * @param int $customFieldID | |
8deefe64 EM |
2155 | * |
2156 | * @return array|int | |
6a488035 | 2157 | */ |
00be9182 | 2158 | public function customFieldDelete($customFieldID) { |
6a488035 TO |
2159 | |
2160 | $params['id'] = $customFieldID; | |
f6722559 | 2161 | return $this->callAPISuccess('custom_field', 'delete', $params); |
6a488035 TO |
2162 | } |
2163 | ||
2164 | /** | |
100fef9d | 2165 | * Create note |
6a488035 | 2166 | * |
100fef9d | 2167 | * @param int $cId |
6a488035 TO |
2168 | * @return array $note |
2169 | */ | |
00be9182 | 2170 | public function noteCreate($cId) { |
6a488035 TO |
2171 | $params = array( |
2172 | 'entity_table' => 'civicrm_contact', | |
2173 | 'entity_id' => $cId, | |
2174 | 'note' => 'hello I am testing Note', | |
2175 | 'contact_id' => $cId, | |
2176 | 'modified_date' => date('Ymd'), | |
2177 | 'subject' => 'Test Note', | |
6a488035 TO |
2178 | ); |
2179 | ||
f6722559 | 2180 | return $this->callAPISuccess('Note', 'create', $params); |
6a488035 TO |
2181 | } |
2182 | ||
07fd63f5 E |
2183 | /** |
2184 | * Enable CiviCampaign Component | |
2185 | */ | |
00be9182 | 2186 | public function enableCiviCampaign() { |
07fd63f5 E |
2187 | CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); |
2188 | // force reload of config object | |
2189 | $config = CRM_Core_Config::singleton(TRUE, TRUE); | |
2190 | //flush cache by calling with reset | |
2191 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE); | |
2192 | } | |
2193 | ||
6a488035 TO |
2194 | /** |
2195 | * Create test generated example in api/v3/examples. | |
2196 | * To turn this off (e.g. on the server) set | |
2197 | * define(DONT_DOCUMENT_TEST_CONFIG ,1); | |
2198 | * in your settings file | |
e16033b4 TO |
2199 | * @param array $params |
2200 | * Array as passed to civicrm_api function. | |
2201 | * @param array $result | |
2202 | * Array as received from the civicrm_api function. | |
2203 | * @param string $function | |
2204 | * Calling function - generally __FUNCTION__. | |
2205 | * @param string $filename | |
2206 | * Called from file - generally __FILE__. | |
2207 | * @param string $description | |
2208 | * Descriptive text for the example file. | |
2209 | * @param string $subfile | |
2210 | * Name for subfile - if this is completed the example will be put in a subfolder (named by the entity). | |
2211 | * @param string $action | |
2212 | * Optional action - otherwise taken from function name. | |
6a488035 | 2213 | */ |
00be9182 | 2214 | public function documentMe($params, $result, $function, $filename, $description = "", $subfile = NULL, $action = NULL) { |
d2d04435 | 2215 | if (defined('DONT_DOCUMENT_TEST_CONFIG') && DONT_DOCUMENT_TEST_CONFIG) { |
6a488035 TO |
2216 | return; |
2217 | } | |
2218 | $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8); | |
2219 | //todo - this is a bit cludgey | |
2220 | if (empty($action)) { | |
2221 | if (strstr($function, 'Create')) { | |
2222 | $action = empty($action) ? 'create' : $action; | |
2223 | $entityAction = 'Create'; | |
2224 | } | |
2225 | elseif (strstr($function, 'GetSingle')) { | |
2226 | $action = empty($action) ? 'getsingle' : $action; | |
2227 | $entityAction = 'GetSingle'; | |
2228 | } | |
2229 | elseif (strstr($function, 'GetValue')) { | |
2230 | $action = empty($action) ? 'getvalue' : $action; | |
2231 | $entityAction = 'GetValue'; | |
2232 | } | |
2233 | elseif (strstr($function, 'GetCount')) { | |
2234 | $action = empty($action) ? 'getcount' : $action; | |
2235 | $entityAction = 'GetCount'; | |
2236 | } | |
5c496c74 | 2237 | elseif (strstr($function, 'GetFields')) { |
2238 | $action = empty($action) ? 'getfields' : $action; | |
2239 | $entityAction = 'GetFields'; | |
2240 | } | |
76ec9ca7 CW |
2241 | elseif (strstr($function, 'GetList')) { |
2242 | $action = empty($action) ? 'getlist' : $action; | |
2243 | $entityAction = 'GetList'; | |
2244 | } | |
92776611 CW |
2245 | elseif (strstr($function, 'GetActions')) { |
2246 | $action = empty($action) ? 'getactions' : $action; | |
2247 | $entityAction = 'GetActions'; | |
2248 | } | |
6a488035 TO |
2249 | elseif (strstr($function, 'Get')) { |
2250 | $action = empty($action) ? 'get' : $action; | |
2251 | $entityAction = 'Get'; | |
2252 | } | |
2253 | elseif (strstr($function, 'Delete')) { | |
2254 | $action = empty($action) ? 'delete' : $action; | |
2255 | $entityAction = 'Delete'; | |
2256 | } | |
2257 | elseif (strstr($function, 'Update')) { | |
2258 | $action = empty($action) ? 'update' : $action; | |
2259 | $entityAction = 'Update'; | |
2260 | } | |
2261 | elseif (strstr($function, 'Subscribe')) { | |
2262 | $action = empty($action) ? 'subscribe' : $action; | |
2263 | $entityAction = 'Subscribe'; | |
2264 | } | |
5c496c74 | 2265 | elseif (strstr($function, 'Submit')) { |
2266 | $action = empty($action) ? 'submit' : $action; | |
2267 | $entityAction = 'Submit'; | |
6a488035 TO |
2268 | } |
2269 | elseif (strstr($function, 'Apply')) { | |
2270 | $action = empty($action) ? 'apply' : $action; | |
2271 | $entityAction = 'Apply'; | |
2272 | } | |
2273 | elseif (strstr($function, 'Replace')) { | |
2274 | $action = empty($action) ? 'replace' : $action; | |
2275 | $entityAction = 'Replace'; | |
2276 | } | |
2277 | } | |
2278 | else { | |
2279 | $entityAction = ucwords($action); | |
2280 | } | |
2281 | ||
fdd48527 | 2282 | $this->tidyExampleResult($result); |
5896d037 | 2283 | if (isset($params['version'])) { |
fb32de45 | 2284 | unset($params['version']); |
2285 | } | |
6a488035 TO |
2286 | // a cleverer person than me would do it in a single regex |
2287 | if (strstr($entity, 'UF')) { | |
2288 | $fnPrefix = strtolower(preg_replace('/(?<! )(?<!^)(?<=UF)[A-Z]/', '_$0', $entity)); | |
2289 | } | |
2290 | else { | |
2291 | $fnPrefix = strtolower(preg_replace('/(?<! )(?<!^)[A-Z]/', '_$0', $entity)); | |
2292 | } | |
2293 | $smarty = CRM_Core_Smarty::singleton(); | |
2294 | $smarty->assign('testfunction', $function); | |
2295 | $function = $fnPrefix . "_" . strtolower($action); | |
2296 | $smarty->assign('function', $function); | |
2297 | $smarty->assign('fnPrefix', $fnPrefix); | |
2298 | $smarty->assign('params', $params); | |
2299 | $smarty->assign('entity', $entity); | |
2300 | $smarty->assign('filename', basename($filename)); | |
2301 | $smarty->assign('description', $description); | |
2302 | $smarty->assign('result', $result); | |
2303 | ||
2304 | $smarty->assign('action', $action); | |
2305 | if (empty($subfile)) { | |
3ec6e38d | 2306 | $subfile = $entityAction; |
6a488035 | 2307 | } |
3ec6e38d CW |
2308 | if (file_exists('../tests/templates/documentFunction.tpl')) { |
2309 | if (!is_dir("../api/v3/examples/$entity")) { | |
2310 | mkdir("../api/v3/examples/$entity"); | |
6a488035 | 2311 | } |
3ec6e38d CW |
2312 | $f = fopen("../api/v3/examples/$entity/$subfile.php", "w+b"); |
2313 | fwrite($f, $smarty->fetch('../tests/templates/documentFunction.tpl')); | |
2314 | fclose($f); | |
6a488035 TO |
2315 | } |
2316 | } | |
2317 | ||
fdd48527 | 2318 | /** |
2319 | * Tidy up examples array so that fields that change often ..don't | |
2320 | * and debug related fields are unset | |
2a6da8d7 | 2321 | * |
c490a46a | 2322 | * @param array $result |
2a6da8d7 | 2323 | * |
fdd48527 | 2324 | */ |
5896d037 TO |
2325 | public function tidyExampleResult(&$result) { |
2326 | if (!is_array($result)) { | |
fdd48527 | 2327 | return; |
2328 | } | |
2329 | $fieldsToChange = array( | |
2330 | 'hash' => '67eac7789eaee00', | |
2331 | 'modified_date' => '2012-11-14 16:02:35', | |
f27f2724 | 2332 | 'created_date' => '2013-07-28 08:49:19', |
fdd48527 | 2333 | 'create_date' => '20120130621222105', |
f27f2724 | 2334 | 'application_received_date' => '20130728084957', |
2335 | 'in_date' => '2013-07-28 08:50:19', | |
2336 | 'scheduled_date' => '20130728085413', | |
2337 | 'approval_date' => '20130728085413', | |
2338 | 'pledge_start_date_high' => '20130726090416', | |
9f1b81e0 | 2339 | 'start_date' => '2013-07-29 00:00:00', |
2340 | 'event_start_date' => '2013-07-29 00:00:00', | |
2341 | 'end_date' => '2013-08-04 00:00:00', | |
2342 | 'event_end_date' => '2013-08-04 00:00:00', | |
2343 | 'decision_date' => '20130805000000', | |
fdd48527 | 2344 | ); |
2345 | ||
6c6e6187 | 2346 | $keysToUnset = array('xdebug', 'undefined_fields'); |
fdd48527 | 2347 | foreach ($keysToUnset as $unwantedKey) { |
5896d037 | 2348 | if (isset($result[$unwantedKey])) { |
fdd48527 | 2349 | unset($result[$unwantedKey]); |
2350 | } | |
2351 | } | |
9f1b81e0 | 2352 | if (isset($result['values'])) { |
5896d037 | 2353 | if (!is_array($result['values'])) { |
9f1b81e0 | 2354 | return; |
2355 | } | |
2356 | $resultArray = &$result['values']; | |
2357 | } | |
5896d037 | 2358 | elseif (is_array($result)) { |
9f1b81e0 | 2359 | $resultArray = &$result; |
2360 | } | |
2361 | else { | |
2362 | return; | |
2363 | } | |
fdd48527 | 2364 | |
9f1b81e0 | 2365 | foreach ($resultArray as $index => &$values) { |
5896d037 | 2366 | if (!is_array($values)) { |
6c6e6187 TO |
2367 | continue; |
2368 | } | |
5896d037 TO |
2369 | foreach ($values as $key => &$value) { |
2370 | if (substr($key, 0, 3) == 'api' && is_array($value)) { | |
2371 | if (isset($value['is_error'])) { | |
6c6e6187 TO |
2372 | // we have a std nested result format |
2373 | $this->tidyExampleResult($value); | |
fdd48527 | 2374 | } |
5896d037 | 2375 | else { |
6c6e6187 TO |
2376 | foreach ($value as &$nestedResult) { |
2377 | // this is an alternative syntax for nested results a keyed array of results | |
2378 | $this->tidyExampleResult($nestedResult); | |
2379 | } | |
b25fe59a | 2380 | } |
fdd48527 | 2381 | } |
5896d037 | 2382 | if (in_array($key, $keysToUnset)) { |
6c6e6187 TO |
2383 | unset($values[$key]); |
2384 | break; | |
2385 | } | |
5896d037 | 2386 | if (array_key_exists($key, $fieldsToChange) && !empty($value)) { |
6c6e6187 TO |
2387 | $value = $fieldsToChange[$key]; |
2388 | } | |
5896d037 | 2389 | if (is_string($value)) { |
6c6e6187 TO |
2390 | $value = addslashes($value); |
2391 | } | |
2392 | } | |
fdd48527 | 2393 | } |
2394 | } | |
2395 | ||
6a488035 | 2396 | /** |
100fef9d | 2397 | * Delete note |
6a488035 | 2398 | * |
c490a46a | 2399 | * @param array $params |
6a488035 | 2400 | * |
c490a46a | 2401 | * @return array|int |
6a488035 | 2402 | */ |
00be9182 | 2403 | public function noteDelete($params) { |
f6722559 | 2404 | return $this->callAPISuccess('Note', 'delete', $params); |
6a488035 TO |
2405 | } |
2406 | ||
2407 | /** | |
100fef9d | 2408 | * Create custom field with Option Values |
6a488035 | 2409 | * |
77b97be7 | 2410 | * @param array $customGroup |
e16033b4 TO |
2411 | * @param string $name |
2412 | * Name of custom field. | |
77b97be7 EM |
2413 | * |
2414 | * @return array|int | |
6a488035 | 2415 | */ |
00be9182 | 2416 | public function customFieldOptionValueCreate($customGroup, $name) { |
6a488035 TO |
2417 | $fieldParams = array( |
2418 | 'custom_group_id' => $customGroup['id'], | |
2419 | 'name' => 'test_custom_group', | |
2420 | 'label' => 'Country', | |
2421 | 'html_type' => 'Select', | |
2422 | 'data_type' => 'String', | |
2423 | 'weight' => 4, | |
2424 | 'is_required' => 1, | |
2425 | 'is_searchable' => 0, | |
2426 | 'is_active' => 1, | |
6a488035 TO |
2427 | ); |
2428 | ||
2429 | $optionGroup = array( | |
2430 | 'domain_id' => 1, | |
2431 | 'name' => 'option_group1', | |
2432 | 'label' => 'option_group_label1', | |
2433 | ); | |
2434 | ||
2435 | $optionValue = array( | |
2436 | 'option_label' => array('Label1', 'Label2'), | |
2437 | 'option_value' => array('value1', 'value2'), | |
2438 | 'option_name' => array($name . '_1', $name . '_2'), | |
2439 | 'option_weight' => array(1, 2), | |
2440 | 'option_status' => 1, | |
2441 | ); | |
2442 | ||
2443 | $params = array_merge($fieldParams, $optionGroup, $optionValue); | |
2444 | ||
f6722559 | 2445 | return $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
2446 | } |
2447 | ||
4cbe18b8 EM |
2448 | /** |
2449 | * @param $entities | |
2450 | * | |
2451 | * @return bool | |
2452 | */ | |
00be9182 | 2453 | public function confirmEntitiesDeleted($entities) { |
6a488035 TO |
2454 | foreach ($entities as $entity) { |
2455 | ||
f6722559 | 2456 | $result = $this->callAPISuccess($entity, 'Get', array()); |
6a488035 TO |
2457 | if ($result['error'] == 1 || $result['count'] > 0) { |
2458 | // > than $entity[0] to allow a value to be passed in? e.g. domain? | |
2459 | return TRUE; | |
2460 | } | |
2461 | } | |
2462 | } | |
2463 | ||
4cbe18b8 EM |
2464 | /** |
2465 | * @param $tablesToTruncate | |
2466 | * @param bool $dropCustomValueTables | |
2467 | */ | |
00be9182 | 2468 | public function quickCleanup($tablesToTruncate, $dropCustomValueTables = FALSE) { |
d67f1f28 TO |
2469 | if ($this->tx) { |
2470 | throw new Exception("CiviUnitTestCase: quickCleanup() is not compatible with useTransaction()"); | |
2471 | } | |
6a488035 TO |
2472 | if ($dropCustomValueTables) { |
2473 | $tablesToTruncate[] = 'civicrm_custom_group'; | |
2474 | $tablesToTruncate[] = 'civicrm_custom_field'; | |
2475 | } | |
2476 | ||
0b6f58fa ARW |
2477 | $tablesToTruncate = array_unique(array_merge($this->_tablesToTruncate, $tablesToTruncate)); |
2478 | ||
6a488035 TO |
2479 | CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;"); |
2480 | foreach ($tablesToTruncate as $table) { | |
2481 | $sql = "TRUNCATE TABLE $table"; | |
2482 | CRM_Core_DAO::executeQuery($sql); | |
2483 | } | |
2484 | CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;"); | |
2485 | ||
2486 | if ($dropCustomValueTables) { | |
2487 | $dbName = self::getDBName(); | |
2488 | $query = " | |
2489 | SELECT TABLE_NAME as tableName | |
2490 | FROM INFORMATION_SCHEMA.TABLES | |
2491 | WHERE TABLE_SCHEMA = '{$dbName}' | |
2492 | AND ( TABLE_NAME LIKE 'civicrm_value_%' ) | |
2493 | "; | |
2494 | ||
2495 | $tableDAO = CRM_Core_DAO::executeQuery($query); | |
2496 | while ($tableDAO->fetch()) { | |
2497 | $sql = "DROP TABLE {$tableDAO->tableName}"; | |
2498 | CRM_Core_DAO::executeQuery($sql); | |
2499 | } | |
2500 | } | |
2501 | } | |
2502 | ||
b38530f2 EM |
2503 | /** |
2504 | * Clean up financial entities after financial tests (so we remember to get all the tables :-)) | |
2505 | */ | |
00be9182 | 2506 | public function quickCleanUpFinancialEntities() { |
b38530f2 EM |
2507 | $tablesToTruncate = array( |
2508 | 'civicrm_contribution', | |
39d632fd | 2509 | 'civicrm_contribution_soft', |
b38530f2 | 2510 | 'civicrm_financial_trxn', |
39d632fd | 2511 | 'civicrm_financial_item', |
b38530f2 EM |
2512 | 'civicrm_contribution_recur', |
2513 | 'civicrm_line_item', | |
2514 | 'civicrm_contribution_page', | |
2515 | 'civicrm_payment_processor', | |
2516 | 'civicrm_entity_financial_trxn', | |
2517 | 'civicrm_membership', | |
2518 | 'civicrm_membership_type', | |
2519 | 'civicrm_membership_payment', | |
cab024d4 | 2520 | 'civicrm_membership_log', |
f9342903 | 2521 | 'civicrm_membership_block', |
b38530f2 EM |
2522 | 'civicrm_event', |
2523 | 'civicrm_participant', | |
2524 | 'civicrm_participant_payment', | |
2525 | 'civicrm_pledge', | |
1cf3c2b1 | 2526 | 'civicrm_price_set_entity', |
108ff21a EM |
2527 | 'civicrm_price_field_value', |
2528 | 'civicrm_price_field', | |
b38530f2 EM |
2529 | ); |
2530 | $this->quickCleanup($tablesToTruncate); | |
4278b952 | 2531 | CRM_Core_DAO::executeQuery("DELETE FROM civicrm_membership_status WHERE name NOT IN('New', 'Current', 'Grace', 'Expired', 'Pending', 'Cancelled', 'Deceased')"); |
108ff21a | 2532 | $this->restoreDefaultPriceSetConfig(); |
6d8a45ed EM |
2533 | $var = TRUE; |
2534 | CRM_Member_BAO_Membership::createRelatedMemberships($var, $var, TRUE); | |
108ff21a EM |
2535 | } |
2536 | ||
00be9182 | 2537 | public function restoreDefaultPriceSetConfig() { |
108ff21a EM |
2538 | CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field` (`id`, `price_set_id`, `name`, `label`, `html_type`, `is_enter_qty`, `help_pre`, `help_post`, `weight`, `is_display_amounts`, `options_per_line`, `is_active`, `is_required`, `active_on`, `expire_on`, `javascript`, `visibility_id`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', 'Text', 0, NULL, NULL, 1, 1, 1, 1, 1, NULL, NULL, NULL, 1)"); |
2539 | CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field_value` (`id`, `price_field_id`, `name`, `label`, `description`, `amount`, `count`, `max_value`, `weight`, `membership_type_id`, `membership_num_terms`, `is_default`, `is_active`, `financial_type_id`, `deductible_amount`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', NULL, '1', NULL, NULL, 1, NULL, NULL, 0, 1, 1, 0.00)"); | |
b38530f2 | 2540 | } |
6a488035 TO |
2541 | /* |
2542 | * Function does a 'Get' on the entity & compares the fields in the Params with those returned | |
2543 | * Default behaviour is to also delete the entity | |
e16033b4 TO |
2544 | * @param array $params |
2545 | * Params array to check agains. | |
2546 | * @param int $id | |
2547 | * Id of the entity concerned. | |
2548 | * @param string $entity | |
2549 | * Name of entity concerned (e.g. membership). | |
2550 | * @param bool $delete | |
2551 | * Should the entity be deleted as part of this check. | |
2552 | * @param string $errorText | |
2553 | * Text to print on error. | |
6a488035 TO |
2554 | * |
2555 | */ | |
4cbe18b8 | 2556 | /** |
c490a46a | 2557 | * @param array $params |
100fef9d | 2558 | * @param int $id |
4cbe18b8 EM |
2559 | * @param $entity |
2560 | * @param int $delete | |
2561 | * @param string $errorText | |
2562 | * | |
2563 | * @throws Exception | |
2564 | */ | |
00be9182 | 2565 | public function getAndCheck($params, $id, $entity, $delete = 1, $errorText = '') { |
6a488035 | 2566 | |
f6722559 | 2567 | $result = $this->callAPISuccessGetSingle($entity, array( |
6a488035 | 2568 | 'id' => $id, |
6a488035 TO |
2569 | )); |
2570 | ||
2571 | if ($delete) { | |
f6722559 | 2572 | $this->callAPISuccess($entity, 'Delete', array( |
6a488035 | 2573 | 'id' => $id, |
6a488035 TO |
2574 | )); |
2575 | } | |
b0aaad8c | 2576 | $dateFields = $keys = $dateTimeFields = array(); |
f6722559 | 2577 | $fields = $this->callAPISuccess($entity, 'getfields', array('version' => 3, 'action' => 'get')); |
6a488035 TO |
2578 | foreach ($fields['values'] as $field => $settings) { |
2579 | if (array_key_exists($field, $result)) { | |
2580 | $keys[CRM_Utils_Array::Value('name', $settings, $field)] = $field; | |
2581 | } | |
2582 | else { | |
2583 | $keys[CRM_Utils_Array::Value('name', $settings, $field)] = CRM_Utils_Array::value('name', $settings, $field); | |
2584 | } | |
afd404ea | 2585 | $type = CRM_Utils_Array::value('type', $settings); |
2586 | if ($type == CRM_Utils_Type::T_DATE) { | |
2587 | $dateFields[] = $settings['name']; | |
2588 | // we should identify both real names & unique names as dates | |
5896d037 | 2589 | if ($field != $settings['name']) { |
afd404ea | 2590 | $dateFields[] = $field; |
2591 | } | |
2592 | } | |
5896d037 | 2593 | if ($type == CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME) { |
afd404ea | 2594 | $dateTimeFields[] = $settings['name']; |
2595 | // we should identify both real names & unique names as dates | |
5896d037 | 2596 | if ($field != $settings['name']) { |
afd404ea | 2597 | $dateTimeFields[] = $field; |
2598 | } | |
6a488035 TO |
2599 | } |
2600 | } | |
2601 | ||
2602 | if (strtolower($entity) == 'contribution') { | |
2603 | $params['receive_date'] = date('Y-m-d', strtotime($params['receive_date'])); | |
2604 | // this is not returned in id format | |
2605 | unset($params['payment_instrument_id']); | |
2606 | $params['contribution_source'] = $params['source']; | |
2607 | unset($params['source']); | |
2608 | } | |
2609 | ||
2610 | foreach ($params as $key => $value) { | |
afd404ea | 2611 | if ($key == 'version' || substr($key, 0, 3) == 'api' || !array_key_exists($keys[$key], $result)) { |
6a488035 TO |
2612 | continue; |
2613 | } | |
2614 | if (in_array($key, $dateFields)) { | |
2615 | $value = date('Y-m-d', strtotime($value)); | |
2616 | $result[$key] = date('Y-m-d', strtotime($result[$key])); | |
2617 | } | |
afd404ea | 2618 | if (in_array($key, $dateTimeFields)) { |
2619 | $value = date('Y-m-d H:i:s', strtotime($value)); | |
a72cec08 | 2620 | $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 | 2621 | } |
2622 | $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 |
2623 | } |
2624 | } | |
2625 | ||
2626 | /** | |
100fef9d | 2627 | * Get formatted values in the actual and expected result |
e16033b4 TO |
2628 | * @param array $actual |
2629 | * Actual calculated values. | |
2630 | * @param array $expected | |
2631 | * Expected values. | |
6a488035 TO |
2632 | * |
2633 | */ | |
00be9182 | 2634 | public function checkArrayEquals(&$actual, &$expected) { |
6a488035 TO |
2635 | self::unsetId($actual); |
2636 | self::unsetId($expected); | |
2637 | $this->assertEquals($actual, $expected); | |
2638 | } | |
2639 | ||
2640 | /** | |
100fef9d | 2641 | * Unset the key 'id' from the array |
e16033b4 TO |
2642 | * @param array $unformattedArray |
2643 | * The array from which the 'id' has to be unset. | |
6a488035 TO |
2644 | * |
2645 | */ | |
00be9182 | 2646 | public static function unsetId(&$unformattedArray) { |
6a488035 TO |
2647 | $formattedArray = array(); |
2648 | if (array_key_exists('id', $unformattedArray)) { | |
2649 | unset($unformattedArray['id']); | |
2650 | } | |
a7488080 | 2651 | if (!empty($unformattedArray['values']) && is_array($unformattedArray['values'])) { |
6a488035 | 2652 | foreach ($unformattedArray['values'] as $key => $value) { |
6c6e6187 | 2653 | if (is_array($value)) { |
6a488035 TO |
2654 | foreach ($value as $k => $v) { |
2655 | if ($k == 'id') { | |
2656 | unset($value[$k]); | |
2657 | } | |
2658 | } | |
2659 | } | |
2660 | elseif ($key == 'id') { | |
2661 | $unformattedArray[$key]; | |
2662 | } | |
2663 | $formattedArray = array($value); | |
2664 | } | |
2665 | $unformattedArray['values'] = $formattedArray; | |
2666 | } | |
2667 | } | |
2668 | ||
2669 | /** | |
2670 | * Helper to enable/disable custom directory support | |
2671 | * | |
e16033b4 TO |
2672 | * @param array $customDirs |
2673 | * With members:. | |
6a488035 TO |
2674 | * 'php_path' Set to TRUE to use the default, FALSE or "" to disable support, or a string path to use another path |
2675 | * 'template_path' Set to TRUE to use the default, FALSE or "" to disable support, or a string path to use another path | |
2676 | */ | |
00be9182 | 2677 | public function customDirectories($customDirs) { |
6a488035 TO |
2678 | require_once 'CRM/Core/Config.php'; |
2679 | $config = CRM_Core_Config::singleton(); | |
2680 | ||
2681 | if (empty($customDirs['php_path']) || $customDirs['php_path'] === FALSE) { | |
2682 | unset($config->customPHPPathDir); | |
2683 | } | |
2684 | elseif ($customDirs['php_path'] === TRUE) { | |
2685 | $config->customPHPPathDir = dirname(dirname(__FILE__)) . '/custom_directories/php/'; | |
2686 | } | |
2687 | else { | |
2688 | $config->customPHPPathDir = $php_path; | |
2689 | } | |
2690 | ||
2691 | if (empty($customDirs['template_path']) || $customDirs['template_path'] === FALSE) { | |
2692 | unset($config->customTemplateDir); | |
2693 | } | |
2694 | elseif ($customDirs['template_path'] === TRUE) { | |
2695 | $config->customTemplateDir = dirname(dirname(__FILE__)) . '/custom_directories/templates/'; | |
2696 | } | |
2697 | else { | |
2698 | $config->customTemplateDir = $template_path; | |
2699 | } | |
2700 | } | |
2701 | ||
2702 | /** | |
2703 | * Generate a temporary folder | |
2704 | * | |
2a6da8d7 EM |
2705 | * @param string $prefix |
2706 | * @return string $string | |
6a488035 | 2707 | */ |
00be9182 | 2708 | public function createTempDir($prefix = 'test-') { |
6a488035 TO |
2709 | $tempDir = CRM_Utils_File::tempdir($prefix); |
2710 | $this->tempDirs[] = $tempDir; | |
2711 | return $tempDir; | |
2712 | } | |
2713 | ||
00be9182 | 2714 | public function cleanTempDirs() { |
6a488035 TO |
2715 | if (!is_array($this->tempDirs)) { |
2716 | // fix test errors where this is not set | |
2717 | return; | |
2718 | } | |
2719 | foreach ($this->tempDirs as $tempDir) { | |
2720 | if (is_dir($tempDir)) { | |
2721 | CRM_Utils_File::cleanDir($tempDir, TRUE, FALSE); | |
2722 | } | |
2723 | } | |
2724 | } | |
2725 | ||
2726 | /** | |
2727 | * Temporarily replace the singleton extension with a different one | |
2728 | */ | |
00be9182 | 2729 | public function setExtensionSystem(CRM_Extension_System $system) { |
6a488035 TO |
2730 | if ($this->origExtensionSystem == NULL) { |
2731 | $this->origExtensionSystem = CRM_Extension_System::singleton(); | |
2732 | } | |
2733 | CRM_Extension_System::setSingleton($this->origExtensionSystem); | |
2734 | } | |
2735 | ||
00be9182 | 2736 | public function unsetExtensionSystem() { |
6a488035 TO |
2737 | if ($this->origExtensionSystem !== NULL) { |
2738 | CRM_Extension_System::setSingleton($this->origExtensionSystem); | |
2739 | $this->origExtensionSystem = NULL; | |
2740 | } | |
2741 | } | |
f17d75bb | 2742 | |
076d8c82 TO |
2743 | /** |
2744 | * Temporarily alter the settings-metadata to add a mock setting. | |
2745 | * | |
2746 | * WARNING: The setting metadata will disappear on the next cache-clear. | |
2747 | * | |
2748 | * @param $extras | |
2749 | * @return void | |
2750 | */ | |
00be9182 | 2751 | public function setMockSettingsMetaData($extras) { |
076d8c82 | 2752 | CRM_Core_BAO_Setting::$_cache = array(); |
6c6e6187 | 2753 | $this->callAPISuccess('system', 'flush', array()); |
076d8c82 TO |
2754 | CRM_Core_BAO_Setting::$_cache = array(); |
2755 | ||
5896d037 TO |
2756 | CRM_Utils_Hook::singleton() |
2757 | ->setHook('civicrm_alterSettingsMetaData', function (&$metadata, $domainId, $profile) use ($extras) { | |
2758 | $metadata = array_merge($metadata, $extras); | |
2759 | }); | |
076d8c82 TO |
2760 | |
2761 | $fields = $this->callAPISuccess('setting', 'getfields', array()); | |
2762 | foreach ($extras as $key => $spec) { | |
2763 | $this->assertNotEmpty($spec['title']); | |
2764 | $this->assertEquals($spec['title'], $fields['values'][$key]['title']); | |
2765 | } | |
2766 | } | |
2767 | ||
4cbe18b8 | 2768 | /** |
100fef9d | 2769 | * @param string $name |
4cbe18b8 | 2770 | */ |
00be9182 | 2771 | public function financialAccountDelete($name) { |
f17d75bb PN |
2772 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
2773 | $financialAccount->name = $name; | |
5896d037 | 2774 | if ($financialAccount->find(TRUE)) { |
f17d75bb PN |
2775 | $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount(); |
2776 | $entityFinancialType->financial_account_id = $financialAccount->id; | |
2777 | $entityFinancialType->delete(); | |
2778 | $financialAccount->delete(); | |
2779 | } | |
2780 | } | |
fb32de45 | 2781 | |
2b7d3f8a | 2782 | /** |
2783 | * FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together | |
2784 | * (NB unclear if this is still required) | |
2785 | */ | |
00be9182 | 2786 | public function _sethtmlGlobals() { |
2b7d3f8a | 2787 | $GLOBALS['_HTML_QuickForm_registered_rules'] = array( |
2788 | 'required' => array( | |
2789 | 'html_quickform_rule_required', | |
21dfd5f5 | 2790 | 'HTML/QuickForm/Rule/Required.php', |
2b7d3f8a | 2791 | ), |
2792 | 'maxlength' => array( | |
2793 | 'html_quickform_rule_range', | |
21dfd5f5 | 2794 | 'HTML/QuickForm/Rule/Range.php', |
2b7d3f8a | 2795 | ), |
2796 | 'minlength' => array( | |
2797 | 'html_quickform_rule_range', | |
21dfd5f5 | 2798 | 'HTML/QuickForm/Rule/Range.php', |
2b7d3f8a | 2799 | ), |
2800 | 'rangelength' => array( | |
2801 | 'html_quickform_rule_range', | |
21dfd5f5 | 2802 | 'HTML/QuickForm/Rule/Range.php', |
2b7d3f8a | 2803 | ), |
2804 | 'email' => array( | |
2805 | 'html_quickform_rule_email', | |
21dfd5f5 | 2806 | 'HTML/QuickForm/Rule/Email.php', |
2b7d3f8a | 2807 | ), |
2808 | 'regex' => array( | |
2809 | 'html_quickform_rule_regex', | |
21dfd5f5 | 2810 | 'HTML/QuickForm/Rule/Regex.php', |
2b7d3f8a | 2811 | ), |
2812 | 'lettersonly' => array( | |
2813 | 'html_quickform_rule_regex', | |
21dfd5f5 | 2814 | 'HTML/QuickForm/Rule/Regex.php', |
2b7d3f8a | 2815 | ), |
2816 | 'alphanumeric' => array( | |
2817 | 'html_quickform_rule_regex', | |
21dfd5f5 | 2818 | 'HTML/QuickForm/Rule/Regex.php', |
2b7d3f8a | 2819 | ), |
2820 | 'numeric' => array( | |
2821 | 'html_quickform_rule_regex', | |
21dfd5f5 | 2822 | 'HTML/QuickForm/Rule/Regex.php', |
2b7d3f8a | 2823 | ), |
2824 | 'nopunctuation' => array( | |
2825 | 'html_quickform_rule_regex', | |
21dfd5f5 | 2826 | 'HTML/QuickForm/Rule/Regex.php', |
2b7d3f8a | 2827 | ), |
2828 | 'nonzero' => array( | |
2829 | 'html_quickform_rule_regex', | |
21dfd5f5 | 2830 | 'HTML/QuickForm/Rule/Regex.php', |
2b7d3f8a | 2831 | ), |
2832 | 'callback' => array( | |
2833 | 'html_quickform_rule_callback', | |
21dfd5f5 | 2834 | 'HTML/QuickForm/Rule/Callback.php', |
2b7d3f8a | 2835 | ), |
2836 | 'compare' => array( | |
2837 | 'html_quickform_rule_compare', | |
21dfd5f5 TO |
2838 | 'HTML/QuickForm/Rule/Compare.php', |
2839 | ), | |
2b7d3f8a | 2840 | ); |
2841 | // FIXME: …ditto for $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] | |
2842 | $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = array( | |
2843 | 'group' => array( | |
2844 | 'HTML/QuickForm/group.php', | |
21dfd5f5 | 2845 | 'HTML_QuickForm_group', |
2b7d3f8a | 2846 | ), |
2847 | 'hidden' => array( | |
2848 | 'HTML/QuickForm/hidden.php', | |
21dfd5f5 | 2849 | 'HTML_QuickForm_hidden', |
2b7d3f8a | 2850 | ), |
2851 | 'reset' => array( | |
2852 | 'HTML/QuickForm/reset.php', | |
21dfd5f5 | 2853 | 'HTML_QuickForm_reset', |
2b7d3f8a | 2854 | ), |
2855 | 'checkbox' => array( | |
2856 | 'HTML/QuickForm/checkbox.php', | |
21dfd5f5 | 2857 | 'HTML_QuickForm_checkbox', |
2b7d3f8a | 2858 | ), |
2859 | 'file' => array( | |
2860 | 'HTML/QuickForm/file.php', | |
21dfd5f5 | 2861 | 'HTML_QuickForm_file', |
2b7d3f8a | 2862 | ), |
2863 | 'image' => array( | |
2864 | 'HTML/QuickForm/image.php', | |
21dfd5f5 | 2865 | 'HTML_QuickForm_image', |
2b7d3f8a | 2866 | ), |
2867 | 'password' => array( | |
2868 | 'HTML/QuickForm/password.php', | |
21dfd5f5 | 2869 | 'HTML_QuickForm_password', |
2b7d3f8a | 2870 | ), |
2871 | 'radio' => array( | |
2872 | 'HTML/QuickForm/radio.php', | |
21dfd5f5 | 2873 | 'HTML_QuickForm_radio', |
2b7d3f8a | 2874 | ), |
2875 | 'button' => array( | |
2876 | 'HTML/QuickForm/button.php', | |
21dfd5f5 | 2877 | 'HTML_QuickForm_button', |
2b7d3f8a | 2878 | ), |
2879 | 'submit' => array( | |
2880 | 'HTML/QuickForm/submit.php', | |
21dfd5f5 | 2881 | 'HTML_QuickForm_submit', |
2b7d3f8a | 2882 | ), |
2883 | 'select' => array( | |
2884 | 'HTML/QuickForm/select.php', | |
21dfd5f5 | 2885 | 'HTML_QuickForm_select', |
2b7d3f8a | 2886 | ), |
2887 | 'hiddenselect' => array( | |
2888 | 'HTML/QuickForm/hiddenselect.php', | |
21dfd5f5 | 2889 | 'HTML_QuickForm_hiddenselect', |
2b7d3f8a | 2890 | ), |
2891 | 'text' => array( | |
2892 | 'HTML/QuickForm/text.php', | |
21dfd5f5 | 2893 | 'HTML_QuickForm_text', |
2b7d3f8a | 2894 | ), |
2895 | 'textarea' => array( | |
2896 | 'HTML/QuickForm/textarea.php', | |
21dfd5f5 | 2897 | 'HTML_QuickForm_textarea', |
2b7d3f8a | 2898 | ), |
2899 | 'fckeditor' => array( | |
2900 | 'HTML/QuickForm/fckeditor.php', | |
21dfd5f5 | 2901 | 'HTML_QuickForm_FCKEditor', |
2b7d3f8a | 2902 | ), |
2903 | 'tinymce' => array( | |
2904 | 'HTML/QuickForm/tinymce.php', | |
21dfd5f5 | 2905 | 'HTML_QuickForm_TinyMCE', |
2b7d3f8a | 2906 | ), |
2907 | 'dojoeditor' => array( | |
2908 | 'HTML/QuickForm/dojoeditor.php', | |
21dfd5f5 | 2909 | 'HTML_QuickForm_dojoeditor', |
2b7d3f8a | 2910 | ), |
2911 | 'link' => array( | |
2912 | 'HTML/QuickForm/link.php', | |
21dfd5f5 | 2913 | 'HTML_QuickForm_link', |
2b7d3f8a | 2914 | ), |
2915 | 'advcheckbox' => array( | |
2916 | 'HTML/QuickForm/advcheckbox.php', | |
21dfd5f5 | 2917 | 'HTML_QuickForm_advcheckbox', |
2b7d3f8a | 2918 | ), |
2919 | 'date' => array( | |
2920 | 'HTML/QuickForm/date.php', | |
21dfd5f5 | 2921 | 'HTML_QuickForm_date', |
2b7d3f8a | 2922 | ), |
2923 | 'static' => array( | |
2924 | 'HTML/QuickForm/static.php', | |
21dfd5f5 | 2925 | 'HTML_QuickForm_static', |
2b7d3f8a | 2926 | ), |
2927 | 'header' => array( | |
2928 | 'HTML/QuickForm/header.php', | |
21dfd5f5 | 2929 | 'HTML_QuickForm_header', |
2b7d3f8a | 2930 | ), |
2931 | 'html' => array( | |
2932 | 'HTML/QuickForm/html.php', | |
21dfd5f5 | 2933 | 'HTML_QuickForm_html', |
2b7d3f8a | 2934 | ), |
2935 | 'hierselect' => array( | |
2936 | 'HTML/QuickForm/hierselect.php', | |
21dfd5f5 | 2937 | 'HTML_QuickForm_hierselect', |
2b7d3f8a | 2938 | ), |
2939 | 'autocomplete' => array( | |
2940 | 'HTML/QuickForm/autocomplete.php', | |
21dfd5f5 | 2941 | 'HTML_QuickForm_autocomplete', |
2b7d3f8a | 2942 | ), |
2943 | 'xbutton' => array( | |
2944 | 'HTML/QuickForm/xbutton.php', | |
21dfd5f5 | 2945 | 'HTML_QuickForm_xbutton', |
2b7d3f8a | 2946 | ), |
2947 | 'advmultiselect' => array( | |
2948 | 'HTML/QuickForm/advmultiselect.php', | |
21dfd5f5 TO |
2949 | 'HTML_QuickForm_advmultiselect', |
2950 | ), | |
2b7d3f8a | 2951 | ); |
2952 | } | |
2953 | ||
48e399ac EM |
2954 | /** |
2955 | * Set up an acl allowing contact to see 2 specified groups | |
c206647d | 2956 | * - $this->_permissionedGroup & $this->_permissionedDisabledGroup |
48e399ac | 2957 | * |
c206647d | 2958 | * You need to have pre-created these groups & created the user e.g |
48e399ac EM |
2959 | * $this->createLoggedInUser(); |
2960 | * $this->_permissionedDisabledGroup = $this->groupCreate(array('title' => 'pick-me-disabled', 'is_active' => 0, 'name' => 'pick-me-disabled')); | |
2961 | * $this->_permissionedGroup = $this->groupCreate(array('title' => 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active')); | |
2962 | * | |
2963 | */ | |
00be9182 | 2964 | public function setupACL() { |
aaac0e0b EM |
2965 | global $_REQUEST; |
2966 | $_REQUEST = $this->_params; | |
108ff21a | 2967 | |
48e399ac EM |
2968 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); |
2969 | $optionGroupID = $this->callAPISuccessGetValue('option_group', array('return' => 'id', 'name' => 'acl_role')); | |
6c6e6187 | 2970 | $optionValue = $this->callAPISuccess('option_value', 'create', array( |
5896d037 | 2971 | 'option_group_id' => $optionGroupID, |
48e399ac EM |
2972 | 'label' => 'pick me', |
2973 | 'value' => 55, | |
2974 | )); | |
2975 | ||
48e399ac EM |
2976 | CRM_Core_DAO::executeQuery(" |
2977 | TRUNCATE civicrm_acl_cache | |
2978 | "); | |
2979 | ||
2980 | CRM_Core_DAO::executeQuery(" | |
2981 | TRUNCATE civicrm_acl_contact_cache | |
2982 | "); | |
2983 | ||
48e399ac EM |
2984 | CRM_Core_DAO::executeQuery(" |
2985 | INSERT INTO civicrm_acl_entity_role ( | |
2986 | `acl_role_id`, `entity_table`, `entity_id` | |
2987 | ) VALUES (55, 'civicrm_group', {$this->_permissionedGroup}); | |
2988 | "); | |
2989 | ||
2990 | CRM_Core_DAO::executeQuery(" | |
2991 | INSERT INTO civicrm_acl ( | |
2992 | `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active` | |
2993 | ) | |
2994 | VALUES ( | |
2995 | 'view picked', 'civicrm_group', $this->_permissionedGroup , 'Edit', 'civicrm_saved_search', {$this->_permissionedGroup}, 1 | |
2996 | ); | |
2997 | "); | |
2998 | ||
2999 | CRM_Core_DAO::executeQuery(" | |
3000 | INSERT INTO civicrm_acl ( | |
3001 | `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active` | |
3002 | ) | |
3003 | VALUES ( | |
3004 | 'view picked', 'civicrm_group', $this->_permissionedGroup, 'Edit', 'civicrm_saved_search', {$this->_permissionedDisabledGroup}, 1 | |
3005 | ); | |
3006 | "); | |
3007 | $this->_loggedInUser = CRM_Core_Session::singleton()->get('userID'); | |
3008 | $this->callAPISuccess('group_contact', 'create', array( | |
3009 | 'group_id' => $this->_permissionedGroup, | |
3010 | 'contact_id' => $this->_loggedInUser, | |
3011 | )); | |
3012 | //flush cache | |
3013 | CRM_ACL_BAO_Cache::resetCache(); | |
aaac0e0b EM |
3014 | CRM_Contact_BAO_Group::getPermissionClause(TRUE); |
3015 | CRM_ACL_API::groupPermission('whatever', 9999, NULL, 'civicrm_saved_search', NULL, NULL, TRUE); | |
48e399ac EM |
3016 | } |
3017 | ||
cab024d4 | 3018 | /** |
100fef9d | 3019 | * Alter default price set so that the field numbers are not all 1 (hiding errors) |
cab024d4 | 3020 | */ |
00be9182 | 3021 | public function offsetDefaultPriceSet() { |
cab024d4 EM |
3022 | $contributionPriceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount')); |
3023 | $firstID = $contributionPriceSet['id']; | |
5896d037 TO |
3024 | $this->callAPISuccess('price_set', 'create', array( |
3025 | 'id' => $contributionPriceSet['id'], | |
3026 | 'is_active' => 0, | |
21dfd5f5 | 3027 | 'name' => 'old', |
5896d037 | 3028 | )); |
cab024d4 EM |
3029 | unset($contributionPriceSet['id']); |
3030 | $newPriceSet = $this->callAPISuccess('price_set', 'create', $contributionPriceSet); | |
5896d037 TO |
3031 | $priceField = $this->callAPISuccess('price_field', 'getsingle', array( |
3032 | 'price_set_id' => $firstID, | |
21dfd5f5 | 3033 | 'options' => array('limit' => 1), |
5896d037 | 3034 | )); |
cab024d4 EM |
3035 | unset($priceField['id']); |
3036 | $priceField['price_set_id'] = $newPriceSet['id']; | |
3037 | $newPriceField = $this->callAPISuccess('price_field', 'create', $priceField); | |
5896d037 TO |
3038 | $priceFieldValue = $this->callAPISuccess('price_field_value', 'getsingle', array( |
3039 | 'price_set_id' => $firstID, | |
3040 | 'sequential' => 1, | |
21dfd5f5 | 3041 | 'options' => array('limit' => 1), |
5896d037 | 3042 | )); |
cab024d4 EM |
3043 | |
3044 | unset($priceFieldValue['id']); | |
3045 | //create some padding to use up ids | |
3046 | $this->callAPISuccess('price_field_value', 'create', $priceFieldValue); | |
3047 | $this->callAPISuccess('price_field_value', 'create', $priceFieldValue); | |
3048 | $this->callAPISuccess('price_field_value', 'create', array_merge($priceFieldValue, array('price_field_id' => $newPriceField['id']))); | |
cab024d4 EM |
3049 | } |
3050 | ||
4aef704e | 3051 | /** |
3052 | * Create an instance of the paypal processor | |
3053 | * @todo this isn't a great place to put it - but really it belongs on a class that extends | |
3054 | * this parent class & we don't have a structure for that yet | |
3055 | * There is another function to this effect on the PaypalPro test but it appears to be silently failing | |
3056 | * & the best protection agains that is the functions this class affords | |
3057 | */ | |
00be9182 | 3058 | public function paymentProcessorCreate($params = array()) { |
4aef704e | 3059 | $params = array_merge(array( |
5896d037 TO |
3060 | 'name' => 'demo', |
3061 | 'domain_id' => CRM_Core_Config::domainID(), | |
3062 | 'payment_processor_type_id' => 'PayPal', | |
3063 | 'is_active' => 1, | |
3064 | 'is_default' => 0, | |
3065 | 'is_test' => 1, | |
3066 | 'user_name' => 'sunil._1183377782_biz_api1.webaccess.co.in', | |
3067 | 'password' => '1183377788', | |
3068 | 'signature' => 'APixCoQ-Zsaj-u3IH7mD5Do-7HUqA9loGnLSzsZga9Zr-aNmaJa3WGPH', | |
3069 | 'url_site' => 'https://www.sandbox.paypal.com/', | |
3070 | 'url_api' => 'https://api-3t.sandbox.paypal.com/', | |
3071 | 'url_button' => 'https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif', | |
3072 | 'class_name' => 'Payment_PayPalImpl', | |
3073 | 'billing_mode' => 3, | |
3074 | 'financial_type_id' => 1, | |
3075 | ), | |
3076 | $params); | |
3077 | if (!is_numeric($params['payment_processor_type_id'])) { | |
4aef704e | 3078 | // really the api should handle this through getoptions but it's not exactly api call so lets just sort it |
3079 | //here | |
3080 | $params['payment_processor_type_id'] = $this->callAPISuccess('payment_processor_type', 'getvalue', array( | |
3081 | 'name' => $params['payment_processor_type_id'], | |
3082 | 'return' => 'id', | |
3083 | ), 'integer'); | |
3084 | } | |
3085 | $result = $this->callAPISuccess('payment_processor', 'create', $params); | |
3086 | return $result['id']; | |
3087 | } | |
a9ac877b | 3088 | |
0dbefed3 EM |
3089 | /** |
3090 | * Set up initial recurring payment allowing subsequent IPN payments | |
3091 | */ | |
00be9182 | 3092 | public function setupRecurringPaymentProcessorTransaction() { |
0dbefed3 EM |
3093 | $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( |
3094 | 'contact_id' => $this->_contactID, | |
3095 | 'amount' => 1000, | |
3096 | 'sequential' => 1, | |
3097 | 'installments' => 5, | |
3098 | 'frequency_unit' => 'Month', | |
3099 | 'frequency_interval' => 1, | |
3100 | 'invoice_id' => $this->_invoiceID, | |
3101 | 'contribution_status_id' => 2, | |
3102 | 'processor_id' => $this->_paymentProcessorID, | |
3103 | 'api.contribution.create' => array( | |
3104 | 'total_amount' => '200', | |
3105 | 'invoice_id' => $this->_invoiceID, | |
3106 | 'financial_type_id' => 1, | |
3107 | 'contribution_status_id' => 'Pending', | |
3108 | 'contact_id' => $this->_contactID, | |
3109 | 'contribution_page_id' => $this->_contributionPageID, | |
3110 | 'payment_processor_id' => $this->_paymentProcessorID, | |
21dfd5f5 | 3111 | ), |
0dbefed3 EM |
3112 | )); |
3113 | $this->_contributionRecurID = $contributionRecur['id']; | |
3114 | $this->_contributionID = $contributionRecur['values']['0']['api.contribution.create']['id']; | |
3115 | } | |
a86d27fc | 3116 | |
a9ac877b | 3117 | /** |
100fef9d | 3118 | * We don't have a good way to set up a recurring contribution with a membership so let's just do one then alter it |
a9ac877b | 3119 | */ |
00be9182 | 3120 | public function setupMembershipRecurringPaymentProcessorTransaction() { |
a9ac877b | 3121 | $this->ids['membership_type'] = $this->membershipTypeCreate(); |
69140e67 EM |
3122 | //create a contribution so our membership & contribution don't both have id = 1 |
3123 | $this->contributionCreate($this->_contactID, 1, 'abcd', '345j'); | |
3124 | $this->setupRecurringPaymentProcessorTransaction(); | |
3125 | ||
a9ac877b EM |
3126 | $this->ids['membership'] = $this->callAPISuccess('membership', 'create', array( |
3127 | 'contact_id' => $this->_contactID, | |
3128 | 'membership_type_id' => $this->ids['membership_type'], | |
3129 | 'contribution_recur_id' => $this->_contributionRecurID, | |
a9ac877b EM |
3130 | 'format.only_id' => TRUE, |
3131 | )); | |
69140e67 EM |
3132 | //CRM-15055 creates line items we don't want so get rid of them so we can set up our own line items |
3133 | CRM_Core_DAO::executeQuery("TRUNCATE civicrm_line_item"); | |
3134 | ||
3135 | $this->callAPISuccess('line_item', 'create', array( | |
3136 | 'entity_table' => 'civicrm_membership', | |
3137 | 'entity_id' => $this->ids['membership'], | |
3138 | 'contribution_id' => $this->_contributionID, | |
3139 | 'label' => 'General', | |
3140 | 'qty' => 1, | |
3141 | 'unit_price' => 200, | |
3142 | 'line_total' => 200, | |
3143 | 'financial_type_id' => 1, | |
5896d037 TO |
3144 | 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', array( |
3145 | 'return' => 'id', | |
21dfd5f5 | 3146 | 'label' => 'Membership Amount', |
5896d037 TO |
3147 | )), |
3148 | 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', array( | |
3149 | 'return' => 'id', | |
21dfd5f5 | 3150 | 'label' => 'General', |
5896d037 | 3151 | )), |
69140e67 | 3152 | )); |
5896d037 TO |
3153 | $this->callAPISuccess('membership_payment', 'create', array( |
3154 | 'contribution_id' => $this->_contributionID, | |
21dfd5f5 | 3155 | 'membership_id' => $this->ids['membership'], |
5896d037 | 3156 | )); |
a9ac877b | 3157 | } |
6a488035 | 3158 | |
4cbe18b8 EM |
3159 | /** |
3160 | * @param $message | |
3161 | * | |
3162 | * @throws Exception | |
a9ac877b | 3163 | */ |
00be9182 | 3164 | public function CiviUnitTestCase_fatalErrorHandler($message) { |
a9ac877b EM |
3165 | throw new Exception("{$message['message']}: {$message['code']}"); |
3166 | } | |
4aef704e | 3167 | |
3168 | /** | |
3169 | * Helper function to create new mailing | |
3170 | * @return mixed | |
3171 | */ | |
00be9182 | 3172 | public function createMailing() { |
4aef704e | 3173 | $params = array( |
3174 | 'subject' => 'maild' . rand(), | |
3175 | 'body_text' => 'bdkfhdskfhduew', | |
3176 | 'name' => 'mailing name' . rand(), | |
3177 | 'created_id' => 1, | |
768c558c | 3178 | 'api.mailing_job.create' => 0, |
4aef704e | 3179 | ); |
3180 | ||
3181 | $result = $this->callAPISuccess('Mailing', 'create', $params); | |
3182 | return $result['id']; | |
3183 | } | |
3184 | ||
3185 | /** | |
3186 | * Helper function to delete mailing | |
3187 | * @param $id | |
3188 | */ | |
00be9182 | 3189 | public function deleteMailing($id) { |
4aef704e | 3190 | $params = array( |
3191 | 'id' => $id, | |
3192 | ); | |
3193 | ||
3194 | $this->callAPISuccess('Mailing', 'delete', $params); | |
3195 | } | |
d67f1f28 TO |
3196 | |
3197 | /** | |
3198 | * Wrap the entire test case in a transaction | |
3199 | * | |
3200 | * Only subsequent DB statements will be wrapped in TX -- this cannot | |
3201 | * retroactively wrap old DB statements. Therefore, it makes sense to | |
3202 | * call this at the beginning of setUp(). | |
3203 | * | |
3204 | * Note: Recall that TRUNCATE and ALTER will force-commit transactions, so | |
3205 | * this option does not work with, e.g., custom-data. | |
3206 | * | |
3207 | * WISHLIST: Monitor SQL queries in unit-tests and generate an exception | |
3208 | * if TRUNCATE or ALTER is called while using a transaction. | |
3209 | * | |
e16033b4 TO |
3210 | * @param bool $nest |
3211 | * Whether to use nesting or reference-counting. | |
d67f1f28 | 3212 | */ |
00be9182 | 3213 | public function useTransaction($nest = TRUE) { |
d67f1f28 TO |
3214 | if (!$this->tx) { |
3215 | $this->tx = new CRM_Core_Transaction($nest); | |
3216 | $this->tx->rollback(); | |
3217 | } | |
3218 | } | |
a335f6b2 | 3219 | |
00be9182 | 3220 | public function clearOutputBuffer() { |
73252974 PH |
3221 | while (ob_get_level() > 0) { |
3222 | ob_end_clean(); | |
3223 | } | |
3224 | } | |
a86d27fc | 3225 | } |