Merge pull request #5526 from mlutfy/4.6-crm6131
[civicrm-core.git] / tests / phpunit / CiviTest / CiviUnitTestCase.php
index 26f40ea2e68aeb00569ec671c8fdd021b19af72d..10388ebc58e0565ec0d343635be4acd2ea7279e1 100755 (executable)
@@ -70,28 +70,28 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    */
   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();
 
@@ -155,7 +155,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   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
@@ -230,7 +230,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   *  Create database connection for this instance
+   *  Create database connection for this instance.
    *
    *  Initialize the test database if it hasn't been initialized
    *
@@ -252,7 +252,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   *  Required implementation of abstract method
+   *  Required implementation of abstract method.
    */
   protected function getDataSet() {
   }
@@ -375,13 +375,10 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
 
     // 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();
@@ -424,6 +421,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
     // 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;
@@ -441,7 +443,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * 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';
@@ -468,7 +470,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
    */
@@ -494,7 +496,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * 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() {
@@ -503,7 +505,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   *  Common teardown functions for all unit tests
+   *  Common teardown functions for all unit tests.
    */
   protected function tearDown() {
     error_reporting(E_ALL & ~E_NOTICE);
@@ -557,7 +559,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * 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.
@@ -674,7 +676,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * 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
@@ -691,7 +693,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * 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
@@ -706,7 +708,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * 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
@@ -749,7 +751,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Assert that two numbers are approximately equal
+   * Assert that two numbers are approximately equal.
    *
    * @param int|float $expected
    * @param int|float $actual
@@ -764,9 +766,11 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
+   * Assert attributes are equal.
+   *
    * @param $expectedValues
    * @param $actualValues
-   * @param null $message
+   * @param string $message
    *
    * @throws PHPUnit_Framework_AssertionFailedError
    */
@@ -808,8 +812,8 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Check that api returned 'is_error' => 0
-   * else provide full message
+   * Check that api returned 'is_error' => 0.
+   *
    * @param array $apiResult
    *   Api result.
    * @param string $prefix
@@ -831,8 +835,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Check that api returned 'is_error' => 1
-   * else provide full message
+   * Check that api returned 'is_error' => 1.
    *
    * @param array $apiResult
    *   Api result.
@@ -861,7 +864,8 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Check that a deleted item has been deleted
+   * Check that a deleted item has been deleted.
+   *
    * @param $entity
    * @param $id
    */
@@ -905,7 +909,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * wrap api functions
+   * wrap api functions.
    * so we can ensure they succeed & throw exceptions without litterering the test with checks
    *
    * @param string $entity
@@ -1037,7 +1041,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * 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
@@ -1048,19 +1052,19 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
    * @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
@@ -1146,7 +1150,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Helper function for getting sample contact properties
+   * Helper function for getting sample contact properties.
    *
    * @param string $contact_type
    *   enum contact type: Individual, Organization
@@ -1192,7 +1196,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * 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.
@@ -1293,7 +1297,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete Membership Type
+   * Delete Membership Type.
    *
    * @param array $params
    */
@@ -1360,7 +1364,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete Relatinship Type
+   * Delete Relatinship Type.
    *
    * @param int $relationshipTypeID
    */
@@ -1394,7 +1398,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create Participant
+   * Create Participant.
    *
    * @param array $params
    *   Array of contact id and event id values.
@@ -1425,7 +1429,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create Payment Processor
+   * Create Payment Processor.
    *
    * @return CRM_Financial_DAO_PaymentProcessor
    *   instance of Payment Processsor
@@ -1447,7 +1451,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create contribution page
+   * Create contribution page.
    *
    * @param array $params
    * @return array
@@ -1470,7 +1474,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create Tag
+   * Create Tag.
    *
    * @param array $params
    * @return array
@@ -1488,7 +1492,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete Tag
+   * Delete Tag.
    *
    * @param int $tagId
    *   Id of the tag to be deleted.
@@ -1517,7 +1521,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create contribution
+   * Create contribution.
    *
    * @param int $cID
    *   Contact_id.
@@ -1546,7 +1550,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete contribution
+   * Delete contribution.
    *
    * @param int $pledgeId
    */
@@ -1558,7 +1562,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create contribution
+   * Create contribution.
    *
    * @param int $cID
    *   Contact_id.
@@ -1598,7 +1602,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create online contribution
+   * Create online contribution.
    *
    * @param array $params
    * @param int $financialType
@@ -1627,7 +1631,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete contribution
+   * Delete contribution.
    *
    * @param int $contributionId
    *
@@ -1642,7 +1646,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create an Event
+   * Create an Event.
    *
    * @param array $params
    *   Name-value pair for an event.
@@ -1682,7 +1686,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete event
+   * Delete event.
    *
    * @param int $id
    *   ID of the event.
@@ -1697,7 +1701,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete participant
+   * Delete participant.
    *
    * @param int $participantID
    *
@@ -1711,7 +1715,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create participant payment
+   * Create participant payment.
    *
    * @param int $participantID
    * @param int $contributionID
@@ -1730,7 +1734,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete participant payment
+   * Delete participant payment.
    *
    * @param int $paymentID
    */
@@ -1742,7 +1746,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Add a Location
+   * Add a Location.
    *
    * @param int $contactID
    * @return int
@@ -1774,7 +1778,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete Locations of contact
+   * Delete Locations of contact.
    *
    * @param array $params
    *   Parameters.
@@ -1784,7 +1788,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Add a Location Type
+   * Add a Location Type.
    *
    * @param array $params
    * @return CRM_Core_DAO_LocationType
@@ -1810,7 +1814,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete a Location Type
+   * Delete a Location Type.
    *
    * @param int $locationTypeId
    */
@@ -1821,7 +1825,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Add a Group
+   * Add a Group.
    *
    * @param array $params
    * @return int
@@ -1847,7 +1851,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
 
 
   /**
-   * Function to add a Group
+   * Function to add a Group.
    *
    * @params array to add group
    *
@@ -1873,7 +1877,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete a Group
+   * Delete a Group.
    *
    * @param int $gid
    */
@@ -1887,7 +1891,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create a UFField
+   * Create a UFField.
    * @param array $params
    */
   public function uFFieldCreate($params = array()) {
@@ -1906,7 +1910,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Add a UF Join Entry
+   * Add a UF Join Entry.
    *
    * @param array $params
    * @return int
@@ -1928,7 +1932,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete a UF Join Entry
+   * Delete a UF Join Entry.
    *
    * @param array $params
    *   with missing uf_group_id
@@ -1949,7 +1953,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create Group for a contact
+   * Create Group for a contact.
    *
    * @param int $contactId
    */
@@ -1963,7 +1967,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete Group for a contact
+   * Delete Group for a contact.
    *
    * @param int $contactId
    */
@@ -1976,7 +1980,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create Activity
+   * Create Activity.
    *
    * @param array $params
    * @return array|int
@@ -2019,7 +2023,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create an activity type
+   * Create an activity type.
    *
    * @param array $params
    *   Parameters.
@@ -2030,7 +2034,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete activity type
+   * Delete activity type.
    *
    * @param int $activityTypeId
    *   Id of the activity type.
@@ -2042,7 +2046,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create custom group
+   * Create custom group.
    *
    * @param array $params
    * @return array|int
@@ -2090,7 +2094,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create custom group with multi fields
+   * Create custom group with multi fields.
    * @param array $params
    * @return array|int
    */
@@ -2104,7 +2108,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create custom group with multi fields
+   * Create custom group with multi fields.
    * @param array $params
    * @return array
    */
@@ -2162,7 +2166,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete custom group
+   * Delete custom group.
    *
    * @param int $customGroupID
    *
@@ -2174,7 +2178,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create custom field
+   * Create custom field.
    *
    * @param array $params
    *   (custom_group_id) is required.
@@ -2198,7 +2202,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete custom field
+   * Delete custom field.
    *
    * @param int $customFieldID
    *
@@ -2211,7 +2215,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create note
+   * Create note.
    *
    * @param int $cId
    * @return array
@@ -2230,7 +2234,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Enable CiviCampaign Component
+   * Enable CiviCampaign Component.
    */
   public function enableCiviCampaign() {
     CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
@@ -2242,123 +2246,77 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
 
   /**
    * Create test generated example in api/v3/examples.
+   *
    * To turn this off (e.g. on the server) set
    * 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);
     }
@@ -2442,7 +2400,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Delete note
+   * Delete note.
    *
    * @param array $params
    *
@@ -2453,7 +2411,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
   }
 
   /**
-   * Create custom field with Option Values
+   * Create custom field with Option Values.
    *
    * @param array $customGroup
    * @param string $name
@@ -2674,7 +2632,7 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
   }
 
   /**
-   * 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
@@ -2748,7 +2706,7 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
   }
 
   /**
-   * Generate a temporary folder
+   * Generate a temporary folder.
    *
    * @param string $prefix
    * @return string
@@ -2772,7 +2730,7 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
   }
 
   /**
-   * 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) {
@@ -3097,7 +3055,7 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
   }
 
   /**
-   * 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
@@ -3137,7 +3095,7 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
   }
 
   /**
-   * 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(
@@ -3216,16 +3174,15 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
   }
 
   /**
-   * 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);
@@ -3233,7 +3190,7 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
   }
 
   /**
-   * Helper function to delete mailing
+   * Helper function to delete mailing.
    * @param $id
    */
   public function deleteMailing($id) {
@@ -3245,7 +3202,7 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
   }
 
   /**
-   * 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
@@ -3273,4 +3230,20 @@ AND    ( TABLE_NAME LIKE 'civicrm_value_%' )
     }
   }
 
+  /**
+   * @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'),
+    ));
+  }
+
 }