3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class api_v3_PriceFieldTest
16 class api_v3_PriceFieldTest
extends CiviUnitTestCase
{
19 protected $priceSetID = 0;
20 protected $_entity = 'price_field';
25 * @throws \CRM_Core_Exception
27 public function setUp() {
30 'name' => 'default_goat_priceset',
31 'title' => 'Goat accommodation',
33 'help_pre' => 'Where does your goat sleep',
34 'help_post' => 'thank you for your time',
36 'financial_type_id' => 1,
37 'is_quick_config' => 1,
41 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetparams);
42 $this->priceSetID
= $price_set['id'];
45 'price_set_id' => $this->priceSetID
,
46 'name' => 'grassvariety',
47 'label' => 'Grass Variety',
48 'html_type' => 'Text',
55 * Clean up after test.
57 * @throws \CRM_Core_Exception
59 public function tearDown() {
62 'civicrm_contribution',
64 $this->quickCleanup($tablesToTruncate);
66 $this->callAPISuccess('PriceSet', 'delete', ['id' => $this->priceSetID
]);
75 * @throws \CRM_Core_Exception
77 * @dataProvider versionThreeAndFour
79 public function testCreatePriceField(int $version) {
80 $this->_apiversion
= $version;
81 $result = $this->callAPIAndDocument($this->_entity
, 'create', $this->_params
, __FUNCTION__
, __FILE__
);
82 $this->assertEquals(1, $result['count']);
83 $this->assertNotNull($result['values'][$result['id']]['id']);
84 $this->getAndCheck($this->_params
, $result['id'], $this->_entity
);
92 * @throws \CRM_Core_Exception
94 * @dataProvider versionThreeAndFour
96 public function testGetBasicPriceField(int $version) {
97 $this->_apiversion
= $version;
98 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
99 $this->id
= $createResult['id'];
100 $this->assertAPISuccess($createResult);
102 'name' => 'contribution_amount',
104 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
105 $this->assertEquals(1, $getResult['count']);
106 $this->callAPISuccess('price_field', 'delete', ['id' => $createResult['id']]);
112 * @param int $version
114 * @throws \CRM_Core_Exception
116 * @dataProvider versionThreeAndFour
118 public function testDeletePriceField($version) {
119 $this->_apiversion
= $version;
120 $startCount = $this->callAPISuccess($this->_entity
, 'getcount', []);
121 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
122 $deleteParams = ['id' => $createResult['id']];
123 $deleteResult = $this->callAPIAndDocument($this->_entity
, 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
124 $this->assertAPISuccess($deleteResult);
125 $endCount = $this->callAPISuccess($this->_entity
, 'getcount', []);
126 $this->assertEquals($startCount, $endCount);
130 * Basic getfields test.
132 * @param int $version
134 * @throws \CRM_Core_Exception
136 * @dataProvider versionThreeAndFour
138 public function testGetFieldsPriceField(int $version) {
139 $this->_apiversion
= $version;
140 $result = $this->callAPISuccess($this->_entity
, 'getfields', ['action' => 'create']);
141 $this->assertEquals('number of options per line for checkbox and radio', $result['values']['options_per_line']['description']);
145 * Test updating the label of a text price field.
147 * CRM-19741 - ensure price field value label is also updated.
149 * @dataProvider versionThreeAndFour
151 * @param int $version
153 * @throws \CRM_Core_Exception
155 public function testUpdatePriceFieldLabel(int $version) {
156 $this->_apiversion
= $version;
157 $field = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
158 $expectedLabel = 'Rose Variety';
159 $this->updateLabel($field, $expectedLabel);
163 * Test that value label only updates if field type is html (CRM-19741).
165 * @dataProvider versionThreeAndFour
167 * @param int $version
169 * @throws \CRM_Core_Exception
171 public function testUpdatePriceFieldLabelNotUpdateField(int $version) {
172 $this->_apiversion
= $version;
173 $expectedLabel = 'juicy and healthy';
174 $field = $this->callAPISuccess($this->_entity
, 'create', array_merge($this->_params
, ['html_type' => 'Radio']));
175 $this->updateLabel($field, $expectedLabel);
179 * Update the label using the api, check against expected final label.
181 * @param array $field
182 * @param string $expectedLabel
184 * @throws \CRM_Core_Exception
186 protected function updateLabel(array $field, string $expectedLabel) {
187 $this->callAPISuccess('PriceFieldValue', 'create', [
188 'price_field_id' => $field['id'],
189 'name' => 'rye grass',
190 'label' => 'juicy and healthy',
192 'financial_type_id' => 1,
194 $this->callAPISuccess($this->_entity
, 'create', ['id' => $field['id'], 'label' => 'Rose Variety']);
195 $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', ['price_field_id' => $field['id']]);
196 $this->assertEquals($expectedLabel, $priceFieldValue['values'][$priceFieldValue['id']]['label']);
197 $this->callAPISuccess('PriceFieldValue', 'delete', ['id' => $priceFieldValue['id']]);
198 $this->callAPISuccess($this->_entity
, 'delete', ['id' => $field['id']]);