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