Import from SVN (r45945, r596)
[civicrm-core.git] / tests / phpunit / api / v3 / DomainTest.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30 require_once 'CiviTest/CiviUnitTestCase.php';
31
32 /**
33 * Test class for Domain API - civicrm_domain_*
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Domain
37 */
38 class api_v3_DomainTest extends CiviUnitTestCase {
39
40 /* This test case doesn't require DB reset - apart from
41 where cleanDB() is called. */
42
43
44
45 public $DBResetRequired = FALSE;
46
47 protected $_apiversion = 3;
48 protected $params;
49 public $_eNoticeCompliant = TRUE;
50
51 /**
52 * Constructor
53 *
54 * Initialize configuration
55 */ function __construct() {
56 parent::__construct();
57 }
58
59 /**
60 * Sets up the fixture, for example, opens a network connection.
61 * This method is called before a test is executed.
62 *
63 * @access protected
64 */
65 protected function setUp() {
66 parent::setUp();
67
68 // taken from form code - couldn't find good method to use
69 $params['entity_id'] = 1;
70 $params['entity_table'] = CRM_Core_BAO_Domain::getTableName();
71 $domain = 1;
72 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
73 $location = array();
74 $domContact = civicrm_api('contact', 'create', array(
75 'version' => $this->_apiversion,
76 'contact_type' => 'Organization',
77 'organization_name' => 'new org',
78 'api.phone.create' => array(
79 'location_type_id' => $defaultLocationType->id,
80 'phone_type_id' => 1,
81 'phone' => '456-456',
82 ),
83 'api.address.create' => array(
84 'location_type_id' => $defaultLocationType->id,
85 'street_address' => '45 Penny Lane',
86 ),
87 'api.email.create' => array(
88 'location_type_id' => $defaultLocationType->id,
89 'email' => 'my@email.com',
90 )
91 )
92 );
93
94 civicrm_api('domain','create',array(
95 'id' => 1,
96 'contact_id' => $domContact['id'],
97 'version' => $this->_apiversion
98 )
99 );
100 $this->_apiversion = 3;
101 $this->params = array(
102 'name' => 'A-team domain',
103 'description' => 'domain of chaos',
104 'version' => $this->_apiversion,
105 'domain_version' => '4.2',
106 'contact_id' => $domContact['id'],
107 );
108 }
109
110 /**
111 * Tears down the fixture, for example, closes a network connection.
112 * This method is called after a test is executed.
113 *
114 * @access protected
115 */
116 protected function tearDown() {
117
118 }
119
120 ///////////////// civicrm_domain_get methods
121
122 /**
123 * Test civicrm_domain_get. Takes no params.
124 * Testing mainly for format.
125 */
126 public function testGet() {
127
128
129 $params = array('version' => 3, 'sequential' => 1,);
130 $result = civicrm_api('domain', 'get', $params);
131 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
132
133 $this->assertType('array', $result, 'In line' . __LINE__);
134
135 $domain = $result['values'][0];
136 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email'], 'In line ' . __LINE__);
137 $this->assertEquals("FIXME", $domain['from_name'], 'In line' . __LINE__);
138 // checking other important parts of domain information
139 // test will fail if backward incompatible changes happen
140 $this->assertArrayHasKey('id', $domain, 'In line' . __LINE__);
141 $this->assertArrayHasKey('name', $domain, 'In line' . __LINE__);
142 $this->assertArrayHasKey('domain_email', $domain, 'In line' . __LINE__);
143 $this->assertArrayHasKey('domain_phone', $domain, 'In line' . __LINE__);
144 $this->assertArrayHasKey('domain_address', $domain, 'In line' . __LINE__);
145 }
146
147 public function testGetCurrentDomain() {
148 $params = array('version' => 3, 'current_domain' => 1);
149 $result = civicrm_api('domain', 'get', $params);
150
151 $this->assertType('array', $result, 'In line' . __LINE__);
152
153 foreach ($result['values'] as $key => $domain) {
154 if ($key == 'version') {
155 continue;
156 }
157
158 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email'], 'In line ' . __LINE__);
159 $this->assertEquals("FIXME", $domain['from_name'], 'In line' . __LINE__);
160
161 // checking other important parts of domain information
162 // test will fail if backward incompatible changes happen
163 $this->assertArrayHasKey('id', $domain, 'In line' . __LINE__);
164 $this->assertArrayHasKey('name', $domain, 'In line' . __LINE__);
165 $this->assertArrayHasKey('domain_email', $domain, 'In line' . __LINE__);
166 $this->assertArrayHasKey('domain_phone', $domain, 'In line' . __LINE__);
167 $this->assertArrayHasKey('domain_address', $domain, 'In line' . __LINE__);
168 $this->assertEquals("my@email.com",$domain['domain_email']);
169 $this->assertEquals("456-456",$domain['domain_phone']['phone']);
170 $this->assertEquals("45 Penny Lane",$domain['domain_address']['street_address']);
171 }
172 }
173
174 ///////////////// civicrm_domain_create methods
175 /*
176 * This test checks for a memory leak observed when doing 2 gets on current domain
177 */
178
179
180
181 public function testGetCurrentDomainTwice() {
182 $domain = civicrm_api('domain', 'getvalue', array(
183 'version' => 3,
184 'current_domain' => 1,
185 'return' => 'name',
186 ));
187 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE) . 'in line ' . __LINE__);
188 $domain = civicrm_api('domain', 'getvalue', array(
189 'version' => 3,
190 'current_domain' => 1,
191 'return' => 'name',
192 ));
193 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE) . 'in line ' . __LINE__);
194 }
195
196 /**
197 * Test civicrm_domain_create.
198 */
199 public function testCreate() {
200 $result = civicrm_api('domain', 'create', $this->params);
201 $this->documentMe($this->params, $result, __FUNCTION__, __FILE__);
202 $this->assertEquals($result['is_error'], 0);
203 $this->assertEquals($result['count'], 1);
204 $this->assertNotNull($result['id']);
205 $this->assertEquals($result['values'][$result['id']]['name'], $this->params['name']);
206 $this->assertEquals($result['values'][$result['id']]['version'], $this->params['domain_version']);
207 }
208
209 /**
210 * Test civicrm_domain_create with empty params.
211 * Error expected.
212 */
213 public function testCreateWithEmptyParams() {
214 $params = array('version' => $this->_apiversion);
215 $result = civicrm_api('domain', 'create', $params);
216 $this->assertEquals($result['is_error'], 1,
217 "In line " . __LINE__
218 );
219 }
220
221 /**
222 * Test civicrm_domain_create with wrong parameter type.
223 */
224 public function testCreateWithWrongParams() {
225 $params = 1;
226 $result = civicrm_api('domain', 'create', $params);
227 $this->assertEquals($result['is_error'], 1,
228 "In line " . __LINE__
229 );
230 }
231 }
232