3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test class for Domain API - civicrm_domain_*
31 * @package CiviCRM_APIv3
32 * @subpackage API_Domain
35 class api_v3_DomainTest
extends CiviUnitTestCase
{
38 * This test case doesn't require DB reset - apart from
39 * where cleanDB() is called.
42 public $DBResetRequired = FALSE;
47 * Sets up the fixture, for example, opens a network connection.
49 * This method is called before a test is executed.
51 protected function setUp() {
53 $this->useTransaction(TRUE);
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
,
67 'api.address.create' => [
68 'location_type_id' => $defaultLocationType->id
,
69 'street_address' => '45 Penny Lane',
71 'api.email.create' => [
72 'location_type_id' => $defaultLocationType->id
,
73 'email' => 'my@email.com',
77 $this->callAPISuccess('domain', 'create', [
79 'contact_id' => $domContact['id'],
82 'name' => 'A-team domain',
83 'description' => 'domain of chaos',
84 'domain_version' => '4.2',
85 'contact_id' => $domContact['id'],
90 * Test civicrm_domain_get.
93 * Testing mainly for format.
95 public function testGet() {
97 $params = ['sequential' => 1];
98 $result = $this->callAPIAndDocument('domain', 'get', $params, __FUNCTION__
, __FILE__
);
100 $this->assertType('array', $result);
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);
118 * Test get function with current domain.
120 public function testGetCurrentDomain() {
121 $params = ['current_domain' => 1];
122 $result = $this->callAPISuccess('domain', 'get', $params);
124 $this->assertType('array', $result);
126 foreach ($result['values'] as $key => $domain) {
127 if ($key == 'version') {
131 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email']);
132 $this->assertEquals("FIXME", $domain['from_name']);
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']);
148 * This test checks for a memory leak.
150 * The leak was observed when doing 2 gets on current domain.
151 * @param int $version
152 * @dataProvider versionThreeAndFour
154 public function testGetCurrentDomainTwice($version) {
155 $this->_apiversion
= $version;
156 $domain = $this->callAPISuccess('domain', 'getvalue', [
157 'current_domain' => 1,
160 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
161 $domain = $this->callAPISuccess('domain', 'getvalue', [
162 'current_domain' => 1,
165 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
169 * Test civicrm_domain_create.
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']);
180 * Test if Domain.create does not touch the version of the domain.
183 * @param int $version
184 * @dataProvider versionThreeAndFour
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']]);
192 // Change domain name.
193 $this->callAPISuccess('Domain', 'create', [
194 'id' => $domain_result['id'],
195 'name' => 'B-Team domain',
199 $domain_after = $this->callAPISuccess('Domain', 'getsingle', ['id' => $domain_result['id']]);
201 // Version should still be the same.
202 $this->assertEquals($domain_before['version'], $domain_after['version']);
206 * Test whether Domain.create returns a correct value for domain_version.
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']);
215 // Check for domain_version in create result.
216 $this->assertEquals($this->params
['domain_version'], $result_value['domain_version']);
220 * Test civicrm_domain_create with empty params.
223 * @param int $version
224 * @dataProvider versionThreeAndFour
226 public function testCreateWithEmptyParams($version) {
227 $this->_apiversion
= $version;
228 $this->callAPIFailure('domain', 'create', []);