From a43deb747f3d0fab276f923140c85c24aef76284 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 11 Nov 2019 14:51:50 +1300 Subject: [PATCH] Add unit test for searching by participant_status_id & switch to generic function --- CRM/Contact/BAO/Query.php | 37 ++++++++++++++++--- CRM/Event/BAO/Query.php | 18 ++++----- tests/phpunit/CRM/Event/BAO/QueryTest.php | 45 +++++++++++++++++++++++ 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 6e73904a0c..84407bc144 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -3574,7 +3574,7 @@ WHERE $smartGroupClause $dbName = $name === 'email_id' ? 'id' : $name; if (is_array($value) || $name === 'email_id') { - $this->_qill[$grouping][] = $this->getQillForField($name, $value, $op); + $this->_qill[$grouping][] = $this->getQillForField($name, $value, $op, [], ts('Email')); $this->_where[$grouping][] = self::buildClause('civicrm_email.' . $dbName, $op, $value, 'String'); return; } @@ -7201,19 +7201,44 @@ AND displayRelType.is_active = 1 /** * Get the qill value for the field. * - * @param $name + * @param string $name * @param array|int|string $value - * @param $op + * @param string $op + * @param array $fieldSpec + * @param string $labelOverride + * Label override, if required. * * @return string */ - protected function getQillForField($name, $value, $op): string { - list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue(NULL, $name, $value, $op); + public function getQillForField($name, $value, $op, $fieldSpec = [], $labelOverride = NULL): string { + list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue($fieldSpec['bao'] ?? NULL, $name, $value, $op); return (string) ts("%1 %2 %3", [ - 1 => ts('Email'), + 1 => $labelOverride ?? $fieldSpec['title'], 2 => $qillop, 3 => $qillVal, ]); } + /** + * Where handling for any field with adequately defined metadata. + * + * @param array $fieldSpec + * @param string $name + * @param string|array|int $value + * @param string $op + * @param string|int $grouping + * + * @throws \CRM_Core_Exception + */ + public function handleWhereFromMetadata($fieldSpec, $name, $value, $op, $grouping = 0) { + $this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause($fieldSpec['where'], $op, $value, CRM_Utils_Type::typeToString($fieldSpec['type'])); + $this->_qill[$grouping][] = $this->getQillForField($name, $value, $op, $fieldSpec); + if (!isset($query->_tables[$fieldSpec['table_name']])) { + $this->_tables[$fieldSpec['table_name']] = 1; + } + if (!isset($query->_whereTables[$fieldSpec['table_name']])) { + $this->_whereTables[$fieldSpec['table_name']] = 1; + } + } + } diff --git a/CRM/Event/BAO/Query.php b/CRM/Event/BAO/Query.php index 595ad4a60d..b0b0bf0c85 100644 --- a/CRM/Event/BAO/Query.php +++ b/CRM/Event/BAO/Query.php @@ -29,8 +29,6 @@ * * @package CRM * @copyright CiviCRM LLC (c) 2004-2020 - * $Id$ - * */ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query { @@ -226,10 +224,11 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query { } /** - * @param $query + * Get event related where clauses. + * + * @param \CRM_Contact_BAO_Query $query */ public static function where(&$query) { - $grouping = NULL; foreach (array_keys($query->_params) as $id) { if (empty($query->_params[$id][0])) { continue; @@ -240,7 +239,6 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query { if ($query->_mode == CRM_Contact_BAO_Query::MODE_CONTACTS) { $query->_useDistinct = TRUE; } - $grouping = $query->_params[$id][3]; self::whereClauseSingle($query->_params[$id], $query); } } @@ -249,11 +247,14 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query { /** * @param $values * @param \CRM_Contact_BAO_Query $query + * + * @throws \CRM_Core_Exception */ public static function whereClauseSingle(&$values, &$query) { $checkPermission = empty($query->_skipPermission); list($name, $op, $value, $grouping, $wildcard) = $values; $fields = array_merge(CRM_Event_BAO_Event::fields(), CRM_Event_BAO_Participant::exportableFields()); + $fieldSpec = $fields[$values[0]] ?? []; switch ($name) { case 'event_low': @@ -362,9 +363,9 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query { return; case 'participant_status_id': - if ($value && is_array($value) && strpos($op, 'IN') === FALSE) { - $op = 'IN'; - } + $query->handleWhereFromMetadata($fieldSpec, $name, $value, $op); + return; + case 'participant_status': case 'participant_source': case 'participant_id': @@ -377,7 +378,6 @@ class CRM_Event_BAO_Query extends CRM_Core_BAO_Query { $qillName = $name; if (in_array($name, [ - 'participant_status_id', 'participant_source', 'participant_id', 'participant_contact_id', diff --git a/tests/phpunit/CRM/Event/BAO/QueryTest.php b/tests/phpunit/CRM/Event/BAO/QueryTest.php index cc7e757616..bdbc79f8f3 100644 --- a/tests/phpunit/CRM/Event/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Event/BAO/QueryTest.php @@ -9,6 +9,11 @@ class CRM_Event_BAO_QueryTest extends CiviUnitTestCase { parent::setUp(); } + /** + * Test searching for participant note. + * + * @throws \CRM_Core_Exception + */ public function testParticipantNote() { $event = $this->eventCreate(); $this->individualCreate([ @@ -42,6 +47,7 @@ class CRM_Event_BAO_QueryTest extends CiviUnitTestCase { /** * Unit test to check if participant search retrieves correct event type id. * + * @throws \CRM_Core_Exception */ public function testEventType() { $event = $this->eventCreate(); @@ -85,4 +91,43 @@ class CRM_Event_BAO_QueryTest extends CiviUnitTestCase { $this->assertEquals($eventTypeId, $result->event_type_id); } + /** + * Test provided event search parameters. + * + * @dataProvider getEventSearchParameters + * + * @throws \CRM_Core_Exception + */ + public function testParameters($parameters, $expected) { + $query = new CRM_Contact_BAO_Query( + $parameters, NULL, NULL, + FALSE, FALSE, CRM_Contact_BAO_Query::MODE_EVENT + ); + $query->query(FALSE); + $this->assertEquals($expected['where'], trim($query->_whereClause)); + $this->assertEquals($expected['qill'], trim($query->_qill[0][0])); + } + + /** + * @return array + */ + public function getEventSearchParameters() { + return [ + [ + [['participant_status_id', '=', 1, 0, 0]], + [ + 'where' => '( civicrm_participant.status_id = 1 )', + 'qill' => 'Participant Status (ID) = Registered', + ], + ], + [ + [['participant_status_id', 'IN', [1, 2], 0, 0]], + [ + 'where' => '( civicrm_participant.status_id IN ("1", "2") )', + 'qill' => 'Participant Status (ID) In Registered, Attended', + ], + ], + ]; + } + } -- 2.25.1