Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / PriceSetTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 = array();
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 = array(
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 public function tearDown() {
60 }
61
62 /**
63 * Test create price set.
64 */
65 public function testCreatePriceSet() {
66 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
67 $this->assertEquals(1, $result['count']);
68 $this->assertNotNull($result['values'][$result['id']]['id']);
69 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
70 }
71
72 /**
73 * Test for creating price sets used for both events and contributions.
74 */
75 public function testCreatePriceSetForEventAndContribution() {
76 // Create the price set
77 $createParams = array(
78 'name' => 'some_price_set',
79 'title' => 'Some Price Set',
80 'is_active' => 1,
81 'financial_type_id' => 1,
82 'extends' => array(1, 2),
83 );
84 $createResult = $this->callAPIAndDocument($this->_entity, 'create', $createParams, __FUNCTION__, __FILE__);
85
86 // Get priceset we just created.
87 $result = $this->callAPISuccess($this->_entity, 'getSingle', array(
88 'id' => $createResult['id'],
89 ));
90
91 // Count the number of items in 'extends'.
92 $this->assertEquals(2, count($result['extends']));
93 }
94
95 /**
96 * Check that no name doesn't cause failure.
97 */
98 public function testCreatePriceSetNoName() {
99 $params = $this->_params;
100 unset($params['name']);
101 $this->callAPISuccess($this->_entity, 'create', $params);
102 }
103
104 /**
105 */
106 public function testGetBasicPriceSet() {
107 $getParams = array(
108 'name' => 'default_contribution_amount',
109 );
110 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
111 $this->assertEquals(1, $getResult['count']);
112 }
113
114 public function testEventPriceSet() {
115 $event = $this->callAPISuccess('event', 'create', array(
116 'title' => 'Event with Price Set',
117 'event_type_id' => 1,
118 'is_public' => 1,
119 'start_date' => 20151021,
120 'end_date' => 20151023,
121 'is_active' => 1,
122 ));
123 $createParams = array(
124 'entity_table' => 'civicrm_event',
125 'entity_id' => $event['id'],
126 'name' => 'event price',
127 'title' => 'event price',
128 'extends' => 1,
129 );
130 $createResult = $this->callAPIAndDocument($this->_entity, 'create', $createParams, __FUNCTION__, __FILE__);
131 $result = $this->callAPISuccess($this->_entity, 'get', array(
132 'id' => $createResult['id'],
133 ));
134 $this->assertEquals(array('civicrm_event' => array($event['id'])), $result['values'][$createResult['id']]['entity']);
135 }
136
137 public function testDeletePriceSet() {
138 $startCount = $this->callAPISuccess($this->_entity, 'getcount', array());
139 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
140 $deleteParams = array('id' => $createResult['id']);
141 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
142 $endCount = $this->callAPISuccess($this->_entity, 'getcount', array());
143 $this->assertEquals($startCount, $endCount);
144 }
145
146 public function testGetFieldsPriceSet() {
147 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
148 $this->assertEquals(16, $result['values']['is_quick_config']['type']);
149 }
150
151 public static function setUpBeforeClass() {
152 // put stuff here that should happen before all tests in this unit
153 }
154
155 public static function tearDownAfterClass() {
156 $tablesToTruncate = array(
157 'civicrm_contact',
158 'civicrm_contribution',
159 );
160 $unitTest = new CiviUnitTestCase();
161 $unitTest->quickCleanup($tablesToTruncate);
162 }
163
164 }