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