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