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