Add templates/CRM/common/unittests.tpl
[civicrm-core.git] / tests / phpunit / api / v3 / UFJoinTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Test class for UFGroup API - civicrm_uf_*
30 * @todo Split UFGroup and UFJoin tests
31 *
6c6e6187 32 * @package CiviCRM
6a488035
TO
33 */
34class 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;
b7c9bc4c 40
6a488035
TO
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;
d5cc0fc2 54 $op = new PHPUnit_Extensions_Database_Operation_Insert();
6a488035
TO
55 $op->execute(
56 $this->_dbconn,
bbfd46a5 57 $this->createFlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml')
6a488035 58 );
6a488035
TO
59 }
60
00be9182 61 public function tearDown() {
6a488035
TO
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 /**
eceb18cc 75 * Find uf join group id.
6a488035
TO
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,
92915c55
TO
84 'is_active' => 1,
85 );
e6ff1593 86 $ufJoin = $this->callAPISuccess('uf_join', 'create', $params);
6a488035
TO
87
88 $searchParams = array(
89 'entity_table' => 'civicrm_contribution_page',
92915c55
TO
90 'entity_id' => 1,
91 );
e6ff1593 92 $result = $this->callAPISuccess('uf_join', 'get', $searchParams);
6a488035
TO
93
94 foreach ($result['values'] as $key => $value) {
a15773db 95 $this->assertEquals($value['uf_group_id'], $this->_ufGroupId);
6a488035
TO
96 }
97 }
98
99
100 public function testUFJoinEditWrongParamsType() {
101 $params = 'a string';
d0e1eff2 102 $result = $this->callAPIFailure('uf_join', 'create', $params);
ba4a1892 103 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
6a488035
TO
104 }
105
106 public function testUFJoinEditEmptyParams() {
107 $params = array();
d0e1eff2 108 $result = $this->callAPIFailure('uf_join', 'create', $params);
ba4a1892 109 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: module, weight, uf_group_id');
6a488035
TO
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,
92915c55
TO
118 'is_active' => 1,
119 );
d0e1eff2 120 $result = $this->callAPIFailure('uf_join', 'create', $params);
ba4a1892 121 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id');
6a488035
TO
122 }
123
124 /**
100fef9d 125 * Create/update uf join
6a488035
TO
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,
6c6e6187 134 'is_active' => 1,
92915c55 135 'sequential' => 1,
6a488035 136 );
e6ff1593 137 $ufJoin = $this->callAPIAndDocument('uf_join', 'create', $params, __FUNCTION__, __FILE__);
ba4a1892
TM
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']);
6a488035
TO
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,
6c6e6187 149 'is_active' => 0,
92915c55 150 'sequential' => 1,
6a488035 151 );
e6ff1593 152 $ufJoinUpdated = $this->callAPISuccess('uf_join', 'create', $params);
ba4a1892
TM
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']);
6a488035
TO
156 }
157
158
159 public function testFindUFJoinWrongParamsType() {
160 $params = 'a string';
d0e1eff2 161 $result = $this->callAPIFailure('uf_join', 'create', $params);
ba4a1892 162 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
6a488035
TO
163 }
164
165 public function testFindUFJoinEmptyParams() {
d0e1eff2 166 $result = $this->callAPIFailure('uf_join', 'create', array());
ba4a1892 167 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: module, weight, uf_group_id');
6a488035
TO
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,
6a488035 177 );
d0e1eff2 178 $result = $this->callAPIFailure('uf_join', 'create', $params);
ba4a1892 179 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id');
6a488035
TO
180 }
181
182 /**
eceb18cc 183 * Find uf join id.
6a488035
TO
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,
92915c55
TO
192 'is_active' => 1,
193 );
6a488035 194
e6ff1593 195 $ufJoin = $this->callAPISuccess('uf_join', 'create', $params);
6a488035
TO
196 $searchParams = array(
197 'entity_table' => 'civicrm_contribution_page',
6c6e6187 198 'entity_id' => 1,
92915c55 199 'sequential' => 1,
6a488035
TO
200 );
201
e6ff1593 202 $result = $this->callAPIAndDocument('uf_join', 'get', $searchParams, __FUNCTION__, __FILE__);
ba4a1892
TM
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']);
6a488035
TO
206 }
207
208 /**
d177a2a6 209 * Test civicrm_activity_create() using example code.
6a488035 210 */
00be9182 211 public function testUFJoinCreateExample() {
3ec6e38d 212 require_once 'api/v3/examples/UFJoin/Create.php';
6a488035
TO
213 $result = UF_join_create_example();
214 $expectedResult = UF_join_create_expectedresult();
215 $this->assertEquals($result, $expectedResult);
216 }
96025800 217
6a488035 218}