CRM-12167: Add Tests for Price Field Option Visibility
authorCamilo Rodriguez <camilo@compucorp.co.uk>
Fri, 25 Aug 2017 15:12:57 +0000 (15:12 +0000)
committerCamilo Rodriguez <camilo@compucorp.co.uk>
Mon, 25 Sep 2017 14:14:58 +0000 (14:14 +0000)
Added tests to verify the field is added to the DAO and validations work on
Field creation form and Option create/edit form.

tests/phpunit/CRM/Price/BAO/PriceFieldValueTest.php [new file with mode: 0755]
tests/phpunit/CRM/Price/Form/FieldTest.php [new file with mode: 0755]
tests/phpunit/CRM/Price/Form/OptionTest.php [new file with mode: 0755]

diff --git a/tests/phpunit/CRM/Price/BAO/PriceFieldValueTest.php b/tests/phpunit/CRM/Price/BAO/PriceFieldValueTest.php
new file mode 100755 (executable)
index 0000000..c8d3252
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
+ +--------------------------------------------------------------------+
+ | 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        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Test class for CRM_Price_BAO_PriceSet.
+ * @group headless
+ */
+class CRM_Price_BAO_PriceFieldValueTest extends CiviUnitTestCase {
+
+  /**
+   * Sets up the fixtures.
+   */
+  protected function setUp() {
+    parent::setUp();
+  }
+
+  /**
+   * Tears down the fixture.
+   */
+  protected function tearDown() {
+  }
+
+  /**
+   * Verifies visibility field exists and is configured as a pseudoconstant
+   * referencing the 'visibility' option group.
+   */
+  public function testVisibilityFieldExists() {
+    $fields = CRM_Price_DAO_PriceFieldValue::fields();
+
+    $this->assertArrayKeyExists('visibility_id', $fields);
+    $this->assertEquals('visibility', $fields['visibility_id']['pseudoconstant']['optionGroupName']);
+  }
+}
diff --git a/tests/phpunit/CRM/Price/Form/FieldTest.php b/tests/phpunit/CRM/Price/Form/FieldTest.php
new file mode 100755 (executable)
index 0000000..f87528f
--- /dev/null
@@ -0,0 +1,84 @@
+<?php\r
+\r
+class CRM_Price_Form_FieldTest extends CiviUnitTestCase {\r
+\r
+  protected $priceFieldValues;\r
+  protected $visibilityOptionsKeys;\r
+  protected $visibilityOptions;\r
+  protected $publicFieldParams;\r
+  protected $adminFieldParams;\r
+\r
+  public function setUp() {\r
+    parent::setUp();\r
+\r
+    $this->visibilityOptionsKeys = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, array(\r
+      'labelColumn' => 'name',\r
+      'flip' => TRUE,\r
+    ));\r
+\r
+    $this->publicFieldParams = $this->initializeFieldParameters(array(\r
+      'label' => 'Public Price Field',\r
+      'name' => 'public_price',\r
+      'visibility_id' => $this->visibilityOptionsKeys['public'],\r
+    ));\r
+\r
+    $this->adminFieldParams = $this->initializeFieldParameters(array(\r
+      'label' => 'Public Price Field',\r
+      'name' => 'public_price',\r
+      'visibility_id' => $this->visibilityOptionsKeys['admin'],\r
+    ));\r
+  }\r
+\r
+  public function testPublicFieldWithOnlyAdminOptionsIsNotAllowed() {\r
+    $this->publicFieldParams['option_label'][1] = 'Admin Price';\r
+    $this->publicFieldParams['option_amount'][1] = 10;\r
+    $this->publicFieldParams['option_visibility_id'][1] = $this->visibilityOptionsKeys['admin'];\r
+\r
+    $form = new CRM_Price_Form_Field();\r
+    $form->_action = CRM_Core_Action::ADD;\r
+    $files = array();\r
+\r
+    $validationResult = $form->formRule($this->publicFieldParams, $files, $form);\r
+    $this->assertType('array', $validationResult);\r
+    $this->assertTrue(array_key_exists('visibility_id', $validationResult));\r
+  }\r
+\r
+  public function testAdminFieldDoesNotAllowPublicOptions() {\r
+    $this->adminFieldParams['option_label'][1] = 'Admin Price';\r
+    $this->adminFieldParams['option_amount'][1] = 10;\r
+    $this->adminFieldParams['option_visibility_id'][1] = $this->visibilityOptionsKeys['public'];\r
+\r
+    $form = new CRM_Price_Form_Field();\r
+    $form->_action = CRM_Core_Action::ADD;\r
+    $files = array();\r
+\r
+    $validationResult = $form->formRule($this->adminFieldParams, $files, $form);\r
+    $this->assertType('array', $validationResult);\r
+    $this->assertTrue(array_key_exists('visibility_id', $validationResult));\r
+  }\r
+\r
+  private function initializeFieldParameters($params) {\r
+    $defaultParams = array(\r
+      'label' => 'Price Field',\r
+      'name' => CRM_Utils_String::titleToVar('Price Field'),\r
+      'html_type' => 'Select',\r
+      'is_display_amounts' => 1,\r
+      'weight' => 1,\r
+      'options_per_line' => 1,\r
+      'is_enter_qty' => 1,\r
+      'financial_type_id' => $this->getFinancialTypeId('Event Fee'),\r
+      'visibility_id' => $this->visibilityOptionsKeys['public'],\r
+    );\r
+\r
+    for ($index = 1; $index <= CRM_Price_Form_Field::NUM_OPTION; $index++) {\r
+      $defaultParams['option_label'][$index] = null;\r
+      $defaultParams['option_value'][$index] = null;\r
+      $defaultParams['option_name'][$index] = null;\r
+      $defaultParams['option_weight'][$index] = null;\r
+      $defaultParams['option_amount'][$index] = null;\r
+      $defaultParams['option_visibility_id'][$index] = null;\r
+    }\r
+\r
+    return array_merge($defaultParams, $params);\r
+  }\r
+}\r
diff --git a/tests/phpunit/CRM/Price/Form/OptionTest.php b/tests/phpunit/CRM/Price/Form/OptionTest.php
new file mode 100755 (executable)
index 0000000..8b31935
--- /dev/null
@@ -0,0 +1,91 @@
+<?php\r
+\r
+class CRM_Price_Form_OptionTest extends CiviUnitTestCase {\r
+\r
+  protected $priceFieldValues;\r
+  protected $visibilityOptionsKeys;\r
+  protected $visibilityOptions;\r
+  protected $publicValue;\r
+  protected $adminValue;\r
+\r
+  public function setUp() {\r
+    parent::setUp();\r
+\r
+    $this->visibilityOptions = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, array(\r
+      'labelColumn' => 'name',\r
+    ));\r
+    $this->visibilityOptionsKeys = CRM_Price_BAO_PriceFieldValue::buildOptions('visibility_id', NULL, array(\r
+      'labelColumn' => 'name',\r
+      'flip' => TRUE,\r
+    ));\r
+  }\r
+\r
+  public function testChangingUniquePublicOptionOnPublicFieldIsNotAllowed() {\r
+    $this->setUpPriceSet(array(\r
+      'html_type' => 'Select',\r
+      'visibility_id' => $this->visibilityOptionsKeys['public'],\r
+      'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),\r
+      'option_value' => array('1' => 100, '2' => 200),\r
+      'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),\r
+      'option_weight' => array('1' => 1, '2' => 2),\r
+      'option_amount' => array('1' => 100, '2' => 200),\r
+      'option_visibility_id' => array(1 => $this->visibilityOptionsKeys['public'], 2 => $this->visibilityOptionsKeys['admin']),\r
+    ));\r
+\r
+    $params = array(\r
+      'fieldId' => $this->publicValue['price_field_id'],\r
+      'optionId' => $this->publicValue['id'],\r
+      'visibility_id' => $this->visibilityOptionsKeys['admin'],\r
+    );\r
+\r
+    $form = new CRM_Price_Form_Option();\r
+    $form->_action = CRM_Core_Action::ADD;\r
+    $files = array();\r
+\r
+    $validationResult = $form->formRule($params, $files, $form);\r
+    $this->assertType('array', $validationResult);\r
+    $this->assertTrue(array_key_exists('visibility_id', $validationResult));\r
+  }\r
+\r
+  public function testAddingPublicOptionToAdminFieldIsNotAllowed() {\r
+    $this->setUpPriceSet(array(\r
+      'html_type' => 'Select',\r
+      'visibility_id' => $this->visibilityOptionsKeys['admin'],\r
+      'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),\r
+      'option_value' => array('1' => 100, '2' => 200),\r
+      'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),\r
+      'option_weight' => array('1' => 1, '2' => 2),\r
+      'option_amount' => array('1' => 100, '2' => 200),\r
+      'option_visibility_id' => array(1 => $this->visibilityOptionsKeys['admin'], 2 => $this->visibilityOptionsKeys['admin']),\r
+    ));\r
+\r
+    $params = array(\r
+      'fieldId' => $this->adminValue['price_field_id'],\r
+      'optionId' => $this->adminValue['id'],\r
+      'visibility_id' => $this->visibilityOptionsKeys['public'],\r
+    );\r
+\r
+    $form = new CRM_Price_Form_Option();\r
+    $form->_action = CRM_Core_Action::ADD;\r
+    $files = array();\r
+\r
+    $validationResult = $form->formRule($params, $files, $form);\r
+    $this->assertType('array', $validationResult);\r
+    $this->assertTrue(array_key_exists('visibility_id', $validationResult));\r
+  }\r
+\r
+  private function setUpPriceSet($params) {\r
+    $priceSetCreateResult = $this->createPriceSet('contribution_page', NULL, $params);\r
+\r
+    $this->priceFieldValues = $priceSetCreateResult['values'];\r
+\r
+    foreach ($this->priceFieldValues as $currentField) {\r
+      if ($this->visibilityOptions[$currentField['visibility_id']] == 'public') {\r
+        $this->publicValue = $currentField;\r
+      } else {\r
+        $this->adminValue = $currentField;\r
+      }\r
+    }\r
+\r
+  }\r
+}\r