Merge pull request #6444 from PalanteJon/CRM-16981
[civicrm-core.git] / tests / phpunit / api / v3 / DomainTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Test class for Domain API - civicrm_domain_*
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Domain
35 */
36 class api_v3_DomainTest extends CiviUnitTestCase {
37
38 /* This test case doesn't require DB reset - apart from
39 where cleanDB() is called. */
40
41
42 public $DBResetRequired = FALSE;
43
44 protected $_apiversion = 3;
45 protected $params;
46
47 /**
48 * Sets up the fixture, for example, opens a network connection.
49 *
50 * This method is called before a test is executed.
51 */
52 protected function setUp() {
53 parent::setUp();
54 $this->useTransaction(TRUE);
55
56 // taken from form code - couldn't find good method to use
57 $params['entity_id'] = 1;
58 $params['entity_table'] = CRM_Core_BAO_Domain::getTableName();
59 $domain = 1;
60 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
61 $location = array();
62 $domContact = $this->callAPISuccess('contact', 'create', array(
63 'contact_type' => 'Organization',
64 'organization_name' => 'new org',
65 'api.phone.create' => array(
66 'location_type_id' => $defaultLocationType->id,
67 'phone_type_id' => 1,
68 'phone' => '456-456',
69 ),
70 'api.address.create' => array(
71 'location_type_id' => $defaultLocationType->id,
72 'street_address' => '45 Penny Lane',
73 ),
74 'api.email.create' => array(
75 'location_type_id' => $defaultLocationType->id,
76 'email' => 'my@email.com',
77 ),
78 )
79 );
80
81 $this->callAPISuccess('domain', 'create', array(
82 'id' => 1,
83 'contact_id' => $domContact['id'],
84 )
85 );
86 $this->params = array(
87 'name' => 'A-team domain',
88 'description' => 'domain of chaos',
89 'domain_version' => '4.2',
90 'contact_id' => $domContact['id'],
91 );
92 }
93
94 /**
95 * Test civicrm_domain_get.
96 *
97 * Takes no params.
98 * Testing mainly for format.
99 */
100 public function testGet() {
101
102 $params = array('sequential' => 1);
103 $result = $this->callAPIAndDocument('domain', 'get', $params, __FUNCTION__, __FILE__);
104
105 $this->assertType('array', $result);
106
107 $domain = $result['values'][0];
108 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email']);
109 $this->assertEquals("FIXME", $domain['from_name']);
110 // checking other important parts of domain information
111 // test will fail if backward incompatible changes happen
112 $this->assertArrayHasKey('id', $domain);
113 $this->assertArrayHasKey('name', $domain);
114 $this->assertArrayHasKey('domain_email', $domain);
115 $this->assertArrayHasKey('domain_phone', $domain);
116 $this->assertArrayHasKey('domain_address', $domain);
117 }
118
119 public function testGetCurrentDomain() {
120 $params = array('current_domain' => 1);
121 $result = $this->callAPISuccess('domain', 'get', $params);
122
123 $this->assertType('array', $result);
124
125 foreach ($result['values'] as $key => $domain) {
126 if ($key == 'version') {
127 continue;
128 }
129
130 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email']);
131 $this->assertEquals("FIXME", $domain['from_name']);
132
133 // checking other important parts of domain information
134 // test will fail if backward incompatible changes happen
135 $this->assertArrayHasKey('id', $domain);
136 $this->assertArrayHasKey('name', $domain);
137 $this->assertArrayHasKey('domain_email', $domain);
138 $this->assertArrayHasKey('domain_phone', $domain);
139 $this->assertArrayHasKey('domain_address', $domain);
140 $this->assertEquals("my@email.com", $domain['domain_email']);
141 $this->assertEquals("456-456", $domain['domain_phone']['phone']);
142 $this->assertEquals("45 Penny Lane", $domain['domain_address']['street_address']);
143 }
144 }
145
146 ///////////////// civicrm_domain_create methods
147
148 /**
149 * This test checks for a memory leak observed when doing 2 gets on current domain
150 */
151 public function testGetCurrentDomainTwice() {
152 $domain = $this->callAPISuccess('domain', 'getvalue', array(
153 'current_domain' => 1,
154 'return' => 'name',
155 ));
156 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
157 $domain = $this->callAPISuccess('domain', 'getvalue', array(
158 'current_domain' => 1,
159 'return' => 'name',
160 ));
161 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
162 }
163
164 /**
165 * Test civicrm_domain_create.
166 */
167 public function testCreate() {
168 $result = $this->callAPIAndDocument('domain', 'create', $this->params, __FUNCTION__, __FILE__);
169 $this->assertEquals($result['count'], 1);
170 $this->assertNotNull($result['id']);
171 $this->assertEquals($result['values'][$result['id']]['name'], $this->params['name']);
172 $this->assertEquals($result['values'][$result['id']]['version'], $this->params['domain_version']);
173 }
174
175 /**
176 * Test civicrm_domain_create with empty params.
177 *
178 * Error expected.
179 */
180 public function testCreateWithEmptyParams() {
181 $this->callAPIFailure('domain', 'create', array());
182 }
183
184 }