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