From 5fa9612e94450ce4ea0d4fb7812c4e83c9b1af23 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 20 Jun 2021 11:14:45 +1200 Subject: [PATCH] Start to tighten open-ended 'return' in tests --- Civi/Test/Api3TestTrait.php | 3 +++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Civi/Test/Api3TestTrait.php b/Civi/Test/Api3TestTrait.php index f544be76f2..58e5563a03 100644 --- a/Civi/Test/Api3TestTrait.php +++ b/Civi/Test/Api3TestTrait.php @@ -228,6 +228,9 @@ trait Api3TestTrait { $params += [ 'version' => $this->_apiversion, ]; + if (!empty($this->isGetSafe) && !isset($params['return'])) { + $params['return'] = 'id'; + } $result = $this->civicrm_api($entity, 'getsingle', $params); if (!is_array($result) || !empty($result['is_error']) || isset($result['values'])) { $unfilteredResult = $this->civicrm_api($entity, 'get', ['version' => $this->_apiversion]); diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index d7eb28e659..b55abcc374 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -162,6 +162,23 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { */ protected $isLocationTypesOnPostAssert = TRUE; + /** + * Has the test class been verified as 'getsafe'. + * + * If a class is getsafe it means that where + * callApiSuccess is called 'return' is specified or 'return' =>'id' + * can be added by that function. This is part of getting away + * from open-ended get calls. + * + * Eventually we want to not be doing these in our test classes & start + * to work to not do them in our main code base. Note they mainly + * cause issues for activity.get and contact.get as these are where the + * too many joins limit is most likely to be hit. + * + * @var bool + */ + protected $isGetSafe = FALSE; + /** * Class used for hooks during tests. * -- 2.25.1