Merge pull request #13926 from pradpnayak/NoticeErrorProfile
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
2fe49090 4| CiviCRM version 5 |
b6708aeb 5+--------------------------------------------------------------------+
6b83d5bd 6| Copyright CiviCRM LLC (c) 2004-2019 |
b6708aeb 7+--------------------------------------------------------------------+
8| This file is a part of CiviCRM. |
9| |
10| CiviCRM is free software; you can copy, modify, and distribute it |
11| under the terms of the GNU Affero General Public License |
12| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13| |
14| CiviCRM is distributed in the hope that it will be useful, but |
15| WITHOUT ANY WARRANTY; without even the implied warranty of |
16| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17| See the GNU Affero General Public License for more details. |
18| |
19| You should have received a copy of the GNU Affero General Public |
20| License and the CiviCRM Licensing Exception along |
21| with this program; if not, contact CiviCRM LLC |
22| at info[AT]civicrm[DOT]org. If you have questions about the |
23| GNU Affero General Public License or the licensing of CiviCRM, |
24| see the CiviCRM license FAQ at http://civicrm.org/licensing |
25+--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035 27
e9479dcf
EM
28/**
29 * Class api_v3_PriceFieldTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class api_v3_PriceFieldTest extends CiviUnitTestCase {
33 protected $_apiversion = 3;
34 protected $_params;
35 protected $id = 0;
36 protected $priceSetID = 0;
37 protected $_entity = 'price_field';
b7c9bc4c 38
6a488035
TO
39 public $DBResetRequired = TRUE;
40
41 public function setUp() {
42 parent::setUp();
43 // put stuff here that should happen before all tests in this unit
44 $priceSetparams = array(
92915c55 45 # [domain_id] =>
6a488035
TO
46 'name' => 'default_goat_priceset',
47 'title' => 'Goat accomodation',
48 'is_active' => 1,
49 'help_pre' => "Where does your goat sleep",
50 'help_post' => "thank you for your time",
51 'extends' => 2,
52 'financial_type_id' => 1,
53 'is_quick_config' => 1,
54 'is_reserved' => 1,
55 );
56
6c6e6187 57 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetparams);
6a488035
TO
58 $this->priceSetID = $price_set['id'];
59
6c6e6187 60 $this->_params = array(
92915c55 61 'price_set_id' => $this->priceSetID,
6a488035
TO
62 'name' => 'grassvariety',
63 'label' => 'Grass Variety',
64 'html_type' => 'Text',
65 'is_enter_qty' => 1,
66 'is_active' => 1,
67 );
68 }
69
00be9182 70 public function tearDown() {
6a488035 71 $tablesToTruncate = array(
92915c55
TO
72 'civicrm_contact',
73 'civicrm_contribution',
6a488035
TO
74 );
75 $this->quickCleanup($tablesToTruncate);
76
6c6e6187 77 $delete = $this->callAPISuccess('PriceSet', 'delete', array(
92915c55 78 'id' => $this->priceSetID,
6a488035
TO
79 ));
80
81 $this->assertAPISuccess($delete);
82 }
83
84 public function testCreatePriceField() {
7fbb4198 85 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
ba4a1892
TM
86 $this->assertEquals(1, $result['count']);
87 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
88 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
89 }
90
91 public function testGetBasicPriceField() {
7fbb4198 92 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
6a488035
TO
93 $this->id = $createResult['id'];
94 $this->assertAPISuccess($createResult);
95 $getParams = array(
6a488035
TO
96 'name' => 'contribution_amount',
97 );
7fbb4198 98 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
ba4a1892 99 $this->assertEquals(1, $getResult['count']);
6c6e6187 100 $this->callAPISuccess('price_field', 'delete', array('id' => $createResult['id']));
6a488035
TO
101 }
102
103 public function testDeletePriceField() {
6c6e6187 104 $startCount = $this->callAPISuccess($this->_entity, 'getcount', array());
7fbb4198 105 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
106 $deleteParams = array('id' => $createResult['id']);
107 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
a15773db 108 $this->assertAPISuccess($deleteResult);
6c6e6187 109 $endCount = $this->callAPISuccess($this->_entity, 'getcount', array());
a15773db 110 $this->assertEquals($startCount, $endCount);
6a488035
TO
111 }
112
113 public function testGetFieldsPriceField() {
7fbb4198 114 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
6a488035
TO
115 $this->assertEquals(1, $result['values']['options_per_line']['type']);
116 }
117
e1defd06
SL
118 /**
119 * CRM-19741
120 * Test updating the label of a texte price field and ensure price field value label is also updated
121 */
122 public function testUpdatePriceFieldLabel() {
123 $field = $this->callAPISuccess($this->_entity, 'create', $this->_params);
124 $this->callAPISuccess('price_field_value', 'create', array(
125 'price_field_id' => $field['id'],
126 'name' => 'rye grass',
127 'label' => 'juicy and healthy',
128 'amount' => 1,
129 'financial_type_id' => 1,
130 ));
131 $priceField = $this->callAPISuccess($this->_entity, 'create', array('id' => $field['id'], 'label' => 'Rose Variety'));
132 $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', array('price_field_id' => $field['id']));
133 $this->assertEquals($priceField['values'][$priceField['id']]['label'], $priceFieldValue['values'][$priceFieldValue['id']]['label']);
134 $this->callAPISuccess('price_field_value', 'delete', array('id' => $priceFieldValue['id']));
135 $this->callAPISuccess($this->_entity, 'delete', array('id' => $field['id']));
136 }
137
138 /**
139 * CRM-19741
140 * Confirm value label only updates if fiedl type is html.
141 */
142 public function testUpdatePriceFieldLabelNotUpdateField() {
143 $field = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, array('html_type' => 'Radio')));
144 $this->callAPISuccess('price_field_value', 'create', array(
145 'price_field_id' => $field['id'],
146 'name' => 'rye grass',
147 'label' => 'juicy and healthy',
148 'amount' => 1,
149 'financial_type_id' => 1,
150 ));
151 $priceField = $this->callAPISuccess($this->_entity, 'create', array('id' => $field['id'], 'label' => 'Rose Variety'));
152 $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', array('price_field_id' => $field['id']));
153 $this->assertEquals('juicy and healthy', $priceFieldValue['values'][$priceFieldValue['id']]['label']);
154 $this->callAPISuccess('price_field_value', 'delete', array('id' => $priceFieldValue['id']));
155 $this->callAPISuccess($this->_entity, 'delete', array('id' => $field['id']));
156 }
157
6a488035 158}