tests/phpunit/** - Remove unnecessary "require_once" statements
[civicrm-core.git] / tests / phpunit / api / v3 / UFJoinTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * Test class for UFGroup API - civicrm_uf_*
30 * @todo Split UFGroup and UFJoin tests
31 *
32 * @package CiviCRM
33 */
34 class api_v3_UFJoinTest extends CiviUnitTestCase {
35 // ids from the uf_group_test.xml fixture
36 protected $_ufGroupId = 11;
37 protected $_ufFieldId;
38 protected $_contactId = 69;
39 protected $_apiversion;
40
41 protected function setUp() {
42 parent::setUp();
43 // Truncate the tables
44 $this->quickCleanup(
45 array(
46 'civicrm_group',
47 'civicrm_contact',
48 'civicrm_uf_group',
49 'civicrm_uf_join',
50 'civicrm_uf_match',
51 )
52 );
53 $this->_apiversion = 3;
54 $op = new PHPUnit_Extensions_Database_Operation_Insert();
55 $op->execute(
56 $this->_dbconn,
57 $this->createFlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml')
58 );
59 }
60
61 public function tearDown() {
62 // Truncate the tables
63 $this->quickCleanup(
64 array(
65 'civicrm_group',
66 'civicrm_contact',
67 'civicrm_uf_group',
68 'civicrm_uf_join',
69 'civicrm_uf_match',
70 )
71 );
72 }
73
74 /**
75 * Find uf join group id.
76 */
77 public function testFindUFGroupId() {
78 $params = array(
79 'module' => 'CiviContribute',
80 'entity_table' => 'civicrm_contribution_page',
81 'entity_id' => 1,
82 'weight' => 1,
83 'uf_group_id' => $this->_ufGroupId,
84 'is_active' => 1,
85 );
86 $ufJoin = $this->callAPISuccess('uf_join', 'create', $params);
87
88 $searchParams = array(
89 'entity_table' => 'civicrm_contribution_page',
90 'entity_id' => 1,
91 );
92 $result = $this->callAPISuccess('uf_join', 'get', $searchParams);
93
94 foreach ($result['values'] as $key => $value) {
95 $this->assertEquals($value['uf_group_id'], $this->_ufGroupId);
96 }
97 }
98
99
100 public function testUFJoinEditWrongParamsType() {
101 $params = 'a string';
102 $result = $this->callAPIFailure('uf_join', 'create', $params);
103 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
104 }
105
106 public function testUFJoinEditEmptyParams() {
107 $params = array();
108 $result = $this->callAPIFailure('uf_join', 'create', $params);
109 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: module, weight, uf_group_id');
110 }
111
112 public function testUFJoinEditWithoutUFGroupId() {
113 $params = array(
114 'module' => 'CiviContribute',
115 'entity_table' => 'civicrm_contribution_page',
116 'entity_id' => 1,
117 'weight' => 1,
118 'is_active' => 1,
119 );
120 $result = $this->callAPIFailure('uf_join', 'create', $params);
121 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id');
122 }
123
124 /**
125 * Create/update uf join
126 */
127 public function testCreateUFJoin() {
128 $params = array(
129 'module' => 'CiviContribute',
130 'entity_table' => 'civicrm_contribution_page',
131 'entity_id' => 1,
132 'weight' => 1,
133 'uf_group_id' => $this->_ufGroupId,
134 'is_active' => 1,
135 'sequential' => 1,
136 );
137 $ufJoin = $this->callAPIAndDocument('uf_join', 'create', $params, __FUNCTION__, __FILE__);
138 $this->assertEquals($ufJoin['values'][0]['module'], $params['module']);
139 $this->assertEquals($ufJoin['values'][0]['uf_group_id'], $params['uf_group_id']);
140 $this->assertEquals($ufJoin['values'][0]['is_active'], $params['is_active']);
141
142 $params = array(
143 'id' => $ufJoin['id'],
144 'module' => 'CiviContribute',
145 'entity_table' => 'civicrm_contribution_page',
146 'entity_id' => 1,
147 'weight' => 1,
148 'uf_group_id' => $this->_ufGroupId,
149 'is_active' => 0,
150 'sequential' => 1,
151 );
152 $ufJoinUpdated = $this->callAPISuccess('uf_join', 'create', $params);
153 $this->assertEquals($ufJoinUpdated['values'][0]['module'], $params['module']);
154 $this->assertEquals($ufJoinUpdated['values'][0]['uf_group_id'], $params['uf_group_id']);
155 $this->assertEquals($ufJoinUpdated['values'][0]['is_active'], $params['is_active']);
156 }
157
158
159 public function testFindUFJoinWrongParamsType() {
160 $params = 'a string';
161 $result = $this->callAPIFailure('uf_join', 'create', $params);
162 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
163 }
164
165 public function testFindUFJoinEmptyParams() {
166 $result = $this->callAPIFailure('uf_join', 'create', array());
167 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: module, weight, uf_group_id');
168 }
169
170 public function testFindUFJoinWithoutUFGroupId() {
171 $params = array(
172 'module' => 'CiviContribute',
173 'entity_table' => 'civicrm_contribution_page',
174 'entity_id' => 1,
175 'weight' => 1,
176 'is_active' => 1,
177 );
178 $result = $this->callAPIFailure('uf_join', 'create', $params);
179 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id');
180 }
181
182 /**
183 * Find uf join id.
184 */
185 public function testGetUFJoinId() {
186 $params = array(
187 'module' => 'CiviContribute',
188 'entity_table' => 'civicrm_contribution_page',
189 'entity_id' => 1,
190 'weight' => 1,
191 'uf_group_id' => $this->_ufGroupId,
192 'is_active' => 1,
193 );
194
195 $ufJoin = $this->callAPISuccess('uf_join', 'create', $params);
196 $searchParams = array(
197 'entity_table' => 'civicrm_contribution_page',
198 'entity_id' => 1,
199 'sequential' => 1,
200 );
201
202 $result = $this->callAPIAndDocument('uf_join', 'get', $searchParams, __FUNCTION__, __FILE__);
203 $this->assertEquals($result['values'][0]['module'], $params['module']);
204 $this->assertEquals($result['values'][0]['uf_group_id'], $params['uf_group_id']);
205 $this->assertEquals($result['values'][0]['entity_id'], $params['entity_id']);
206 }
207
208 /**
209 * Test civicrm_activity_create() using example code.
210 */
211 public function testUFJoinCreateExample() {
212 require_once 'api/v3/examples/UFJoin/Create.php';
213 $result = UF_join_create_example();
214 $expectedResult = UF_join_create_expectedresult();
215 $this->assertEquals($result, $expectedResult);
216 }
217
218 }