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