commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tests / phpunit / api / v3 / DomainTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public $DBResetRequired = FALSE;
42
43 protected $_apiversion = 3;
44 protected $params;
45
46 /**
47 * Sets up the fixture, for example, opens a network connection.
48 *
49 * This method is called before a test is executed.
50 */
51 protected function setUp() {
52 parent::setUp();
53 $this->useTransaction(TRUE);
54
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', array(
60 'contact_type' => 'Organization',
61 'organization_name' => 'new org',
62 'api.phone.create' => array(
63 'location_type_id' => $defaultLocationType->id,
64 'phone_type_id' => 1,
65 'phone' => '456-456',
66 ),
67 'api.address.create' => array(
68 'location_type_id' => $defaultLocationType->id,
69 'street_address' => '45 Penny Lane',
70 ),
71 'api.email.create' => array(
72 'location_type_id' => $defaultLocationType->id,
73 'email' => 'my@email.com',
74 ),
75 )
76 );
77
78 $this->callAPISuccess('domain', 'create', array(
79 'id' => 1,
80 'contact_id' => $domContact['id'],
81 )
82 );
83 $this->params = array(
84 'name' => 'A-team domain',
85 'description' => 'domain of chaos',
86 'domain_version' => '4.2',
87 'contact_id' => $domContact['id'],
88 );
89 }
90
91 /**
92 * Test civicrm_domain_get.
93 *
94 * Takes no params.
95 * Testing mainly for format.
96 */
97 public function testGet() {
98
99 $params = array('sequential' => 1);
100 $result = $this->callAPIAndDocument('domain', 'get', $params, __FUNCTION__, __FILE__);
101
102 $this->assertType('array', $result);
103
104 $domain = $result['values'][0];
105 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email']);
106 $this->assertEquals("FIXME", $domain['from_name']);
107 // checking other important parts of domain information
108 // test will fail if backward incompatible changes happen
109 $this->assertArrayHasKey('id', $domain);
110 $this->assertArrayHasKey('name', $domain);
111 $this->assertArrayHasKey('domain_email', $domain);
112 $this->assertEquals(array(
113 'phone_type' => 'Phone',
114 'phone' => '456-456',
115 ), $domain['domain_phone']);
116 $this->assertArrayHasKey('domain_address', $domain);
117 }
118
119 /**
120 * Test get function with current domain.
121 */
122 public function testGetCurrentDomain() {
123 $params = array('current_domain' => 1);
124 $result = $this->callAPISuccess('domain', 'get', $params);
125
126 $this->assertType('array', $result);
127
128 foreach ($result['values'] as $key => $domain) {
129 if ($key == 'version') {
130 continue;
131 }
132
133 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email']);
134 $this->assertEquals("FIXME", $domain['from_name']);
135
136 // checking other important parts of domain information
137 // test will fail if backward incompatible changes happen
138 $this->assertArrayHasKey('id', $domain);
139 $this->assertArrayHasKey('name', $domain);
140 $this->assertArrayHasKey('domain_email', $domain);
141 $this->assertArrayHasKey('domain_phone', $domain);
142 $this->assertArrayHasKey('domain_address', $domain);
143 $this->assertEquals("my@email.com", $domain['domain_email']);
144 $this->assertEquals("456-456", $domain['domain_phone']['phone']);
145 $this->assertEquals("45 Penny Lane", $domain['domain_address']['street_address']);
146 }
147 }
148
149 /**
150 * This test checks for a memory leak.
151 *
152 * The leak was observed when doing 2 gets on current domain.
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 *
181 * Error expected.
182 */
183 public function testCreateWithEmptyParams() {
184 $this->callAPIFailure('domain', 'create', array());
185 }
186
187 }