dev/core#2079 [REF] clean up call to apiQuery
authoreileen <emcnaughton@wikimedia.org>
Fri, 2 Oct 2020 20:59:47 +0000 (09:59 +1300)
committereileen <emcnaughton@wikimedia.org>
Sat, 3 Oct 2020 00:52:46 +0000 (13:52 +1300)
    This fixes
    1) fixes apiQuery to be called statically as it is a static function
    2) casts results directly to detail

This is the copy & paste source of https://github.com/civicrm/civicrm-core/pull/18664 I guess....

CRM/Contact/Form/Task/Label.php
tests/phpunit/CRM/Contact/Form/Task/PrintMailingLabelTest.php

index 92252cad4b5439a99dff6d7c8959767dfe876b0a..724b6ef786455a5962b1f77876effdb9e505e3ea 100644 (file)
@@ -96,7 +96,6 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task {
    */
   public function postProcess($params = NULL) {
     $fv = $params ?: $this->controller->exportValues($this->_name);
-    $config = CRM_Core_Config::singleton();
     $locName = NULL;
     //get the address format sequence from the config file
     $mailingFormat = Civi::settings()->get('mailing_format');
@@ -146,15 +145,12 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task {
       $returnProperties['last_name'] = 1;
     }
 
-    $individualFormat = FALSE;
-
     /*
      * CRM-8338: replace ids of household members with the id of their household
      * so we can merge labels by household.
      */
     if (isset($fv['merge_same_household'])) {
       $this->mergeContactIdsByHousehold();
-      $individualFormat = TRUE;
     }
 
     //get the contacts information
@@ -200,13 +196,12 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task {
 
     //get the total number of contacts to fetch from database.
     $numberofContacts = count($this->_contactIds);
-    $query = new CRM_Contact_BAO_Query($params, $returnProperties);
-    $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts, TRUE, FALSE, TRUE, CRM_Contact_BAO_Query::MODE_CONTACTS, NULL, $primaryLocationOnly);
+    [$details] = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts, TRUE, FALSE, TRUE, CRM_Contact_BAO_Query::MODE_CONTACTS, NULL, $primaryLocationOnly);
     $messageToken = CRM_Utils_Token::getTokens($mailingFormat);
 
-    // $details[0] is an array of [ contactID => contactDetails ]
+    // $details is an array of [ contactID => contactDetails ]
     // also get all token values
-    CRM_Utils_Hook::tokenValues($details[0],
+    CRM_Utils_Hook::tokenValues($details,
       $this->_contactIds,
       NULL,
       $messageToken,
@@ -224,11 +219,11 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task {
 
     foreach ($this->_contactIds as $value) {
       foreach ($custom as $cfID) {
-        if (isset($details[0][$value]["custom_{$cfID}"])) {
-          $details[0][$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::displayValue($details[0][$value]["custom_{$cfID}"], $cfID);
+        if (isset($details[$value]["custom_{$cfID}"])) {
+          $details[$value]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::displayValue($details[$value]["custom_{$cfID}"], $cfID);
         }
       }
-      $contact = $details['0'][$value] ?? NULL;
+      $contact = $details[$value] ?? NULL;
 
       if (is_a($contact, 'CRM_Core_Error')) {
         return NULL;
@@ -271,7 +266,7 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task {
                   'im',
                   'openid',
                 ])) {
-                  if ($k == 'im') {
+                  if ($k === 'im') {
                     $rows[$value][$k] = $v['1']['name'];
                   }
                   else {
index 9c01a73a9d3f686bc72055f288e1fb429d6e87ff..e07f17c5d095d01fbda47431a06de4f95276d439 100644 (file)
@@ -15,7 +15,7 @@
   */
 class CRM_Contact_Form_Task_PrintMailingLabelTest extends CiviUnitTestCase {
 
-  protected $_contactIds = NULL;
+  protected $_contactIds;
 
   protected function setUp() {
     parent::setUp();
@@ -25,6 +25,14 @@ class CRM_Contact_Form_Task_PrintMailingLabelTest extends CiviUnitTestCase {
     ];
   }
 
+  /**
+   * Clean up after test.
+   */
+  protected function tearDown() {
+    unset($this->_contactIds);
+    parent::tearDown();
+  }
+
   /**
    * core/issue-1158: Test the mailing label rows contain the primary addresses when location_type_id = none (as primary) is chosen in form
    */
@@ -38,7 +46,7 @@ class CRM_Contact_Form_Task_PrintMailingLabelTest extends CiviUnitTestCase {
       // create the non-primary address first
       foreach (['non-primary', 'primary'] as $flag) {
         // @TODO: bug - this doesn't affect as if its the first and only address created for a contact then it always consider it as primary
-        $isPrimary = ($flag == 'primary');
+        $isPrimary = ($flag === 'primary');
         $streetName = substr(sha1(rand()), 0, 7);
         $addresses[$contactID][$flag] = $this->callAPISuccess('Address', 'create', [
           'street_name' => $streetName,
@@ -53,7 +61,7 @@ class CRM_Contact_Form_Task_PrintMailingLabelTest extends CiviUnitTestCase {
           'sequential' => 1,
         ])['values'][0];
 
-        if ($flag == 'non-primary') {
+        if ($flag === 'non-primary') {
           $addresses[$contactID][$flag] = $this->callAPISuccess('Address', 'create', [
             'is_primary' => $isPrimary,
             'id' => $addresses[$contactID][$flag]['id'],