3 * File for the CiviUnitTestCase class
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
12 * This file is part of CiviCRM
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.
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.
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/>.
29 use Civi\Payment\System
;
30 use League\Csv\Reader
;
33 * Include class definitions
35 require_once 'api/api.php';
36 define('API_LATEST_VERSION', 3);
39 * Base class for CiviCRM unit tests
41 * This class supports two (mutually-exclusive) techniques for cleaning up test data. Subclasses
42 * may opt for one or neither:
44 * 1. quickCleanup() is a helper which truncates a series of tables. Call quickCleanup()
45 * as part of setUp() and/or tearDown(). quickCleanup() is thorough - but it can
46 * be cumbersome to use (b/c you must identify the tables to cleanup) and slow to execute.
47 * 2. useTransaction() executes the test inside a transaction. It's easier to use
48 * (because you don't need to identify specific tables), but it doesn't work for tests
49 * which manipulate schema or truncate data -- and could behave inconsistently
50 * for tests which specifically examine DB transactions.
52 * Common functions for unit tests
56 class CiviUnitTestCase
extends PHPUnit\Framework\TestCase
{
58 use \Civi\Test\Api3DocTrait
;
59 use \Civi\Test\GenericAssertionsTrait
;
60 use \Civi\Test\DbTestTrait
;
61 use \Civi\Test\ContactTestTrait
;
62 use \Civi\Test\MailingTestTrait
;
65 * Database has been initialized.
69 private static $dbInit = FALSE;
72 * Database connection.
74 * @var PHPUnit_Extensions_Database_DB_IDatabaseConnection
83 static protected $_dbName;
86 * Track tables we have modified during a test.
90 protected $_tablesToTruncate = [];
94 * Array of temporary directory names
100 * populateOnce allows to skip db resets in setUp
102 * WARNING! USE WITH CAUTION - IT'LL RENDER DATA DEPENDENCIES
103 * BETWEEN TESTS WHEN RUN IN SUITE. SUITABLE FOR LOCAL, LIMITED
106 * IF POSSIBLE, USE $this->DBResetRequired = FALSE IN YOUR TEST CASE!
108 * @see http://forum.civicrm.org/index.php/topic,18065.0.html
110 public static $populateOnce = FALSE;
113 * DBResetRequired allows skipping DB reset
114 * in specific test case. If you still need
115 * to reset single test (method) of such case, call
116 * $this->cleanDB() in the first line of this
120 public $DBResetRequired = TRUE;
123 * @var CRM_Core_Transaction|null
128 * Array of IDs created to support the test.
131 * $this->ids = ['Contact' => ['descriptive_key' => $contactID], 'Group' => [$groupID]];
138 * Should financials be checked after the test but before tear down.
140 * Ideally all tests (or at least all that call any financial api calls ) should do this but there
141 * are some test data issues and some real bugs currently blockinng.
145 protected $isValidateFinancialsOnPostAssert = FALSE;
148 * Should location types be checked to ensure primary addresses are correctly assigned after each test.
152 protected $isLocationTypesOnPostAssert = TRUE;
155 * Class used for hooks during tests.
157 * This can be used to test hooks within tests. For example in the ACL_PermissionTrait:
159 * $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereHookAllResults'));
161 * @var \CRM_Utils_Hook_UnitTests
163 public $hookClass = NULL;
167 * Common values to be re-used multiple times within a class - usually to create the relevant entity
169 protected $_params = [];
172 * @var CRM_Extension_System
174 protected $origExtensionSystem;
177 * Array of IDs created during test setup routine.
179 * The cleanUpSetUpIds method can be used to clear these at the end of the test.
183 public $setupIDs = [];
188 * Because we are overriding the parent class constructor, we
189 * need to show the same arguments as exist in the constructor of
190 * PHPUnit_Framework_TestCase, since
191 * PHPUnit_Framework_TestSuite::createTest() creates a
192 * ReflectionClass of the Test class and checks the constructor
193 * of that class to decide how to set up the test.
195 * @param string $name
197 * @param string $dataName
199 public function __construct($name = NULL, array $data = [], $dataName = '') {
200 parent
::__construct($name, $data, $dataName);
202 // we need full error reporting
203 error_reporting(E_ALL
& ~E_NOTICE
);
205 self
::$_dbName = self
::getDBName();
207 // also load the class loader
208 require_once 'CRM/Core/ClassLoader.php';
209 CRM_Core_ClassLoader
::singleton()->register();
210 if (function_exists('_civix_phpunit_setUp')) {
211 // FIXME: loosen coupling
212 _civix_phpunit_setUp();
217 * Override to run the test and assert its state.
221 * @throws \PHPUnit_Framework_IncompleteTest
222 * @throws \PHPUnit_Framework_SkippedTest
224 protected function runTest() {
226 return parent
::runTest();
228 catch (PEAR_Exception
$e) {
229 // PEAR_Exception has metadata in funny places, and PHPUnit won't log it nicely
230 throw new Exception(\CRM_Core_Error
::formatTextException($e), $e->getCode());
237 public function requireDBReset() {
238 return $this->DBResetRequired
;
244 public static function getDBName() {
245 static $dbName = NULL;
246 if ($dbName === NULL) {
247 require_once "DB.php";
248 $dsn = CRM_Utils_SQL
::autoSwitchDSN(CIVICRM_DSN
);
249 $dsninfo = DB
::parseDSN($dsn);
250 $dbName = $dsninfo['database'];
256 * Create database connection for this instance.
258 * Initialize the test database if it hasn't been initialized
261 protected function getConnection() {
262 if (!self
::$dbInit) {
263 $dbName = self
::getDBName();
265 // install test database
266 echo PHP_EOL
. "Installing {$dbName} database" . PHP_EOL
;
268 static::_populateDB(FALSE, $this);
270 self
::$dbInit = TRUE;
276 * Required implementation of abstract method.
278 protected function getDataSet() {
282 * @param bool $perClass
283 * @param null $object
286 * TRUE if the populate logic runs; FALSE if it is skipped
288 protected static function _populateDB($perClass = FALSE, &$object = NULL) {
289 if (CIVICRM_UF
!== 'UnitTests') {
290 throw new \
RuntimeException("_populateDB requires CIVICRM_UF=UnitTests");
293 if ($perClass ||
$object == NULL) {
297 $dbreset = $object->requireDBReset();
300 if (self
::$populateOnce ||
!$dbreset) {
303 self
::$populateOnce = NULL;
305 Civi\Test
::data()->populate();
310 public static function setUpBeforeClass() {
311 static::_populateDB(TRUE);
313 // also set this global hack
314 $GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = [];
318 * Common setup functions for all unit tests.
320 protected function setUp() {
321 $session = CRM_Core_Session
::singleton();
322 $session->set('userID', NULL);
324 $this->_apiversion
= 3;
326 // Use a temporary file for STDIN
327 $GLOBALS['stdin'] = tmpfile();
328 if ($GLOBALS['stdin'] === FALSE) {
329 echo "Couldn't open temporary file\n";
333 // Get and save a connection to the database
334 $this->_dbconn
= $this->getConnection();
336 // reload database before each test
337 // $this->_populateDB();
339 // "initialize" CiviCRM to avoid problems when running single tests
340 // FIXME: look at it closer in second stage
342 $GLOBALS['civicrm_setting']['domain']['fatalErrorHandler'] = 'CiviUnitTestCase_fatalErrorHandler';
343 $GLOBALS['civicrm_setting']['domain']['backtrace'] = 1;
345 // disable any left-over test extensions
346 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_extension WHERE full_name LIKE "test.%"');
348 // reset all the caches
349 CRM_Utils_System
::flushCache();
351 // initialize the object once db is loaded
352 \Civi
::$statics = [];
354 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
356 // when running unit tests, use mockup user framework
357 $this->hookClass
= CRM_Utils_Hook
::singleton();
359 // Make sure the DB connection is setup properly
360 $config->userSystem
->setMySQLTimeZone();
361 $env = new CRM_Utils_Check_Component_Env();
362 CRM_Utils_Check
::singleton()->assertValid($env->checkMysqlTime());
364 // clear permissions stub to not check permissions
365 $config->userPermissionClass
->permissions
= NULL;
367 //flush component settings
368 CRM_Core_Component
::getEnabledComponents(TRUE);
370 $_REQUEST = $_GET = $_POST = [];
371 error_reporting(E_ALL
);
373 $this->renameLabels();
374 $this->_sethtmlGlobals();
378 * Read everything from the datasets directory and insert into the db.
380 public function loadAllFixtures() {
381 $fixturesDir = __DIR__
. '/../../fixtures';
383 CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
385 $jsonFiles = glob($fixturesDir . '/*.json');
386 foreach ($jsonFiles as $jsonFixture) {
387 $json = json_decode(file_get_contents($jsonFixture));
388 foreach ($json as $tableName => $vars) {
389 if ($tableName === 'civicrm_contact') {
390 CRM_Core_DAO
::executeQuery('DELETE c FROM civicrm_contact c LEFT JOIN civicrm_domain d ON d.contact_id = c.id WHERE d.id IS NULL');
393 CRM_Core_DAO
::executeQuery("TRUNCATE $tableName");
395 foreach ($vars as $entity) {
396 $keys = $values = [];
397 foreach ($entity as $key => $value) {
399 $values[] = is_numeric($value) ?
$value : "'{$value}'";
401 CRM_Core_DAO
::executeQuery("
402 INSERT INTO $tableName (" . implode(',', $keys) . ') VALUES(' . implode(',', $values) . ')'
409 CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
413 * Load the data that used to be handled by the discontinued dbunit class.
415 * This could do with further tidy up - the initial priority is simply to get rid of
416 * the dbunity package which is no longer supported.
418 * @param string $fileName
420 protected function loadXMLDataSet($fileName) {
421 CRM_Core_DAO
::executeQuery('SET FOREIGN_KEY_CHECKS = 0');
422 $xml = json_decode(json_encode(simplexml_load_file($fileName)), TRUE);
423 foreach ($xml as $tableName => $element) {
424 if (!empty($element)) {
425 foreach ($element as $row) {
426 $keys = $values = [];
427 if (isset($row['@attributes'])) {
428 foreach ($row['@attributes'] as $key => $value) {
430 $values[] = is_numeric($value) ?
$value : "'{$value}'";
433 elseif (!empty($row)) {
434 // cos we copied it & it is inconsistent....
435 foreach ($row as $key => $value) {
437 $values[] = is_numeric($value) ?
$value : "'{$value}'";
441 if (!empty($values)) {
442 CRM_Core_DAO
::executeQuery("
443 INSERT INTO $tableName (" . implode(',', $keys) . ') VALUES(' . implode(',', $values) . ')'
449 CRM_Core_DAO
::executeQuery('SET FOREIGN_KEY_CHECKS = 1');
453 * Create default domain contacts for the two domains added during test class.
454 * database population.
456 public function createDomainContacts() {
457 $this->organizationCreate();
458 $this->organizationCreate(['organization_name' => 'Second Domain']);
462 * Common teardown functions for all unit tests.
464 protected function tearDown() {
465 $this->_apiversion
= 3;
466 $this->resetLabels();
468 error_reporting(E_ALL
& ~E_NOTICE
);
469 CRM_Utils_Hook
::singleton()->reset();
470 if ($this->hookClass
) {
471 $this->hookClass
->reset();
473 CRM_Core_Session
::singleton()->reset(1);
476 $this->tx
->rollback()->commit();
479 CRM_Core_Transaction
::forceRollbackIfEnabled();
480 \Civi\Core\Transaction\Manager
::singleton(TRUE);
483 CRM_Core_Transaction
::forceRollbackIfEnabled();
484 \Civi\Core\Transaction\Manager
::singleton(TRUE);
486 $tablesToTruncate = ['civicrm_contact', 'civicrm_uf_match'];
487 $this->quickCleanup($tablesToTruncate);
488 $this->createDomainContacts();
491 $this->cleanTempDirs();
492 $this->unsetExtensionSystem();
493 $this->assertEquals([], CRM_Core_DAO
::$_nullArray);
494 $this->assertEquals(NULL, CRM_Core_DAO
::$_nullObject);
498 * CHeck that all tests that have created payments have created them with the right financial entities.
500 * @throws \CRM_Core_Exception
502 protected function assertPostConditions() {
503 if ($this->isLocationTypesOnPostAssert
) {
504 $this->assertLocationValidity();
506 if (!$this->isValidateFinancialsOnPostAssert
) {
509 $this->validateAllPayments();
510 $this->validateAllContributions();
514 * Create a batch of external API calls which can
515 * be executed concurrently.
518 * $calls = $this->createExternalAPI()
519 * ->addCall('Contact', 'get', ...)
520 * ->addCall('Contact', 'get', ...)
526 * @return \Civi\API\ExternalBatch
527 * @throws PHPUnit_Framework_SkippedTestError
529 public function createExternalAPI() {
530 global $civicrm_root;
532 'version' => $this->_apiversion
,
536 $calls = new \Civi\API\
ExternalBatch($defaultParams);
538 if (!$calls->isSupported()) {
539 $this->markTestSkipped('The test relies on Civi\API\ExternalBatch. This is unsupported in the local environment.');
546 * Create required data based on $this->entity & $this->params
547 * This is just a way to set up the test data for delete & get functions
548 * so the distinction between set
549 * up & tested functions is clearer
554 public function createTestEntity() {
555 return $entity = $this->callAPISuccess($this->entity
, 'create', $this->params
);
559 * @param int $contactTypeId
563 public function contactTypeDelete($contactTypeId) {
564 $result = CRM_Contact_BAO_ContactType
::del($contactTypeId);
566 throw new Exception('Could not delete contact type');
571 * @param array $params
575 public function membershipTypeCreate($params = []) {
576 CRM_Member_PseudoConstant
::flush('membershipType');
577 CRM_Core_Config
::clearDBCache();
578 $this->setupIDs
['contact'] = $memberOfOrganization = $this->organizationCreate();
579 $params = array_merge([
581 'duration_unit' => 'year',
582 'duration_interval' => 1,
583 'period_type' => 'rolling',
584 'member_of_contact_id' => $memberOfOrganization,
586 'financial_type_id' => 2,
589 'visibility' => 'Public',
592 $result = $this->callAPISuccess('MembershipType', 'Create', $params);
594 CRM_Member_PseudoConstant
::flush('membershipType');
595 CRM_Utils_Cache
::singleton()->flush();
597 return $result['id'];
603 * @param array $params
606 * @throws \CRM_Core_Exception
608 public function contactMembershipCreate($params) {
609 $params = array_merge([
610 'join_date' => '2007-01-21',
611 'start_date' => '2007-01-21',
612 'end_date' => '2007-12-21',
613 'source' => 'Payment',
614 'membership_type_id' => 'General',
616 if (!is_numeric($params['membership_type_id'])) {
617 $membershipTypes = $this->callAPISuccess('Membership', 'getoptions', ['action' => 'create', 'field' => 'membership_type_id']);
618 if (!in_array($params['membership_type_id'], $membershipTypes['values'], TRUE)) {
619 $this->membershipTypeCreate(['name' => $params['membership_type_id']]);
623 $result = $this->callAPISuccess('Membership', 'create', $params);
624 return $result['id'];
628 * Delete Membership Type.
630 * @param array $params
632 public function membershipTypeDelete($params) {
633 $this->callAPISuccess('MembershipType', 'Delete', $params);
637 * @param int $membershipID
639 public function membershipDelete($membershipID) {
640 $deleteParams = ['id' => $membershipID];
641 $result = $this->callAPISuccess('Membership', 'Delete', $deleteParams);
645 * @param string $name
649 * @throws \CRM_Core_Exception
651 public function membershipStatusCreate($name = 'test member status') {
652 $params['name'] = $name;
653 $params['start_event'] = 'start_date';
654 $params['end_event'] = 'end_date';
655 $params['is_current_member'] = 1;
656 $params['is_active'] = 1;
658 $result = $this->callAPISuccess('MembershipStatus', 'Create', $params);
659 CRM_Member_PseudoConstant
::flush('membershipStatus');
660 return (int) $result['id'];
664 * Delete the given membership status, deleting any memberships of the status first.
666 * @param int $membershipStatusID
668 * @throws \CRM_Core_Exception
670 public function membershipStatusDelete(int $membershipStatusID) {
671 $this->callAPISuccess('Membership', 'get', ['status_id' => $membershipStatusID, 'api.Membership.delete' => 1]);
672 $this->callAPISuccess('MembershipStatus', 'Delete', ['id' => $membershipStatusID]);
675 public function membershipRenewalDate($durationUnit, $membershipEndDate) {
676 // We only have an end_date if frequency units match, otherwise membership won't be autorenewed and dates won't be calculated.
677 $renewedMembershipEndDate = new DateTime($membershipEndDate);
678 switch ($durationUnit) {
680 $renewedMembershipEndDate->add(new DateInterval('P1Y'));
684 // We have to add 1 day first in case it's the end of the month, then subtract afterwards
685 // eg. 2018-02-28 should renew to 2018-03-31, if we just added 1 month we'd get 2018-03-28
686 $renewedMembershipEndDate->add(new DateInterval('P1D'));
687 $renewedMembershipEndDate->add(new DateInterval('P1M'));
688 $renewedMembershipEndDate->sub(new DateInterval('P1D'));
691 return $renewedMembershipEndDate->format('Y-m-d');
695 * Create a relationship type.
697 * @param array $params
701 * @throws \CRM_Core_Exception
703 public function relationshipTypeCreate($params = []) {
704 $params = array_merge([
705 'name_a_b' => 'Relation 1 for relationship type create',
706 'name_b_a' => 'Relation 2 for relationship type create',
707 'contact_type_a' => 'Individual',
708 'contact_type_b' => 'Organization',
713 $result = $this->callAPISuccess('relationship_type', 'create', $params);
714 CRM_Core_PseudoConstant
::flush('relationshipType');
716 return $result['id'];
720 * Delete Relatinship Type.
722 * @param int $relationshipTypeID
724 public function relationshipTypeDelete($relationshipTypeID) {
725 $params['id'] = $relationshipTypeID;
726 $check = $this->callAPISuccess('relationship_type', 'get', $params);
727 if (!empty($check['count'])) {
728 $this->callAPISuccess('relationship_type', 'delete', $params);
733 * @param array $params
736 * @throws \CRM_Core_Exception
738 public function paymentProcessorTypeCreate($params = []) {
739 $params = array_merge([
740 'name' => 'API_Test_PP',
741 'title' => 'API Test Payment Processor',
742 'class_name' => 'CRM_Core_Payment_APITest',
743 'billing_mode' => 'form',
748 $result = $this->callAPISuccess('PaymentProcessorType', 'create', $params);
750 CRM_Core_PseudoConstant
::flush('paymentProcessorType');
752 return $result['id'];
756 * Create test Authorize.net instance.
758 * @param array $params
761 * @throws \CRM_Core_Exception
763 public function paymentProcessorAuthorizeNetCreate($params = []) {
764 $params = array_merge([
765 'name' => 'Authorize',
766 'domain_id' => CRM_Core_Config
::domainID(),
767 'payment_processor_type_id' => 'AuthNet',
768 'title' => 'AuthNet',
773 'user_name' => '4y5BfuW7jm',
774 'password' => '4cAmW927n8uLf5J8',
775 'url_site' => 'https://test.authorize.net/gateway/transact.dll',
776 'url_recur' => 'https://apitest.authorize.net/xml/v1/request.api',
777 'class_name' => 'Payment_AuthorizeNet',
781 $result = $this->callAPISuccess('PaymentProcessor', 'create', $params);
782 return (int) $result['id'];
786 * Create Participant.
788 * @param array $params
789 * Array of contact id and event id values.
792 * $id of participant created
794 public function participantCreate($params = []) {
795 if (empty($params['contact_id'])) {
796 $params['contact_id'] = $this->individualCreate();
798 if (empty($params['event_id'])) {
799 $event = $this->eventCreate();
800 $params['event_id'] = $event['id'];
805 'register_date' => 20070219,
806 'source' => 'Wimbeldon',
807 'event_level' => 'Payment',
811 $params = array_merge($defaults, $params);
812 $result = $this->callAPISuccess('Participant', 'create', $params);
813 return $result['id'];
817 * Create Payment Processor.
820 * Id Payment Processor
822 public function processorCreate($params = []) {
826 'payment_processor_type_id' => 'Dummy',
827 'financial_account_id' => 12,
831 'url_site' => 'http://dummy.com',
832 'url_recur' => 'http://dummy.com',
835 'payment_instrument_id' => 'Debit Card',
837 $processorParams = array_merge($processorParams, $params);
838 $processor = $this->callAPISuccess('PaymentProcessor', 'create', $processorParams);
839 return $processor['id'];
843 * Create Payment Processor.
845 * @param array $processorParams
847 * @return \CRM_Core_Payment_Dummy
848 * Instance of Dummy Payment Processor
850 * @throws \CiviCRM_API3_Exception
852 public function dummyProcessorCreate($processorParams = []) {
853 $paymentProcessorID = $this->processorCreate($processorParams);
854 // For the tests we don't need a live processor, but as core ALWAYS creates a processor in live mode and one in test mode we do need to create both
855 // Otherwise we are testing a scenario that only exists in tests (and some tests fail because the live processor has not been defined).
856 $processorParams['is_test'] = FALSE;
857 $this->processorCreate($processorParams);
858 return System
::singleton()->getById($paymentProcessorID);
862 * Create contribution page.
864 * @param array $params
867 * Array of contribution page
869 public function contributionPageCreate($params = []) {
870 $this->_pageParams
= array_merge([
871 'title' => 'Test Contribution Page',
872 'financial_type_id' => 1,
874 'financial_account_id' => 1,
876 'is_allow_other_amount' => 1,
878 'max_amount' => 1000,
880 return $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
884 * Create a sample batch.
886 public function batchCreate() {
887 $params = $this->_params
;
888 $params['name'] = $params['title'] = 'Batch_433397';
889 $params['status_id'] = 1;
890 $result = $this->callAPISuccess('batch', 'create', $params);
891 return $result['id'];
897 * @param array $params
900 * result of created tag
902 public function tagCreate($params = []) {
904 'name' => 'New Tag3',
905 'description' => 'This is description for Our New Tag ',
908 $params = array_merge($defaults, $params);
909 $result = $this->callAPISuccess('Tag', 'create', $params);
910 return $result['values'][$result['id']];
917 * Id of the tag to be deleted.
921 public function tagDelete($tagId) {
922 require_once 'api/api.php';
926 $result = $this->callAPISuccess('Tag', 'delete', $params);
927 return $result['id'];
931 * Add entity(s) to the tag
933 * @param array $params
937 public function entityTagAdd($params) {
938 $result = $this->callAPISuccess('entity_tag', 'create', $params);
945 * @param array $params
949 * id of created pledge
951 * @throws \CRM_Core_Exception
953 public function pledgeCreate($params) {
954 $params = array_merge([
955 'pledge_create_date' => date('Ymd'),
956 'start_date' => date('Ymd'),
957 'scheduled_date' => date('Ymd'),
959 'pledge_status_id' => '2',
960 'financial_type_id' => '1',
961 'pledge_original_installment_amount' => 20,
962 'frequency_interval' => 5,
963 'frequency_unit' => 'year',
964 'frequency_day' => 15,
969 $result = $this->callAPISuccess('Pledge', 'create', $params);
970 return $result['id'];
974 * Delete contribution.
976 * @param int $pledgeId
978 * @throws \CRM_Core_Exception
980 public function pledgeDelete($pledgeId) {
982 'pledge_id' => $pledgeId,
984 $this->callAPISuccess('Pledge', 'delete', $params);
988 * Create contribution.
990 * @param array $params
991 * Array of parameters.
994 * id of created contribution
995 * @throws \CRM_Core_Exception
997 public function contributionCreate($params) {
999 $params = array_merge([
1001 'receive_date' => date('Ymd'),
1002 'total_amount' => 100.00,
1003 'fee_amount' => 5.00,
1004 'financial_type_id' => 1,
1005 'payment_instrument_id' => 1,
1006 'non_deductible_amount' => 10.00,
1008 'contribution_status_id' => 1,
1011 $result = $this->callAPISuccess('contribution', 'create', $params);
1012 return $result['id'];
1016 * Delete contribution.
1018 * @param int $contributionId
1021 * @throws \CRM_Core_Exception
1023 public function contributionDelete($contributionId) {
1025 'contribution_id' => $contributionId,
1027 $result = $this->callAPISuccess('contribution', 'delete', $params);
1034 * @param array $params
1035 * Name-value pair for an event.
1038 * @throws \CRM_Core_Exception
1040 public function eventCreate($params = []) {
1041 // if no contact was passed, make up a dummy event creator
1042 if (!isset($params['contact_id'])) {
1043 $params['contact_id'] = $this->_contactCreate([
1044 'contact_type' => 'Individual',
1045 'first_name' => 'Event',
1046 'last_name' => 'Creator',
1050 // set defaults for missing params
1051 $params = array_merge([
1052 'title' => 'Annual CiviCRM meet',
1053 'summary' => 'If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now',
1054 'description' => 'This event is intended to give brief idea about progess of CiviCRM and giving solutions to common user issues',
1055 'event_type_id' => 1,
1057 'start_date' => 20081021,
1058 'end_date' => 20081023,
1059 'is_online_registration' => 1,
1060 'registration_start_date' => 20080601,
1061 'registration_end_date' => 20081015,
1062 'max_participants' => 100,
1063 'event_full_text' => 'Sorry! We are already full',
1066 'is_show_location' => 0,
1067 'is_email_confirm' => 1,
1070 return $this->callAPISuccess('Event', 'create', $params);
1074 * Create a paid event.
1076 * @param array $params
1078 * @param array $options
1080 * @param string $key
1081 * Index for storing event ID in ids array.
1085 * @throws \CRM_Core_Exception
1087 protected function eventCreatePaid($params, $options = [['name' => 'hundy', 'amount' => 100]], $key = 'event') {
1088 $params['is_monetary'] = TRUE;
1089 $event = $this->eventCreate($params);
1090 $this->ids
['Event'][$key] = (int) $event['id'];
1091 $this->ids
['PriceSet'][$key] = $this->eventPriceSetCreate(55, 0, 'Radio', $options);
1092 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $event['id'], $this->ids
['PriceSet'][$key]);
1093 $priceSet = CRM_Price_BAO_PriceSet
::getSetDetail($this->ids
['PriceSet'][$key], TRUE, FALSE);
1094 $priceSet = $priceSet[$this->ids
['PriceSet'][$key]] ??
NULL;
1095 $this->eventFeeBlock
= $priceSet['fields'] ??
NULL;
1107 public function eventDelete($id) {
1111 return $this->callAPISuccess('event', 'delete', $params);
1115 * Delete participant.
1117 * @param int $participantID
1121 public function participantDelete($participantID) {
1123 'id' => $participantID,
1125 $check = $this->callAPISuccess('Participant', 'get', $params);
1126 if ($check['count'] > 0) {
1127 return $this->callAPISuccess('Participant', 'delete', $params);
1132 * Create participant payment.
1134 * @param int $participantID
1135 * @param int $contributionID
1138 * $id of created payment
1140 public function participantPaymentCreate($participantID, $contributionID = NULL) {
1141 //Create Participant Payment record With Values
1143 'participant_id' => $participantID,
1144 'contribution_id' => $contributionID,
1147 $result = $this->callAPISuccess('participant_payment', 'create', $params);
1148 return $result['id'];
1152 * Delete participant payment.
1154 * @param int $paymentID
1156 public function participantPaymentDelete($paymentID) {
1160 $result = $this->callAPISuccess('participant_payment', 'delete', $params);
1166 * @param int $contactID
1169 * location id of created location
1171 public function locationAdd($contactID) {
1174 'location_type' => 'New Location Type',
1176 'name' => 'Saint Helier St',
1177 'county' => 'Marin',
1178 'country' => 'UNITED STATES',
1179 'state_province' => 'Michigan',
1180 'supplemental_address_1' => 'Hallmark Ct',
1181 'supplemental_address_2' => 'Jersey Village',
1182 'supplemental_address_3' => 'My Town',
1187 'contact_id' => $contactID,
1188 'address' => $address,
1189 'location_format' => '2.0',
1190 'location_type' => 'New Location Type',
1193 $result = $this->callAPISuccess('Location', 'create', $params);
1198 * Delete Locations of contact.
1200 * @param array $params
1203 public function locationDelete($params) {
1204 $this->callAPISuccess('Location', 'delete', $params);
1208 * Add a Location Type.
1210 * @param array $params
1212 * @return CRM_Core_DAO_LocationType
1213 * location id of created location
1215 public function locationTypeCreate($params = NULL) {
1216 if ($params === NULL) {
1218 'name' => 'New Location Type',
1219 'vcard_name' => 'New Location Type',
1220 'description' => 'Location Type for Delete',
1225 $locationType = new CRM_Core_DAO_LocationType();
1226 $locationType->copyValues($params);
1227 $locationType->save();
1228 // clear getfields cache
1229 CRM_Core_PseudoConstant
::flush();
1230 $this->callAPISuccess('phone', 'getfields', ['version' => 3, 'cache_clear' => 1]);
1231 return $locationType->id
;
1235 * Delete a Location Type.
1237 * @param int $locationTypeId
1239 public function locationTypeDelete($locationTypeId) {
1240 $locationType = new CRM_Core_DAO_LocationType();
1241 $locationType->id
= $locationTypeId;
1242 $locationType->delete();
1248 * @param array $params
1250 * @return CRM_Core_DAO_Mapping
1251 * Mapping id of created mapping
1253 public function mappingCreate($params = NULL) {
1254 if ($params === NULL) {
1256 'name' => 'Mapping name',
1257 'description' => 'Mapping description',
1258 // 'Export Contact' mapping.
1259 'mapping_type_id' => 7,
1263 $mapping = new CRM_Core_DAO_Mapping();
1264 $mapping->copyValues($params);
1266 // clear getfields cache
1267 CRM_Core_PseudoConstant
::flush();
1268 $this->callAPISuccess('mapping', 'getfields', ['version' => 3, 'cache_clear' => 1]);
1275 * @param int $mappingId
1277 public function mappingDelete($mappingId) {
1278 $mapping = new CRM_Core_DAO_Mapping();
1279 $mapping->id
= $mappingId;
1284 * Prepare class for ACLs.
1286 protected function prepareForACLs() {
1287 $config = CRM_Core_Config
::singleton();
1288 $config->userPermissionClass
->permissions
= [];
1294 protected function cleanUpAfterACLs() {
1295 CRM_Utils_Hook
::singleton()->reset();
1296 $tablesToTruncate = [
1298 'civicrm_acl_cache',
1299 'civicrm_acl_entity_role',
1300 'civicrm_acl_contact_cache',
1302 $this->quickCleanup($tablesToTruncate);
1303 $config = CRM_Core_Config
::singleton();
1304 unset($config->userPermissionClass
->permissions
);
1308 * Create a smart group.
1310 * By default it will be a group of households.
1312 * @param array $smartGroupParams
1313 * @param array $groupParams
1314 * @param string $contactType
1318 public function smartGroupCreate($smartGroupParams = [], $groupParams = [], $contactType = 'Household') {
1319 $smartGroupParams = array_merge(['form_values' => ['contact_type' => ['IN' => [$contactType]]]], $smartGroupParams);
1320 $savedSearch = CRM_Contact_BAO_SavedSearch
::create($smartGroupParams);
1322 $groupParams['saved_search_id'] = $savedSearch->id
;
1323 return $this->groupCreate($groupParams);
1329 * @param array $params
1331 public function uFFieldCreate($params = []) {
1332 $params = array_merge([
1334 'field_name' => 'first_name',
1337 'visibility' => 'Public Pages and Listings',
1338 'is_searchable' => '1',
1339 'label' => 'first_name',
1340 'field_type' => 'Individual',
1343 $this->callAPISuccess('uf_field', 'create', $params);
1347 * Add a UF Join Entry.
1349 * @param array $params
1352 * $id of created UF Join
1354 public function ufjoinCreate($params = NULL) {
1355 if ($params === NULL) {
1358 'module' => 'CiviEvent',
1359 'entity_table' => 'civicrm_event',
1365 $result = $this->callAPISuccess('uf_join', 'create', $params);
1370 * @param array $params
1371 * Optional parameters.
1372 * @param bool $reloadConfig
1373 * While enabling CiviCampaign component, we shouldn't always forcibly
1374 * reload config as this hinder hook call in test environment
1379 public function campaignCreate($params = [], $reloadConfig = TRUE) {
1380 $this->enableCiviCampaign($reloadConfig);
1381 $campaign = $this->callAPISuccess('campaign', 'create', array_merge([
1382 'name' => 'big_campaign',
1383 'title' => 'Campaign',
1385 return $campaign['id'];
1389 * Create Group for a contact.
1391 * @param int $contactId
1393 public function contactGroupCreate($contactId) {
1395 'contact_id.1' => $contactId,
1399 $this->callAPISuccess('GroupContact', 'Create', $params);
1403 * Delete Group for a contact.
1405 * @param int $contactId
1407 public function contactGroupDelete($contactId) {
1409 'contact_id.1' => $contactId,
1412 $this->civicrm_api('GroupContact', 'Delete', $params);
1418 * @param array $params
1422 * @throws \CRM_Core_Exception
1423 * @throws \CiviCRM_API3_Exception
1425 public function activityCreate($params = []) {
1426 $params = array_merge([
1427 'subject' => 'Discussion on warm beer',
1428 'activity_date_time' => date('Ymd'),
1430 'location' => 'Baker Street',
1431 'details' => 'Lets schedule a meeting',
1433 'activity_type_id' => 'Meeting',
1435 if (!isset($params['source_contact_id'])) {
1436 $params['source_contact_id'] = $this->individualCreate();
1438 if (!isset($params['target_contact_id'])) {
1439 $params['target_contact_id'] = $this->individualCreate([
1440 'first_name' => 'Julia',
1441 'last_name' => 'Anderson',
1443 'email' => 'julia_anderson@civicrm.org',
1444 'contact_type' => 'Individual',
1447 if (!isset($params['assignee_contact_id'])) {
1448 $params['assignee_contact_id'] = $params['target_contact_id'];
1451 $result = civicrm_api3('Activity', 'create', $params);
1453 $result['target_contact_id'] = $params['target_contact_id'];
1454 $result['assignee_contact_id'] = $params['assignee_contact_id'];
1459 * Create an activity type.
1461 * @param array $params
1466 public function activityTypeCreate($params) {
1467 return $this->callAPISuccess('ActivityType', 'create', $params);
1471 * Delete activity type.
1473 * @param int $activityTypeId
1474 * Id of the activity type.
1478 public function activityTypeDelete($activityTypeId) {
1479 $params['activity_type_id'] = $activityTypeId;
1480 return $this->callAPISuccess('ActivityType', 'delete', $params);
1484 * Create custom group.
1486 * @param array $params
1490 public function customGroupCreate($params = []) {
1492 'title' => 'new custom group',
1493 'extends' => 'Contact',
1495 'style' => 'Inline',
1499 $params = array_merge($defaults, $params);
1501 return $this->callAPISuccess('custom_group', 'create', $params);
1505 * Existing function doesn't allow params to be over-ridden so need a new one
1506 * this one allows you to only pass in the params you want to change
1508 * @param array $params
1512 public function CustomGroupCreateByParams($params = []) {
1514 'title' => "API Custom Group",
1515 'extends' => 'Contact',
1517 'style' => 'Inline',
1520 $params = array_merge($defaults, $params);
1521 return $this->callAPISuccess('custom_group', 'create', $params);
1525 * Create custom group with multi fields.
1527 * @param array $params
1531 public function CustomGroupMultipleCreateByParams($params = []) {
1536 $params = array_merge($defaults, $params);
1537 return $this->CustomGroupCreateByParams($params);
1541 * Create custom group with multi fields.
1543 * @param array $params
1547 public function CustomGroupMultipleCreateWithFields($params = []) {
1548 // also need to pass on $params['custom_field'] if not set but not in place yet
1550 $customGroup = $this->CustomGroupMultipleCreateByParams($params);
1551 $ids['custom_group_id'] = $customGroup['id'];
1553 $customField = $this->customFieldCreate([
1554 'custom_group_id' => $ids['custom_group_id'],
1555 'label' => 'field_1' . $ids['custom_group_id'],
1559 $ids['custom_field_id'][] = $customField['id'];
1561 $customField = $this->customFieldCreate([
1562 'custom_group_id' => $ids['custom_group_id'],
1563 'default_value' => '',
1564 'label' => 'field_2' . $ids['custom_group_id'],
1567 $ids['custom_field_id'][] = $customField['id'];
1569 $customField = $this->customFieldCreate([
1570 'custom_group_id' => $ids['custom_group_id'],
1571 'default_value' => '',
1572 'label' => 'field_3' . $ids['custom_group_id'],
1575 $ids['custom_field_id'][] = $customField['id'];
1581 * Create a custom group with a single text custom field. See
1582 * participant:testCreateWithCustom for how to use this
1584 * @param string $function
1586 * @param string $filename
1590 * ids of created objects
1592 public function entityCustomGroupWithSingleFieldCreate($function, $filename) {
1593 $params = ['title' => $function];
1594 $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8);
1595 $params['extends'] = $entity ?
$entity : 'Contact';
1596 $customGroup = $this->customGroupCreate($params);
1597 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'label' => $function]);
1598 CRM_Core_PseudoConstant
::flush();
1600 return ['custom_group_id' => $customGroup['id'], 'custom_field_id' => $customField['id']];
1604 * Create a custom group with a single text custom field, multi-select widget, with a variety of option values including upper and lower case.
1605 * See api_v3_SyntaxConformanceTest:testCustomDataGet for how to use this
1607 * @param string $function
1609 * @param string $filename
1613 * ids of created objects
1615 public function entityCustomGroupWithSingleStringMultiSelectFieldCreate($function, $filename) {
1616 $params = ['title' => $function];
1617 $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8);
1618 $params['extends'] = $entity ?
$entity : 'Contact';
1619 $customGroup = $this->customGroupCreate($params);
1620 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'label' => $function, 'html_type' => 'Multi-Select', 'default_value' => 1]);
1621 CRM_Core_PseudoConstant
::flush();
1623 'defaultValue' => 'Default Value',
1624 'lowercasevalue' => 'Lowercase Value',
1625 1 => 'Integer Value',
1628 $custom_field_params = ['sequential' => 1, 'id' => $customField['id']];
1629 $custom_field_api_result = $this->callAPISuccess('custom_field', 'get', $custom_field_params);
1630 $this->assertNotEmpty($custom_field_api_result['values'][0]['option_group_id']);
1631 $option_group_params = ['sequential' => 1, 'id' => $custom_field_api_result['values'][0]['option_group_id']];
1632 $option_group_result = $this->callAPISuccess('OptionGroup', 'get', $option_group_params);
1633 $this->assertNotEmpty($option_group_result['values'][0]['name']);
1634 foreach ($options as $option_value => $option_label) {
1635 $option_group_params = ['option_group_id' => $option_group_result['values'][0]['name'], 'value' => $option_value, 'label' => $option_label];
1636 $option_value_result = $this->callAPISuccess('OptionValue', 'create', $option_group_params);
1640 'custom_group_id' => $customGroup['id'],
1641 'custom_field_id' => $customField['id'],
1642 'custom_field_option_group_id' => $custom_field_api_result['values'][0]['option_group_id'],
1643 'custom_field_group_options' => $options,
1648 * Delete custom group.
1650 * @param int $customGroupID
1654 public function customGroupDelete($customGroupID) {
1655 $params['id'] = $customGroupID;
1656 return $this->callAPISuccess('custom_group', 'delete', $params);
1660 * Create custom field.
1662 * @param array $params
1663 * (custom_group_id) is required.
1667 public function customFieldCreate($params) {
1668 $params = array_merge([
1669 'label' => 'Custom Field',
1670 'data_type' => 'String',
1671 'html_type' => 'Text',
1672 'is_searchable' => 1,
1674 'default_value' => 'defaultValue',
1677 $result = $this->callAPISuccess('custom_field', 'create', $params);
1678 // these 2 functions are called with force to flush static caches
1679 CRM_Core_BAO_CustomField
::getTableColumnGroup($result['id'], 1);
1680 CRM_Core_Component
::getEnabledComponents(1);
1685 * Delete custom field.
1687 * @param int $customFieldID
1691 public function customFieldDelete($customFieldID) {
1693 $params['id'] = $customFieldID;
1694 return $this->callAPISuccess('custom_field', 'delete', $params);
1704 public function noteCreate($cId) {
1706 'entity_table' => 'civicrm_contact',
1707 'entity_id' => $cId,
1708 'note' => 'hello I am testing Note',
1709 'contact_id' => $cId,
1710 'modified_date' => date('Ymd'),
1711 'subject' => 'Test Note',
1714 return $this->callAPISuccess('Note', 'create', $params);
1718 * Enable CiviCampaign Component.
1720 * @param bool $reloadConfig
1721 * Force relaod config or not
1723 public function enableCiviCampaign($reloadConfig = TRUE) {
1724 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
1725 if ($reloadConfig) {
1726 // force reload of config object
1727 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
1729 //flush cache by calling with reset
1730 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
1734 * Create custom field with Option Values.
1736 * @param array $customGroup
1737 * @param string $name
1738 * Name of custom field.
1739 * @param array $extraParams
1740 * Additional parameters to pass through.
1744 public function customFieldOptionValueCreate($customGroup, $name, $extraParams = []) {
1746 'custom_group_id' => $customGroup['id'],
1747 'name' => 'test_custom_group',
1748 'label' => 'Country',
1749 'html_type' => 'Select',
1750 'data_type' => 'String',
1753 'is_searchable' => 0,
1759 'name' => 'option_group1',
1760 'label' => 'option_group_label1',
1764 'option_label' => ['Label1', 'Label2'],
1765 'option_value' => ['value1', 'value2'],
1766 'option_name' => [$name . '_1', $name . '_2'],
1767 'option_weight' => [1, 2],
1768 'option_status' => [1, 1],
1771 $params = array_merge($fieldParams, $optionGroup, $optionValue, $extraParams);
1773 return $this->callAPISuccess('custom_field', 'create', $params);
1781 public function confirmEntitiesDeleted($entities) {
1782 foreach ($entities as $entity) {
1784 $result = $this->callAPISuccess($entity, 'Get', []);
1785 if ($result['error'] == 1 ||
$result['count'] > 0) {
1786 // > than $entity[0] to allow a value to be passed in? e.g. domain?
1794 * Quick clean by emptying tables created for the test.
1796 * @param array $tablesToTruncate
1797 * @param bool $dropCustomValueTables
1799 * @throws \CRM_Core_Exception
1801 public function quickCleanup($tablesToTruncate, $dropCustomValueTables = FALSE) {
1803 throw new \
CRM_Core_Exception("CiviUnitTestCase: quickCleanup() is not compatible with useTransaction()");
1805 if ($dropCustomValueTables) {
1806 $optionGroupResult = CRM_Core_DAO
::executeQuery('SELECT option_group_id FROM civicrm_custom_field');
1807 while ($optionGroupResult->fetch()) {
1808 // We have a test that sets the option_group_id for a custom group to that of 'activity_type'.
1809 // Then test tearDown deletes it. This is all mildly terrifying but for the context here we can be pretty
1810 // sure the low-numbered (50 is arbitrary) option groups are not ones to 'just delete' in a
1811 // generic cleanup routine.
1812 if (!empty($optionGroupResult->option_group_id
) && $optionGroupResult->option_group_id
> 50) {
1813 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_option_group WHERE id = ' . $optionGroupResult->option_group_id
);
1816 $tablesToTruncate[] = 'civicrm_custom_group';
1817 $tablesToTruncate[] = 'civicrm_custom_field';
1820 $tablesToTruncate = array_unique(array_merge($this->_tablesToTruncate
, $tablesToTruncate));
1822 CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
1823 foreach ($tablesToTruncate as $table) {
1824 $sql = "TRUNCATE TABLE $table";
1825 CRM_Core_DAO
::executeQuery($sql);
1827 CRM_Core_DAO
::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
1829 if ($dropCustomValueTables) {
1830 $dbName = self
::getDBName();
1832 SELECT TABLE_NAME as tableName
1833 FROM INFORMATION_SCHEMA.TABLES
1834 WHERE TABLE_SCHEMA = '{$dbName}'
1835 AND ( TABLE_NAME LIKE 'civicrm_value_%' )
1838 $tableDAO = CRM_Core_DAO
::executeQuery($query);
1839 while ($tableDAO->fetch()) {
1840 $sql = "DROP TABLE {$tableDAO->tableName}";
1841 CRM_Core_DAO
::executeQuery($sql);
1847 * Clean up financial entities after financial tests (so we remember to get all the tables :-))
1849 * @throws \CRM_Core_Exception
1851 public function quickCleanUpFinancialEntities() {
1852 $tablesToTruncate = [
1854 'civicrm_activity_contact',
1855 'civicrm_contribution',
1856 'civicrm_contribution_soft',
1857 'civicrm_contribution_product',
1858 'civicrm_financial_trxn',
1859 'civicrm_financial_item',
1860 'civicrm_contribution_recur',
1861 'civicrm_line_item',
1862 'civicrm_contribution_page',
1863 'civicrm_payment_processor',
1864 'civicrm_entity_financial_trxn',
1865 'civicrm_membership',
1866 'civicrm_membership_type',
1867 'civicrm_membership_payment',
1868 'civicrm_membership_log',
1869 'civicrm_membership_block',
1871 'civicrm_participant',
1872 'civicrm_participant_payment',
1874 'civicrm_pcp_block',
1876 'civicrm_pledge_block',
1877 'civicrm_pledge_payment',
1878 'civicrm_price_set_entity',
1879 'civicrm_price_field_value',
1880 'civicrm_price_field',
1882 $this->quickCleanup($tablesToTruncate);
1883 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_membership_status WHERE name NOT IN('New', 'Current', 'Grace', 'Expired', 'Pending', 'Cancelled', 'Deceased')");
1884 $this->restoreDefaultPriceSetConfig();
1885 $this->disableTaxAndInvoicing();
1886 $this->setCurrencySeparators(',');
1887 CRM_Core_PseudoConstant
::flush('taxRates');
1888 System
::singleton()->flushProcessors();
1889 // @fixme this parameter is leaking - it should not be defined as a class static
1890 // but for now we just handle in tear down.
1891 CRM_Contribute_BAO_Query
::$_contribOrSoftCredit = 'only contribs';
1895 * Reset the price set config so results exist.
1897 public function restoreDefaultPriceSetConfig() {
1898 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_price_set WHERE name NOT IN('default_contribution_amount', 'default_membership_type_amount')");
1899 CRM_Core_DAO
::executeQuery("UPDATE civicrm_price_set SET id = 1 WHERE name ='default_contribution_amount'");
1900 CRM_Core_DAO
::executeQuery("INSERT INTO `civicrm_price_field` (`id`, `price_set_id`, `name`, `label`, `html_type`, `is_enter_qty`, `help_pre`, `help_post`, `weight`, `is_display_amounts`, `options_per_line`, `is_active`, `is_required`, `active_on`, `expire_on`, `javascript`, `visibility_id`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', 'Text', 0, NULL, NULL, 1, 1, 1, 1, 1, NULL, NULL, NULL, 1)");
1901 CRM_Core_DAO
::executeQuery("INSERT INTO `civicrm_price_field_value` (`id`, `price_field_id`, `name`, `label`, `description`, `amount`, `count`, `max_value`, `weight`, `membership_type_id`, `membership_num_terms`, `is_default`, `is_active`, `financial_type_id`, `non_deductible_amount`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', NULL, '1', NULL, NULL, 1, NULL, NULL, 0, 1, 1, 0.00)");
1905 * Recreate default membership types.
1907 public function restoreMembershipTypes() {
1908 CRM_Core_DAO
::executeQuery(
1909 "REPLACE INTO civicrm_membership_type
1910 (id, domain_id, name, description, member_of_contact_id, financial_type_id, minimum_fee, duration_unit, duration_interval, period_type, fixed_period_start_day, fixed_period_rollover_day, relationship_type_id, relationship_direction, visibility, weight, is_active)
1912 (1, 1, 'General', 'Regular annual membership.', 1, 2, 100.00, 'year', 2, 'rolling', NULL, NULL, 7, 'b_a', 'Public', 1, 1),
1913 (2, 1, 'Student', 'Discount membership for full-time students.', 1, 2, 50.00, 'year', 1, 'rolling', NULL, NULL, NULL, NULL, 'Public', 2, 1),
1914 (3, 1, 'Lifetime', 'Lifetime membership.', 1, 2, 1200.00, 'lifetime', 1, 'rolling', NULL, NULL, 7, 'b_a', 'Admin', 3, 1);
1919 * Function does a 'Get' on the entity & compares the fields in the Params with those returned
1920 * Default behaviour is to also delete the entity
1921 * @param array $params
1922 * Params array to check against.
1924 * Id of the entity concerned.
1925 * @param string $entity
1926 * Name of entity concerned (e.g. membership).
1927 * @param bool $delete
1928 * Should the entity be deleted as part of this check.
1929 * @param string $errorText
1930 * Text to print on error.
1934 * @param array $params
1937 * @param int $delete
1938 * @param string $errorText
1940 * @throws CRM_Core_Exception
1942 public function getAndCheck($params, $id, $entity, $delete = 1, $errorText = '') {
1944 $result = $this->callAPISuccessGetSingle($entity, [
1949 $this->callAPISuccess($entity, 'Delete', [
1953 $dateFields = $keys = $dateTimeFields = [];
1954 $fields = $this->callAPISuccess($entity, 'getfields', ['version' => 3, 'action' => 'get']);
1955 foreach ($fields['values'] as $field => $settings) {
1956 if (array_key_exists($field, $result)) {
1957 $keys[CRM_Utils_Array
::value('name', $settings, $field)] = $field;
1960 $keys[CRM_Utils_Array
::value('name', $settings, $field)] = CRM_Utils_Array
::value('name', $settings, $field);
1962 $type = $settings['type'] ??
NULL;
1963 if ($type == CRM_Utils_Type
::T_DATE
) {
1964 $dateFields[] = $settings['name'];
1965 // we should identify both real names & unique names as dates
1966 if ($field != $settings['name']) {
1967 $dateFields[] = $field;
1970 if ($type == CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
) {
1971 $dateTimeFields[] = $settings['name'];
1972 // we should identify both real names & unique names as dates
1973 if ($field != $settings['name']) {
1974 $dateTimeFields[] = $field;
1979 if (strtolower($entity) == 'contribution') {
1980 $params['receive_date'] = date('Y-m-d', strtotime($params['receive_date']));
1981 // this is not returned in id format
1982 unset($params['payment_instrument_id']);
1983 $params['contribution_source'] = $params['source'];
1984 unset($params['source']);
1987 foreach ($params as $key => $value) {
1988 if ($key == 'version' ||
substr($key, 0, 3) == 'api' ||
!array_key_exists($keys[$key], $result)) {
1991 if (in_array($key, $dateFields)) {
1992 $value = date('Y-m-d', strtotime($value));
1993 $result[$key] = date('Y-m-d', strtotime($result[$key]));
1995 if (in_array($key, $dateTimeFields)) {
1996 $value = date('Y-m-d H:i:s', strtotime($value));
1997 $result[$keys[$key]] = date('Y-m-d H:i:s', strtotime(CRM_Utils_Array
::value($keys[$key], $result, CRM_Utils_Array
::value($key, $result))));
1999 $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);
2004 * Get formatted values in the actual and expected result.
2006 * @param array $actual
2007 * Actual calculated values.
2008 * @param array $expected
2011 public function checkArrayEquals(&$actual, &$expected) {
2012 self
::unsetId($actual);
2013 self
::unsetId($expected);
2014 $this->assertEquals($expected, $actual);
2018 * Unset the key 'id' from the array
2020 * @param array $unformattedArray
2021 * The array from which the 'id' has to be unset.
2023 public static function unsetId(&$unformattedArray) {
2024 $formattedArray = [];
2025 if (array_key_exists('id', $unformattedArray)) {
2026 unset($unformattedArray['id']);
2028 if (!empty($unformattedArray['values']) && is_array($unformattedArray['values'])) {
2029 foreach ($unformattedArray['values'] as $key => $value) {
2030 if (is_array($value)) {
2031 foreach ($value as $k => $v) {
2037 elseif ($key == 'id') {
2038 $unformattedArray[$key];
2040 $formattedArray = [$value];
2042 $unformattedArray['values'] = $formattedArray;
2047 * Helper to enable/disable custom directory support
2049 * @param array $customDirs
2051 * 'php_path' Set to TRUE to use the default, FALSE or "" to disable support, or a string path to use another path
2052 * 'template_path' Set to TRUE to use the default, FALSE or "" to disable support, or a string path to use another path
2054 public function customDirectories($customDirs) {
2055 $config = CRM_Core_Config
::singleton();
2057 if (empty($customDirs['php_path']) ||
$customDirs['php_path'] === FALSE) {
2058 unset($config->customPHPPathDir
);
2060 elseif ($customDirs['php_path'] === TRUE) {
2061 $config->customPHPPathDir
= dirname(dirname(__FILE__
)) . '/custom_directories/php/';
2064 $config->customPHPPathDir
= $php_path;
2067 if (empty($customDirs['template_path']) ||
$customDirs['template_path'] === FALSE) {
2068 unset($config->customTemplateDir
);
2070 elseif ($customDirs['template_path'] === TRUE) {
2071 $config->customTemplateDir
= dirname(dirname(__FILE__
)) . '/custom_directories/templates/';
2074 $config->customTemplateDir
= $template_path;
2079 * Generate a temporary folder.
2081 * @param string $prefix
2085 public function createTempDir($prefix = 'test-') {
2086 $tempDir = CRM_Utils_File
::tempdir($prefix);
2087 $this->tempDirs
[] = $tempDir;
2091 public function cleanTempDirs() {
2092 if (!is_array($this->tempDirs
)) {
2093 // fix test errors where this is not set
2096 foreach ($this->tempDirs
as $tempDir) {
2097 if (is_dir($tempDir)) {
2098 CRM_Utils_File
::cleanDir($tempDir, TRUE, FALSE);
2104 * Temporarily replace the singleton extension with a different one.
2106 * @param \CRM_Extension_System $system
2108 public function setExtensionSystem(CRM_Extension_System
$system) {
2109 if ($this->origExtensionSystem
== NULL) {
2110 $this->origExtensionSystem
= CRM_Extension_System
::singleton();
2112 CRM_Extension_System
::setSingleton($this->origExtensionSystem
);
2115 public function unsetExtensionSystem() {
2116 if ($this->origExtensionSystem
!== NULL) {
2117 CRM_Extension_System
::setSingleton($this->origExtensionSystem
);
2118 $this->origExtensionSystem
= NULL;
2123 * Temporarily alter the settings-metadata to add a mock setting.
2125 * WARNING: The setting metadata will disappear on the next cache-clear.
2131 public function setMockSettingsMetaData($extras) {
2132 CRM_Utils_Hook
::singleton()
2133 ->setHook('civicrm_alterSettingsMetaData', function (&$metadata, $domainId, $profile) use ($extras) {
2134 $metadata = array_merge($metadata, $extras);
2137 Civi
::service('settings_manager')->flush();
2139 $fields = $this->callAPISuccess('setting', 'getfields', []);
2140 foreach ($extras as $key => $spec) {
2141 $this->assertNotEmpty($spec['title']);
2142 $this->assertEquals($spec['title'], $fields['values'][$key]['title']);
2147 * @param string $name
2149 public function financialAccountDelete($name) {
2150 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
2151 $financialAccount->name
= $name;
2152 if ($financialAccount->find(TRUE)) {
2153 $entityFinancialType = new CRM_Financial_DAO_EntityFinancialAccount();
2154 $entityFinancialType->financial_account_id
= $financialAccount->id
;
2155 $entityFinancialType->delete();
2156 $financialAccount->delete();
2161 * FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together
2162 * (NB unclear if this is still required)
2164 public function _sethtmlGlobals() {
2165 $GLOBALS['_HTML_QuickForm_registered_rules'] = [
2167 'html_quickform_rule_required',
2168 'HTML/QuickForm/Rule/Required.php',
2171 'html_quickform_rule_range',
2172 'HTML/QuickForm/Rule/Range.php',
2175 'html_quickform_rule_range',
2176 'HTML/QuickForm/Rule/Range.php',
2179 'html_quickform_rule_range',
2180 'HTML/QuickForm/Rule/Range.php',
2183 'html_quickform_rule_email',
2184 'HTML/QuickForm/Rule/Email.php',
2187 'html_quickform_rule_regex',
2188 'HTML/QuickForm/Rule/Regex.php',
2191 'html_quickform_rule_regex',
2192 'HTML/QuickForm/Rule/Regex.php',
2195 'html_quickform_rule_regex',
2196 'HTML/QuickForm/Rule/Regex.php',
2199 'html_quickform_rule_regex',
2200 'HTML/QuickForm/Rule/Regex.php',
2202 'nopunctuation' => [
2203 'html_quickform_rule_regex',
2204 'HTML/QuickForm/Rule/Regex.php',
2207 'html_quickform_rule_regex',
2208 'HTML/QuickForm/Rule/Regex.php',
2211 'html_quickform_rule_callback',
2212 'HTML/QuickForm/Rule/Callback.php',
2215 'html_quickform_rule_compare',
2216 'HTML/QuickForm/Rule/Compare.php',
2219 // FIXME: …ditto for $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']
2220 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = [
2222 'HTML/QuickForm/group.php',
2223 'HTML_QuickForm_group',
2226 'HTML/QuickForm/hidden.php',
2227 'HTML_QuickForm_hidden',
2230 'HTML/QuickForm/reset.php',
2231 'HTML_QuickForm_reset',
2234 'HTML/QuickForm/checkbox.php',
2235 'HTML_QuickForm_checkbox',
2238 'HTML/QuickForm/file.php',
2239 'HTML_QuickForm_file',
2242 'HTML/QuickForm/image.php',
2243 'HTML_QuickForm_image',
2246 'HTML/QuickForm/password.php',
2247 'HTML_QuickForm_password',
2250 'HTML/QuickForm/radio.php',
2251 'HTML_QuickForm_radio',
2254 'HTML/QuickForm/button.php',
2255 'HTML_QuickForm_button',
2258 'HTML/QuickForm/submit.php',
2259 'HTML_QuickForm_submit',
2262 'HTML/QuickForm/select.php',
2263 'HTML_QuickForm_select',
2266 'HTML/QuickForm/hiddenselect.php',
2267 'HTML_QuickForm_hiddenselect',
2270 'HTML/QuickForm/text.php',
2271 'HTML_QuickForm_text',
2274 'HTML/QuickForm/textarea.php',
2275 'HTML_QuickForm_textarea',
2278 'HTML/QuickForm/fckeditor.php',
2279 'HTML_QuickForm_FCKEditor',
2282 'HTML/QuickForm/tinymce.php',
2283 'HTML_QuickForm_TinyMCE',
2286 'HTML/QuickForm/dojoeditor.php',
2287 'HTML_QuickForm_dojoeditor',
2290 'HTML/QuickForm/link.php',
2291 'HTML_QuickForm_link',
2294 'HTML/QuickForm/advcheckbox.php',
2295 'HTML_QuickForm_advcheckbox',
2298 'HTML/QuickForm/date.php',
2299 'HTML_QuickForm_date',
2302 'HTML/QuickForm/static.php',
2303 'HTML_QuickForm_static',
2306 'HTML/QuickForm/header.php',
2307 'HTML_QuickForm_header',
2310 'HTML/QuickForm/html.php',
2311 'HTML_QuickForm_html',
2314 'HTML/QuickForm/hierselect.php',
2315 'HTML_QuickForm_hierselect',
2318 'HTML/QuickForm/autocomplete.php',
2319 'HTML_QuickForm_autocomplete',
2322 'HTML/QuickForm/xbutton.php',
2323 'HTML_QuickForm_xbutton',
2325 'advmultiselect' => [
2326 'HTML/QuickForm/advmultiselect.php',
2327 'HTML_QuickForm_advmultiselect',
2333 * Set up an acl allowing contact to see 2 specified groups
2334 * - $this->_permissionedGroup & $this->_permissionedDisabledGroup
2336 * You need to have pre-created these groups & created the user e.g
2337 * $this->createLoggedInUser();
2338 * $this->_permissionedDisabledGroup = $this->groupCreate(array('title' => 'pick-me-disabled', 'is_active' => 0, 'name' => 'pick-me-disabled'));
2339 * $this->_permissionedGroup = $this->groupCreate(array('title' => 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active'));
2341 * @param bool $isProfile
2343 public function setupACL($isProfile = FALSE) {
2345 $_REQUEST = $this->_params
;
2347 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM'];
2348 $optionGroupID = $this->callAPISuccessGetValue('option_group', ['return' => 'id', 'name' => 'acl_role']);
2349 $ov = new CRM_Core_DAO_OptionValue();
2350 $ov->option_group_id
= $optionGroupID;
2352 if ($ov->find(TRUE)) {
2353 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_option_value WHERE id = {$ov->id}");
2355 $optionValue = $this->callAPISuccess('option_value', 'create', [
2356 'option_group_id' => $optionGroupID,
2357 'label' => 'pick me',
2361 CRM_Core_DAO
::executeQuery("
2362 TRUNCATE civicrm_acl_cache
2365 CRM_Core_DAO
::executeQuery("
2366 TRUNCATE civicrm_acl_contact_cache
2369 CRM_Core_DAO
::executeQuery("
2370 INSERT INTO civicrm_acl_entity_role (
2371 `acl_role_id`, `entity_table`, `entity_id`, `is_active`
2372 ) VALUES (55, 'civicrm_group', {$this->_permissionedGroup}, 1);
2376 CRM_Core_DAO
::executeQuery("
2377 INSERT INTO civicrm_acl (
2378 `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
2381 'view picked', 'civicrm_acl_role', 55, 'Edit', 'civicrm_uf_group', 0, 1
2386 CRM_Core_DAO
::executeQuery("
2387 INSERT INTO civicrm_acl (
2388 `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
2391 'view picked', 'civicrm_group', $this->_permissionedGroup , 'Edit', 'civicrm_saved_search', {$this->_permissionedGroup}, 1
2395 CRM_Core_DAO
::executeQuery("
2396 INSERT INTO civicrm_acl (
2397 `name`, `entity_table`, `entity_id`, `operation`, `object_table`, `object_id`, `is_active`
2400 'view picked', 'civicrm_group', $this->_permissionedGroup, 'Edit', 'civicrm_saved_search', {$this->_permissionedDisabledGroup}, 1
2405 $this->_loggedInUser
= CRM_Core_Session
::singleton()->get('userID');
2406 $this->callAPISuccess('group_contact', 'create', [
2407 'group_id' => $this->_permissionedGroup
,
2408 'contact_id' => $this->_loggedInUser
,
2413 CRM_ACL_BAO_Cache
::resetCache();
2414 CRM_ACL_API
::groupPermission('whatever', 9999, NULL, 'civicrm_saved_search', NULL, NULL);
2419 * Alter default price set so that the field numbers are not all 1 (hiding errors)
2421 public function offsetDefaultPriceSet() {
2422 $contributionPriceSet = $this->callAPISuccess('price_set', 'getsingle', ['name' => 'default_contribution_amount']);
2423 $firstID = $contributionPriceSet['id'];
2424 $this->callAPISuccess('price_set', 'create', [
2425 'id' => $contributionPriceSet['id'],
2429 unset($contributionPriceSet['id']);
2430 $newPriceSet = $this->callAPISuccess('price_set', 'create', $contributionPriceSet);
2431 $priceField = $this->callAPISuccess('price_field', 'getsingle', [
2432 'price_set_id' => $firstID,
2433 'options' => ['limit' => 1],
2435 unset($priceField['id']);
2436 $priceField['price_set_id'] = $newPriceSet['id'];
2437 $newPriceField = $this->callAPISuccess('price_field', 'create', $priceField);
2438 $priceFieldValue = $this->callAPISuccess('price_field_value', 'getsingle', [
2439 'price_set_id' => $firstID,
2441 'options' => ['limit' => 1],
2444 unset($priceFieldValue['id']);
2445 //create some padding to use up ids
2446 $this->callAPISuccess('price_field_value', 'create', $priceFieldValue);
2447 $this->callAPISuccess('price_field_value', 'create', $priceFieldValue);
2448 $this->callAPISuccess('price_field_value', 'create', array_merge($priceFieldValue, ['price_field_id' => $newPriceField['id']]));
2452 * Create an instance of the paypal processor.
2454 * @todo this isn't a great place to put it - but really it belongs on a class that extends
2455 * this parent class & we don't have a structure for that yet
2456 * There is another function to this effect on the PaypalPro test but it appears to be silently failing
2457 * & the best protection against that is the functions this class affords
2459 * @param array $params
2461 * @return int $result['id'] payment processor id
2463 public function paymentProcessorCreate($params = []) {
2464 $params = array_merge([
2466 'domain_id' => CRM_Core_Config
::domainID(),
2467 'payment_processor_type_id' => 'PayPal',
2471 'user_name' => 'sunil._1183377782_biz_api1.webaccess.co.in',
2472 'password' => '1183377788',
2473 'signature' => 'APixCoQ-Zsaj-u3IH7mD5Do-7HUqA9loGnLSzsZga9Zr-aNmaJa3WGPH',
2474 'url_site' => 'https://www.sandbox.paypal.com/',
2475 'url_api' => 'https://api-3t.sandbox.paypal.com/',
2476 'url_button' => 'https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif',
2477 'class_name' => 'Payment_PayPalImpl',
2478 'billing_mode' => 3,
2479 'financial_type_id' => 1,
2480 'financial_account_id' => 12,
2481 // Credit card = 1 so can pass 'by accident'.
2482 'payment_instrument_id' => 'Debit Card',
2484 if (!is_numeric($params['payment_processor_type_id'])) {
2485 // really the api should handle this through getoptions but it's not exactly api call so lets just sort it
2487 $params['payment_processor_type_id'] = $this->callAPISuccess('payment_processor_type', 'getvalue', [
2488 'name' => $params['payment_processor_type_id'],
2492 $result = $this->callAPISuccess('payment_processor', 'create', $params);
2493 return $result['id'];
2497 * Set up initial recurring payment allowing subsequent IPN payments.
2499 * @param array $recurParams (Optional)
2500 * @param array $contributionParams (Optional)
2502 * @throws \CRM_Core_Exception
2504 public function setupRecurringPaymentProcessorTransaction($recurParams = [], $contributionParams = []) {
2505 $this->ids
['campaign'][0] = $this->callAPISuccess('Campaign', 'create', ['title' => 'get the money'])['id'];
2506 $contributionParams = array_merge([
2507 'total_amount' => '200',
2508 'invoice_id' => $this->_invoiceID
,
2509 'financial_type_id' => 'Donation',
2510 'contribution_status_id' => 'Pending',
2511 'contact_id' => $this->_contactID
,
2512 'contribution_page_id' => $this->_contributionPageID
,
2513 'payment_processor_id' => $this->_paymentProcessorID
,
2515 'receive_date' => '2019-07-25 07:34:23',
2516 'skipCleanMoney' => TRUE,
2517 'amount_level' => 'expensive',
2518 'campaign_id' => $this->ids
['campaign'][0],
2519 'source' => 'Online Contribution: Page name',
2520 ], $contributionParams);
2521 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge([
2522 'contact_id' => $this->_contactID
,
2525 'installments' => 5,
2526 'frequency_unit' => 'Month',
2527 'frequency_interval' => 1,
2528 'invoice_id' => $this->_invoiceID
,
2529 'contribution_status_id' => 2,
2530 'payment_processor_id' => $this->_paymentProcessorID
,
2531 // processor provided ID - use contact ID as proxy.
2532 'processor_id' => $this->_contactID
,
2533 'api.Order.create' => $contributionParams,
2534 ], $recurParams))['values'][0];
2535 $this->_contributionRecurID
= $contributionRecur['id'];
2536 $this->_contributionID
= $contributionRecur['api.Order.create']['id'];
2537 $this->ids
['Contribution'][0] = $this->_contributionID
;
2541 * We don't have a good way to set up a recurring contribution with a membership so let's just do one then alter it
2543 * @param array $params Optionally modify params for membership/recur (duration_unit/frequency_unit)
2545 * @throws \CRM_Core_Exception
2547 public function setupMembershipRecurringPaymentProcessorTransaction($params = []) {
2548 $membershipParams = $recurParams = [];
2549 if (!empty($params['duration_unit'])) {
2550 $membershipParams['duration_unit'] = $params['duration_unit'];
2552 if (!empty($params['frequency_unit'])) {
2553 $recurParams['frequency_unit'] = $params['frequency_unit'];
2556 $this->ids
['membership_type'] = $this->membershipTypeCreate($membershipParams);
2557 //create a contribution so our membership & contribution don't both have id = 1
2558 if ($this->callAPISuccess('Contribution', 'getcount', []) === 0) {
2559 $this->contributionCreate([
2560 'contact_id' => $this->_contactID
,
2562 'financial_type_id' => 1,
2563 'invoice_id' => 'abcd',
2565 'receive_date' => '2019-07-25 07:34:23',
2569 $this->ids
['membership'] = $this->callAPISuccess('Membership', 'create', [
2570 'contact_id' => $this->_contactID
,
2571 'membership_type_id' => $this->ids
['membership_type'],
2572 'format.only_id' => TRUE,
2573 'source' => 'Payment',
2574 'skipLineItem' => TRUE,
2576 $this->setupRecurringPaymentProcessorTransaction($recurParams, [
2581 'entity_table' => 'civicrm_membership',
2582 'entity_id' => $this->ids
['membership'],
2583 'label' => 'General',
2585 'unit_price' => 200,
2586 'line_total' => 200,
2587 'financial_type_id' => 1,
2588 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
2590 'label' => 'Membership Amount',
2591 'options' => ['limit' => 1, 'sort' => 'id DESC'],
2593 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [
2595 'label' => 'General',
2596 'options' => ['limit' => 1, 'sort' => 'id DESC'],
2603 $this->callAPISuccess('Membership', 'create', ['id' => $this->ids
['membership'], 'contribution_recur_id' => $this->_contributionRecurID
]);
2611 public function CiviUnitTestCase_fatalErrorHandler($message) {
2612 throw new Exception("{$message['message']}: {$message['code']}");
2616 * Wrap the entire test case in a transaction.
2618 * Only subsequent DB statements will be wrapped in TX -- this cannot
2619 * retroactively wrap old DB statements. Therefore, it makes sense to
2620 * call this at the beginning of setUp().
2622 * Note: Recall that TRUNCATE and ALTER will force-commit transactions, so
2623 * this option does not work with, e.g., custom-data.
2625 * WISHLIST: Monitor SQL queries in unit-tests and generate an exception
2626 * if TRUNCATE or ALTER is called while using a transaction.
2629 * Whether to use nesting or reference-counting.
2631 public function useTransaction($nest = TRUE) {
2633 $this->tx
= new CRM_Core_Transaction($nest);
2634 $this->tx
->rollback();
2639 * Assert the attachment exists.
2641 * @param bool $exists
2642 * @param array $apiResult
2644 protected function assertAttachmentExistence($exists, $apiResult) {
2645 $fileId = $apiResult['id'];
2646 $this->assertTrue(is_numeric($fileId));
2647 $this->assertEquals($exists, file_exists($apiResult['values'][$fileId]['path']));
2648 $this->assertDBQuery($exists ?
1 : 0, 'SELECT count(*) FROM civicrm_file WHERE id = %1', [
2649 1 => [$fileId, 'Int'],
2651 $this->assertDBQuery($exists ?
1 : 0, 'SELECT count(*) FROM civicrm_entity_file WHERE id = %1', [
2652 1 => [$fileId, 'Int'],
2657 * Assert 2 sql strings are the same, ignoring double spaces.
2659 * @param string $expectedSQL
2660 * @param string $actualSQL
2661 * @param string $message
2663 protected function assertLike($expectedSQL, $actualSQL, $message = 'different sql') {
2664 $expected = trim((preg_replace('/[ \r\n\t]+/', ' ', $expectedSQL)));
2665 $actual = trim((preg_replace('/[ \r\n\t]+/', ' ', $actualSQL)));
2666 $this->assertEquals($expected, $actual, $message);
2670 * Create a price set for an event.
2672 * @param int $feeTotal
2673 * @param int $minAmt
2674 * @param string $type
2676 * @param array $options
2680 * @throws \CRM_Core_Exception
2682 protected function eventPriceSetCreate($feeTotal, $minAmt = 0, $type = 'Text', $options = [['name' => 'hundy', 'amount' => 100]]) {
2683 // creating price set, price field
2684 $paramsSet['title'] = 'Price Set';
2685 $paramsSet['name'] = CRM_Utils_String
::titleToVar('Price Set');
2686 $paramsSet['is_active'] = FALSE;
2687 $paramsSet['extends'] = 1;
2688 $paramsSet['min_amount'] = $minAmt;
2690 $priceSet = CRM_Price_BAO_PriceSet
::create($paramsSet);
2691 $this->_ids
['price_set'] = $priceSet->id
;
2694 'label' => 'Price Field',
2695 'name' => CRM_Utils_String
::titleToVar('Price Field'),
2696 'html_type' => $type,
2697 'price' => $feeTotal,
2698 'option_label' => ['1' => 'Price Field'],
2699 'option_value' => ['1' => $feeTotal],
2700 'option_name' => ['1' => $feeTotal],
2701 'option_weight' => ['1' => 1],
2702 'option_amount' => ['1' => 1],
2703 'is_display_amounts' => 1,
2705 'options_per_line' => 1,
2706 'is_active' => ['1' => 1],
2707 'price_set_id' => $this->_ids
['price_set'],
2708 'is_enter_qty' => 1,
2709 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
2711 if ($type === 'Radio') {
2712 foreach ($options as $index => $option) {
2713 $paramsField['is_enter_qty'] = 0;
2714 $optionID = $index +
2;
2715 $paramsField['option_value'][$optionID] = $paramsField['option_weight'][$optionID] = $paramsField['option_amount'][$optionID] = $option['amount'];
2716 $paramsField['option_label'][$optionID] = $paramsField['option_name'][$optionID] = $option['name'];
2720 $this->callAPISuccess('PriceField', 'create', $paramsField);
2721 $fields = $this->callAPISuccess('PriceField', 'get', ['price_set_id' => $this->_ids
['price_set']]);
2722 $this->_ids
['price_field'] = array_keys($fields['values']);
2723 $fieldValues = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $this->_ids
['price_field'][0]]);
2724 $this->_ids
['price_field_value'] = array_keys($fieldValues['values']);
2726 return $this->_ids
['price_set'];
2730 * Add a profile to a contribution page.
2732 * @param string $name
2733 * @param int $contributionPageID
2734 * @param string $module
2736 protected function addProfile($name, $contributionPageID, $module = 'CiviContribute') {
2738 'uf_group_id' => $name,
2739 'module' => $module,
2740 'entity_table' => 'civicrm_contribution_page',
2741 'entity_id' => $contributionPageID,
2744 if ($module !== 'CiviContribute') {
2745 $params['module_data'] = [$module => []];
2747 $this->callAPISuccess('UFJoin', 'create', $params);
2751 * Add participant with contribution
2755 * @throws \CRM_Core_Exception
2757 protected function createPartiallyPaidParticipantOrder() {
2758 $orderParams = $this->getParticipantOrderParams();
2759 $orderParams['api.Payment.create'] = ['total_amount' => 150];
2760 return $this->callAPISuccess('Order', 'create', $orderParams);
2766 * @param string $component
2767 * @param int $componentId
2768 * @param array $priceFieldOptions
2772 protected function createPriceSet($component = 'contribution_page', $componentId = NULL, $priceFieldOptions = []) {
2773 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 7);
2774 $paramsSet['name'] = CRM_Utils_String
::titleToVar($paramsSet['title']);
2775 $paramsSet['is_active'] = TRUE;
2776 $paramsSet['financial_type_id'] = 'Event Fee';
2777 $paramsSet['extends'] = 1;
2778 $priceSet = $this->callAPISuccess('price_set', 'create', $paramsSet);
2779 $priceSetId = $priceSet['id'];
2780 //Checking for priceset added in the table.
2781 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
2782 'id', $paramsSet['title'], 'Check DB for created priceset'
2784 $paramsField = array_merge([
2785 'label' => 'Price Field',
2786 'name' => CRM_Utils_String
::titleToVar('Price Field'),
2787 'html_type' => 'CheckBox',
2788 'option_label' => ['1' => 'Price Field 1', '2' => 'Price Field 2'],
2789 'option_value' => ['1' => 100, '2' => 200],
2790 'option_name' => ['1' => 'Price Field 1', '2' => 'Price Field 2'],
2791 'option_weight' => ['1' => 1, '2' => 2],
2792 'option_amount' => ['1' => 100, '2' => 200],
2793 'is_display_amounts' => 1,
2795 'options_per_line' => 1,
2796 'is_active' => ['1' => 1, '2' => 1],
2797 'price_set_id' => $priceSet['id'],
2798 'is_enter_qty' => 1,
2799 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
2800 ], $priceFieldOptions);
2802 $priceField = CRM_Price_BAO_PriceField
::create($paramsField);
2804 CRM_Price_BAO_PriceSet
::addTo('civicrm_' . $component, $componentId, $priceSetId);
2806 return $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $priceField->id
]);
2810 * Replace the template with a test-oriented template designed to show all the variables.
2812 * @param string $templateName
2813 * @param string $type
2815 protected function swapMessageTemplateForTestTemplate($templateName = 'contribution_online_receipt', $type = 'html') {
2816 $testTemplate = file_get_contents(__DIR__
. '/../../templates/message_templates/' . $templateName . '_' . $type . '.tpl');
2817 CRM_Core_DAO
::executeQuery(
2818 "UPDATE civicrm_msg_template
2819 SET msg_{$type} = %1
2820 WHERE workflow_name = '{$templateName}'
2821 AND is_default = 1", [1 => [$testTemplate, 'String']]
2826 * Reinstate the default template.
2828 * @param string $templateName
2829 * @param string $type
2831 protected function revertTemplateToReservedTemplate($templateName = 'contribution_online_receipt', $type = 'html') {
2832 CRM_Core_DAO
::executeQuery(
2833 "UPDATE civicrm_option_group og
2834 LEFT JOIN civicrm_option_value ov ON ov.option_group_id = og.id
2835 LEFT JOIN civicrm_msg_template m ON m.workflow_id = ov.id
2836 LEFT JOIN civicrm_msg_template m2 ON m2.workflow_id = ov.id AND m2.is_reserved = 1
2837 SET m.msg_{$type} = m2.msg_{$type}
2838 WHERE og.name = 'msg_tpl_workflow_contribution'
2839 AND ov.name = '{$templateName}'
2840 AND m.is_default = 1"
2845 * Flush statics relating to financial type.
2847 protected function flushFinancialTypeStatics() {
2848 if (isset(\Civi
::$statics['CRM_Financial_BAO_FinancialType'])) {
2849 unset(\Civi
::$statics['CRM_Financial_BAO_FinancialType']);
2851 if (isset(\Civi
::$statics['CRM_Contribute_PseudoConstant'])) {
2852 unset(\Civi
::$statics['CRM_Contribute_PseudoConstant']);
2854 CRM_Contribute_PseudoConstant
::flush('financialType');
2855 CRM_Contribute_PseudoConstant
::flush('membershipType');
2856 // Pseudoconstants may be saved to the cache table.
2857 CRM_Core_DAO
::executeQuery("TRUNCATE civicrm_cache");
2858 CRM_Financial_BAO_FinancialType
::$_statusACLFt = [];
2859 CRM_Financial_BAO_FinancialType
::$_availableFinancialTypes = NULL;
2863 * Set the permissions to the supplied array.
2865 * @param array $permissions
2867 protected function setPermissions($permissions) {
2868 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= $permissions;
2869 $this->flushFinancialTypeStatics();
2873 * @param array $params
2876 public function _checkFinancialRecords($params, $context) {
2878 'entity_id' => $params['id'],
2879 'entity_table' => 'civicrm_contribution',
2881 $contribution = $this->callAPISuccess('contribution', 'getsingle', ['id' => $params['id']]);
2882 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
2883 if ($context == 'pending') {
2884 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
2885 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2888 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2890 'id' => $trxn['financial_trxn_id'],
2892 if ($context != 'online' && $context != 'payLater') {
2894 'to_financial_account_id' => 6,
2895 'total_amount' => (float) CRM_Utils_Array
::value('total_amount', $params, 100.00),
2899 if ($context == 'feeAmount') {
2900 $compareParams['fee_amount'] = 50;
2902 elseif ($context == 'online') {
2904 'to_financial_account_id' => 12,
2905 'total_amount' => (float) CRM_Utils_Array
::value('total_amount', $params, 100.00),
2907 'payment_instrument_id' => CRM_Utils_Array
::value('payment_instrument_id', $params, 1),
2910 elseif ($context == 'payLater') {
2912 'to_financial_account_id' => 7,
2913 'total_amount' => (float) CRM_Utils_Array
::value('total_amount', $params, 100.00),
2917 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2919 'financial_trxn_id' => $trxn['financial_trxn_id'],
2920 'entity_table' => 'civicrm_financial_item',
2922 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2924 'id' => $entityTrxn['entity_id'],
2927 'amount' => (float) CRM_Utils_Array
::value('total_amount', $params, 100.00),
2929 'financial_account_id' => CRM_Utils_Array
::value('financial_account_id', $params, 1),
2931 if ($context == 'payLater') {
2933 'amount' => (float) CRM_Utils_Array
::value('total_amount', $params, 100.00),
2935 'financial_account_id' => CRM_Utils_Array
::value('financial_account_id', $params, 1),
2938 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2939 if ($context == 'feeAmount') {
2941 'entity_id' => $params['id'],
2942 'entity_table' => 'civicrm_contribution',
2944 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
2946 'id' => $maxTrxn['financial_trxn_id'],
2949 'to_financial_account_id' => 5,
2950 'from_financial_account_id' => 6,
2951 'total_amount' => 50,
2954 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
2955 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2957 'entity_id' => $trxnId['financialTrxnId'],
2958 'entity_table' => 'civicrm_financial_trxn',
2963 'financial_account_id' => 5,
2965 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2967 // This checks that empty Sales tax rows are not being created. If for any reason it needs to be removed the
2968 // line should be copied into all the functions that call this function & evaluated there
2969 // Be really careful not to remove or bypass this without ensuring stray rows do not re-appear
2970 // when calling completeTransaction or repeatTransaction.
2971 $this->callAPISuccessGetCount('FinancialItem', ['description' => 'Sales Tax', 'amount' => 0], 0);
2975 * Return financial type id on basis of name
2977 * @param string $name Financial type m/c name
2981 public function getFinancialTypeId($name) {
2982 return CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType', $name, 'id', 'name');
2986 * Cleanup function for contents of $this->ids.
2988 * This is a best effort cleanup to use in tear downs etc.
2990 * It will not fail if the data has already been removed (some tests may do
2991 * their own cleanup).
2993 protected function cleanUpSetUpIDs() {
2994 foreach ($this->setupIDs
as $entity => $id) {
2996 civicrm_api3($entity, 'delete', ['id' => $id, 'skip_undelete' => 1]);
2998 catch (CiviCRM_API3_Exception
$e) {
2999 // This is a best-effort cleanup function, ignore.
3005 * Create Financial Type.
3007 * @param array $params
3011 protected function createFinancialType($params = []) {
3012 $params = array_merge($params,
3014 'name' => 'Financial-Type -' . substr(sha1(rand()), 0, 7),
3018 return $this->callAPISuccess('FinancialType', 'create', $params);
3022 * Create Payment Instrument.
3024 * @param array $params
3025 * @param string $financialAccountName
3029 protected function createPaymentInstrument($params = [], $financialAccountName = 'Donation') {
3030 $params = array_merge([
3031 'label' => 'Payment Instrument -' . substr(sha1(rand()), 0, 7),
3032 'option_group_id' => 'payment_instrument',
3035 $newPaymentInstrument = $this->callAPISuccess('OptionValue', 'create', $params)['id'];
3037 $relationTypeID = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3039 $financialAccountParams = [
3040 'entity_table' => 'civicrm_option_value',
3041 'entity_id' => $newPaymentInstrument,
3042 'account_relationship' => $relationTypeID,
3043 'financial_account_id' => $this->callAPISuccess('FinancialAccount', 'getValue', ['name' => $financialAccountName, 'return' => 'id']),
3045 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
3047 return CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $params['label']);
3051 * Enable Tax and Invoicing
3053 * @param array $params
3055 * @return \Civi\Core\SettingsBag
3057 protected function enableTaxAndInvoicing($params = []) {
3058 // Enable component contribute setting
3059 $contributeSetting = array_merge($params,
3062 'invoice_prefix' => 'INV_',
3064 'due_date_period' => 'days',
3066 'is_email_pdf' => 1,
3067 'tax_term' => 'Sales Tax',
3068 'tax_display_settings' => 'Inclusive',
3071 return Civi
::settings()->set('contribution_invoice_settings', $contributeSetting);
3075 * Enable Tax and Invoicing
3077 * @throws \CRM_Core_Exception
3079 protected function disableTaxAndInvoicing() {
3080 $accounts = $this->callAPISuccess('EntityFinancialAccount', 'get', ['account_relationship' => 'Sales Tax Account is'])['values'];
3081 foreach ($accounts as $account) {
3082 $this->callAPISuccess('EntityFinancialAccount', 'delete', ['id' => $account['id']]);
3083 $this->callAPISuccess('FinancialAccount', 'delete', ['id' => $account['financial_account_id']]);
3086 if (!empty(\Civi
::$statics['CRM_Core_PseudoConstant']) && isset(\Civi
::$statics['CRM_Core_PseudoConstant']['taxRates'])) {
3087 unset(\Civi
::$statics['CRM_Core_PseudoConstant']['taxRates']);
3089 return Civi
::settings()->set('invoicing', FALSE);
3093 * Add Sales Tax Account for the financial type.
3095 * @param int $financialTypeId
3097 * @param array $accountParams
3099 * @return CRM_Financial_DAO_EntityFinancialAccount
3100 * @throws \CRM_Core_Exception
3102 protected function addTaxAccountToFinancialType(int $financialTypeId, $accountParams = []) {
3103 $params = array_merge([
3104 'name' => 'Sales tax account ' . substr(sha1(rand()), 0, 4),
3105 'financial_account_type_id' => key(CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Liability' ")),
3106 'is_deductible' => 1,
3111 $account = CRM_Financial_BAO_FinancialAccount
::add($params);
3113 'entity_table' => 'civicrm_financial_type',
3114 'entity_id' => $financialTypeId,
3115 'account_relationship' => key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Sales Tax Account is' ")),
3118 // set tax rate (as 10) for provided financial type ID to static variable, later used to fetch tax rates of all financial types
3119 \Civi
::$statics['CRM_Core_PseudoConstant']['taxRates'][$financialTypeId] = $params['tax_rate'];
3121 //CRM-20313: As per unique index added in civicrm_entity_financial_account table,
3122 // first check if there's any record on basis of unique key (entity_table, account_relationship, entity_id)
3123 $dao = new CRM_Financial_DAO_EntityFinancialAccount();
3124 $dao->copyValues($entityParams);
3126 if ($dao->fetch()) {
3127 $entityParams['id'] = $dao->id
;
3129 $entityParams['financial_account_id'] = $account->id
;
3131 return CRM_Financial_BAO_FinancialTypeAccount
::add($entityParams);
3135 * Create price set with contribution test for test setup.
3137 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
3138 * on parent class at some point (fn is not in 4.4).
3141 * @param array $params
3143 public function createPriceSetWithPage($entity = NULL, $params = []) {
3144 $membershipTypeID = $this->membershipTypeCreate(['name' => 'Special']);
3145 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', [
3146 'title' => "Test Contribution Page",
3147 'financial_type_id' => 1,
3148 'currency' => 'NZD',
3149 'goal_amount' => 50,
3150 'is_pay_later' => 1,
3151 'is_monetary' => TRUE,
3152 'is_email_receipt' => FALSE,
3154 $priceSet = $this->callAPISuccess('price_set', 'create', [
3155 'is_quick_config' => 0,
3156 'extends' => 'CiviMember',
3157 'financial_type_id' => 1,
3158 'title' => 'my Page',
3160 $priceSetID = $priceSet['id'];
3162 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
3163 $priceField = $this->callAPISuccess('price_field', 'create', [
3164 'price_set_id' => $priceSetID,
3165 'label' => 'Goat Breed',
3166 'html_type' => 'Radio',
3168 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
3169 'price_set_id' => $priceSetID,
3170 'price_field_id' => $priceField['id'],
3171 'label' => 'Long Haired Goat',
3173 'financial_type_id' => 'Donation',
3174 'membership_type_id' => $membershipTypeID,
3175 'membership_num_terms' => 1,
3177 $this->_ids
['price_field_value'] = [$priceFieldValue['id']];
3178 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
3179 'price_set_id' => $priceSetID,
3180 'price_field_id' => $priceField['id'],
3181 'label' => 'Shoe-eating Goat',
3183 'financial_type_id' => 'Donation',
3184 'membership_type_id' => $membershipTypeID,
3185 'membership_num_terms' => 2,
3187 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
3189 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
3190 'price_set_id' => $priceSetID,
3191 'price_field_id' => $priceField['id'],
3192 'label' => 'Shoe-eating Goat',
3194 'financial_type_id' => 'Donation',
3196 $this->_ids
['price_field_value']['cont'] = $priceFieldValue['id'];
3198 $this->_ids
['price_set'] = $priceSetID;
3199 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
3200 $this->_ids
['price_field'] = [$priceField['id']];
3202 $this->_ids
['membership_type'] = $membershipTypeID;
3206 * Only specified contact returned.
3208 * @implements CRM_Utils_Hook::aclWhereClause
3212 * @param $whereTables
3216 public function aclWhereMultipleContacts($type, &$tables, &$whereTables, &$contactID, &$where) {
3217 $where = " contact_a.id IN (" . implode(', ', $this->allowedContacts
) . ")";
3221 * @implements CRM_Utils_Hook::selectWhereClause
3223 * @param string $entity
3224 * @param array $clauses
3226 public function selectWhereClauseHook($entity, &$clauses) {
3227 if ($entity == 'Event') {
3228 $clauses['event_type_id'][] = "IN (2, 3, 4)";
3233 * An implementation of hook_civicrm_post used with all our test cases.
3236 * @param string $objectName
3237 * @param int $objectId
3240 public function onPost($op, $objectName, $objectId, &$objectRef) {
3241 if ($op == 'create' && $objectName == 'Individual') {
3242 CRM_Core_DAO
::executeQuery(
3243 "UPDATE civicrm_contact SET nick_name = 'munged' WHERE id = %1",
3245 1 => [$objectId, 'Integer'],
3250 if ($op == 'edit' && $objectName == 'Participant') {
3252 1 => [$objectId, 'Integer'],
3254 $query = "UPDATE civicrm_participant SET source = 'Post Hook Update' WHERE id = %1";
3255 CRM_Core_DAO
::executeQuery($query, $params);
3260 * Instantiate form object.
3262 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
3264 * @param string $class
3265 * Name of form class.
3267 * @param array $formValues
3269 * @param string $pageName
3271 * @return \CRM_Core_Form
3272 * @throws \CRM_Core_Exception
3274 public function getFormObject($class, $formValues = [], $pageName = '') {
3275 $_POST = $formValues;
3276 /* @var CRM_Core_Form $form */
3277 $form = new $class();
3278 $_SERVER['REQUEST_METHOD'] = 'GET';
3280 case 'CRM_Event_Cart_Form_Checkout_Payment':
3281 case 'CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices':
3282 $form->controller
= new CRM_Event_Cart_Controller_Checkout();
3286 $form->controller
= new CRM_Core_Controller();
3289 $pageName = $form->getName();
3291 $form->controller
->setStateMachine(new CRM_Core_StateMachine($form->controller
));
3292 $_SESSION['_' . $form->controller
->_name
. '_container']['values'][$pageName] = $formValues;
3297 * Get possible thousand separators.
3301 public function getThousandSeparators() {
3302 return [['.'], [',']];
3306 * Get the boolean options as a provider.
3310 public function getBooleanDataProvider() {
3311 return [[TRUE], [FALSE]];
3315 * Set the separators for thousands and decimal points.
3317 * Note that this only covers some common scenarios.
3319 * It does not cater for a situation where the thousand separator is a [space]
3320 * Latter is the Norwegian localization. At least some tests need to
3321 * use setMonetaryDecimalPoint and setMonetaryThousandSeparator directly
3322 * to provide broader coverage.
3324 * @param string $thousandSeparator
3326 protected function setCurrencySeparators($thousandSeparator) {
3327 Civi
::settings()->set('monetaryThousandSeparator', $thousandSeparator);
3328 Civi
::settings()->set('monetaryDecimalPoint', ($thousandSeparator === ',' ?
'.' : ','));
3332 * Sets the thousand separator.
3334 * If you use this function also set the decimal separator: setMonetaryDecimalSeparator
3336 * @param $thousandSeparator
3338 protected function setMonetaryThousandSeparator($thousandSeparator) {
3339 Civi
::settings()->set('monetaryThousandSeparator', $thousandSeparator);
3343 * Sets the decimal separator.
3345 * If you use this function also set the thousand separator setMonetaryDecimalPoint
3347 * @param $decimalPoint
3349 protected function setMonetaryDecimalPoint($decimalPoint) {
3350 Civi
::settings()->set('monetaryDecimalPoint', $decimalPoint);
3354 * Sets the default currency.
3358 protected function setDefaultCurrency($currency) {
3359 Civi
::settings()->set('defaultCurrency', $currency);
3363 * Format money as it would be input.
3365 * @param string $amount
3369 protected function formatMoneyInput($amount) {
3370 return CRM_Utils_Money
::format($amount, NULL, '%a');
3374 * Get the contribution object.
3376 * @param int $contributionID
3378 * @return \CRM_Contribute_BAO_Contribution
3380 protected function getContributionObject($contributionID) {
3381 $contributionObj = new CRM_Contribute_BAO_Contribution();
3382 $contributionObj->id
= $contributionID;
3383 $contributionObj->find(TRUE);
3384 return $contributionObj;
3388 * Enable multilingual.
3390 public function enableMultilingual() {
3391 $this->callAPISuccess('Setting', 'create', [
3392 'lcMessages' => 'en_US',
3393 'languageLimit' => [
3398 CRM_Core_I18n_Schema
::makeMultilingual('en_US');
3401 $dbLocale = '_en_US';
3405 * Setup or clean up SMS tests
3407 * @param bool $teardown
3409 * @throws \CiviCRM_API3_Exception
3411 public function setupForSmsTests($teardown = FALSE) {
3412 require_once 'CiviTest/CiviTestSMSProvider.php';
3414 // Option value params for CiviTestSMSProvider
3415 $groupID = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'sms_provider_name', 'id', 'name');
3417 'option_group_id' => $groupID,
3418 'label' => 'unittestSMS',
3419 'value' => 'unit.test.sms',
3420 'name' => 'CiviTestSMSProvider',
3427 // Test completed, delete provider
3428 $providerOptionValueResult = civicrm_api3('option_value', 'get', $params);
3429 civicrm_api3('option_value', 'delete', ['id' => $providerOptionValueResult['id']]);
3433 // Create an SMS provider "CiviTestSMSProvider". Civi handles "CiviTestSMSProvider" as a special case and allows it to be instantiated
3434 // in CRM/Sms/Provider.php even though it is not an extension.
3435 return civicrm_api3('option_value', 'create', $params);
3439 * Start capturing browser output.
3441 * The starts the process of browser output being captured, setting any variables needed for e-notice prevention.
3443 protected function startCapturingOutput() {
3445 $_SERVER['HTTP_USER_AGENT'] = 'unittest';
3449 * Stop capturing browser output and return as a csv.
3451 * @param bool $isFirstRowHeaders
3453 * @return \League\Csv\Reader
3455 * @throws \League\Csv\Exception
3457 protected function captureOutputToCSV($isFirstRowHeaders = TRUE) {
3458 $output = ob_get_flush();
3459 $stream = fopen('php://memory', 'r+');
3460 fwrite($stream, $output);
3462 $this->assertEquals("\xEF\xBB\xBF", substr($output, 0, 3));
3463 $csv = Reader
::createFromString($output);
3464 if ($isFirstRowHeaders) {
3465 $csv->setHeaderOffset(0);
3472 * Rename various labels to not match the names.
3474 * Doing these mimics the fact the name != the label in international installs & triggers failures in
3475 * code that expects it to.
3477 protected function renameLabels() {
3478 $replacements = ['Pending', 'Refunded'];
3479 foreach ($replacements as $name) {
3480 CRM_Core_DAO
::executeQuery("UPDATE civicrm_option_value SET label = '{$name} Label**' where label = '{$name}' AND name = '{$name}'");
3485 * Undo any label renaming.
3487 protected function resetLabels() {
3488 CRM_Core_DAO
::executeQuery("UPDATE civicrm_option_value SET label = REPLACE(name, ' Label**', '') WHERE label LIKE '% Label**'");
3492 * Get parameters to set up a multi-line participant order.
3495 * @throws \CRM_Core_Exception
3497 protected function getParticipantOrderParams(): array {
3498 $this->_contactId
= $this->individualCreate();
3499 $event = $this->eventCreate();
3500 $this->_eventId
= $event['id'];
3502 'id' => $this->_eventId
,
3503 'financial_type_id' => 4,
3506 $this->callAPISuccess('event', 'create', $eventParams);
3507 $priceFields = $this->createPriceSet('event', $this->_eventId
);
3508 $participantParams = [
3509 'financial_type_id' => 4,
3510 'event_id' => $this->_eventId
,
3513 'fee_currency' => 'USD',
3514 'contact_id' => $this->_contactId
,
3516 $participant = $this->callAPISuccess('Participant', 'create', $participantParams);
3518 'total_amount' => 300,
3519 'currency' => 'USD',
3520 'contact_id' => $this->_contactId
,
3521 'financial_type_id' => 4,
3522 'contribution_status_id' => 'Pending',
3523 'contribution_mode' => 'participant',
3524 'participant_id' => $participant['id'],
3526 foreach ($priceFields['values'] as $key => $priceField) {
3527 $orderParams['line_items'][] = [
3530 'price_field_id' => $priceField['price_field_id'],
3531 'price_field_value_id' => $priceField['id'],
3532 'label' => $priceField['label'],
3533 'field_title' => $priceField['label'],
3535 'unit_price' => $priceField['amount'],
3536 'line_total' => $priceField['amount'],
3537 'financial_type_id' => $priceField['financial_type_id'],
3538 'entity_table' => 'civicrm_participant',
3541 'params' => $participant,
3544 return $orderParams;
3550 * @throws \CRM_Core_Exception
3552 protected function validatePayments($payments) {
3553 foreach ($payments as $payment) {
3554 $balance = CRM_Contribute_BAO_Contribution
::getContributionBalance($payment['contribution_id']);
3555 if ($balance < 0 && $balance +
$payment['total_amount'] === 0.0) {
3556 // This is an overpayment situation. there are no financial items to allocate the overpayment.
3557 // This is a pretty rough way at guessing which payment is the overpayment - but
3558 // for the test suite it should be enough.
3561 $items = $this->callAPISuccess('EntityFinancialTrxn', 'get', [
3562 'financial_trxn_id' => $payment['id'],
3563 'entity_table' => 'civicrm_financial_item',
3564 'return' => ['amount'],
3567 foreach ($items as $item) {
3568 $itemTotal +
= $item['amount'];
3570 $this->assertEquals($payment['total_amount'], $itemTotal);
3575 * Validate all created payments.
3577 * @throws \CRM_Core_Exception
3579 protected function validateAllPayments() {
3580 $payments = $this->callAPISuccess('Payment', 'get', ['options' => ['limit' => 0]])['values'];
3581 $this->validatePayments($payments);
3585 * Validate all created contributions.
3587 * @throws \CRM_Core_Exception
3589 protected function validateAllContributions() {
3590 $contributions = $this->callAPISuccess('Contribution', 'get', ['return' => ['tax_amount', 'total_amount']])['values'];
3591 foreach ($contributions as $contribution) {
3592 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $contribution['id']])['values'];
3595 foreach ($lineItems as $lineItem) {
3596 $total +
= $lineItem['line_total'];
3597 $taxTotal +
= (float) ($lineItem['tax_amount'] ??
0);
3599 $this->assertEquals($taxTotal, (float) ($contribution['tax_amount'] ??
0));
3600 $this->assertEquals($total, $contribution['total_amount']);
3606 * @throws \CRM_Core_Exception
3608 protected function createRuleGroup() {
3609 $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
3610 'contact_type' => 'Individual',
3612 'used' => 'General',
3613 'name' => 'TestRule',
3614 'title' => 'TestRule',
3621 * Generic create test.
3623 * @param int $version
3625 * @throws \CRM_Core_Exception
3627 protected function basicCreateTest(int $version) {
3628 $this->_apiversion
= $version;
3629 $result = $this->callAPIAndDocument($this->_entity
, 'create', $this->params
, __FUNCTION__
, __FILE__
);
3630 $this->assertEquals(1, $result['count']);
3631 $this->assertNotNull($result['values'][$result['id']]['id']);
3632 $this->getAndCheck($this->params
, $result['id'], $this->_entity
);
3636 * Generic delete test.
3638 * @param int $version
3640 * @throws \CRM_Core_Exception
3642 protected function basicDeleteTest($version) {
3643 $this->_apiversion
= $version;
3644 $result = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
3645 $deleteParams = ['id' => $result['id']];
3646 $this->callAPIAndDocument($this->_entity
, 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
3647 $checkDeleted = $this->callAPISuccess($this->_entity
, 'get', []);
3648 $this->assertEquals(0, $checkDeleted['count']);
3652 * Create and return a case object for the given Client ID.
3654 * @param int $clientId
3655 * @param int $loggedInUser
3656 * Omit or pass NULL to use the same as clientId
3657 * @param array $extra
3658 * Optional specific parameters such as start_date
3660 * @return CRM_Case_BAO_Case
3662 public function createCase($clientId, $loggedInUser = NULL, $extra = []) {
3663 if (empty($loggedInUser)) {
3664 // backwards compatibility - but it's more typical that the creator is a different person than the client
3665 $loggedInUser = $clientId;
3667 $caseParams = array_merge([
3668 'activity_subject' => 'Case Subject',
3669 'client_id' => $clientId,
3670 'case_type_id' => 1,
3672 'case_type' => 'housing_support',
3673 'subject' => 'Case Subject',
3674 'start_date' => date("Y-m-d"),
3675 'start_date_time' => date("YmdHis"),
3677 'activity_details' => '',
3679 $form = new CRM_Case_Form_Case();
3680 return $form->testSubmit($caseParams, 'OpenCase', $loggedInUser, 'standalone');
3684 * Validate that all location entities have exactly one primary.
3686 * This query takes about 2 minutes on a DB with 10s of millions of contacts.
3688 public function assertLocationValidity() {
3689 $this->assertEquals(0, CRM_Core_DAO
::singleValueQuery('SELECT COUNT(*) FROM
3691 (SELECT a1.contact_id
3692 FROM civicrm_address a1
3693 LEFT JOIN civicrm_address a2 ON a1.id <> a2.id AND a2.is_primary = 1
3694 AND a1.contact_id = a2.contact_id
3697 AND a2.id IS NOT NULL
3698 AND a1.contact_id IS NOT NULL
3700 SELECT a1.contact_id
3701 FROM civicrm_address a1
3702 LEFT JOIN civicrm_address a2 ON a1.id <> a2.id AND a2.is_primary = 1
3703 AND a1.contact_id = a2.contact_id
3704 WHERE a1.is_primary = 0
3706 AND a1.contact_id IS NOT NULL
3710 SELECT a1.contact_id
3711 FROM civicrm_email a1
3712 LEFT JOIN civicrm_email a2 ON a1.id <> a2.id AND a2.is_primary = 1
3713 AND a1.contact_id = a2.contact_id
3716 AND a2.id IS NOT NULL
3717 AND a1.contact_id IS NOT NULL
3719 SELECT a1.contact_id
3720 FROM civicrm_email a1
3721 LEFT JOIN civicrm_email a2 ON a1.id <> a2.id AND a2.is_primary = 1
3722 AND a1.contact_id = a2.contact_id
3723 WHERE a1.is_primary = 0
3725 AND a1.contact_id IS NOT NULL
3729 SELECT a1.contact_id
3730 FROM civicrm_phone a1
3731 LEFT JOIN civicrm_phone a2 ON a1.id <> a2.id AND a2.is_primary = 1
3732 AND a1.contact_id = a2.contact_id
3735 AND a2.id IS NOT NULL
3736 AND a1.contact_id IS NOT NULL
3738 SELECT a1.contact_id
3739 FROM civicrm_phone a1
3740 LEFT JOIN civicrm_phone a2 ON a1.id <> a2.id AND a2.is_primary = 1
3741 AND a1.contact_id = a2.contact_id
3742 WHERE a1.is_primary = 0
3744 AND a1.contact_id IS NOT NULL
3748 SELECT a1.contact_id
3750 LEFT JOIN civicrm_im a2 ON a1.id <> a2.id AND a2.is_primary = 1
3751 AND a1.contact_id = a2.contact_id
3754 AND a2.id IS NOT NULL
3755 AND a1.contact_id IS NOT NULL
3757 SELECT a1.contact_id
3759 LEFT JOIN civicrm_im a2 ON a1.id <> a2.id AND a2.is_primary = 1
3760 AND a1.contact_id = a2.contact_id
3761 WHERE a1.is_primary = 0
3763 AND a1.contact_id IS NOT NULL
3767 SELECT a1.contact_id
3768 FROM civicrm_openid a1
3769 LEFT JOIN civicrm_openid a2 ON a1.id <> a2.id AND a2.is_primary = 1
3770 AND a1.contact_id = a2.contact_id
3771 WHERE (a1.is_primary = 1 AND a2.id IS NOT NULL)
3774 SELECT a1.contact_id
3775 FROM civicrm_openid a1
3776 LEFT JOIN civicrm_openid a2 ON a1.id <> a2.id AND a2.is_primary = 1
3777 AND a1.contact_id = a2.contact_id
3780 AND a2.id IS NOT NULL
3781 AND a1.contact_id IS NOT NULL
3783 SELECT a1.contact_id
3784 FROM civicrm_openid a1
3785 LEFT JOIN civicrm_openid a2 ON a1.id <> a2.id AND a2.is_primary = 1
3786 AND a1.contact_id = a2.contact_id
3787 WHERE a1.is_primary = 0
3789 AND a1.contact_id IS NOT NULL) as primary_descrepancies