From 57b75c5c63a7fec56a62fc531e5e1569fa0f442c Mon Sep 17 00:00:00 2001 From: eileen <emcnaughton@wikimedia.org> Date: Mon, 7 Oct 2019 15:09:50 +0200 Subject: [PATCH] dev/financial#72 Make is_template=0 the default for api get requests. We are planning on adding is_template to the civicrm_contribution table - following the precedent from event. By default it is NOT desirable to get template entities returned in a get request - this fixes that at the generic apiv4 level and means that it will be a default filter when present --- .../Provider/GetActionDefaultsProvider.php | 5 ++ tests/phpunit/api/v4/Action/EventTest.php | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/phpunit/api/v4/Action/EventTest.php diff --git a/Civi/Api4/Service/Spec/Provider/GetActionDefaultsProvider.php b/Civi/Api4/Service/Spec/Provider/GetActionDefaultsProvider.php index f532e356096..58c59e47f62 100644 --- a/Civi/Api4/Service/Spec/Provider/GetActionDefaultsProvider.php +++ b/Civi/Api4/Service/Spec/Provider/GetActionDefaultsProvider.php @@ -56,6 +56,11 @@ class GetActionDefaultsProvider implements Generic\SpecProviderInterface { if ($isTestField) { $isTestField->setDefaultValue('0'); } + + $isTemplateField = $spec->getFieldByName('is_template'); + if ($isTemplateField) { + $isTemplateField->setDefaultValue('0'); + } } /** diff --git a/tests/phpunit/api/v4/Action/EventTest.php b/tests/phpunit/api/v4/Action/EventTest.php new file mode 100644 index 00000000000..052629c6f03 --- /dev/null +++ b/tests/phpunit/api/v4/Action/EventTest.php @@ -0,0 +1,53 @@ +<?php + +/* + +--------------------------------------------------------------------+ + | CiviCRM version 5 | + +--------------------------------------------------------------------+ + | Copyright CiviCRM LLC (c) 2004-2019 | + +--------------------------------------------------------------------+ + | This file is a part of CiviCRM. | + | | + | CiviCRM is free software; you can copy, modify, and distribute it | + | under the terms of the GNU Affero General Public License | + | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | + | | + | CiviCRM is distributed in the hope that it will be useful, but | + | WITHOUT ANY WARRANTY; without even the implied warranty of | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | + | See the GNU Affero General Public License for more details. | + | | + | You should have received a copy of the GNU Affero General Public | + | License and the CiviCRM Licensing Exception along | + | with this program; if not, contact CiviCRM LLC | + | at info[AT]civicrm[DOT]org. If you have questions about the | + | GNU Affero General Public License or the licensing of CiviCRM, | + | see the CiviCRM license FAQ at http://civicrm.org/licensing | + +--------------------------------------------------------------------+ + */ + +/** + * + * @package CRM + * @copyright CiviCRM LLC (c) 2004-2019 + */ + +use Civi\Api4\Event; + +/** + * @group headless + */ +class EventTest extends \api\v4\UnitTestCase { + + /** + * Test that the event api filters out templates by default. + * + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function testTemplateFilterByDefault() { + Event::create()->setValues(['template_title' => 'Big Event', 'is_template' => 1, 'start_date' => 'now', 'event_type_id' => 'Meeting'])->execute(); + Event::create()->setValues(['title' => 'Bigger Event', 'start_date' => 'now', 'event_type_id' => 'Meeting'])->execute(); + $this->assertEquals(1, Event::get()->selectRowCount()->execute()->count()); + } + +} -- GitLab