From 5e66fa9263fbba2eac5e35145d7e81fd76fd09e0 Mon Sep 17 00:00:00 2001 From: colemanw Date: Tue, 22 Aug 2023 16:15:55 -0400 Subject: [PATCH] SearchKit - Fix tasks when ID field is not selected --- .../SearchDisplay/AbstractRunAction.php | 4 +- .../api/v4/SearchDisplay/SearchRunTest.php | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php index 34438e515f9c..1debd9896fb2 100644 --- a/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php +++ b/ext/search_kit/Civi/Api4/Action/SearchDisplay/AbstractRunAction.php @@ -147,8 +147,8 @@ protected function formatResult(iterable $result): array { 'columns' => $columns, 'cssClass' => implode(' ', $style), ]; - if (isset($data[$keyName])) { - $row['key'] = $data[$keyName]; + if (isset($record[$keyName])) { + $row['key'] = $record[$keyName]; } $rows[] = $row; } diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php index 2790bf143d8d..c55317d581bf 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchDisplay/SearchRunTest.php @@ -543,6 +543,8 @@ public function testInPlaceEditAndCreate() { ]; $result = civicrm_api4('SearchDisplay', 'run', $params); + $this->assertEquals($contacts[0], $result[0]['key']); + // Contact 1 first name can be updated $this->assertEquals('One', $result[0]['columns'][0]['val']); $this->assertEquals($contacts[0], $result[0]['columns'][0]['edit']['record']['id']); @@ -1507,6 +1509,7 @@ public function testEditableContactFields() { // Second Individual $expectedFirstNameEdit['record']['id'] = $contact[1]['id']; $expectedFirstNameEdit['value'] = NULL; + $this->assertEquals($contact[1]['id'], $result[1]['key']); $this->assertEquals($expectedFirstNameEdit, $result[1]['columns'][0]['edit']); $this->assertTrue(!isset($result[1]['columns'][1]['edit'])); $this->assertTrue(!isset($result[1]['columns'][2]['edit'])); @@ -1743,6 +1746,49 @@ public function testContactTypeIcons(): void { $this->assertEquals('fa-star', $result[2]['columns'][0]['icons'][0]['class']); } + public function testKeyIsReturned(): void { + $id = $this->createTestRecord('Email')['id']; + $params = [ + 'checkPermissions' => FALSE, + 'return' => 'page:1', + 'savedSearch' => [ + 'api_entity' => 'Email', + 'api_params' => [ + 'version' => 4, + 'select' => ['email'], + 'where' => [ + ['id', 'IN', [$id]], + ], + ], + ], + 'display' => [ + 'type' => 'table', + 'label' => '', + 'settings' => [ + 'limit' => 20, + 'pager' => TRUE, + 'actions' => TRUE, + 'columns' => [ + [ + 'key' => 'email', + 'label' => 'Email', + 'dataType' => 'String', + 'type' => 'field', + ], + ], + 'sort' => [ + ['id', 'ASC'], + ], + ], + ], + 'afform' => NULL, + ]; + + $result = civicrm_api4('SearchDisplay', 'run', $params); + $this->assertCount(1, $result); + $this->assertEquals($id, $result[0]['key']); + } + /** * Returns all contacts in VIEW mode but only specified contact for EDIT. *