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