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