Merge pull request #15666 from revati90/shared_address
[civicrm-core.git] / tests / phpunit / api / v3 / DomainTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test class for Domain API - civicrm_domain_*
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Domain
33 * @group headless
34 */
35 class api_v3_DomainTest extends CiviUnitTestCase {
36
37 /**
38 * This test case doesn't require DB reset - apart from
39 * where cleanDB() is called.
40 * @var bool
41 */
42 public $DBResetRequired = FALSE;
43
44 protected $params;
45
46 /**
47 * Sets up the fixture, for example, opens a network connection.
48 *
49 * This method is called before a test is executed.
50 */
51 protected function setUp() {
52 parent::setUp();
53 $this->useTransaction(TRUE);
54
55 // taken from form code - couldn't find good method to use
56 $params['entity_id'] = 1;
57 $params['entity_table'] = CRM_Core_BAO_Domain::getTableName();
58 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
59 $domContact = $this->callAPISuccess('contact', 'create', [
60 'contact_type' => 'Organization',
61 'organization_name' => 'new org',
62 'api.phone.create' => [
63 'location_type_id' => $defaultLocationType->id,
64 'phone_type_id' => 1,
65 'phone' => '456-456',
66 ],
67 'api.address.create' => [
68 'location_type_id' => $defaultLocationType->id,
69 'street_address' => '45 Penny Lane',
70 ],
71 'api.email.create' => [
72 'location_type_id' => $defaultLocationType->id,
73 'email' => 'my@email.com',
74 ],
75 ]);
76
77 $this->callAPISuccess('domain', 'create', [
78 'id' => 1,
79 'contact_id' => $domContact['id'],
80 ]);
81 $this->params = [
82 'name' => 'A-team domain',
83 'description' => 'domain of chaos',
84 'domain_version' => '4.2',
85 'contact_id' => $domContact['id'],
86 ];
87 }
88
89 /**
90 * Test civicrm_domain_get.
91 *
92 * Takes no params.
93 * Testing mainly for format.
94 */
95 public function testGet() {
96
97 $params = ['sequential' => 1];
98 $result = $this->callAPIAndDocument('domain', 'get', $params, __FUNCTION__, __FILE__);
99
100 $this->assertType('array', $result);
101
102 $domain = $result['values'][0];
103 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email']);
104 $this->assertEquals("FIXME", $domain['from_name']);
105 // checking other important parts of domain information
106 // test will fail if backward incompatible changes happen
107 $this->assertArrayHasKey('id', $domain);
108 $this->assertArrayHasKey('name', $domain);
109 $this->assertArrayHasKey('domain_email', $domain);
110 $this->assertEquals([
111 'phone_type' => 'Phone',
112 'phone' => '456-456',
113 ], $domain['domain_phone']);
114 $this->assertArrayHasKey('domain_address', $domain);
115 }
116
117 /**
118 * Test get function with current domain.
119 */
120 public function testGetCurrentDomain() {
121 $params = ['current_domain' => 1];
122 $result = $this->callAPISuccess('domain', 'get', $params);
123
124 $this->assertType('array', $result);
125
126 foreach ($result['values'] as $key => $domain) {
127 if ($key == 'version') {
128 continue;
129 }
130
131 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email']);
132 $this->assertEquals("FIXME", $domain['from_name']);
133
134 // checking other important parts of domain information
135 // test will fail if backward incompatible changes happen
136 $this->assertArrayHasKey('id', $domain);
137 $this->assertArrayHasKey('name', $domain);
138 $this->assertArrayHasKey('domain_email', $domain);
139 $this->assertArrayHasKey('domain_phone', $domain);
140 $this->assertArrayHasKey('domain_address', $domain);
141 $this->assertEquals("my@email.com", $domain['domain_email']);
142 $this->assertEquals("456-456", $domain['domain_phone']['phone']);
143 $this->assertEquals("45 Penny Lane", $domain['domain_address']['street_address']);
144 }
145 }
146
147 /**
148 * This test checks for a memory leak.
149 *
150 * The leak was observed when doing 2 gets on current domain.
151 * @param int $version
152 * @dataProvider versionThreeAndFour
153 */
154 public function testGetCurrentDomainTwice($version) {
155 $this->_apiversion = $version;
156 $domain = $this->callAPISuccess('domain', 'getvalue', [
157 'current_domain' => 1,
158 'return' => 'name',
159 ]);
160 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
161 $domain = $this->callAPISuccess('domain', 'getvalue', [
162 'current_domain' => 1,
163 'return' => 'name',
164 ]);
165 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
166 }
167
168 /**
169 * Test civicrm_domain_create.
170 */
171 public function testCreate() {
172 $result = $this->callAPIAndDocument('domain', 'create', $this->params, __FUNCTION__, __FILE__);
173 $this->assertEquals($result['count'], 1);
174 $this->assertNotNull($result['id']);
175 $this->assertEquals($result['values'][$result['id']]['name'], $this->params['name']);
176 $this->assertEquals($result['values'][$result['id']]['domain_version'], $this->params['domain_version']);
177 }
178
179 /**
180 * Test if Domain.create does not touch the version of the domain.
181 *
182 * See CRM-17430.
183 * @param int $version
184 * @dataProvider versionThreeAndFour
185 */
186 public function testUpdateDomainName($version) {
187 $this->_apiversion = $version;
188 // First create a domain.
189 $domain_result = $this->callAPISuccess('domain', 'create', $this->params);
190 $domain_before = $this->callAPISuccess('Domain', 'getsingle', ['id' => $domain_result['id']]);
191
192 // Change domain name.
193 $this->callAPISuccess('Domain', 'create', [
194 'id' => $domain_result['id'],
195 'name' => 'B-Team domain',
196 ]);
197
198 // Get domain again.
199 $domain_after = $this->callAPISuccess('Domain', 'getsingle', ['id' => $domain_result['id']]);
200
201 // Version should still be the same.
202 $this->assertEquals($domain_before['version'], $domain_after['version']);
203 }
204
205 /**
206 * Test whether Domain.create returns a correct value for domain_version.
207 *
208 * See CRM-17430.
209 */
210 public function testCreateDomainResult() {
211 // First create a domain.
212 $domain_result = $this->callAPISuccess('Domain', 'create', $this->params);
213 $result_value = CRM_Utils_Array::first($domain_result['values']);
214
215 // Check for domain_version in create result.
216 $this->assertEquals($this->params['domain_version'], $result_value['domain_version']);
217 }
218
219 /**
220 * Test civicrm_domain_create with empty params.
221 *
222 * Error expected.
223 * @param int $version
224 * @dataProvider versionThreeAndFour
225 */
226 public function testCreateWithEmptyParams($version) {
227 $this->_apiversion = $version;
228 $this->callAPIFailure('domain', 'create', []);
229 }
230
231 }