Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / UFMatchTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test class for UFGroup API - civicrm_uf_*
14 * @todo Split UFGroup and UFJoin tests
15 *
16 * @package CiviCRM
17 * @group headless
18 */
19 class api_v3_UFMatchTest extends CiviUnitTestCase {
20 /**
21 * ids from the uf_group_test.xml fixture
22 * @var int
23 */
24 protected $_ufGroupId = 11;
25 protected $_ufFieldId;
26 protected $_contactId;
27 protected $_params = [];
28
29 protected function setUp() {
30 parent::setUp();
31 $this->quickCleanup(
32 [
33 'civicrm_group',
34 'civicrm_contact',
35 'civicrm_uf_group',
36 'civicrm_uf_join',
37 'civicrm_uf_match',
38 ]
39 );
40 $this->_contactId = $this->individualCreate();
41 $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml');
42
43 $this->_params = [
44 'contact_id' => $this->_contactId,
45 'uf_id' => '2',
46 'uf_name' => 'blahdyblah@gmail.com',
47 'domain_id' => 1,
48 ];
49 }
50
51 public function tearDown() {
52 // Truncate the tables
53 $this->quickCleanup(
54 [
55 'civicrm_group',
56 'civicrm_contact',
57 'civicrm_uf_group',
58 'civicrm_uf_join',
59 'civicrm_uf_match',
60 ]
61 );
62 }
63
64 /**
65 * Fetch contact id by uf id.
66 * @param int $version
67 * @dataProvider versionThreeAndFour
68 */
69 public function testGetUFMatchID($version) {
70 $this->_apiversion = $version;
71 $params = [
72 'uf_id' => 42,
73 ];
74 $result = $this->callAPISuccess('uf_match', 'get', $params);
75 $this->assertEquals($result['values'][$result['id']]['contact_id'], 69);
76 }
77
78 /**
79 * Fetch uf id by contact id.
80 * @param int $version
81 * @dataProvider versionThreeAndFour
82 */
83 public function testGetUFID($version) {
84 $this->_apiversion = $version;
85 $params = [
86 'contact_id' => 69,
87 ];
88 $result = $this->callAPIAndDocument('uf_match', 'get', $params, __FUNCTION__, __FILE__);
89 $this->assertEquals($result['values'][$result['id']]['uf_id'], 42);
90 }
91
92 /**
93 * Test civicrm_activity_create() using example code
94 * @param int $version
95 * @dataProvider versionThreeAndFour
96 */
97 public function testUFMatchGetExample($version) {
98 $this->_apiversion = $version;
99 require_once 'api/v3/examples/UFMatch/Get.ex.php';
100 $result = UF_match_get_example();
101 $expectedResult = UF_match_get_expectedresult();
102 $this->assertEquals($result, $expectedResult);
103 }
104
105 /**
106 * @param int $version
107 * @dataProvider versionThreeAndFour
108 */
109 public function testCreate($version) {
110 $this->_apiversion = $version;
111 $result = $this->callAPISuccess('uf_match', 'create', $this->_params);
112 $this->getAndCheck($this->_params, $result['id'], 'uf_match');
113 }
114
115 /**
116 * Test Civi to CMS email sync optional
117 * @param int $version
118 * @dataProvider versionThreeAndFour
119 */
120 public function testUFNameMatchSync($version) {
121 $this->_apiversion = $version;
122 $this->callAPISuccess('uf_match', 'create', $this->_params);
123 $email1 = substr(sha1(rand()), 0, 7) . '@test.com';
124 $email2 = substr(sha1(rand()), 0, 7) . '@test.com';
125
126 // Case A: Enable CMS integration
127 Civi::settings()->set('syncCMSEmail', TRUE);
128 $this->callAPISuccess('email', 'create', [
129 'contact_id' => $this->_contactId,
130 'email' => $email1,
131 'is_primary' => 1,
132 ]);
133 $ufName = $this->callAPISuccess('uf_match', 'getvalue', [
134 'contact_id' => $this->_contactId,
135 'return' => 'uf_name',
136 ]);
137 $this->assertEquals($email1, $ufName);
138
139 // Case B: Disable CMS integration
140 Civi::settings()->set('syncCMSEmail', FALSE);
141 $this->callAPISuccess('email', 'create', [
142 'contact_id' => $this->_contactId,
143 'email' => $email2,
144 'is_primary' => 1,
145 ]);
146 $ufName = $this->callAPISuccess('uf_match', 'getvalue', [
147 'contact_id' => $this->_contactId,
148 'return' => 'uf_name',
149 ]);
150 $this->assertNotEquals($email2, $ufName, 'primary email will not match if changed on disabled CMS integration setting');
151 $this->assertEquals($email1, $ufName);
152 }
153
154 /**
155 * @param int $version
156 * @dataProvider versionThreeAndFour
157 */
158 public function testDelete($version) {
159 $this->_apiversion = $version;
160 $result = $this->callAPISuccess('uf_match', 'create', $this->_params);
161 $this->assertEquals(1, $this->callAPISuccess('uf_match', 'getcount', [
162 'id' => $result['id'],
163 ]));
164 $this->callAPISuccess('uf_match', 'delete', [
165 'id' => $result['id'],
166 ]);
167 $this->assertEquals(0, $this->callAPISuccess('uf_match', 'getcount', [
168 'id' => $result['id'],
169 ]));
170 }
171
172 }