CRM-12274
[civicrm-core.git] / tests / phpunit / api / v3 / UFMatchTest.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30
31
32
33 require_once 'CiviTest/CiviUnitTestCase.php';
34
35 /**
36 * Test class for UFGroup API - civicrm_uf_*
37 * @todo Split UFGroup and UFJoin tests
38 *
39 * @package CiviCRM
40 */
41 class api_v3_UFMatchTest extends CiviUnitTestCase {
42 // ids from the uf_group_test.xml fixture
43 protected $_ufGroupId = 11;
44 protected $_ufFieldId;
45 protected $_contactId;
46 protected $_apiversion;
47 protected $_params = array();
48
49 public $_eNoticeCompliant = TRUE;
50
51 protected function setUp() {
52 parent::setUp();
53 $this->_apiversion = 3;
54 $this->quickCleanup(
55 array(
56 'civicrm_group',
57 'civicrm_contact',
58 'civicrm_uf_group',
59 'civicrm_uf_join',
60 'civicrm_uf_match',
61 )
62 );
63 $this->_contactId = $this->individualCreate();
64 $op = new PHPUnit_Extensions_Database_Operation_Insert;
65 $op->execute(
66 $this->_dbconn,
67 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml')
68 );
69
70 $this->_params = array(
71 'contact_id' => $this->_contactId,
72 'uf_id' => '2',
73 'uf_name' => 'blahdyblah@gmail.com',
74 'version' => '3',
75 'domain_id' => 1,
76 );
77 }
78
79 function tearDown() {
80 // Truncate the tables
81 $this->quickCleanup(
82 array(
83 'civicrm_group',
84 'civicrm_contact',
85 'civicrm_uf_group',
86 'civicrm_uf_join',
87 'civicrm_uf_match',
88 )
89 );
90 }
91
92 /**
93 * fetch contact id by uf id
94 */
95 public function testGetUFMatchID() {
96 $params = array(
97 'uf_id' => 42,
98 'version' => $this->_apiversion,
99 );
100 $result = civicrm_api('uf_match', 'get', $params);
101 $this->assertEquals($result['values'][$result['id']]['contact_id'], 69);
102 $this->assertAPISuccess($result);
103 }
104
105 function testGetUFMatchIDWrongParam() {
106 $params = 'a string';
107 $result = civicrm_api('uf_match', 'get', $params);
108 $this->assertEquals($result['is_error'], 1);
109 }
110
111 /**
112 * fetch uf id by contact id
113 */
114 public function testGetUFID() {
115 $params = array(
116 'contact_id' => 69,
117 'version' => $this->_apiversion,
118 );
119 $result = civicrm_api('uf_match', 'get', $params);
120 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
121 $this->assertEquals($result['values'][$result['id']]['uf_id'], 42);
122 $this->assertAPISuccess($result);
123 }
124
125 function testGetUFIDWrongParam() {
126 $params = 'a string';
127 $result = civicrm_api('uf_match', 'get', $params);
128 $this->assertEquals($result['is_error'], 1);
129 }
130
131 /**
132 * Test civicrm_activity_create() using example code
133 */
134 function testUFMatchGetExample() {
135 require_once 'api/v3/examples/UFMatchGet.php';
136 $result = UF_match_get_example();
137 $expectedResult = UF_match_get_expectedresult();
138 $this->assertEquals($result, $expectedResult);
139 }
140
141 function testCreate() {
142 $result = civicrm_api('uf_match', 'create', $this->_params);
143 $this->assertAPISuccess($result);
144 $this->getAndCheck($this->_params, $result['id'], 'uf_match');
145 }
146
147 function testDelete() {
148 $result = civicrm_api('uf_match', 'create', $this->_params);
149 $this->assertEquals(1, civicrm_api('uf_match', 'getcount', array(
150 'version' => $this->_apiversion,
151 'id' => $result['id'],
152 )));
153 civicrm_api('uf_match', 'delete', array(
154 'version' => $this->_apiversion,
155 'id' => $result['id'],
156 ));
157 $this->assertEquals(0, civicrm_api('uf_match', 'getcount', array(
158 'version' => $this->_apiversion,
159 'id' => $result['id'],
160 )));
161 }
162 }
163