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