Merge pull request #11086 from agileware/CRM-21277
[civicrm-core.git] / tests / phpunit / api / v3 / UFGroupTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
acb109b7 33 * @group headless
6a488035
TO
34 */
35class api_v3_UFGroupTest extends CiviUnitTestCase {
36 // ids from the uf_group_test.xml fixture
7fbb4198 37 protected $_ufGroupId;
6a488035 38 protected $_ufFieldId;
7fbb4198 39 protected $_contactId;
40 protected $_groupId;
6c6e6187 41 protected $_apiversion = 3;
6a488035
TO
42 protected $params;
43
44 protected function setUp() {
45 parent::setUp();
7fbb4198 46 $this->_groupId = $this->groupCreate();
47 $this->_contactId = $this->individualCreate();
48 $this->createLoggedInUser();
49 $ufGroup = $this->callAPISuccess('uf_group', 'create', array(
50 'group_type' => 'Contact',
92915c55 51 'help_pre' => 'Profile to Test API',
6c6e6187 52 'title' => 'Test Profile',
7fbb4198 53 ));
54 $this->_ufGroupId = $ufGroup['id'];
55 $ufMatch = $this->callAPISuccess('uf_match', 'create', array(
56 'contact_id' => $this->_contactId,
57 'uf_id' => 42,
58 'uf_name' => 'email@mail.com',
59 ));
08fe8c7e 60 $this->_ufMatchId = $ufMatch['id'];
6a488035
TO
61 $this->params = array(
62 'add_captcha' => 1,
7fbb4198 63 'add_contact_to_group' => $this->_groupId,
64 'group' => $this->_groupId,
6a488035
TO
65 'cancel_URL' => 'http://example.org/cancel',
66 'created_date' => '2009-06-27 00:00:00',
7fbb4198 67 'created_id' => $this->_contactId,
6a488035
TO
68 'group_type' => 'Individual,Contact',
69 'help_post' => 'help post',
70 'help_pre' => 'help pre',
71 'is_active' => 0,
72 'is_cms_user' => 1,
73 'is_edit_link' => 1,
74 'is_map' => 1,
75 'is_reserved' => 1,
76 'is_uf_link' => 1,
77 'is_update_dupe' => 1,
78 'name' => 'Test_Group',
79 'notify' => 'admin@example.org',
80 'post_URL' => 'http://example.org/post',
81 'title' => 'Test Group',
6a488035
TO
82 );
83 }
84
00be9182 85 public function tearDown() {
6a488035
TO
86 // Truncate the tables
87 $this->quickCleanup(
88 array(
89 'civicrm_group',
90 'civicrm_contact',
91 'civicrm_uf_group',
92 'civicrm_uf_join',
93 'civicrm_uf_match',
94 )
95 );
96 }
97
98 /**
eceb18cc 99 * Updating group.
6a488035
TO
100 */
101 public function testUpdateUFGroup() {
102 $params = array(
103 'title' => 'Edited Test Profile',
104 'help_post' => 'Profile Pro help text.',
105 'is_active' => 1,
21dfd5f5 106 'id' => $this->_ufGroupId,
92915c55 107 );
6a488035 108
7fbb4198 109 $result = $this->callAPISuccess('uf_group', 'create', $params);
6a488035
TO
110 foreach ($params as $key => $value) {
111 $this->assertEquals($result['values'][$result['id']][$key], $value);
112 }
113 }
114
00be9182 115 public function testUFGroupCreate() {
6a488035 116
7fbb4198 117 $result = $this->callAPIAndDocument('uf_group', 'create', $this->params, __FUNCTION__, __FILE__);
6a73ef3f 118 $this->assertAPISuccess($result);
ba4a1892
TM
119 $this->assertEquals($result['values'][$result['id']]['add_to_group_id'], $this->params['add_contact_to_group']);
120 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $this->params['group']);
6a488035
TO
121 $this->params['created_date'] = date('YmdHis', strtotime($this->params['created_date']));
122 foreach ($this->params as $key => $value) {
7fbb4198 123 if ($key == 'add_contact_to_group' or $key == 'group') {
6a488035
TO
124 continue;
125 }
126 $expected = $this->params[$key];
127 $received = $result['values'][$result['id']][$key];
6a488035
TO
128 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
129 }
130 }
131
00be9182 132 public function testUFGroupCreateWithWrongParams() {
7fbb4198 133 $result = $this->callAPIFailure('uf_group', 'create', array('name' => 'A title-less group'));
6a488035
TO
134 }
135
00be9182 136 public function testUFGroupUpdate() {
6a488035
TO
137 $params = array(
138 'id' => $this->_ufGroupId,
139 'add_captcha' => 1,
7fbb4198 140 'add_contact_to_group' => $this->_groupId,
6a488035
TO
141 'cancel_URL' => 'http://example.org/cancel',
142 'created_date' => '2009-06-27',
7fbb4198 143 'created_id' => $this->_contactId,
144 'group' => $this->_groupId,
6a488035
TO
145 'group_type' => 'Individual,Contact',
146 'help_post' => 'help post',
147 'help_pre' => 'help pre',
148 'is_active' => 0,
149 'is_cms_user' => 1,
150 'is_edit_link' => 1,
151 'is_map' => 1,
152 'is_reserved' => 1,
153 'is_uf_link' => 1,
154 'is_update_dupe' => 1,
155 'name' => 'test_group',
156 'notify' => 'admin@example.org',
157 'post_URL' => 'http://example.org/post',
92915c55
TO
158 'title' => 'Test Group',
159 );
7fbb4198 160 $result = $this->callAPISuccess('uf_group', 'create', $params);
6a488035
TO
161 $params['created_date'] = date('YmdHis', strtotime($params['created_date']));
162 foreach ($params as $key => $value) {
163 if ($key == 'add_contact_to_group' or $key == 'group') {
164 continue;
165 }
166 $this->assertEquals($result['values'][$result['id']][$key], $params[$key], $key . " doesn't match " . $value);
167 }
168
ba4a1892
TM
169 $this->assertEquals($result['values'][$this->_ufGroupId]['add_to_group_id'], $params['add_contact_to_group']);
170 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $params['group']);
6a488035
TO
171 }
172
00be9182 173 public function testUFGroupGet() {
7fbb4198 174 $result = $this->callAPISuccess('uf_group', 'create', $this->params);
175 $params = array('id' => $result['id']);
176 $result = $this->callAPIAndDocument('uf_group', 'get', $params, __FUNCTION__, __FILE__);
ba4a1892
TM
177 $this->assertEquals($result['values'][$result['id']]['add_to_group_id'], $this->params['add_contact_to_group']);
178 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $this->params['group']);
6a488035
TO
179 foreach ($this->params as $key => $value) {
180 // skip created date because it doesn't seem to be working properly & fixing date handling is for another day
7fbb4198 181 if ($key == 'add_contact_to_group' or $key == 'group' or $key == 'created_date') {
6a488035
TO
182 continue;
183 }
184 $expected = $this->params[$key];
185 $received = $result['values'][$result['id']][$key];
6a488035
TO
186 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
187 }
188 }
189
00be9182 190 public function testUFGroupUpdateWithEmptyParams() {
86939032 191 $result = $this->callAPIFailure('uf_group', 'create', array(), 'Mandatory key(s) missing from params array: title');
6a488035
TO
192 }
193
00be9182 194 public function testUFGroupDelete() {
7fbb4198 195 $ufGroup = $this->callAPISuccess('uf_group', 'create', $this->params);
196 $params = array('id' => $ufGroup['id']);
197 $this->assertEquals(1, $this->callAPISuccess('uf_group', 'getcount', $params), "in line " . __LINE__);
198 $result = $this->callAPIAndDocument('uf_group', 'delete', $params, __FUNCTION__, __FILE__);
199 $this->assertEquals(0, $this->callAPISuccess('uf_group', 'getcount', $params), "in line " . __LINE__);
6a488035 200 }
96025800 201
6a488035 202}