| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 | * Test class for UFGroup API - civicrm_uf_* |
| 30 | * @todo Split UFGroup and UFJoin tests |
| 31 | * |
| 32 | * @package CiviCRM |
| 33 | * @group headless |
| 34 | */ |
| 35 | class api_v3_UFGroupTest extends CiviUnitTestCase { |
| 36 | // ids from the uf_group_test.xml fixture |
| 37 | protected $_ufGroupId; |
| 38 | protected $_ufFieldId; |
| 39 | protected $_contactId; |
| 40 | protected $_groupId; |
| 41 | protected $_apiversion = 3; |
| 42 | protected $params; |
| 43 | |
| 44 | protected function setUp() { |
| 45 | parent::setUp(); |
| 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', |
| 51 | 'help_pre' => 'Profile to Test API', |
| 52 | 'title' => 'Test Profile', |
| 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 | )); |
| 60 | $this->_ufMatchId = $ufMatch['id']; |
| 61 | $this->params = array( |
| 62 | 'add_captcha' => 1, |
| 63 | 'add_contact_to_group' => $this->_groupId, |
| 64 | 'group' => $this->_groupId, |
| 65 | 'cancel_URL' => 'http://example.org/cancel', |
| 66 | 'created_date' => '2009-06-27 00:00:00', |
| 67 | 'created_id' => $this->_contactId, |
| 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', |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | public function tearDown() { |
| 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 | /** |
| 99 | * Updating group. |
| 100 | */ |
| 101 | public function testUpdateUFGroup() { |
| 102 | $params = array( |
| 103 | 'title' => 'Edited Test Profile', |
| 104 | 'help_post' => 'Profile Pro help text.', |
| 105 | 'is_active' => 1, |
| 106 | 'id' => $this->_ufGroupId, |
| 107 | ); |
| 108 | |
| 109 | $result = $this->callAPISuccess('uf_group', 'create', $params); |
| 110 | foreach ($params as $key => $value) { |
| 111 | $this->assertEquals($result['values'][$result['id']][$key], $value); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public function testUFGroupCreate() { |
| 116 | |
| 117 | $result = $this->callAPIAndDocument('uf_group', 'create', $this->params, __FUNCTION__, __FILE__); |
| 118 | $this->assertAPISuccess($result); |
| 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']); |
| 121 | $this->params['created_date'] = date('YmdHis', strtotime($this->params['created_date'])); |
| 122 | foreach ($this->params as $key => $value) { |
| 123 | if ($key == 'add_contact_to_group' or $key == 'group') { |
| 124 | continue; |
| 125 | } |
| 126 | $expected = $this->params[$key]; |
| 127 | $received = $result['values'][$result['id']][$key]; |
| 128 | $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public function testUFGroupCreateWithWrongParams() { |
| 133 | $result = $this->callAPIFailure('uf_group', 'create', array('name' => 'A title-less group')); |
| 134 | } |
| 135 | |
| 136 | public function testUFGroupUpdate() { |
| 137 | $params = array( |
| 138 | 'id' => $this->_ufGroupId, |
| 139 | 'add_captcha' => 1, |
| 140 | 'add_contact_to_group' => $this->_groupId, |
| 141 | 'cancel_URL' => 'http://example.org/cancel', |
| 142 | 'created_date' => '2009-06-27', |
| 143 | 'created_id' => $this->_contactId, |
| 144 | 'group' => $this->_groupId, |
| 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', |
| 158 | 'title' => 'Test Group', |
| 159 | ); |
| 160 | $result = $this->callAPISuccess('uf_group', 'create', $params); |
| 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 | |
| 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']); |
| 171 | } |
| 172 | |
| 173 | public function testUFGroupGet() { |
| 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__); |
| 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']); |
| 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 |
| 181 | if ($key == 'add_contact_to_group' or $key == 'group' or $key == 'created_date') { |
| 182 | continue; |
| 183 | } |
| 184 | $expected = $this->params[$key]; |
| 185 | $received = $result['values'][$result['id']][$key]; |
| 186 | $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public function testUFGroupUpdateWithEmptyParams() { |
| 191 | $result = $this->callAPIFailure('uf_group', 'create', array(), 'Mandatory key(s) missing from params array: title'); |
| 192 | } |
| 193 | |
| 194 | public function testUFGroupDelete() { |
| 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__); |
| 200 | } |
| 201 | |
| 202 | } |