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