3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test class for Domain API - civicrm_domain_*
15 * @package CiviCRM_APIv3
16 * @subpackage API_Domain
19 class api_v3_DomainTest
extends CiviUnitTestCase
{
22 * This test case doesn't require DB reset - apart from
23 * where cleanDB() is called.
26 public $DBResetRequired = FALSE;
31 * Sets up the fixture, for example, opens a network connection.
33 * This method is called before a test is executed.
35 protected function setUp() {
37 $this->useTransaction(TRUE);
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
,
51 'api.address.create' => [
52 'location_type_id' => $defaultLocationType->id
,
53 'street_address' => '45 Penny Lane',
55 'api.email.create' => [
56 'location_type_id' => $defaultLocationType->id
,
57 'email' => 'my@email.com',
61 $this->callAPISuccess('domain', 'create', [
63 'contact_id' => $domContact['id'],
66 'name' => 'A-team domain',
67 'description' => 'domain of chaos',
68 'domain_version' => '4.2',
69 'contact_id' => $domContact['id'],
74 * Test civicrm_domain_get.
77 * Testing mainly for format.
79 public function testGet() {
81 $params = ['sequential' => 1];
82 $result = $this->callAPIAndDocument('domain', 'get', $params, __FUNCTION__
, __FILE__
);
84 $this->assertType('array', $result);
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);
95 'phone_type' => 'Phone',
97 ], $domain['domain_phone']);
98 $this->assertArrayHasKey('domain_address', $domain);
102 * Test get function with current domain.
104 public function testGetCurrentDomain() {
105 $params = ['current_domain' => 1];
106 $result = $this->callAPISuccess('domain', 'get', $params);
108 $this->assertType('array', $result);
110 foreach ($result['values'] as $key => $domain) {
111 if ($key == 'version') {
115 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email']);
116 $this->assertEquals("FIXME", $domain['from_name']);
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']);
132 * This test checks for a memory leak.
134 * The leak was observed when doing 2 gets on current domain.
135 * @param int $version
136 * @dataProvider versionThreeAndFour
138 public function testGetCurrentDomainTwice($version) {
139 $this->_apiversion
= $version;
140 $domain = $this->callAPISuccess('domain', 'getvalue', [
141 'current_domain' => 1,
144 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
145 $domain = $this->callAPISuccess('domain', 'getvalue', [
146 'current_domain' => 1,
149 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
153 * Test civicrm_domain_create.
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']);
164 * Test if Domain.create does not touch the version of the domain.
167 * @param int $version
168 * @dataProvider versionThreeAndFour
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']]);
176 // Change domain name.
177 $this->callAPISuccess('Domain', 'create', [
178 'id' => $domain_result['id'],
179 'name' => 'B-Team domain',
183 $domain_after = $this->callAPISuccess('Domain', 'getsingle', ['id' => $domain_result['id']]);
185 // Version should still be the same.
186 $this->assertEquals($domain_before['version'], $domain_after['version']);
190 * Test whether Domain.create returns a correct value for domain_version.
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']);
199 // Check for domain_version in create result.
200 $this->assertEquals($this->params
['domain_version'], $result_value['domain_version']);
204 * Test civicrm_domain_create with empty params.
207 * @param int $version
208 * @dataProvider versionThreeAndFour
210 public function testCreateWithEmptyParams($version) {
211 $this->_apiversion
= $version;
212 $this->callAPIFailure('domain', 'create', []);