Update copyright date for 2020
[civicrm-core.git] / tests / phpunit / api / v3 / UFMatchTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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_UFMatchTest extends CiviUnitTestCase {
36 /**
37 * ids from the uf_group_test.xml fixture
38 * @var int
39 */
40 protected $_ufGroupId = 11;
41 protected $_ufFieldId;
42 protected $_contactId;
43 protected $_params = [];
44
45 protected function setUp() {
46 parent::setUp();
47 $this->quickCleanup(
48 [
49 'civicrm_group',
50 'civicrm_contact',
51 'civicrm_uf_group',
52 'civicrm_uf_join',
53 'civicrm_uf_match',
54 ]
55 );
56 $this->_contactId = $this->individualCreate();
57 $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml');
58
59 $this->_params = [
60 'contact_id' => $this->_contactId,
61 'uf_id' => '2',
62 'uf_name' => 'blahdyblah@gmail.com',
63 'domain_id' => 1,
64 ];
65 }
66
67 public function tearDown() {
68 // Truncate the tables
69 $this->quickCleanup(
70 [
71 'civicrm_group',
72 'civicrm_contact',
73 'civicrm_uf_group',
74 'civicrm_uf_join',
75 'civicrm_uf_match',
76 ]
77 );
78 }
79
80 /**
81 * Fetch contact id by uf id.
82 * @param int $version
83 * @dataProvider versionThreeAndFour
84 */
85 public function testGetUFMatchID($version) {
86 $this->_apiversion = $version;
87 $params = [
88 'uf_id' => 42,
89 ];
90 $result = $this->callAPISuccess('uf_match', 'get', $params);
91 $this->assertEquals($result['values'][$result['id']]['contact_id'], 69);
92 }
93
94 /**
95 * @param int $version
96 * @dataProvider versionThreeAndFour
97 */
98 public function testGetUFMatchIDWrongParam($version) {
99 $this->_apiversion = $version;
100 $params = 'a string';
101 $result = $this->callAPIFailure('uf_match', 'get', $params);
102 }
103
104 /**
105 * Fetch uf id by contact id.
106 * @param int $version
107 * @dataProvider versionThreeAndFour
108 */
109 public function testGetUFID($version) {
110 $this->_apiversion = $version;
111 $params = [
112 'contact_id' => 69,
113 ];
114 $result = $this->callAPIAndDocument('uf_match', 'get', $params, __FUNCTION__, __FILE__);
115 $this->assertEquals($result['values'][$result['id']]['uf_id'], 42);
116 }
117
118 /**
119 * @param int $version
120 * @dataProvider versionThreeAndFour
121 */
122 public function testGetUFIDWrongParam($version) {
123 $this->_apiversion = $version;
124 $params = 'a string';
125 $result = $this->callAPIFailure('uf_match', 'get', $params);
126 }
127
128 /**
129 * Test civicrm_activity_create() using example code
130 * @param int $version
131 * @dataProvider versionThreeAndFour
132 */
133 public function testUFMatchGetExample($version) {
134 $this->_apiversion = $version;
135 require_once 'api/v3/examples/UFMatch/Get.ex.php';
136 $result = UF_match_get_example();
137 $expectedResult = UF_match_get_expectedresult();
138 $this->assertEquals($result, $expectedResult);
139 }
140
141 /**
142 * @param int $version
143 * @dataProvider versionThreeAndFour
144 */
145 public function testCreate($version) {
146 $this->_apiversion = $version;
147 $result = $this->callAPISuccess('uf_match', 'create', $this->_params);
148 $this->getAndCheck($this->_params, $result['id'], 'uf_match');
149 }
150
151 /**
152 * Test Civi to CMS email sync optional
153 * @param int $version
154 * @dataProvider versionThreeAndFour
155 */
156 public function testUFNameMatchSync($version) {
157 $this->_apiversion = $version;
158 $this->callAPISuccess('uf_match', 'create', $this->_params);
159 $email1 = substr(sha1(rand()), 0, 7) . '@test.com';
160 $email2 = substr(sha1(rand()), 0, 7) . '@test.com';
161
162 // Case A: Enable CMS integration
163 Civi::settings()->set('syncCMSEmail', TRUE);
164 $this->callAPISuccess('email', 'create', [
165 'contact_id' => $this->_contactId,
166 'email' => $email1,
167 'is_primary' => 1,
168 ]);
169 $ufName = $this->callAPISuccess('uf_match', 'getvalue', [
170 'contact_id' => $this->_contactId,
171 'return' => 'uf_name',
172 ]);
173 $this->assertEquals($email1, $ufName);
174
175 // Case B: Disable CMS integration
176 Civi::settings()->set('syncCMSEmail', FALSE);
177 $this->callAPISuccess('email', 'create', [
178 'contact_id' => $this->_contactId,
179 'email' => $email2,
180 'is_primary' => 1,
181 ]);
182 $ufName = $this->callAPISuccess('uf_match', 'getvalue', [
183 'contact_id' => $this->_contactId,
184 'return' => 'uf_name',
185 ]);
186 $this->assertNotEquals($email2, $ufName, 'primary email will not match if changed on disabled CMS integration setting');
187 $this->assertEquals($email1, $ufName);
188 }
189
190 /**
191 * @param int $version
192 * @dataProvider versionThreeAndFour
193 */
194 public function testDelete($version) {
195 $this->_apiversion = $version;
196 $result = $this->callAPISuccess('uf_match', 'create', $this->_params);
197 $this->assertEquals(1, $this->callAPISuccess('uf_match', 'getcount', [
198 'id' => $result['id'],
199 ]));
200 $this->callAPISuccess('uf_match', 'delete', [
201 'id' => $result['id'],
202 ]);
203 $this->assertEquals(0, $this->callAPISuccess('uf_match', 'getcount', [
204 'id' => $result['id'],
205 ]));
206 }
207
208 }