*/
protected $_apiversion = API_LATEST_VERSION;
/**
- * Database has been initialized
+ * Database has been initialized.
*
* @var boolean
*/
private static $dbInit = FALSE;
/**
- * Database connection
+ * Database connection.
*
* @var PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
protected $_dbconn;
/**
- * The database name
+ * The database name.
*
* @var string
*/
static protected $_dbName;
/**
- * Track tables we have modified during a test
+ * Track tables we have modified during a test.
*/
protected $_tablesToTruncate = array();
protected $origExtensionSystem;
/**
- * Constructor
+ * Constructor.
*
* Because we are overriding the parent class constructor, we
* need to show the same arguments as exist in the constructor of
}
/**
- * Create database connection for this instance
+ * Create database connection for this instance.
*
* Initialize the test database if it hasn't been initialized
*
}
/**
- * Required implementation of abstract method
+ * Required implementation of abstract method.
*/
protected function getDataSet() {
}
// also set this global hack
$GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array();
-
- $env = new CRM_Utils_Check_Env();
- CRM_Utils_Check::singleton()->assertValid($env->checkMysqlTime());
}
/**
- * Common setup functions for all unit tests
+ * Common setup functions for all unit tests.
*/
protected function setUp() {
$session = CRM_Core_Session::singleton();
// reset all the caches
CRM_Utils_System::flushCache();
+ // Make sure the DB connection is setup properly
+ $config->userSystem->setMySQLTimeZone();
+ $env = new CRM_Utils_Check_Env();
+ CRM_Utils_Check::singleton()->assertValid($env->checkMysqlTime());
+
// clear permissions stub to not check permissions
$config = CRM_Core_Config::singleton();
$config->userPermissionClass->permissions = NULL;
}
/**
- * Read everything from the datasets directory and insert into the db
+ * Read everything from the datasets directory and insert into the db.
*/
public function loadAllFixtures() {
$fixturesDir = __DIR__ . '/../../fixtures';
}
/**
- * 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
*/
}
/**
- * Create default domain contacts for the two domains added during test class
+ * Create default domain contacts for the two domains added during test class.
* database population.
*/
public function createDomainContacts() {
}
/**
- * Common teardown functions for all unit tests
+ * Common teardown functions for all unit tests.
*/
protected function tearDown() {
error_reporting(E_ALL & ~E_NOTICE);
}
/**
- * Generic function to compare expected values after an api call to retrieved
+ * Generic function to compare expected values after an api call to retrieved.
* DB values.
*
* @daoName string DAO Name of object we're evaluating.
}
/**
- * Compare a single column value in a retrieved DB record to an expected value
+ * Compare a single column value in a retrieved DB record to an expected value.
* @param string $daoName
* @param $searchValue
* @param $returnColumn
}
/**
- * Compare all values in a single retrieved DB record to an array of expected values
+ * Compare all values in a single retrieved DB record to an array of expected values.
* @param string $daoName
* @param array $searchParams
* @param $expectedValues
}
/**
- * Assert that a SQL query returns a given value
+ * Assert that a SQL query returns a given value.
*
* The first argument is an expected value. The remaining arguments are passed
* to CRM_Core_DAO::singleValueQuery
}
/**
- * wrap api functions
+ * wrap api functions.
* so we can ensure they succeed & throw exceptions without litterering the test with checks
*
* @param string $entity
}
/**
- * This function exists to wrap api functions
+ * This function exists to wrap api functions.
* so we can ensure they succeed, generate and example & throw exceptions without litterering the test with checks
*
* @param string $entity
* @param string $file
* Pass this in to create a generated example.
* @param string $description
- * @param string|null $subfile
- * @param string|null $actionName
+ * @param string|null $exampleName
+ *
* @return array|int
*/
- public function callAPIAndDocument($entity, $action, $params, $function, $file, $description = "", $subfile = NULL, $actionName = NULL) {
+ public function callAPIAndDocument($entity, $action, $params, $function, $file, $description = "", $exampleName = NULL) {
$params['version'] = $this->_apiversion;
$result = $this->callAPISuccess($entity, $action, $params);
- $this->documentMe($params, $result, $function, $file, $description, $subfile, $actionName);
+ $this->documentMe($entity, $action, $params, $result, $function, $file, $description, $exampleName);
return $result;
}
/**
- * This function exists to wrap api functions
+ * This function exists to wrap api functions.
* so we can ensure they fail where expected & throw exceptions without litterering the test with checks
* @param string $entity
* @param string $action
}
/**
- * Helper function for getting sample contact properties
+ * Helper function for getting sample contact properties.
*
* @param string $contact_type
* enum contact type: Individual, Organization
}
/**
- * Private helper function for calling civicrm_contact_add
+ * Private helper function for calling civicrm_contact_add.
*
* @param array $params
* For civicrm_contact_add api function call.
}
/**
- * Delete Membership Type
+ * Delete Membership Type.
*
* @param array $params
*/
}
/**
- * Delete Relatinship Type
+ * Delete Relatinship Type.
*
* @param int $relationshipTypeID
*/
}
/**
- * Create Participant
+ * Create Participant.
*
* @param array $params
* Array of contact id and event id values.
}
/**
- * Create Payment Processor
+ * Create Payment Processor.
*
* @return CRM_Financial_DAO_PaymentProcessor
* instance of Payment Processsor
}
/**
- * Create contribution page
+ * Create contribution page.
*
* @param array $params
* @return array
}
/**
- * Create Tag
+ * Create Tag.
*
* @param array $params
* @return array
}
/**
- * Delete Tag
+ * Delete Tag.
*
* @param int $tagId
* Id of the tag to be deleted.
}
/**
- * Create contribution
+ * Create contribution.
*
* @param int $cID
* Contact_id.
}
/**
- * Delete contribution
+ * Delete contribution.
*
* @param int $pledgeId
*/
}
/**
- * Create contribution
+ * Create contribution.
*
* @param int $cID
* Contact_id.
}
/**
- * Create online contribution
+ * Create online contribution.
*
* @param array $params
* @param int $financialType
}
/**
- * Delete contribution
+ * Delete contribution.
*
* @param int $contributionId
*
}
/**
- * Create an Event
+ * Create an Event.
*
* @param array $params
* Name-value pair for an event.
}
/**
- * Delete event
+ * Delete event.
*
* @param int $id
* ID of the event.
}
/**
- * Delete participant
+ * Delete participant.
*
* @param int $participantID
*
}
/**
- * Create participant payment
+ * Create participant payment.
*
* @param int $participantID
* @param int $contributionID
}
/**
- * Delete participant payment
+ * Delete participant payment.
*
* @param int $paymentID
*/
}
/**
- * Add a Location
+ * Add a Location.
*
* @param int $contactID
* @return int
}
/**
- * Delete Locations of contact
+ * Delete Locations of contact.
*
* @param array $params
* Parameters.
}
/**
- * Add a Location Type
+ * Add a Location Type.
*
* @param array $params
* @return CRM_Core_DAO_LocationType
}
/**
- * Delete a Location Type
+ * Delete a Location Type.
*
* @param int $locationTypeId
*/
}
/**
- * Add a Group
+ * Add a Group.
*
* @param array $params
* @return int
/**
- * Function to add a Group
+ * Function to add a Group.
*
* @params array to add group
*
}
/**
- * Delete a Group
+ * Delete a Group.
*
* @param int $gid
*/
}
/**
- * Create a UFField
+ * Create a UFField.
* @param array $params
*/
public function uFFieldCreate($params = array()) {
}
/**
- * Add a UF Join Entry
+ * Add a UF Join Entry.
*
* @param array $params
* @return int
}
/**
- * Delete a UF Join Entry
+ * Delete a UF Join Entry.
*
* @param array $params
* with missing uf_group_id
}
/**
- * Create Group for a contact
+ * Create Group for a contact.
*
* @param int $contactId
*/
}
/**
- * Delete Group for a contact
+ * Delete Group for a contact.
*
* @param int $contactId
*/
}
/**
- * Create Activity
+ * Create Activity.
*
* @param array $params
* @return array|int
}
/**
- * Create an activity type
+ * Create an activity type.
*
* @param array $params
* Parameters.
}
/**
- * Delete activity type
+ * Delete activity type.
*
* @param int $activityTypeId
* Id of the activity type.
}
/**
- * Create custom group
+ * Create custom group.
*
* @param array $params
* @return array|int
}
/**
- * Create custom group with multi fields
+ * Create custom group with multi fields.
* @param array $params
* @return array|int
*/
}
/**
- * Create custom group with multi fields
+ * Create custom group with multi fields.
* @param array $params
* @return array
*/
}
/**
- * Delete custom group
+ * Delete custom group.
*
* @param int $customGroupID
*
}
/**
- * Create custom field
+ * Create custom field.
*
* @param array $params
* (custom_group_id) is required.
}
/**
- * Delete custom field
+ * Delete custom field.
*
* @param int $customFieldID
*
}
/**
- * Create note
+ * Create note.
*
* @param int $cId
* @return array
}
/**
- * Enable CiviCampaign Component
+ * Enable CiviCampaign Component.
*/
public function enableCiviCampaign() {
CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
* define(DONT_DOCUMENT_TEST_CONFIG ,1);
* in your settings file
*
+ * @param string $entity
+ * @param string $action
* @param array $params
* Array as passed to civicrm_api function.
* @param array $result
* Array as received from the civicrm_api function.
- * @param string $function
+ * @param string $testFunction
* Calling function - generally __FUNCTION__.
- * @param string $filename
+ * @param string $testFile
* Called from file - generally __FILE__.
* @param string $description
* Descriptive text for the example file.
- * @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.
+ * @param string $exampleName
+ * Name for this example file (CamelCase) - if ommitted the action name will be substituted.
*/
- public function documentMe($params, $result, $function, $filename, $description = "", $subfile = NULL, $action = NULL) {
+ private function documentMe($entity, $action, $params, $result, $testFunction, $testFile, $description = "", $exampleName = NULL) {
if (defined('DONT_DOCUMENT_TEST_CONFIG') && DONT_DOCUMENT_TEST_CONFIG) {
return;
}
- $entity = substr(basename($filename), 0, strlen(basename($filename)) - 8);
- //todo - this is a bit cludgey
- if (empty($action)) {
- if (strstr($function, 'Create')) {
- $action = empty($action) ? 'create' : $action;
- $entityAction = 'Create';
- }
- elseif (strstr($function, 'GetSingle')) {
- $action = empty($action) ? 'getsingle' : $action;
- $entityAction = 'GetSingle';
- }
- elseif (strstr($function, 'GetValue')) {
- $action = empty($action) ? 'getvalue' : $action;
- $entityAction = 'GetValue';
- }
- elseif (strstr($function, 'GetCount')) {
- $action = empty($action) ? 'getcount' : $action;
- $entityAction = 'GetCount';
- }
- elseif (strstr($function, 'GetFields')) {
- $action = empty($action) ? 'getfields' : $action;
- $entityAction = 'GetFields';
- }
- elseif (strstr($function, 'GetList')) {
- $action = empty($action) ? 'getlist' : $action;
- $entityAction = 'GetList';
- }
- elseif (strstr($function, 'GetActions')) {
- $action = empty($action) ? 'getactions' : $action;
- $entityAction = 'GetActions';
- }
- elseif (strstr($function, 'Get')) {
- $action = empty($action) ? 'get' : $action;
- $entityAction = 'Get';
- }
- elseif (strstr($function, 'Delete')) {
- $action = empty($action) ? 'delete' : $action;
- $entityAction = 'Delete';
- }
- elseif (strstr($function, 'Update')) {
- $action = empty($action) ? 'update' : $action;
- $entityAction = 'Update';
- }
- elseif (strstr($function, 'Subscribe')) {
- $action = empty($action) ? 'subscribe' : $action;
- $entityAction = 'Subscribe';
- }
- elseif (strstr($function, 'Submit')) {
- $action = empty($action) ? 'submit' : $action;
- $entityAction = 'Submit';
- }
- elseif (strstr($function, 'Apply')) {
- $action = empty($action) ? 'apply' : $action;
- $entityAction = 'Apply';
- }
- elseif (strstr($function, 'Replace')) {
- $action = empty($action) ? 'replace' : $action;
- $entityAction = 'Replace';
+ $entity = _civicrm_api_get_camel_name($entity);
+ $action = strtolower($action);
+
+ if (empty($exampleName)) {
+ // Attempt to convert lowercase action name to CamelCase.
+ // This is clunky/imperfect due to the convention of all lowercase actions.
+ $exampleName = CRM_Utils_String::convertStringToCamel($action);
+ $knownPrefixes = array(
+ 'Get',
+ 'Set',
+ 'Create',
+ 'Update',
+ 'Send',
+ );
+ foreach ($knownPrefixes as $prefix) {
+ if (strpos($exampleName, $prefix) === 0 && $prefix != $exampleName) {
+ $exampleName[strlen($prefix)] = strtoupper($exampleName[strlen($prefix)]);
+ }
}
}
- else {
- $entityAction = ucwords($action);
- }
$this->tidyExampleResult($result);
if (isset($params['version'])) {
unset($params['version']);
}
- // a cleverer person than me would do it in a single regex
- if (strstr($entity, 'UF')) {
- $fnPrefix = strtolower(preg_replace('/(?<! )(?<!^)(?<=UF)[A-Z]/', '_$0', $entity));
- }
- else {
- $fnPrefix = strtolower(preg_replace('/(?<! )(?<!^)[A-Z]/', '_$0', $entity));
+ // Format multiline description as array
+ $desc = array();
+ if (is_string($description) && strlen($description)) {
+ foreach (explode("\n", $description) as $line) {
+ $desc[] = trim($line);
+ }
}
$smarty = CRM_Core_Smarty::singleton();
- $smarty->assign('testfunction', $function);
- $function = $fnPrefix . "_" . strtolower($action);
- $smarty->assign('function', $function);
- $smarty->assign('fnPrefix', $fnPrefix);
+ $smarty->assign('testFunction', $testFunction);
+ $smarty->assign('function', _civicrm_api_get_entity_name_from_camel($entity) . "_$action");
$smarty->assign('params', $params);
$smarty->assign('entity', $entity);
- $smarty->assign('filename', basename($filename));
- $smarty->assign('description', $description);
+ $smarty->assign('testFile', basename($testFile));
+ $smarty->assign('description', $desc);
$smarty->assign('result', $result);
-
$smarty->assign('action', $action);
- if (empty($subfile)) {
- $subfile = $entityAction;
- }
+
if (file_exists('../tests/templates/documentFunction.tpl')) {
if (!is_dir("../api/v3/examples/$entity")) {
mkdir("../api/v3/examples/$entity");
}
- $f = fopen("../api/v3/examples/$entity/$subfile.php", "w+b");
+ $f = fopen("../api/v3/examples/$entity/$exampleName.php", "w+b");
fwrite($f, $smarty->fetch('../tests/templates/documentFunction.tpl'));
fclose($f);
}
}
/**
- * Delete note
+ * Delete note.
*
* @param array $params
*
}
/**
- * Create custom field with Option Values
+ * Create custom field with Option Values.
*
* @param array $customGroup
* @param string $name
}
/**
- * 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
}
/**
- * Generate a temporary folder
+ * Generate a temporary folder.
*
* @param string $prefix
* @return string
}
/**
- * Temporarily replace the singleton extension with a different one
+ * Temporarily replace the singleton extension with a different one.
* @param \CRM_Extension_System $system
*/
public function setExtensionSystem(CRM_Extension_System $system) {
}
/**
- * Create an instance of the paypal processor
+ * Create an instance of the paypal processor.
* @todo this isn't a great place to put it - but really it belongs on a class that extends
* this parent class & we don't have a structure for that yet
* There is another function to this effect on the PaypalPro test but it appears to be silently failing
}
/**
- * Set up initial recurring payment allowing subsequent IPN payments
+ * Set up initial recurring payment allowing subsequent IPN payments.
*/
public function setupRecurringPaymentProcessorTransaction() {
$contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
}
/**
- * Helper function to create new mailing
+ * Helper function to create new mailing.
* @return mixed
*/
public function createMailing() {
$params = array(
'subject' => 'maild' . rand(),
- 'body_text' => 'bdkfhdskfhduew',
+ 'body_text' => 'bdkfhdskfhduew{domain.address}{action.optOutUrl}',
'name' => 'mailing name' . rand(),
'created_id' => 1,
- 'api.mailing_job.create' => 0,
);
$result = $this->callAPISuccess('Mailing', 'create', $params);
}
/**
- * Helper function to delete mailing
+ * Helper function to delete mailing.
* @param $id
*/
public function deleteMailing($id) {
}
/**
- * Wrap the entire test case in a transaction
+ * 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
}
}
+ /**
+ * @param $exists
+ * @param array $apiResult
+ */
+ protected function assertAttachmentExistence($exists, $apiResult) {
+ $fileId = $apiResult['id'];
+ $this->assertTrue(is_numeric($fileId));
+ $this->assertEquals($exists, file_exists($apiResult['values'][$fileId]['path']));
+ $this->assertDBQuery($exists ? 1 : 0, 'SELECT count(*) FROM civicrm_file WHERE id = %1', array(
+ 1 => array($fileId, 'Int'),
+ ));
+ $this->assertDBQuery($exists ? 1 : 0, 'SELECT count(*) FROM civicrm_entity_file WHERE id = %1', array(
+ 1 => array($fileId, 'Int'),
+ ));
+ }
+
}