From 64c57b7207e3af5c73ac4df38310c01e6d4754bc Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 3 Oct 2020 09:59:47 +1300 Subject: [PATCH] dev/core#2079 [REF] clean up call to apiQuery 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 | 19 +++++++------------ .../Form/Task/PrintMailingLabelTest.php | 14 +++++++++++--- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/CRM/Contact/Form/Task/Label.php b/CRM/Contact/Form/Task/Label.php index 92252cad4b54..724b6ef78645 100644 --- a/CRM/Contact/Form/Task/Label.php +++ b/CRM/Contact/Form/Task/Label.php @@ -96,7 +96,6 @@ public function setDefaultValues() { */ 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 @@ public function postProcess($params = NULL) { $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 @@ public function postProcess($params = NULL) { //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 @@ public function postProcess($params = NULL) { 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 @@ public function postProcess($params = NULL) { 'im', 'openid', ])) { - if ($k == 'im') { + if ($k === 'im') { $rows[$value][$k] = $v['1']['name']; } else { diff --git a/tests/phpunit/CRM/Contact/Form/Task/PrintMailingLabelTest.php b/tests/phpunit/CRM/Contact/Form/Task/PrintMailingLabelTest.php index 9c01a73a9d3f..e07f17c5d095 100644 --- a/tests/phpunit/CRM/Contact/Form/Task/PrintMailingLabelTest.php +++ b/tests/phpunit/CRM/Contact/Form/Task/PrintMailingLabelTest.php @@ -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 @@ protected function setUp() { ]; } + /** + * 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 @@ public function testMailingLabel() { // 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 @@ public function testMailingLabel() { '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'],