From 8554bd5cea7b5361fe55037536366d2006c28f18 Mon Sep 17 00:00:00 2001 From: Brienne Kordis Date: Fri, 26 Aug 2022 12:39:55 -0400 Subject: [PATCH] modified rewrite token func to return full array testingReplacToken --- .../SearchDisplay/AbstractRunAction.php | 9 +- .../SearchDisplay/AbstractRunActionTest.php | 241 ++++++++++++++++++ 2 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 ext/search_kit/tests/phpunit/api/v4/SearchDisplay/AbstractRunActionTest.php diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php index 5df9ef6d1e..ef5e8a39c7 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php @@ -728,14 +728,19 @@ abstract class AbstractRunAction extends \Civi\Api4\Generic\AbstractAction { * @param int $index * @return string */ - private function replaceTokens($tokenExpr, $data, $format, $index = 0) { + private function replaceTokens($tokenExpr, $data, $format, $index = NULL) { if (strpos(($tokenExpr ?? ''), '[') !== FALSE) { foreach ($this->getTokens($tokenExpr) as $token) { $val = $data[$token] ?? NULL; if (isset($val) && $format === 'view') { $val = $this->formatViewValue($token, $val); } - $replacement = is_array($val) ? $val[$index] ?? '' : $val; + if (!(is_null($index))) { + $replacement = is_array($val) ? $val[$index] ?? '' : $val; + } + else { + $replacement = implode(', ', (array) $val); + } // A missing token value in a url invalidates it if ($format === 'url' && (!isset($replacement) || $replacement === '')) { return NULL; diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/AbstractRunActionTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/AbstractRunActionTest.php new file mode 100644 index 0000000000..053d51d33b --- /dev/null +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/AbstractRunActionTest.php @@ -0,0 +1,241 @@ +installMe(__DIR__) + ->apply(); + } + + public function testReplaceTokens() { + CustomGroup::create(FALSE) + ->addValue('title', 'Foods') + ->addValue('name', 'Foods') + ->execute(); + + CustomField::create(FALSE) + ->addValue('custom_group_id.name', 'Foods') + ->addValue('label', 'I Like') + ->addValue('serialize:name', \CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND) + ->addValue('html_type', 'Autocomplete-Select') + ->addValue('data_type', 'String') + ->addValue('option_values', ['Pie', 'Cake', 'Anything you make']) + ->execute(); + + Contact::create(FALSE) + ->addValue('contact_type', 'Individual') + ->addValue('first_name', 'Lee') + ->addValue('last_name', 'Morse') + ->addValue('Foods.I_Like', [0, 1, 2]) + ->execute(); + + $entity = 'SearchDisplay'; + $action = 'run'; + $params = array( + 'return' => 'page:1', + 'savedSearch' => + array( + 'id' => 1, + 'name' => 'Multi_Select_Test', + 'label' => 'Multi Select Test', + 'form_values' => NULL, + 'mapping_id' => NULL, + 'search_custom_id' => NULL, + 'api_entity' => 'Contact', + 'api_params' => + array( + 'version' => 4, + 'select' => + array( + 0 => 'display_name', + 1 => 'Foods.I_Like:label', + ), + 'orderBy' => + array(), + 'where' => + array( + 0 => + array( + 0 => 'contact_type:name', + 1 => '=', + 2 => 'Individual', + ), + 1 => + array( + 0 => 'Foods.I_Like:name', + 1 => 'IS NOT EMPTY', + ), + ), + 'groupBy' => + array( + 0 => 'id', + ), + 'having' => + array(), + ), + 'created_id' => 203, + 'modified_id' => 203, + 'expires_date' => NULL, + 'created_date' => '2022-08-12 13:49:17', + 'modified_date' => '2022-08-12 17:18:24', + 'description' => NULL, + 'tag_id' => + array(), + 'groups' => + array(), + 'displays' => + array( + 0 => + array( + 'id' => 1, + 'name' => 'Contacts_Table_1', + 'label' => 'Contacts Table 1', + 'saved_search_id' => 1, + 'type' => 'table', + 'settings' => + array( + 'actions' => TRUE, + 'limit' => 50, + 'classes' => + array( + 0 => 'table', + 1 => 'table-striped', + ), + 'pager' => + array(), + 'placeholder' => 5, + 'sort' => + array( + 0 => + array( + 0 => 'sort_name', + 1 => 'ASC', + ), + ), + 'columns' => + array( + 0 => + array( + 'type' => 'field', + 'key' => 'display_name', + 'dataType' => 'String', + 'label' => 'Display Name', + 'sortable' => TRUE, + 'link' => + array( + 'path' => '', + 'entity' => 'Contact', + 'action' => 'view', + 'join' => '', + 'target' => '_blank', + ), + 'title' => 'View Contact', + ), + 1 => + array( + 'type' => 'field', + 'key' => 'Foods.I_Like:label', + 'dataType' => 'String', + 'label' => 'Foods: I Like', + 'sortable' => TRUE, + 'rewrite' => '[Foods.I_Like:label]', + ), + ), + ), + 'acl_bypass' => FALSE, + ), + ), + ), + 'display' => + array( + 'id' => 1, + 'name' => 'Contacts_Table_1', + 'label' => 'Contacts Table 1', + 'saved_search_id' => 1, + 'type' => 'table', + 'settings' => + array( + 'actions' => TRUE, + 'limit' => 50, + 'classes' => + array( + 0 => 'table', + 1 => 'table-striped', + ), + 'pager' => + array(), + 'placeholder' => 5, + 'sort' => + array( + 0 => + array( + 0 => 'sort_name', + 1 => 'ASC', + ), + ), + 'columns' => + array( + 0 => + array( + 'type' => 'field', + 'key' => 'display_name', + 'dataType' => 'String', + 'label' => 'Display Name', + 'sortable' => TRUE, + 'link' => + array( + 'path' => '', + 'entity' => 'Contact', + 'action' => 'view', + 'join' => '', + 'target' => '_blank', + ), + 'title' => 'View Contact', + ), + 1 => + array( + 'type' => 'field', + 'key' => 'Foods.I_Like:label', + 'dataType' => 'String', + 'label' => 'Foods: I Like', + 'sortable' => TRUE, + 'rewrite' => '[Foods.I_Like:label]', + ), + ), + ), + 'acl_bypass' => FALSE, + ), + 'sort' => + array( + 0 => + array( + 0 => 'sort_name', + 1 => 'ASC', + ), + ), + 'limit' => 50, + 'seed' => 1660599799146, + 'filters' => + array(), + 'afform' => NULL, + 'debug' => TRUE, + 'checkPermissions' => TRUE, + ); + $result = civicrm_api4($entity, $action, $params); + $resultData = $result[0]['data']['Foods.I_Like:label']; + $this->assertTrue(implode(', ', $resultData) === $result[0]['columns'][1]['val']); + } + +} -- 2.25.1