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