X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FCiviTest%2FCiviUnitTestCase.php;h=5bb07b4d6472e7bac873eaff5f067cc3cd956deb;hb=bf38705ff050b801cccacb041140fdf236c73a96;hp=e41eadb08ae0ec25eb0e3648be247a0aa28d1ccf;hpb=9c41996008a0cb938cd30d8996cdec462c6a61a7;p=civicrm-core.git diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index e41eadb08a..5bb07b4d64 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -47,13 +47,24 @@ define('API_LATEST_VERSION', 3); /** * Base class for CiviCRM unit tests * + * This class supports two (mutually-exclusive) techniques for cleaning up test data. Subclasses + * may opt for one or neither: + * + * 1. quickCleanup() is a helper which truncates a series of tables. Call quickCleanup() + * as part of setUp() and/or tearDown(). quickCleanup() is thorough - but it can + * be cumbersome to use (b/c you must identify the tables to cleanup) and slow to execute. + * 2. useTransaction() executes the test inside a transaction. It's easier to use + * (because you don't need to identify specific tables), but it doesn't work for tests + * which manipulate schema or truncate data -- and could behave inconsistently + * for tests which specifically examine DB transactions. + * * Common functions for unit tests * @package CiviCRM */ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { /** - * api version - easier to override than just a define + * Api version - easier to override than just a define */ protected $_apiversion = API_LATEST_VERSION; /** @@ -119,6 +130,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { */ public $DBResetRequired = TRUE; + /** + * @var CRM_Core_Transaction|NULL + */ + private $tx = NULL; + /** * Constructor * @@ -133,7 +149,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param array $data * @param string $dataName */ - function __construct($name = NULL, array$data = array(), $dataName = '') { + public function __construct($name = NULL, array$data = array(), $dataName = '') { parent::__construct($name, $data, $dataName); // we need full error reporting @@ -161,17 +177,26 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } } + protected function runTest() { + try { + return parent::runTest(); + } catch (PEAR_Exception $e) { + // PEAR_Exception has metadata in funny places, and PHPUnit won't log it nicely + throw new Exception(\CRM_Core_Error::formatTextException($e), $e->getCode()); + } + } + /** * @return bool */ - function requireDBReset() { + public function requireDBReset() { return $this->DBResetRequired; } /** * @return string */ - static function getDBName() { + public static function getDBName() { $dbName = !empty($GLOBALS['mysql_db']) ? $GLOBALS['mysql_db'] : 'civicrm_tests_dev'; return $dbName; } @@ -373,6 +398,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { //flush component settings CRM_Core_Component::getEnabledComponents(TRUE); + if ($this->_eNoticeCompliant) { error_reporting(E_ALL); } @@ -410,7 +436,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * emulate a logged in user since certain functions use that + * Emulate a logged in user since certain functions use that * value to store a record in the DB (like activity) * CRM-8180 */ @@ -435,6 +461,15 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $this->tempDirs = array(); } + /** + * Create default domain contacts for the two domains added during test class + * database population. + */ + public function createDomainContacts() { + $default_domain_contact = $this->organizationCreate(); + $second_domain_contact = $this->organizationCreate(); + } + /** * Common teardown functions for all unit tests */ @@ -442,16 +477,31 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { error_reporting(E_ALL & ~E_NOTICE); $session = CRM_Core_Session::singleton(); $session->set('userID', NULL); - $tablesToTruncate = array('civicrm_contact'); - $this->quickCleanup($tablesToTruncate); + + if ($this->tx) { + $this->tx->rollback()->commit(); + $this->tx = NULL; + + CRM_Core_Transaction::forceRollbackIfEnabled(); + \Civi\Core\Transaction\Manager::singleton(TRUE); + } else { + CRM_Core_Transaction::forceRollbackIfEnabled(); + \Civi\Core\Transaction\Manager::singleton(TRUE); + + $tablesToTruncate = array('civicrm_contact'); + $this->quickCleanup($tablesToTruncate); + $this->createDomainContacts(); + } + $this->cleanTempDirs(); $this->unsetExtensionSystem(); + $this->clearOutputBuffer(); } /** * FIXME: Maybe a better way to do it */ - function foreignKeyChecksOff() { + public function foreignKeyChecksOff() { self::$utils = new Utils($GLOBALS['mysql_host'], $GLOBALS['mysql_port'], $GLOBALS['mysql_user'], @@ -467,7 +517,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { return TRUE; } - function foreignKeyChecksOn() { + public function foreignKeyChecksOn() { // FIXME: might not be needed if previous fixme implemented } @@ -481,7 +531,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * that a DELETE occurred * @delete boolean True if we're checking that a DELETE action occurred. */ - function assertDBState($daoName, $id, $match, $delete = FALSE) { + public function assertDBState($daoName, $id, $match, $delete = FALSE) { if (empty($id)) { // adding this here since developers forget to check for an id // and hence we get the first value in the db @@ -527,7 +577,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { // Request a record from the DB by seachColumn+searchValue. Success if a record is found. /** - * @param $daoName + * @param string $daoName * @param $searchValue * @param $returnColumn * @param $searchColumn @@ -536,7 +586,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @return null|string * @throws PHPUnit_Framework_AssertionFailedError */ - function assertDBNotNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { + public function assertDBNotNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { if (empty($searchValue)) { $this->fail("empty value passed to assertDBNotNull"); } @@ -548,24 +598,24 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { // Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL. /** - * @param $daoName + * @param string $daoName * @param $searchValue * @param $returnColumn * @param $searchColumn * @param $message */ - function assertDBNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { + public function assertDBNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); $this->assertNull($value, $message); } // Request a record from the DB by id. Success if row not found. /** - * @param $daoName - * @param $id + * @param string $daoName + * @param int $id * @param null $message */ - function assertDBRowNotExist($daoName, $id, $message = NULL) { + public function assertDBRowNotExist($daoName, $id, $message = NULL) { $message = $message ? $message : "$daoName (#$id) should not exist"; $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE); $this->assertNull($value, $message); @@ -573,11 +623,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { // Request a record from the DB by id. Success if row not found. /** - * @param $daoName - * @param $id + * @param string $daoName + * @param int $id * @param null $message */ - function assertDBRowExist($daoName, $id, $message = NULL) { + public function assertDBRowExist($daoName, $id, $message = NULL) { $message = $message ? $message : "$daoName (#$id) should exist"; $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE); $this->assertEquals($id, $value, $message); @@ -585,7 +635,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { // Compare a single column value in a retrieved DB record to an expected value /** - * @param $daoName + * @param string $daoName * @param $searchValue * @param $returnColumn * @param $searchColumn @@ -601,11 +651,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { // Compare all values in a single retrieved DB record to an array of expected values /** - * @param $daoName - * @param $searchParams + * @param string $daoName + * @param array $searchParams * @param $expectedValues */ - function assertDBCompareValues($daoName, $searchParams, $expectedValues) { + public function assertDBCompareValues($daoName, $searchParams, $expectedValues) { //get the values from db $dbValues = array(); CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues); @@ -623,11 +673,12 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * Example: $this->assertSql(2, 'select count(*) from foo where foo.bar like "%1"', * array(1 => array("Whiz", "String"))); */ - function assertDBQuery($expected, $query, $params = array()) { + public function assertDBQuery($expected, $query, $params = array(), $message = '') { + if ($message) $message .= ': '; $actual = CRM_Core_DAO::singleValueQuery($query, $params); $this->assertEquals($expected, $actual, - sprintf('expected=[%s] actual=[%s] query=[%s]', - $expected, $actual, CRM_Core_DAO::composeQuery($query, $params, FALSE) + sprintf('%sexpected=[%s] actual=[%s] query=[%s]', + $message, $expected, $actual, CRM_Core_DAO::composeQuery($query, $params, FALSE) ) ); } @@ -639,7 +690,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param array $expected * @param array $actual */ - function assertTreeEquals($expected, $actual) { + public function assertTreeEquals($expected, $actual) { $e = array(); $a = array(); CRM_Utils_Array::flatten($expected, $e, '', ':::'); @@ -658,7 +709,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param int|float $tolerance * @param string $message */ - function assertApproxEquals($expected, $actual, $tolerance, $message = NULL) { + public function assertApproxEquals($expected, $actual, $tolerance, $message = NULL) { if ($message === NULL) { $message = sprintf("approx-equals: expected=[%.3f] actual=[%.3f] tolerance=[%.3f]", $expected, $actual, $tolerance); } @@ -672,7 +723,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @throws PHPUnit_Framework_AssertionFailedError */ - function assertAttributesEquals($expectedValues, $actualValues, $message = NULL) { + public function assertAttributesEquals($expectedValues, $actualValues, $message = NULL) { foreach ($expectedValues as $paramName => $paramValue) { if (isset($actualValues[$paramName])) { $this->assertEquals($paramValue, $actualValues[$paramName], "Value Mismatch On $paramName - value 1 is " . print_r($paramValue, TRUE) . " value 2 is " . print_r($actualValues[$paramName], TRUE) ); @@ -687,7 +738,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param $key * @param $list */ - function assertArrayKeyExists($key, &$list) { + public function assertArrayKeyExists($key, &$list) { $result = isset($list[$key]) ? TRUE : FALSE; $this->assertTrue($result, ts("%1 element exists?", array(1 => $key) @@ -698,7 +749,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param $key * @param $list */ - function assertArrayValueNotNull($key, &$list) { + public function assertArrayValueNotNull($key, &$list) { $this->assertArrayKeyExists($key, $list); $value = isset($list[$key]) ? $list[$key] : NULL; @@ -710,12 +761,12 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * check that api returned 'is_error' => 0 + * Check that api returned 'is_error' => 0 * else provide full message * @param array $apiResult api result * @param string $prefix extra test to add to message */ - function assertAPISuccess($apiResult, $prefix = '') { + public function assertAPISuccess($apiResult, $prefix = '') { if (!empty($prefix)) { $prefix .= ': '; } @@ -731,14 +782,14 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * check that api returned 'is_error' => 1 + * Check that api returned 'is_error' => 1 * else provide full message * * @param array $apiResult api result * @param string $prefix extra test to add to message * @param null $expectedError */ - function assertAPIFailure($apiResult, $prefix = '', $expectedError = NULL) { + public function assertAPIFailure($apiResult, $prefix = '', $expectedError = NULL) { if (!empty($prefix)) { $prefix .= ': '; } @@ -754,28 +805,27 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param $actual * @param string $message */ - function assertType($expected, $actual, $message = '') { + public function assertType($expected, $actual, $message = '') { return $this->assertInternalType($expected, $actual, $message); } /** - * check that a deleted item has been deleted + * Check that a deleted item has been deleted */ - function assertAPIDeleted($entity, $id) { + public function assertAPIDeleted($entity, $id) { $this->callAPISuccess($entity, 'getcount', array('id' => $id), 0); } /** - * check that api returned 'is_error' => 1 + * Check that api returned 'is_error' => 1 * else provide full message - * @param $result + * @param array $result * @param $expected * @param array $valuesToExclude * @param string $prefix extra test to add to message - * @internal param array $apiResult api result */ - function assertAPIArrayComparison($result, $expected, $valuesToExclude = array(), $prefix = '') { + public function assertAPIArrayComparison($result, $expected, $valuesToExclude = array(), $prefix = '') { $valuesToExclude = array_merge($valuesToExclude, array('debug', 'xdebug', 'sequential')); foreach ($valuesToExclude as $value) { if(isset($result[$value])) { @@ -793,10 +843,10 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @param $entity * @param $action - * @param $params + * @param array $params * @return array|int */ - function civicrm_api($entity, $action, $params) { + public function civicrm_api($entity, $action, $params) { return civicrm_api($entity, $action, $params); } @@ -814,7 +864,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return array|int */ - function callAPISuccess($entity, $action, $params, $checkAgainst = NULL) { + public function callAPISuccess($entity, $action, $params, $checkAgainst = NULL) { $params = array_merge(array( 'version' => $this->_apiversion, 'debug' => 1, @@ -851,7 +901,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return array|int */ - function callAPISuccessGetValue($entity, $params, $type = NULL) { + public function callAPISuccessGetValue($entity, $params, $type = NULL) { $params += array( 'version' => $this->_apiversion, 'debug' => 1, @@ -886,7 +936,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @throws Exception * @return array|int */ - function callAPISuccessGetSingle($entity, $params, $checkAgainst = NULL) { + public function callAPISuccessGetSingle($entity, $params, $checkAgainst = NULL) { $params += array( 'version' => $this->_apiversion, 'debug' => 1, @@ -919,7 +969,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * - array * - object */ - function callAPISuccessGetCount($entity, $params, $count = NULL) { + public function callAPISuccessGetCount($entity, $params, $count = NULL) { $params += array( 'version' => $this->_apiversion, 'debug' => 1, @@ -948,7 +998,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param string|null $actionName * @return array|int */ - function callAPIAndDocument($entity, $action, $params, $function, $file, $description = "", $subfile = NULL, $actionName = NULL){ + public function callAPIAndDocument($entity, $action, $params, $function, $file, $description = "", $subfile = NULL, $actionName = NULL){ $params['version'] = $this->_apiversion; $result = $this->callAPISuccess($entity, $action, $params); $this->documentMe($params, $result, $function, $file, $description, $subfile, $actionName); @@ -965,7 +1015,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param null $extraOutput * @return array|int */ - function callAPIFailure($entity, $action, $params, $expectedErrorMessage = NULL, $extraOutput = NULL) { + public function callAPIFailure($entity, $action, $params, $expectedErrorMessage = NULL, $extraOutput = NULL) { if (is_array($params)) { $params += array( 'version' => $this->_apiversion, @@ -996,7 +1046,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return int id of Organisation created */ - function organizationCreate($params = array(), $seq = 0) { + public function organizationCreate($params = array(), $seq = 0) { if (!$params) { $params = array(); } @@ -1012,7 +1062,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return int id of Individual created */ - function individualCreate($params = array(), $seq = 0) { + public function individualCreate($params = array(), $seq = 0) { $params = array_merge($this->sampleContact('Individual', $seq), $params); return $this->_contactCreate($params); } @@ -1025,7 +1075,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return int id of Household created */ - function householdCreate($params = array(), $seq = 0) { + public function householdCreate($params = array(), $seq = 0) { $params = array_merge($this->sampleContact('Household', $seq), $params); return $this->_contactCreate($params); } @@ -1038,7 +1088,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return array properties of sample contact (ie. $params for API call) */ - function sampleContact($contact_type, $seq = 0) { + public function sampleContact($contact_type, $seq = 0) { $samples = array( 'Individual' => array( // The number of values in each list need to be coprime numbers to not have duplicates @@ -1070,10 +1120,9 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { /** * Private helper function for calling civicrm_contact_add * - * @param $params + * @param array $params for civicrm_contact_add api function call * * @throws Exception - * @internal param \parameters $array for civicrm_contact_add api function call * * @return int id of Household created */ @@ -1086,11 +1135,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * @param $contactID + * @param int $contactID * * @return array|int */ - function contactDelete($contactID) { + public function contactDelete($contactID) { $params = array( 'id' => $contactID, 'skip_undelete' => 1, @@ -1108,11 +1157,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * @param $contactTypeId + * @param int $contactTypeId * * @throws Exception */ - function contactTypeDelete($contactTypeId) { + public function contactTypeDelete($contactTypeId) { require_once 'CRM/Contact/BAO/ContactType.php'; $result = CRM_Contact_BAO_ContactType::del($contactTypeId); if (!$result) { @@ -1125,7 +1174,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return mixed */ - function membershipTypeCreate($params = array()) { + public function membershipTypeCreate($params = array()) { CRM_Member_PseudoConstant::flush('membershipType'); CRM_Core_Config::clearDBCache(); $memberOfOrganization = $this->organizationCreate(); @@ -1151,11 +1200,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * @param $params + * @param array $params * * @return mixed */ - function contactMembershipCreate($params) { + public function contactMembershipCreate($params) { $pre = array( 'join_date' => '2007-01-21', 'start_date' => '2007-01-21', @@ -1174,19 +1223,18 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete Membership Type + * Delete Membership Type * - * @param $params - * @internal param int $membershipTypeID + * @param array $params */ - function membershipTypeDelete($params) { + public function membershipTypeDelete($params) { $this->callAPISuccess('MembershipType', 'Delete', $params); } /** - * @param $membershipID + * @param int $membershipID */ - function membershipDelete($membershipID) { + public function membershipDelete($membershipID) { $deleteParams = array('id' => $membershipID); $result = $this->callAPISuccess('Membership', 'Delete', $deleteParams); return; @@ -1197,7 +1245,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return mixed */ - function membershipStatusCreate($name = 'test member status') { + public function membershipStatusCreate($name = 'test member status') { $params['name'] = $name; $params['start_event'] = 'start_date'; $params['end_event'] = 'end_date'; @@ -1210,9 +1258,9 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * @param $membershipStatusID + * @param int $membershipStatusID */ - function membershipStatusDelete($membershipStatusID) { + public function membershipStatusDelete($membershipStatusID) { if (!$membershipStatusID) { return; } @@ -1225,7 +1273,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return mixed */ - function relationshipTypeCreate($params = array()) { + public function relationshipTypeCreate($params = array()) { $params = array_merge(array( 'name_a_b' => 'Relation 1 for relationship type create', 'name_b_a' => 'Relation 2 for relationship type create', @@ -1244,21 +1292,21 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete Relatinship Type + * Delete Relatinship Type * * @param int $relationshipTypeID */ - function relationshipTypeDelete($relationshipTypeID) { + public function relationshipTypeDelete($relationshipTypeID) { $params['id'] = $relationshipTypeID; $this->callAPISuccess('relationship_type', 'delete', $params); } /** - * @param null $params + * @param array $params * * @return mixed */ - function paymentProcessorTypeCreate($params = NULL) { + public function paymentProcessorTypeCreate($params = NULL) { if (is_null($params)) { $params = array( 'name' => 'API_Test_PP', @@ -1278,13 +1326,13 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create Participant + * Create Participant * * @param array $params array of contact id and event id values * * @return int $id of participant created */ - function participantCreate($params) { + public function participantCreate($params) { if(empty($params['contact_id'])){ $params['contact_id'] = $this->individualCreate(); } @@ -1307,11 +1355,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create Payment Processor + * Create Payment Processor * * @return object of Payment Processsor */ - function processorCreate() { + public function processorCreate() { $processorParams = array( 'domain_id' => 1, 'name' => 'Dummy', @@ -1328,12 +1376,12 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create contribution page + * Create contribution page * - * @param $params + * @param array $params * @return object of contribution page */ - function contributionPageCreate($params) { + public function contributionPageCreate($params) { $this->_pageParams = array( 'title' => 'Test Contribution Page', 'financial_type_id' => 1, @@ -1350,12 +1398,12 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create Tag + * Create Tag * * @param array $params * @return array result of created tag */ - function tagCreate($params = array()) { + public function tagCreate($params = array()) { $defaults = array( 'name' => 'New Tag3', 'description' => 'This is description for Our New Tag ', @@ -1367,11 +1415,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete Tag + * Delete Tag * * @param int $tagId id of the tag to be deleted */ - function tagDelete($tagId) { + public function tagDelete($tagId) { require_once 'api/api.php'; $params = array( 'tag_id' => $tagId, @@ -1387,21 +1435,19 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return bool */ - function entityTagAdd($params) { + public function entityTagAdd($params) { $result = $this->callAPISuccess('entity_tag', 'create', $params); return TRUE; } /** - * Function to create contribution + * Create contribution * * @param int $cID contact_id * - * @internal param int $cTypeID id of financial type - * * @return int id of created contribution */ - function pledgeCreate($cID) { + public function pledgeCreate($cID) { $params = array( 'contact_id' => $cID, 'pledge_create_date' => date('Ymd'), @@ -1422,12 +1468,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete contribution + * Delete contribution * - * @param $pledgeId - * @internal param int $contributionId + * @param int $pledgeId */ - function pledgeDelete($pledgeId) { + public function pledgeDelete($pledgeId) { $params = array( 'pledge_id' => $pledgeId, ); @@ -1435,7 +1480,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create contribution + * Create contribution * * @param int $cID contact_id * @param int $cTypeID id of financial type @@ -1446,7 +1491,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param bool $isFee * @return int id of created contribution */ - function contributionCreate($cID, $cTypeID = 1, $invoiceID = 67890, $trxnID = 12345, $paymentInstrumentID = 1, $isFee = TRUE) { + public function contributionCreate($cID, $cTypeID = 1, $invoiceID = 67890, $trxnID = 12345, $paymentInstrumentID = 1, $isFee = TRUE) { $params = array( 'domain_id' => 1, 'contact_id' => $cID, @@ -1472,16 +1517,16 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create online contribution + * Create online contribution * - * @param $params + * @param array $params * @param int $financialType id of financial type * * @param int $invoiceID * @param int $trxnID * @return int id of created contribution */ - function onlineContributionCreate($params, $financialType, $invoiceID = 67890, $trxnID = 12345) { + public function onlineContributionCreate($params, $financialType, $invoiceID = 67890, $trxnID = 12345) { $contribParams = array( 'contact_id' => $params['contact_id'], 'receive_date' => date('Ymd'), @@ -1499,13 +1544,13 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete contribution + * Delete contribution * * @param int $contributionId * * @return array|int */ - function contributionDelete($contributionId) { + public function contributionDelete($contributionId) { $params = array( 'contribution_id' => $contributionId, ); @@ -1514,13 +1559,13 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create an Event + * Create an Event * * @param array $params name-value pair for an event * * @return array $event */ - function eventCreate($params = array()) { + public function eventCreate($params = array()) { // if no contact was passed, make up a dummy event creator if (!isset($params['contact_id'])) { $params['contact_id'] = $this->_contactCreate(array( @@ -1553,13 +1598,13 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete event + * Delete event * * @param int $id ID of the event * * @return array|int */ - function eventDelete($id) { + public function eventDelete($id) { $params = array( 'event_id' => $id, ); @@ -1567,13 +1612,13 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete participant + * Delete participant * * @param int $participantID * * @return array|int */ - function participantDelete($participantID) { + public function participantDelete($participantID) { $params = array( 'id' => $participantID, ); @@ -1581,13 +1626,13 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create participant payment + * Create participant payment * - * @param $participantID - * @param null $contributionID + * @param int $participantID + * @param int $contributionID * @return int $id of created payment */ - function participantPaymentCreate($participantID, $contributionID = NULL) { + public function participantPaymentCreate($participantID, $contributionID = NULL) { //Create Participant Payment record With Values $params = array( 'participant_id' => $participantID, @@ -1599,11 +1644,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete participant payment + * Delete participant payment * * @param int $paymentID */ - function participantPaymentDelete($paymentID) { + public function participantPaymentDelete($paymentID) { $params = array( 'id' => $paymentID, ); @@ -1611,12 +1656,12 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to add a Location + * Add a Location * - * @param $contactID + * @param int $contactID * @return int location id of created location */ - function locationAdd($contactID) { + public function locationAdd($contactID) { $address = array( 1 => array( 'location_type' => 'New Location Type', @@ -1642,21 +1687,21 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete Locations of contact + * Delete Locations of contact * - * @params array $pamars parameters + * @param array $params parameters */ - function locationDelete($params) { - $result = $this->callAPISuccess('Location', 'delete', $params); + public function locationDelete($params) { + $this->callAPISuccess('Location', 'delete', $params); } /** - * Function to add a Location Type + * Add a Location Type * - * @param null $params + * @param array $params * @return int location id of created location */ - function locationTypeCreate($params = NULL) { + public function locationTypeCreate($params = NULL) { if ($params === NULL) { $params = array( 'name' => 'New Location Type', @@ -1676,25 +1721,23 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete a Location Type + * Delete a Location Type * * @param int location type id */ - function locationTypeDelete($locationTypeId) { + public function locationTypeDelete($locationTypeId) { $locationType = new CRM_Core_DAO_LocationType(); $locationType->id = $locationTypeId; $locationType->delete(); } /** - * Function to add a Group - * - * @params array to add group + * Add a Group * * @param array $params * @return int groupId of created group */ - function groupCreate($params = array()) { + public function groupCreate($params = array()) { $params = array_merge(array( 'name' => 'Test Group 1', 'domain_id' => 1, @@ -1721,7 +1764,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param array $params * @return int groupId of created group */ - function groupContactCreate($groupID, $totalCount = 10) { + public function groupContactCreate($groupID, $totalCount = 10) { $params = array('group_id' => $groupID); for ($i=1; $i <= $totalCount; $i++) { $contactID = $this->individualCreate(); @@ -1738,12 +1781,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete a Group + * Delete a Group * - * @param $gid - * @internal param int $id + * @param int $gid */ - function groupDelete($gid) { + public function groupDelete($gid) { $params = array( 'id' => $gid, @@ -1756,7 +1798,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * Create a UFField * @param array $params */ - function uFFieldCreate($params = array()) { + public function uFFieldCreate($params = array()) { $params = array_merge(array( 'uf_group_id' => 1, 'field_name' => 'first_name', @@ -1772,12 +1814,12 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to add a UF Join Entry + * Add a UF Join Entry * - * @param null $params + * @param array $params * @return int $id of created UF Join */ - function ufjoinCreate($params = NULL) { + public function ufjoinCreate($params = NULL) { if ($params === NULL) { $params = array( 'is_active' => 1, @@ -1793,11 +1835,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete a UF Join Entry + * Delete a UF Join Entry * * @param array with missing uf_group_id */ - function ufjoinDelete($params = NULL) { + public function ufjoinDelete($params = NULL) { if ($params === NULL) { $params = array( 'is_active' => 1, @@ -1813,11 +1855,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create Group for a contact + * Create Group for a contact * * @param int $contactId */ - function contactGroupCreate($contactId) { + public function contactGroupCreate($contactId) { $params = array( 'contact_id.1' => $contactId, 'group_id' => 1, @@ -1827,12 +1869,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete Group for a contact + * Delete Group for a contact * - * @param $contactId - * @internal param array $params + * @param int $contactId */ - function contactGroupDelete($contactId) { + public function contactGroupDelete($contactId) { $params = array( 'contact_id.1' => $contactId, 'group_id' => 1, @@ -1841,13 +1882,12 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create Activity + * Create Activity * - * @param null $params + * @param array $params * @return array|int - * @internal param int $contactId */ - function activityCreate($params = NULL) { + public function activityCreate($params = NULL) { if ($params === NULL) { $individualSourceID = $this->individualCreate(); @@ -1885,35 +1925,33 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to create an activity type + * Create an activity type * - * @params array $params parameters + * @param array $params parameters */ - function activityTypeCreate($params) { + public function activityTypeCreate($params) { $result = $this->callAPISuccess('ActivityType', 'create', $params); return $result; } /** - * Function to delete activity type + * Delete activity type * - * @params Integer $activityTypeId id of the activity type + * @param int $activityTypeId id of the activity type */ - function activityTypeDelete($activityTypeId) { + public function activityTypeDelete($activityTypeId) { $params['activity_type_id'] = $activityTypeId; $result = $this->callAPISuccess('ActivityType', 'delete', $params); return $result; } /** - * Function to create custom group + * Create custom group * * @param array $params * @return array|int - * @internal param string $className - * @internal param string $title name of custom group */ - function customGroupCreate($params = array()) { + public function customGroupCreate($params = array()) { $defaults = array( 'title' => 'new custom group', 'extends' => 'Contact', @@ -1935,10 +1973,10 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * existing function doesn't allow params to be over-ridden so need a new one + * Existing function doesn't allow params to be over-ridden so need a new one * this one allows you to only pass in the params you want to change */ - function CustomGroupCreateByParams($params = array()) { + public function CustomGroupCreateByParams($params = array()) { $defaults = array( 'title' => "API Custom Group", 'extends' => 'Contact', @@ -1953,7 +1991,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { /** * Create custom group with multi fields */ - function CustomGroupMultipleCreateByParams($params = array()) { + public function CustomGroupMultipleCreateByParams($params = array()) { $defaults = array( 'style' => 'Tab', 'is_multiple' => 1, @@ -1965,7 +2003,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { /** * Create custom group with multi fields */ - function CustomGroupMultipleCreateWithFields($params = array()) { + public function CustomGroupMultipleCreateWithFields($params = array()) { // also need to pass on $params['custom_field'] if not set but not in place yet $ids = array(); $customGroup = $this->CustomGroupMultipleCreateByParams($params); @@ -1989,12 +2027,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * participant:testCreateWithCustom for how to use this * * @param string $function __FUNCTION__ - * @param $filename - * @internal param string $file __FILE__ + * @param $filename string $file __FILE__ * * @return array $ids ids of created objects */ - function entityCustomGroupWithSingleFieldCreate($function, $filename) { + public function entityCustomGroupWithSingleFieldCreate($function, $filename) { $params = array('title' => $function); $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8); $params['extends'] = $entity ? $entity : 'Contact'; @@ -2006,26 +2043,24 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete custom group + * Delete custom group * * @param int $customGroupID * * @return array|int */ - function customGroupDelete($customGroupID) { + public function customGroupDelete($customGroupID) { $params['id'] = $customGroupID; return $this->callAPISuccess('custom_group', 'delete', $params); } /** - * Function to create custom field + * Create custom field * * @param array $params (custom_group_id) is required * @return array|int - * @internal param string $name name of custom field - * @internal param int $apiversion API version to use */ - function customFieldCreate($params) { + public function customFieldCreate($params) { $params = array_merge(array( 'label' => 'Custom Field', 'data_type' => 'String', @@ -2046,27 +2081,25 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete custom field + * Delete custom field * * @param int $customFieldID * * @return array|int */ - function customFieldDelete($customFieldID) { + public function customFieldDelete($customFieldID) { $params['id'] = $customFieldID; return $this->callAPISuccess('custom_field', 'delete', $params); } /** - * Function to create note + * Create note * - * @params array $params name-value pair for an event - * - * @param $cId + * @param int $cId * @return array $note */ - function noteCreate($cId) { + public function noteCreate($cId) { $params = array( 'entity_table' => 'civicrm_contact', 'entity_id' => $cId, @@ -2082,7 +2115,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { /** * Enable CiviCampaign Component */ - function enableCiviCampaign() { + public function enableCiviCampaign() { CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); // force reload of config object $config = CRM_Core_Config::singleton(TRUE, TRUE); @@ -2103,7 +2136,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param string $subfile name for subfile - if this is completed the example will be put in a subfolder (named by the entity) * @param string $action - optional action - otherwise taken from function name */ - function documentMe($params, $result, $function, $filename, $description = "", $subfile = NULL, $action = NULL) { + public function documentMe($params, $result, $function, $filename, $description = "", $subfile = NULL, $action = NULL) { if (defined('DONT_DOCUMENT_TEST_CONFIG') && DONT_DOCUMENT_TEST_CONFIG) { return; } @@ -2211,11 +2244,10 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * Tidy up examples array so that fields that change often ..don't * and debug related fields are unset * - * @param $result + * @param array $result * - * @internal param array $params */ - function tidyExampleResult(&$result){ + public function tidyExampleResult(&$result){ if(!is_array($result)) { return; } @@ -2287,24 +2319,25 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { } /** - * Function to delete note + * Delete note * - * @params int $noteID + * @param array $params * + * @return array|int */ - function noteDelete($params) { + public function noteDelete($params) { return $this->callAPISuccess('Note', 'delete', $params); } /** - * Function to create custom field with Option Values + * Create custom field with Option Values * * @param array $customGroup * @param string $name name of custom field * * @return array|int */ - function customFieldOptionValueCreate($customGroup, $name) { + public function customFieldOptionValueCreate($customGroup, $name) { $fieldParams = array( 'custom_group_id' => $customGroup['id'], 'name' => 'test_custom_group', @@ -2341,7 +2374,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * * @return bool */ - function confirmEntitiesDeleted($entities) { + public function confirmEntitiesDeleted($entities) { foreach ($entities as $entity) { $result = $this->callAPISuccess($entity, 'Get', array()); @@ -2356,7 +2389,10 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { * @param $tablesToTruncate * @param bool $dropCustomValueTables */ - function quickCleanup($tablesToTruncate, $dropCustomValueTables = FALSE) { + public function quickCleanup($tablesToTruncate, $dropCustomValueTables = FALSE) { + if ($this->tx) { + throw new Exception("CiviUnitTestCase: quickCleanup() is not compatible with useTransaction()"); + } if ($dropCustomValueTables) { $tablesToTruncate[] = 'civicrm_custom_group'; $tablesToTruncate[] = 'civicrm_custom_field'; @@ -2391,7 +2427,7 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) /** * Clean up financial entities after financial tests (so we remember to get all the tables :-)) */ - function quickCleanUpFinancialEntities() { + public function quickCleanUpFinancialEntities() { $tablesToTruncate = array( 'civicrm_contribution', 'civicrm_contribution_soft', @@ -2422,7 +2458,7 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) CRM_Member_BAO_Membership::createRelatedMemberships($var, $var, TRUE); } - function restoreDefaultPriceSetConfig() { + public function restoreDefaultPriceSetConfig() { 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)"); CRM_Core_DAO::executeQuery("INSERT INTO `civicrm_price_field_value` (`id`, `price_field_id`, `name`, `label`, `description`, `amount`, `count`, `max_value`, `weight`, `membership_type_id`, `membership_num_terms`, `is_default`, `is_active`, `financial_type_id`, `deductible_amount`) VALUES (1, 1, 'contribution_amount', 'Contribution Amount', NULL, '1', NULL, NULL, 1, NULL, NULL, 0, 1, 1, 0.00)"); } @@ -2437,15 +2473,15 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * */ /** - * @param $params - * @param $id + * @param array $params + * @param int $id * @param $entity * @param int $delete * @param string $errorText * * @throws Exception */ - function getAndCheck($params, $id, $entity, $delete = 1, $errorText = '') { + public function getAndCheck($params, $id, $entity, $delete = 1, $errorText = '') { $result = $this->callAPISuccessGetSingle($entity, array( 'id' => $id, @@ -2507,23 +2543,23 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) } /** - * Function to get formatted values in the actual and expected result + * Get formatted values in the actual and expected result * @param array $actual actual calculated values * @param array $expected expected values * */ - function checkArrayEquals(&$actual, &$expected) { + public function checkArrayEquals(&$actual, &$expected) { self::unsetId($actual); self::unsetId($expected); $this->assertEquals($actual, $expected); } /** - * Function to unset the key 'id' from the array + * Unset the key 'id' from the array * @param array $unformattedArray The array from which the 'id' has to be unset * */ - static function unsetId(&$unformattedArray) { + public static function unsetId(&$unformattedArray) { $formattedArray = array(); if (array_key_exists('id', $unformattedArray)) { unset($unformattedArray['id']); @@ -2553,7 +2589,7 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * 'php_path' Set to TRUE to use the default, FALSE or "" to disable support, or a string path to use another path * 'template_path' Set to TRUE to use the default, FALSE or "" to disable support, or a string path to use another path */ - function customDirectories($customDirs) { + public function customDirectories($customDirs) { require_once 'CRM/Core/Config.php'; $config = CRM_Core_Config::singleton(); @@ -2584,13 +2620,13 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * @param string $prefix * @return string $string */ - function createTempDir($prefix = 'test-') { + public function createTempDir($prefix = 'test-') { $tempDir = CRM_Utils_File::tempdir($prefix); $this->tempDirs[] = $tempDir; return $tempDir; } - function cleanTempDirs() { + public function cleanTempDirs() { if (!is_array($this->tempDirs)) { // fix test errors where this is not set return; @@ -2605,14 +2641,14 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) /** * Temporarily replace the singleton extension with a different one */ - function setExtensionSystem(CRM_Extension_System $system) { + public function setExtensionSystem(CRM_Extension_System $system) { if ($this->origExtensionSystem == NULL) { $this->origExtensionSystem = CRM_Extension_System::singleton(); } CRM_Extension_System::setSingleton($this->origExtensionSystem); } - function unsetExtensionSystem() { + public function unsetExtensionSystem() { if ($this->origExtensionSystem !== NULL) { CRM_Extension_System::setSingleton($this->origExtensionSystem); $this->origExtensionSystem = NULL; @@ -2627,7 +2663,7 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * @param $extras * @return void */ - function setMockSettingsMetaData($extras) { + public function setMockSettingsMetaData($extras) { CRM_Core_BAO_Setting::$_cache = array(); $this->callAPISuccess('system','flush', array()); CRM_Core_BAO_Setting::$_cache = array(); @@ -2644,9 +2680,9 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) } /** - * @param $name + * @param string $name */ - function financialAccountDelete($name) { + public function financialAccountDelete($name) { $financialAccount = new CRM_Financial_DAO_FinancialAccount(); $financialAccount->name = $name; if($financialAccount->find(TRUE)) { @@ -2657,22 +2693,11 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) } } - /** - * Use $ids as an instruction to do test cleanup - */ - function deleteFromIDSArray() { - foreach ($this->ids as $entity => $ids) { - foreach ($ids as $id) { - $this->callAPISuccess($entity, 'delete', array('id' => $id)); - } - } - } - /** * FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together * (NB unclear if this is still required) */ - function _sethtmlGlobals() { + public function _sethtmlGlobals() { $GLOBALS['_HTML_QuickForm_registered_rules'] = array( 'required' => array( 'html_quickform_rule_required', @@ -2850,7 +2875,7 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * $this->_permissionedGroup = $this->groupCreate(array('title' => 'pick-me-active', 'is_active' => 1, 'name' => 'pick-me-active')); * */ - function setupACL() { + public function setupACL() { global $_REQUEST; $_REQUEST = $this->_params; @@ -2906,9 +2931,9 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) } /** - * alter default price set so that the field numbers are not all 1 (hiding errors) + * Alter default price set so that the field numbers are not all 1 (hiding errors) */ - function offsetDefaultPriceSet() { + public function offsetDefaultPriceSet() { $contributionPriceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount')); $firstID = $contributionPriceSet['id']; $this->callAPISuccess('price_set', 'create', array('id' => $contributionPriceSet['id'], 'is_active' => 0, 'name' => 'old')); @@ -2934,26 +2959,26 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * There is another function to this effect on the PaypalPro test but it appears to be silently failing * & the best protection agains that is the functions this class affords */ - function paymentProcessorCreate($params = array()) { + public function paymentProcessorCreate($params = array()) { $params = array_merge(array( - 'name' => 'demo', - 'domain_id' => CRM_Core_Config::domainID(), - 'payment_processor_type_id' => 'PayPal', - 'is_active' => 1, - 'is_default' => 0, - 'is_test' => 1, - 'user_name' => 'sunil._1183377782_biz_api1.webaccess.co.in', - 'password' => '1183377788', - 'signature' => 'APixCoQ-Zsaj-u3IH7mD5Do-7HUqA9loGnLSzsZga9Zr-aNmaJa3WGPH', - 'url_site' => 'https://www.sandbox.paypal.com/', - 'url_api' => 'https://api-3t.sandbox.paypal.com/', - 'url_button' => 'https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif', - 'class_name' => 'Payment_PayPalImpl', - 'billing_mode' => 3, - 'financial_type_id' => 1, - ), - $params); - if (!is_numeric($params['payment_processor_type_id'])) { + 'name' => 'demo', + 'domain_id' => CRM_Core_Config::domainID(), + 'payment_processor_type_id' => 'PayPal', + 'is_active' => 1, + 'is_default' => 0, + 'is_test' => 1, + 'user_name' => 'sunil._1183377782_biz_api1.webaccess.co.in', + 'password' => '1183377788', + 'signature' => 'APixCoQ-Zsaj-u3IH7mD5Do-7HUqA9loGnLSzsZga9Zr-aNmaJa3WGPH', + 'url_site' => 'https://www.sandbox.paypal.com/', + 'url_api' => 'https://api-3t.sandbox.paypal.com/', + 'url_button' => 'https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif', + 'class_name' => 'Payment_PayPalImpl', + 'billing_mode' => 3, + 'financial_type_id' => 1, + ), + $params); + if(!is_numeric($params['payment_processor_type_id'])) { // really the api should handle this through getoptions but it's not exactly api call so lets just sort it //here $params['payment_processor_type_id'] = $this->callAPISuccess('payment_processor_type', 'getvalue', array( @@ -2968,7 +2993,7 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) /** * Set up initial recurring payment allowing subsequent IPN payments */ - function setupRecurringPaymentProcessorTransaction() { + public function setupRecurringPaymentProcessorTransaction() { $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( 'contact_id' => $this->_contactID, 'amount' => 1000, @@ -2994,22 +3019,36 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) } /** - * 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 + * 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 */ - function setupMembershipRecurringPaymentProcessorTransaction() { - $this->setupRecurringPaymentProcessorTransaction(); + public function setupMembershipRecurringPaymentProcessorTransaction() { $this->ids['membership_type'] = $this->membershipTypeCreate(); + //create a contribution so our membership & contribution don't both have id = 1 + $this->contributionCreate($this->_contactID, 1, 'abcd', '345j'); + $this->setupRecurringPaymentProcessorTransaction(); + $this->ids['membership'] = $this->callAPISuccess('membership', 'create', array( 'contact_id' => $this->_contactID, 'membership_type_id' => $this->ids['membership_type'], 'contribution_recur_id' => $this->_contributionRecurID, - 'api.membership_payment.create' => array('contribution_id' => $this->_contributionID), 'format.only_id' => TRUE, )); - CRM_Core_DAO::executeQuery( - "UPDATE civicrm_line_item SET entity_table = 'civicrm_membership', entity_id = {$this->ids['membership']} - WHERE entity_id = {$this->_contributionID} AND entity_table = 'civicrm_contribution'" - ); + //CRM-15055 creates line items we don't want so get rid of them so we can set up our own line items + CRM_Core_DAO::executeQuery("TRUNCATE civicrm_line_item"); + + $this->callAPISuccess('line_item', 'create', array( + 'entity_table' => 'civicrm_membership', + 'entity_id' => $this->ids['membership'], + 'contribution_id' => $this->_contributionID, + 'label' => 'General', + 'qty' => 1, + 'unit_price' => 200, + 'line_total' => 200, + 'financial_type_id' => 1, + 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', array('return' => 'id', 'label' => 'Membership Amount')), + 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', array('return' => 'id', 'label' => 'General')), + )); + $this->callAPISuccess('membership_payment', 'create', array('contribution_id' => $this->_contributionID, 'membership_id' => $this->ids['membership'])); } /** @@ -3017,7 +3056,7 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * * @throws Exception */ - function CiviUnitTestCase_fatalErrorHandler($message) { + public function CiviUnitTestCase_fatalErrorHandler($message) { throw new Exception("{$message['message']}: {$message['code']}"); } @@ -3025,12 +3064,13 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * Helper function to create new mailing * @return mixed */ - function createMailing() { + public function createMailing() { $params = array( 'subject' => 'maild' . rand(), 'body_text' => 'bdkfhdskfhduew', 'name' => 'mailing name' . rand(), 'created_id' => 1, + 'api.mailing_job.create' => 0, ); $result = $this->callAPISuccess('Mailing', 'create', $params); @@ -3041,11 +3081,39 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' ) * Helper function to delete mailing * @param $id */ - function deleteMailing($id) { + public function deleteMailing($id) { $params = array( 'id' => $id, ); $this->callAPISuccess('Mailing', 'delete', $params); } + + /** + * Wrap the entire test case in a transaction + * + * Only subsequent DB statements will be wrapped in TX -- this cannot + * retroactively wrap old DB statements. Therefore, it makes sense to + * call this at the beginning of setUp(). + * + * Note: Recall that TRUNCATE and ALTER will force-commit transactions, so + * this option does not work with, e.g., custom-data. + * + * WISHLIST: Monitor SQL queries in unit-tests and generate an exception + * if TRUNCATE or ALTER is called while using a transaction. + * + * @param bool $nest whether to use nesting or reference-counting + */ + public function useTransaction($nest = TRUE) { + if (!$this->tx) { + $this->tx = new CRM_Core_Transaction($nest); + $this->tx->rollback(); + } + } + + public function clearOutputBuffer() { + while (ob_get_level() > 0) { + ob_end_clean(); + } + } }