Merge pull request #14367 from MegaphoneJon/financial-58
[civicrm-core.git] / tests / phpunit / api / v3 / PriceSetTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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_PriceSetTest
30 * @group headless
31 */
32 class api_v3_PriceSetTest extends CiviUnitTestCase {
33 protected $_apiversion = 3;
34 protected $_params;
35 protected $id = 0;
36 protected $contactIds = [];
37 protected $_entity = 'price_set';
38
39 public $DBResetRequired = TRUE;
40
41 /**
42 * Set up for class.
43 */
44 public function setUp() {
45 parent::setUp();
46 $this->_params = [
47 'name' => 'default_goat_priceset',
48 'title' => 'Goat accessories',
49 'is_active' => 1,
50 'help_pre' => "Please describe your goat in detail",
51 'help_post' => "thank you for your time",
52 'extends' => 2,
53 'financial_type_id' => 1,
54 'is_quick_config' => 1,
55 'is_reserved' => 1,
56 ];
57 }
58
59 /**
60 * Test create price set.
61 */
62 public function testCreatePriceSet() {
63 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
64 $this->assertEquals(1, $result['count']);
65 $this->assertNotNull($result['values'][$result['id']]['id']);
66 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
67 }
68
69 /**
70 * Test for creating price sets used for both events and contributions.
71 */
72 public function testCreatePriceSetForEventAndContribution() {
73 // Create the price set
74 $createParams = [
75 'name' => 'some_price_set',
76 'title' => 'Some Price Set',
77 'is_active' => 1,
78 'financial_type_id' => 1,
79 'extends' => [1, 2],
80 ];
81 $createResult = $this->callAPIAndDocument($this->_entity, 'create', $createParams, __FUNCTION__, __FILE__);
82
83 // Get priceset we just created.
84 $result = $this->callAPISuccess($this->_entity, 'getSingle', [
85 'id' => $createResult['id'],
86 ]);
87
88 // Count the number of items in 'extends'.
89 $this->assertEquals(2, count($result['extends']));
90 }
91
92 /**
93 * Check that no name doesn't cause failure.
94 */
95 public function testCreatePriceSetNoName() {
96 $params = $this->_params;
97 unset($params['name']);
98 $this->callAPISuccess($this->_entity, 'create', $params);
99 }
100
101 /**
102 */
103 public function testGetBasicPriceSet() {
104 $getParams = [
105 'name' => 'default_contribution_amount',
106 ];
107 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
108 $this->assertEquals(1, $getResult['count']);
109 }
110
111 public function testEventPriceSet() {
112 $event = $this->callAPISuccess('event', 'create', [
113 'title' => 'Event with Price Set',
114 'event_type_id' => 1,
115 'is_public' => 1,
116 'start_date' => 20151021,
117 'end_date' => 20151023,
118 'is_active' => 1,
119 ]);
120 $createParams = [
121 'entity_table' => 'civicrm_event',
122 'entity_id' => $event['id'],
123 'name' => 'event price',
124 'title' => 'event price',
125 'extends' => 1,
126 ];
127 $createResult = $this->callAPIAndDocument($this->_entity, 'create', $createParams, __FUNCTION__, __FILE__);
128 $result = $this->callAPISuccess($this->_entity, 'get', [
129 'id' => $createResult['id'],
130 ]);
131 $this->assertEquals(['civicrm_event' => [$event['id']]], $result['values'][$createResult['id']]['entity']);
132 }
133
134 public function testDeletePriceSet() {
135 $startCount = $this->callAPISuccess($this->_entity, 'getcount', []);
136 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
137 $deleteParams = ['id' => $createResult['id']];
138 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
139 $endCount = $this->callAPISuccess($this->_entity, 'getcount', []);
140 $this->assertEquals($startCount, $endCount);
141 }
142
143 public function testGetFieldsPriceSet() {
144 $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
145 $this->assertEquals(16, $result['values']['is_quick_config']['type']);
146 }
147
148 public static function setUpBeforeClass() {
149 // put stuff here that should happen before all tests in this unit
150 }
151
152 public static function tearDownAfterClass() {
153 $tablesToTruncate = [
154 'civicrm_contact',
155 'civicrm_contribution',
156 ];
157 $unitTest = new CiviUnitTestCase();
158 $unitTest->quickCleanup($tablesToTruncate);
159 }
160
161 }