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