Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class api_v3_PriceFieldTest
14 * @group headless
15 */
16 class api_v3_PriceFieldTest extends CiviUnitTestCase {
17 protected $_apiversion = 3;
18 protected $_params;
19 protected $id = 0;
20 protected $priceSetID = 0;
21 protected $_entity = 'price_field';
22
23 public $DBResetRequired = TRUE;
24
25 public function setUp() {
26 parent::setUp();
27 // put stuff here that should happen before all tests in this unit
28 $priceSetparams = [
29 # [domain_id] =>
30 'name' => 'default_goat_priceset',
31 'title' => 'Goat accomodation',
32 'is_active' => 1,
33 'help_pre' => "Where does your goat sleep",
34 'help_post' => "thank you for your time",
35 'extends' => 2,
36 'financial_type_id' => 1,
37 'is_quick_config' => 1,
38 'is_reserved' => 1,
39 ];
40
41 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetparams);
42 $this->priceSetID = $price_set['id'];
43
44 $this->_params = [
45 'price_set_id' => $this->priceSetID,
46 'name' => 'grassvariety',
47 'label' => 'Grass Variety',
48 'html_type' => 'Text',
49 'is_enter_qty' => 1,
50 'is_active' => 1,
51 ];
52 }
53
54 public function tearDown() {
55 $tablesToTruncate = [
56 'civicrm_contact',
57 'civicrm_contribution',
58 ];
59 $this->quickCleanup($tablesToTruncate);
60
61 $delete = $this->callAPISuccess('PriceSet', 'delete', [
62 'id' => $this->priceSetID,
63 ]);
64
65 $this->assertAPISuccess($delete);
66 parent::tearDown();
67 }
68
69 public function testCreatePriceField() {
70 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
71 $this->assertEquals(1, $result['count']);
72 $this->assertNotNull($result['values'][$result['id']]['id']);
73 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
74 }
75
76 public function testGetBasicPriceField() {
77 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
78 $this->id = $createResult['id'];
79 $this->assertAPISuccess($createResult);
80 $getParams = [
81 'name' => 'contribution_amount',
82 ];
83 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
84 $this->assertEquals(1, $getResult['count']);
85 $this->callAPISuccess('price_field', 'delete', ['id' => $createResult['id']]);
86 }
87
88 public function testDeletePriceField() {
89 $startCount = $this->callAPISuccess($this->_entity, 'getcount', []);
90 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
91 $deleteParams = ['id' => $createResult['id']];
92 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
93 $this->assertAPISuccess($deleteResult);
94 $endCount = $this->callAPISuccess($this->_entity, 'getcount', []);
95 $this->assertEquals($startCount, $endCount);
96 }
97
98 public function testGetFieldsPriceField() {
99 $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
100 $this->assertEquals(1, $result['values']['options_per_line']['type']);
101 }
102
103 /**
104 * CRM-19741
105 * Test updating the label of a texte price field and ensure price field value label is also updated
106 */
107 public function testUpdatePriceFieldLabel() {
108 $field = $this->callAPISuccess($this->_entity, 'create', $this->_params);
109 $this->callAPISuccess('price_field_value', 'create', [
110 'price_field_id' => $field['id'],
111 'name' => 'rye grass',
112 'label' => 'juicy and healthy',
113 'amount' => 1,
114 'financial_type_id' => 1,
115 ]);
116 $priceField = $this->callAPISuccess($this->_entity, 'create', ['id' => $field['id'], 'label' => 'Rose Variety']);
117 $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', ['price_field_id' => $field['id']]);
118 $this->assertEquals($priceField['values'][$priceField['id']]['label'], $priceFieldValue['values'][$priceFieldValue['id']]['label']);
119 $this->callAPISuccess('price_field_value', 'delete', ['id' => $priceFieldValue['id']]);
120 $this->callAPISuccess($this->_entity, 'delete', ['id' => $field['id']]);
121 }
122
123 /**
124 * CRM-19741
125 * Confirm value label only updates if fiedl type is html.
126 */
127 public function testUpdatePriceFieldLabelNotUpdateField() {
128 $field = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, ['html_type' => 'Radio']));
129 $this->callAPISuccess('price_field_value', 'create', [
130 'price_field_id' => $field['id'],
131 'name' => 'rye grass',
132 'label' => 'juicy and healthy',
133 'amount' => 1,
134 'financial_type_id' => 1,
135 ]);
136 $priceField = $this->callAPISuccess($this->_entity, 'create', ['id' => $field['id'], 'label' => 'Rose Variety']);
137 $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', ['price_field_id' => $field['id']]);
138 $this->assertEquals('juicy and healthy', $priceFieldValue['values'][$priceFieldValue['id']]['label']);
139 $this->callAPISuccess('price_field_value', 'delete', ['id' => $priceFieldValue['id']]);
140 $this->callAPISuccess($this->_entity, 'delete', ['id' => $field['id']]);
141 }
142
143 }