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