Add Price api entities
authoreileen <emcnaughton@wikimedia.org>
Wed, 20 May 2020 02:25:15 +0000 (14:25 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 25 May 2020 23:56:29 +0000 (11:56 +1200)
Api4 entities for Price, PriceField & PriceFieldValue.

All 3 entities are accessed in the PriceFieldTest

Civi/Api4/LocationType.php
Civi/Api4/PriceField.php [new file with mode: 0644]
Civi/Api4/PriceFieldValue.php [new file with mode: 0644]
Civi/Api4/PriceSet.php [new file with mode: 0644]
tests/phpunit/api/v3/PriceFieldTest.php

index 836e59463f7384cd00a71f76c5773a4b8f2ba416..cef52216207fadbf192241e151e0f3085b595192 100644 (file)
  +--------------------------------------------------------------------+
  */
 
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
- */
-
-
 namespace Civi\Api4;
 
 /**
diff --git a/Civi/Api4/PriceField.php b/Civi/Api4/PriceField.php
new file mode 100644 (file)
index 0000000..66aaa44
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4;
+
+/**
+ * PriceField entity.
+ *
+ * @package Civi\Api4
+ */
+class PriceField extends Generic\DAOEntity {
+
+}
diff --git a/Civi/Api4/PriceFieldValue.php b/Civi/Api4/PriceFieldValue.php
new file mode 100644 (file)
index 0000000..851abbc
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4;
+
+/**
+ * PriceFieldValue entity.
+ *
+ * @package Civi\Api4
+ */
+class PriceFieldValue extends Generic\DAOEntity {
+
+}
diff --git a/Civi/Api4/PriceSet.php b/Civi/Api4/PriceSet.php
new file mode 100644 (file)
index 0000000..91a7e27
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4;
+
+/**
+ * PriceSet entity.
+ *
+ * @package Civi\Api4
+ */
+class PriceSet extends Generic\DAOEntity {
+
+}
index 474afccf6db223f457ed05cd702b22e6fb198fc5..c4c6ec7f445444cb73966ba7053cbc7cba42cbfd 100644 (file)
  * @group headless
  */
 class api_v3_PriceFieldTest extends CiviUnitTestCase {
-  protected $_apiversion = 3;
   protected $_params;
   protected $id = 0;
   protected $priceSetID = 0;
   protected $_entity = 'price_field';
 
-  public $DBResetRequired = TRUE;
-
+  /**
+   * Set up for test.
+   *
+   * @throws \CRM_Core_Exception
+   */
   public function setUp() {
     parent::setUp();
-    // put stuff here that should happen before all tests in this unit
     $priceSetparams = [
-      #     [domain_id] =>
       'name' => 'default_goat_priceset',
-      'title' => 'Goat accomodation',
+      'title' => 'Goat accommodation',
       'is_active' => 1,
-      'help_pre' => "Where does your goat sleep",
-      'help_post' => "thank you for your time",
+      'help_pre' => 'Where does your goat sleep',
+      'help_post' => 'thank you for your time',
       'extends' => 2,
       'financial_type_id' => 1,
       'is_quick_config' => 1,
@@ -51,6 +51,11 @@ class api_v3_PriceFieldTest extends CiviUnitTestCase {
     ];
   }
 
+  /**
+   * Clean up after test.
+   *
+   * @throws \CRM_Core_Exception
+   */
   public function tearDown() {
     $tablesToTruncate = [
       'civicrm_contact',
@@ -58,22 +63,38 @@ class api_v3_PriceFieldTest extends CiviUnitTestCase {
     ];
     $this->quickCleanup($tablesToTruncate);
 
-    $delete = $this->callAPISuccess('PriceSet', 'delete', [
-      'id' => $this->priceSetID,
-    ]);
-
-    $this->assertAPISuccess($delete);
+    $this->callAPISuccess('PriceSet', 'delete', ['id' => $this->priceSetID]);
     parent::tearDown();
   }
 
-  public function testCreatePriceField() {
+  /**
+   * Basic create test.
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
+   *
+   * @dataProvider versionThreeAndFour
+   */
+  public function testCreatePriceField(int $version) {
+    $this->_apiversion = $version;
     $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
     $this->assertEquals(1, $result['count']);
     $this->assertNotNull($result['values'][$result['id']]['id']);
     $this->getAndCheck($this->_params, $result['id'], $this->_entity);
   }
 
-  public function testGetBasicPriceField() {
+  /**
+   * Basic get test.
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
+   *
+   * @dataProvider versionThreeAndFour
+   */
+  public function testGetBasicPriceField(int $version) {
+    $this->_apiversion = $version;
     $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
     $this->id = $createResult['id'];
     $this->assertAPISuccess($createResult);
@@ -85,7 +106,17 @@ class api_v3_PriceFieldTest extends CiviUnitTestCase {
     $this->callAPISuccess('price_field', 'delete', ['id' => $createResult['id']]);
   }
 
-  public function testDeletePriceField() {
+  /**
+   * Basic delete test.
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
+   *
+   * @dataProvider versionThreeAndFour
+   */
+  public function testDeletePriceField($version) {
+    $this->_apiversion = $version;
     $startCount = $this->callAPISuccess($this->_entity, 'getcount', []);
     $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
     $deleteParams = ['id' => $createResult['id']];
@@ -95,48 +126,75 @@ class api_v3_PriceFieldTest extends CiviUnitTestCase {
     $this->assertEquals($startCount, $endCount);
   }
 
-  public function testGetFieldsPriceField() {
+  /**
+   * Basic getfields test.
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
+   *
+   * @dataProvider versionThreeAndFour
+   */
+  public function testGetFieldsPriceField(int $version) {
+    $this->_apiversion = $version;
     $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
-    $this->assertEquals(1, $result['values']['options_per_line']['type']);
+    $this->assertEquals('number of options per line for checkbox and radio', $result['values']['options_per_line']['description']);
   }
 
   /**
-   * CRM-19741
-   * Test updating the label of a texte price field and ensure price field value label is also updated
+   * Test updating the label of a text price field.
+   *
+   * CRM-19741 - ensure price field value label is also updated.
+   *
+   * @dataProvider versionThreeAndFour
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testUpdatePriceFieldLabel() {
+  public function testUpdatePriceFieldLabel(int $version) {
+    $this->_apiversion = $version;
     $field = $this->callAPISuccess($this->_entity, 'create', $this->_params);
-    $this->callAPISuccess('price_field_value', 'create', [
-      'price_field_id' => $field['id'],
-      'name' => 'rye grass',
-      'label' => 'juicy and healthy',
-      'amount' => 1,
-      'financial_type_id' => 1,
-    ]);
-    $priceField = $this->callAPISuccess($this->_entity, 'create', ['id' => $field['id'], 'label' => 'Rose Variety']);
-    $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', ['price_field_id' => $field['id']]);
-    $this->assertEquals($priceField['values'][$priceField['id']]['label'], $priceFieldValue['values'][$priceFieldValue['id']]['label']);
-    $this->callAPISuccess('price_field_value', 'delete', ['id' => $priceFieldValue['id']]);
-    $this->callAPISuccess($this->_entity, 'delete', ['id' => $field['id']]);
+    $expectedLabel = 'Rose Variety';
+    $this->updateLabel($field, $expectedLabel);
   }
 
   /**
-   * CRM-19741
-   * Confirm value label only updates if fiedl type is html.
+   * Test that value label only updates if field type is html (CRM-19741).
+   *
+   * @dataProvider versionThreeAndFour
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testUpdatePriceFieldLabelNotUpdateField() {
+  public function testUpdatePriceFieldLabelNotUpdateField(int $version) {
+    $this->_apiversion = $version;
+    $expectedLabel = 'juicy and healthy';
     $field = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, ['html_type' => 'Radio']));
-    $this->callAPISuccess('price_field_value', 'create', [
+    $this->updateLabel($field, $expectedLabel);
+  }
+
+  /**
+   * Update the label using the api, check against expected final label.
+   *
+   * @param array $field
+   * @param string $expectedLabel
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function updateLabel(array $field, string $expectedLabel) {
+    $this->callAPISuccess('PriceFieldValue', 'create', [
       'price_field_id' => $field['id'],
       'name' => 'rye grass',
       'label' => 'juicy and healthy',
       'amount' => 1,
       'financial_type_id' => 1,
     ]);
-    $priceField = $this->callAPISuccess($this->_entity, 'create', ['id' => $field['id'], 'label' => 'Rose Variety']);
+    $this->callAPISuccess($this->_entity, 'create', ['id' => $field['id'], 'label' => 'Rose Variety']);
     $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', ['price_field_id' => $field['id']]);
-    $this->assertEquals('juicy and healthy', $priceFieldValue['values'][$priceFieldValue['id']]['label']);
-    $this->callAPISuccess('price_field_value', 'delete', ['id' => $priceFieldValue['id']]);
+    $this->assertEquals($expectedLabel, $priceFieldValue['values'][$priceFieldValue['id']]['label']);
+    $this->callAPISuccess('PriceFieldValue', 'delete', ['id' => $priceFieldValue['id']]);
     $this->callAPISuccess($this->_entity, 'delete', ['id' => $field['id']]);
   }