CRM-13807 - Fix last to commit to getBAO and add unit test
authorColeman Watts <coleman@civicrm.org>
Thu, 21 Nov 2013 04:25:15 +0000 (20:25 -0800)
committerColeman Watts <coleman@civicrm.org>
Thu, 21 Nov 2013 04:25:15 +0000 (20:25 -0800)
api/v3/utils.php
tests/phpunit/api/v3/UtilsTest.php

index 4c7d369978a39270b0d6e900543b73c1b469e1b5..b0b2900b1ebdaaeddd6b6b814997b8bcbc3b46e8 100644 (file)
@@ -308,8 +308,8 @@ function _civicrm_api3_get_DAO($name) {
 function _civicrm_api3_get_BAO($name) {
   $dao = _civicrm_api3_get_DAO($name);
   $bao = str_replace("DAO", "BAO", $dao);
-  $file = str_replace('_', '/', $dao) . '.php';
-  return file_exists($file) ? $bao : $dao;
+  $file = strtr($bao, '_', '/') . '.php';
+  return stream_resolve_include_path($file) ? $bao : $dao;
 }
 
 /**
index 8cbd5fa4488697844488e73be2dfc28f5ae5596c..9d903b3ffb3fdfe06da4e958a5708e2373888047 100644 (file)
@@ -175,33 +175,37 @@ class api_v3_UtilsTest extends CiviUnitTestCase {
    * Test GET DAO function returns DAO
    */
   function testGetDAO() {
-    $DAO = _civicrm_api3_get_DAO('civicrm_api3_custom_group_get');
-    $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO);
-    $DAO = _civicrm_api3_get_DAO('custom_group');
-    $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO);
-    $DAO = _civicrm_api3_get_DAO('CustomGroup');
-    $this->assertEquals('CRM_Core_DAO_CustomGroup', $DAO);
-    $DAO = _civicrm_api3_get_DAO('civicrm_api3_custom_field_get');
-    $this->assertEquals('CRM_Core_DAO_CustomField', $DAO);
-    $DAO = _civicrm_api3_get_DAO('civicrm_api3_survey_get');
-    $this->assertEquals('CRM_Campaign_DAO_Survey', $DAO);
-    $DAO = _civicrm_api3_get_DAO('civicrm_api3_pledge_payment_get');
-    $this->assertEquals('CRM_Pledge_DAO_PledgePayment', $DAO);
-    $DAO = _civicrm_api3_get_DAO('civicrm_api3_website_get');
-    $this->assertEquals('CRM_Core_DAO_Website', $DAO);
-    $DAO = _civicrm_api3_get_DAO('Membership');
-    $this->assertEquals('CRM_Member_DAO_Membership', $DAO);
+    $params = array(
+      'civicrm_api3_custom_group_get' => 'CRM_Core_DAO_CustomGroup',
+      'custom_group' => 'CRM_Core_DAO_CustomGroup',
+      'CustomGroup' => 'CRM_Core_DAO_CustomGroup',
+      'civicrm_api3_custom_field_get' => 'CRM_Core_DAO_CustomField',
+      'civicrm_api3_survey_get' => 'CRM_Campaign_DAO_Survey',
+      'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_DAO_PledgePayment',
+      'civicrm_api3_website_get' => 'CRM_Core_DAO_Website',
+      'Membership' => 'CRM_Member_DAO_Membership',
+    );
+    foreach ($params as $input => $expected) {
+      $result = _civicrm_api3_get_DAO($input);
+      $this->assertEquals($expected, $result);
+    }
   }
   /*
-   * Test GET DAO function returns DAO
+   * Test GET BAO function returns BAO when it exists
    */
   function testGetBAO() {
-    $BAO = _civicrm_api3_get_BAO('civicrm_api3_website_get');
-    $this->assertEquals('CRM_Core_BAO_Website', $BAO);
-    $BAO = _civicrm_api3_get_BAO('civicrm_api3_survey_get');
-    $this->assertEquals('CRM_Campaign_BAO_Survey', $BAO);
-    $BAO = _civicrm_api3_get_BAO('civicrm_api3_pledge_payment_get');
-    $this->assertEquals('CRM_Pledge_BAO_PledgePayment', $BAO);
+    $params = array(
+      'civicrm_api3_website_get' => 'CRM_Core_BAO_Website',
+      'civicrm_api3_survey_get' => 'CRM_Campaign_BAO_Survey',
+      'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_BAO_PledgePayment',
+      'Household' => 'CRM_Contact_BAO_Contact',
+      // Note this one DOES NOT have a BAO so we expect to fall back on returning the DAO
+      'mailing_group' => 'CRM_Mailing_DAO_MailingGroup',
+    );
+    foreach ($params as $input => $expected) {
+      $result = _civicrm_api3_get_BAO($input);
+      $this->assertEquals($expected, $result);
+    }
   }
 
   function test_civicrm_api3_validate_fields() {