--CRM-17240, fixed tests
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldValueTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
81621fee 4| CiviCRM version 4.7 |
b6708aeb 5+--------------------------------------------------------------------+
e7112fa7 6| Copyright CiviCRM LLC (c) 2004-2015 |
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
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
e9479dcf
EM
29
30/**
31 * Class api_v3_PriceFieldValueTest
32 */
6a488035
TO
33class api_v3_PriceFieldValueTest extends CiviUnitTestCase {
34 protected $_apiversion = 3;
35 protected $_params;
36 protected $id = 0;
37 protected $priceSetID = 0;
38 protected $_entity = 'price_field_value';
b7c9bc4c 39
6a488035
TO
40 public $DBResetRequired = TRUE;
41
78373706
EM
42 /**
43 * @var int
44 */
45 protected $priceFieldID;
46
47 /**
48 * Setup function.
49 */
6a488035
TO
50 public function setUp() {
51 parent::setUp();
78373706
EM
52 // Put stuff here that should happen before all tests in this unit.
53 $priceSetParams = array(
6a488035 54 'name' => 'default_goat_priceset',
78373706 55 'title' => 'Goat accommodation',
6a488035
TO
56 'is_active' => 1,
57 'help_pre' => "Where does your goat sleep",
58 'help_post' => "thank you for your time",
59 'extends' => 2,
60 'financial_type_id' => 1,
61 'is_quick_config' => 1,
62 'is_reserved' => 1,
63 );
64
78373706 65 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetParams);
6a488035
TO
66 $this->priceSetID = $price_set['id'];
67
78373706 68 $priceFieldParams = array(
6a488035
TO
69 'price_set_id' => $this->priceSetID,
70 'name' => 'grassvariety',
71 'label' => 'Grass Variety',
72 'html_type' => 'Text',
73 'is_enter_qty' => 1,
74 'is_active' => 1,
75 );
78373706 76 $priceField = $this->callAPISuccess('price_field', 'create', $priceFieldParams);
6a488035
TO
77 $this->priceFieldID = $priceField['id'];
78 $this->_params = array(
6a488035 79 'price_field_id' => $this->priceFieldID,
78373706 80 'name' => 'rye grass',
6a488035 81 'label' => 'juicy and healthy',
21dfd5f5 82 'amount' => 1,
43dbd988 83 'financial_type_id' => 1,
92915c55 84 );
b6708aeb 85
6a488035 86 $membershipOrgId = $this->organizationCreate(NULL);
75638074 87 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $membershipOrgId));
78373706 88 $priceSetParams1 = array(
6a488035
TO
89 'name' => 'priceset',
90 'title' => 'Priceset with Multiple Terms',
91 'is_active' => 1,
92 'extends' => 3,
93 'financial_type_id' => 2,
94 'is_quick_config' => 1,
95 'is_reserved' => 1,
96 );
78373706 97 $price_set1 = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
6a488035 98 $this->priceSetID1 = $price_set1['id'];
78373706 99 $priceFieldParams1 = array(
6a488035
TO
100 'price_set_id' => $this->priceSetID1,
101 'name' => 'memtype',
102 'label' => 'memtype',
103 'html_type' => 'Radio',
104 'is_enter_qty' => 1,
105 'is_active' => 1,
106 );
78373706 107 $priceField1 = $this->callAPISuccess('price_field', 'create', $priceFieldParams1);
6a488035
TO
108 $this->priceFieldID1 = $priceField1['id'];
109 }
110
78373706
EM
111 /**
112 * Tear down function.
113 *
114 * @throws \Exception
115 */
00be9182 116 public function tearDown() {
6a488035 117 $tablesToTruncate = array(
92915c55
TO
118 'civicrm_contact',
119 'civicrm_contribution',
6a488035
TO
120 );
121 $this->quickCleanup($tablesToTruncate);
122 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
6c6e6187 123 $this->callAPISuccess('PriceField', 'delete', array(
92915c55 124 'id' => $this->priceFieldID1,
6a488035 125 ));
6c6e6187 126 $this->callAPISuccess('PriceSet', 'delete', array(
6a488035
TO
127 'id' => $this->priceSetID1,
128 ));
6c6e6187 129 $this->callAPISuccess('PriceField', 'delete', array(
92915c55 130 'id' => $this->priceFieldID,
6a488035 131 ));
6c6e6187 132 $delete = $this->callAPISuccess('PriceSet', 'delete', array(
6a488035
TO
133 'id' => $this->priceSetID,
134 ));
135
136 $this->assertAPISuccess($delete);
137 }
138
139 public function testCreatePriceFieldValue() {
ca985406 140 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
a15773db 141 $this->assertAPISuccess($result);
ba4a1892
TM
142 $this->assertEquals(1, $result['count']);
143 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
144 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
145 }
146
147 public function testGetBasicPriceFieldValue() {
ca985406 148 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
6a488035
TO
149 $this->id = $createResult['id'];
150 $this->assertAPISuccess($createResult);
151 $getParams = array(
6a488035
TO
152 'name' => 'contribution_amount',
153 );
ca985406 154 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
ba4a1892 155 $this->assertEquals(1, $getResult['count']);
6c6e6187 156 $this->callAPISuccess('price_field_value', 'delete', array('id' => $createResult['id']));
6a488035
TO
157 }
158
159 public function testDeletePriceFieldValue() {
ca985406 160 $startCount = $this->callAPISuccess($this->_entity, 'getcount', array());
161 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
162 $deleteParams = array('id' => $createResult['id']);
163 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
164
165 $endCount = $this->callAPISuccess($this->_entity, 'getcount', array());
a15773db 166 $this->assertEquals($startCount, $endCount);
6a488035
TO
167 }
168
169 public function testGetFieldsPriceFieldValue() {
ca985406 170 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
6a488035
TO
171 $this->assertEquals(1, $result['values']['max_value']['type']);
172 }
173
174 public function testCreatePriceFieldValuewithMultipleTerms() {
175 $params = array(
6a488035 176 'price_field_id' => $this->priceFieldID1,
6c6e6187 177 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
178 'name' => 'memType1',
179 'label' => 'memType1',
180 'amount' => 90,
181 'membership_num_terms' => 2,
182 'is_active' => 1,
183 'financial_type_id' => 2,
92915c55 184 );
ca985406 185 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 186 $this->assertEquals($result['values'][$result['id']]['membership_num_terms'], 2);
ba4a1892 187 $this->assertEquals(1, $result['count']);
ca985406 188 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id']));
6a488035 189 }
92915c55 190
6a488035
TO
191 public function testGetPriceFieldValuewithMultipleTerms() {
192 $params1 = array(
6a488035 193 'price_field_id' => $this->priceFieldID1,
6c6e6187 194 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
195 'name' => 'memType1',
196 'label' => 'memType1',
197 'amount' => 90,
198 'membership_num_terms' => 2,
199 'is_active' => 1,
200 'financial_type_id' => 2,
92915c55 201 );
6a488035 202 $params2 = array(
6a488035 203 'price_field_id' => $this->priceFieldID1,
6c6e6187 204 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
205 'name' => 'memType2',
206 'label' => 'memType2',
207 'amount' => 120,
208 'membership_num_terms' => 3,
209 'is_active' => 1,
210 'financial_type_id' => 2,
92915c55 211 );
ca985406 212 $result1 = $this->callAPISuccess($this->_entity, 'create', $params1);
213 $result2 = $this->callAPISuccess($this->_entity, 'create', $params2);
481a74f4 214 $result = $this->callAPISuccess($this->_entity, 'get', array('price_field_id' => $this->priceFieldID1));
ba4a1892 215 $this->assertEquals(2, $result['count']);
6c6e6187
TO
216 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result1['id']));
217 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result2['id']));
6a488035 218 }
96025800 219
6a488035 220}