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