From 53a3f459526c03f97cc5f4c754d2d21dcbbd9282 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sat, 10 Dec 2016 08:35:26 +1100 Subject: [PATCH] CRM-19743 Ensure that checking for id in sort column works in arrays as well as strings --- api/v3/utils.php | 14 ++++++++------ tests/phpunit/api/v3/SyntaxConformanceTest.php | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/api/v3/utils.php b/api/v3/utils.php index 3f4e5cc611..0597cd1b27 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -872,12 +872,6 @@ function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $ $returnProperties[$lowercase_entity . '_id'] = 1; unset($returnProperties['id']); } - switch (trim(strtolower($sort))) { - case 'id': - case 'id desc': - case 'id asc': - $sort = str_replace('id', $lowercase_entity . '_id', $sort); - } } $options = array( @@ -892,6 +886,14 @@ function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $ if (!empty($sort)) { foreach ((array) $sort as $s) { if (CRM_Utils_Rule::mysqlOrderBy($s)) { + if ($entity && $action == 'get') { + switch (trim(strtolower($s))) { + case 'id': + case 'id desc': + case 'id asc': + $s = str_replace('id', $lowercase_entity . '_id', $s); + } + } $finalSort[] = $s; } else { diff --git a/tests/phpunit/api/v3/SyntaxConformanceTest.php b/tests/phpunit/api/v3/SyntaxConformanceTest.php index b224739996..eadc674212 100644 --- a/tests/phpunit/api/v3/SyntaxConformanceTest.php +++ b/tests/phpunit/api/v3/SyntaxConformanceTest.php @@ -1052,6 +1052,24 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { $result = $this->callAPIFailure($Entity, 'get', array('options' => array('sort' => 'sleep(1)'))); } + /** + * @dataProvider entities_create + * + * Check that create doesn't work with an invalid + * @param $Entity + * @throws \PHPUnit_Framework_IncompleteTestError + */ + public function testValidSortSingleArrayById_get($Entity) { + $invalidEntitys = array('ActivityType', 'Setting', 'System'); + if (in_array($Entity, $invalidEntitys)) { + $this->markTestSkipped('It seems OK for ' . $Entity . ' to skip here as it silently sips invalid params'); + } + $params = array('sort' => array('id')); + $result = _civicrm_api3_get_options_from_params($params, FALSE, $Entity, 'get'); + $lowercase_entity = _civicrm_api_get_entity_name_from_camel($Entity); + $this->assertEquals($result['sort'], $lowercase_entity . '_id'); + } + /** * @dataProvider entities_create * -- 2.25.1