Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
7d61e75f
TO
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
e70a7fc0 10 */
6a488035 11
e9479dcf
EM
12/**
13 * Class api_v3_PriceFieldTest
acb109b7 14 * @group headless
e9479dcf 15 */
6a488035 16class api_v3_PriceFieldTest extends CiviUnitTestCase {
6a488035
TO
17 protected $_params;
18 protected $id = 0;
19 protected $priceSetID = 0;
20 protected $_entity = 'price_field';
b7c9bc4c 21
f1e6d665 22 /**
23 * Set up for test.
24 *
25 * @throws \CRM_Core_Exception
26 */
6a488035
TO
27 public function setUp() {
28 parent::setUp();
9099cab3 29 $priceSetparams = [
6a488035 30 'name' => 'default_goat_priceset',
f1e6d665 31 'title' => 'Goat accommodation',
6a488035 32 'is_active' => 1,
f1e6d665 33 'help_pre' => 'Where does your goat sleep',
34 'help_post' => 'thank you for your time',
6a488035
TO
35 'extends' => 2,
36 'financial_type_id' => 1,
37 'is_quick_config' => 1,
38 'is_reserved' => 1,
9099cab3 39 ];
6a488035 40
6c6e6187 41 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetparams);
6a488035
TO
42 $this->priceSetID = $price_set['id'];
43
9099cab3 44 $this->_params = [
92915c55 45 'price_set_id' => $this->priceSetID,
6a488035
TO
46 'name' => 'grassvariety',
47 'label' => 'Grass Variety',
48 'html_type' => 'Text',
49 'is_enter_qty' => 1,
50 'is_active' => 1,
9099cab3 51 ];
6a488035
TO
52 }
53
f1e6d665 54 /**
55 * Clean up after test.
56 *
57 * @throws \CRM_Core_Exception
58 */
00be9182 59 public function tearDown() {
9099cab3 60 $tablesToTruncate = [
92915c55
TO
61 'civicrm_contact',
62 'civicrm_contribution',
9099cab3 63 ];
6a488035
TO
64 $this->quickCleanup($tablesToTruncate);
65
f1e6d665 66 $this->callAPISuccess('PriceSet', 'delete', ['id' => $this->priceSetID]);
fda18dc3 67 parent::tearDown();
6a488035
TO
68 }
69
f1e6d665 70 /**
71 * Basic create test.
72 *
73 * @param int $version
74 *
75 * @throws \CRM_Core_Exception
76 *
77 * @dataProvider versionThreeAndFour
78 */
79 public function testCreatePriceField(int $version) {
80 $this->_apiversion = $version;
7fbb4198 81 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
ba4a1892
TM
82 $this->assertEquals(1, $result['count']);
83 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
84 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
85 }
86
f1e6d665 87 /**
88 * Basic get test.
89 *
90 * @param int $version
91 *
92 * @throws \CRM_Core_Exception
93 *
94 * @dataProvider versionThreeAndFour
95 */
96 public function testGetBasicPriceField(int $version) {
97 $this->_apiversion = $version;
7fbb4198 98 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
6a488035
TO
99 $this->id = $createResult['id'];
100 $this->assertAPISuccess($createResult);
9099cab3 101 $getParams = [
6a488035 102 'name' => 'contribution_amount',
9099cab3 103 ];
7fbb4198 104 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
ba4a1892 105 $this->assertEquals(1, $getResult['count']);
9099cab3 106 $this->callAPISuccess('price_field', 'delete', ['id' => $createResult['id']]);
6a488035
TO
107 }
108
f1e6d665 109 /**
110 * Basic delete test.
111 *
112 * @param int $version
113 *
114 * @throws \CRM_Core_Exception
115 *
116 * @dataProvider versionThreeAndFour
117 */
118 public function testDeletePriceField($version) {
119 $this->_apiversion = $version;
9099cab3 120 $startCount = $this->callAPISuccess($this->_entity, 'getcount', []);
7fbb4198 121 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
9099cab3 122 $deleteParams = ['id' => $createResult['id']];
7fbb4198 123 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
a15773db 124 $this->assertAPISuccess($deleteResult);
9099cab3 125 $endCount = $this->callAPISuccess($this->_entity, 'getcount', []);
a15773db 126 $this->assertEquals($startCount, $endCount);
6a488035
TO
127 }
128
f1e6d665 129 /**
130 * Basic getfields test.
131 *
132 * @param int $version
133 *
134 * @throws \CRM_Core_Exception
135 *
136 * @dataProvider versionThreeAndFour
137 */
138 public function testGetFieldsPriceField(int $version) {
139 $this->_apiversion = $version;
9099cab3 140 $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
f1e6d665 141 $this->assertEquals('number of options per line for checkbox and radio', $result['values']['options_per_line']['description']);
6a488035
TO
142 }
143
e1defd06 144 /**
f1e6d665 145 * Test updating the label of a text price field.
146 *
147 * CRM-19741 - ensure price field value label is also updated.
148 *
149 * @dataProvider versionThreeAndFour
150 *
151 * @param int $version
152 *
153 * @throws \CRM_Core_Exception
e1defd06 154 */
f1e6d665 155 public function testUpdatePriceFieldLabel(int $version) {
156 $this->_apiversion = $version;
e1defd06 157 $field = $this->callAPISuccess($this->_entity, 'create', $this->_params);
f1e6d665 158 $expectedLabel = 'Rose Variety';
159 $this->updateLabel($field, $expectedLabel);
e1defd06
SL
160 }
161
162 /**
f1e6d665 163 * Test that value label only updates if field type is html (CRM-19741).
164 *
165 * @dataProvider versionThreeAndFour
166 *
167 * @param int $version
168 *
169 * @throws \CRM_Core_Exception
e1defd06 170 */
f1e6d665 171 public function testUpdatePriceFieldLabelNotUpdateField(int $version) {
172 $this->_apiversion = $version;
173 $expectedLabel = 'juicy and healthy';
9099cab3 174 $field = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, ['html_type' => 'Radio']));
f1e6d665 175 $this->updateLabel($field, $expectedLabel);
176 }
177
178 /**
179 * Update the label using the api, check against expected final label.
180 *
181 * @param array $field
182 * @param string $expectedLabel
183 *
184 * @throws \CRM_Core_Exception
185 */
186 protected function updateLabel(array $field, string $expectedLabel) {
187 $this->callAPISuccess('PriceFieldValue', 'create', [
e1defd06
SL
188 'price_field_id' => $field['id'],
189 'name' => 'rye grass',
190 'label' => 'juicy and healthy',
191 'amount' => 1,
192 'financial_type_id' => 1,
9099cab3 193 ]);
f1e6d665 194 $this->callAPISuccess($this->_entity, 'create', ['id' => $field['id'], 'label' => 'Rose Variety']);
9099cab3 195 $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', ['price_field_id' => $field['id']]);
f1e6d665 196 $this->assertEquals($expectedLabel, $priceFieldValue['values'][$priceFieldValue['id']]['label']);
197 $this->callAPISuccess('PriceFieldValue', 'delete', ['id' => $priceFieldValue['id']]);
9099cab3 198 $this->callAPISuccess($this->_entity, 'delete', ['id' => $field['id']]);
e1defd06
SL
199 }
200
6a488035 201}