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