Merge branch '5.11' of https://github.com/civicrm/civicrm-core
[civicrm-core.git] / tests / phpunit / api / v3 / UFMatchTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Test class for UFGroup API - civicrm_uf_*
30 * @todo Split UFGroup and UFJoin tests
31 *
6c6e6187 32 * @package CiviCRM
acb109b7 33 * @group headless
6a488035
TO
34 */
35class api_v3_UFMatchTest extends CiviUnitTestCase {
36 // ids from the uf_group_test.xml fixture
37 protected $_ufGroupId = 11;
38 protected $_ufFieldId;
39 protected $_contactId;
40 protected $_apiversion;
41 protected $_params = array();
42
b7c9bc4c 43
6a488035
TO
44 protected function setUp() {
45 parent::setUp();
46 $this->_apiversion = 3;
47 $this->quickCleanup(
48 array(
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();
87a890cc 57 $op = new PHPUnit_Extensions_Database_Operation_Insert();
6a488035
TO
58 $op->execute(
59 $this->_dbconn,
bbfd46a5 60 $this->createFlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml')
6a488035
TO
61 );
62
63 $this->_params = array(
64 'contact_id' => $this->_contactId,
65 'uf_id' => '2',
66 'uf_name' => 'blahdyblah@gmail.com',
6a488035 67 'domain_id' => 1,
92915c55 68 );
6a488035
TO
69 }
70
00be9182 71 public function tearDown() {
6a488035
TO
72 // Truncate the tables
73 $this->quickCleanup(
74 array(
75 'civicrm_group',
76 'civicrm_contact',
77 'civicrm_uf_group',
78 'civicrm_uf_join',
79 'civicrm_uf_match',
80 )
81 );
82 }
83
84 /**
eceb18cc 85 * Fetch contact id by uf id.
6a488035
TO
86 */
87 public function testGetUFMatchID() {
88 $params = array(
89 'uf_id' => 42,
6a488035 90 );
e6ff1593 91 $result = $this->callAPISuccess('uf_match', 'get', $params);
6a488035 92 $this->assertEquals($result['values'][$result['id']]['contact_id'], 69);
6a488035
TO
93 }
94
00be9182 95 public function testGetUFMatchIDWrongParam() {
6a488035 96 $params = 'a string';
d0e1eff2 97 $result = $this->callAPIFailure('uf_match', 'get', $params);
6a488035
TO
98 }
99
100 /**
eceb18cc 101 * Fetch uf id by contact id.
6a488035
TO
102 */
103 public function testGetUFID() {
104 $params = array(
105 'contact_id' => 69,
6a488035 106 );
e6ff1593 107 $result = $this->callAPIAndDocument('uf_match', 'get', $params, __FUNCTION__, __FILE__);
6a488035 108 $this->assertEquals($result['values'][$result['id']]['uf_id'], 42);
6a488035
TO
109 }
110
00be9182 111 public function testGetUFIDWrongParam() {
6a488035 112 $params = 'a string';
d0e1eff2 113 $result = $this->callAPIFailure('uf_match', 'get', $params);
6a488035
TO
114 }
115
116 /**
d177a2a6 117 * Test civicrm_activity_create() using example code
6a488035 118 */
00be9182 119 public function testUFMatchGetExample() {
3ec6e38d 120 require_once 'api/v3/examples/UFMatch/Get.php';
6a488035
TO
121 $result = UF_match_get_example();
122 $expectedResult = UF_match_get_expectedresult();
123 $this->assertEquals($result, $expectedResult);
124 }
125
00be9182 126 public function testCreate() {
e6ff1593 127 $result = $this->callAPISuccess('uf_match', 'create', $this->_params);
6a488035
TO
128 $this->getAndCheck($this->_params, $result['id'], 'uf_match');
129 }
130
5e7f101a 131 /**
132 * Test Civi to CMS email sync optional
133 */
134 public function testUFNameMatchSync() {
135 $this->callAPISuccess('uf_match', 'create', $this->_params);
136 $email1 = substr(sha1(rand()), 0, 7) . '@test.com';
137 $email2 = substr(sha1(rand()), 0, 7) . '@test.com';
138
139 // Case A: Enable CMS integration
140 Civi::settings()->set('syncCMSEmail', TRUE);
141 $this->callAPISuccess('email', 'create', array(
142 'contact_id' => $this->_contactId,
143 'email' => $email1,
144 'is_primary' => 1,
145 ));
146 $ufName = $this->callAPISuccess('uf_match', 'getvalue', array(
147 'contact_id' => $this->_contactId,
148 'return' => 'uf_name',
149 ));
150 $this->assertEquals($email1, $ufName);
151
152 // Case B: Disable CMS integration
153 Civi::settings()->set('syncCMSEmail', FALSE);
154 $this->callAPISuccess('email', 'create', array(
155 'contact_id' => $this->_contactId,
156 'email' => $email2,
157 'is_primary' => 1,
158 ));
159 $ufName = $this->callAPISuccess('uf_match', 'getvalue', array(
160 'contact_id' => $this->_contactId,
161 'return' => 'uf_name',
162 ));
163 $this->assertNotEquals($email2, $ufName, 'primary email will not match if changed on disabled CMS integration setting');
164 $this->assertEquals($email1, $ufName);
165 }
166
00be9182 167 public function testDelete() {
e6ff1593 168 $result = $this->callAPISuccess('uf_match', 'create', $this->_params);
6c6e6187 169 $this->assertEquals(1, $this->callAPISuccess('uf_match', 'getcount', array(
92915c55
TO
170 'id' => $result['id'],
171 )));
6c6e6187 172 $this->callAPISuccess('uf_match', 'delete', array(
92915c55 173 'id' => $result['id'],
6a488035 174 ));
6c6e6187 175 $this->assertEquals(0, $this->callAPISuccess('uf_match', 'getcount', array(
92915c55
TO
176 'id' => $result['id'],
177 )));
6a488035 178 }
96025800 179
6a488035 180}