dev/financial#72 Make is_template=0 the default for api get requests.
authoreileen <emcnaughton@wikimedia.org>
Mon, 7 Oct 2019 13:09:50 +0000 (15:09 +0200)
committereileen <emcnaughton@wikimedia.org>
Mon, 7 Oct 2019 13:09:50 +0000 (15:09 +0200)
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

Civi/Api4/Service/Spec/Provider/GetActionDefaultsProvider.php
tests/phpunit/api/v4/Action/EventTest.php [new file with mode: 0644]

index f532e356096c449e885aae33c7b19e911e8c73d9..58c59e47f622bfe069f9e05ab7254534301039a9 100644 (file)
@@ -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 (file)
index 0000000..052629c
--- /dev/null
@@ -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());
+  }
+
+}