Merge pull request #4796 from jitendrapurohit/webtest4.6
[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 * @access protected
53 */
54 protected function setUp() {
55 parent::setUp();
56 $this->useTransaction(TRUE);
57
58 // taken from form code - couldn't find good method to use
59 $params['entity_id'] = 1;
60 $params['entity_table'] = CRM_Core_BAO_Domain::getTableName();
61 $domain = 1;
62 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
63 $location = array();
64 $domContact = $this->callAPISuccess('contact', 'create', array(
65 'contact_type' => 'Organization',
66 'organization_name' => 'new org',
67 'api.phone.create' => array(
68 'location_type_id' => $defaultLocationType->id,
69 'phone_type_id' => 1,
70 'phone' => '456-456',
71 ),
72 'api.address.create' => array(
73 'location_type_id' => $defaultLocationType->id,
74 'street_address' => '45 Penny Lane',
75 ),
76 'api.email.create' => array(
77 'location_type_id' => $defaultLocationType->id,
78 'email' => 'my@email.com',
79 )
80 )
81 );
82
83 $this->callAPISuccess('domain','create',array(
84 'id' => 1,
85 'contact_id' => $domContact['id'],
86 )
87 );
88 $this->params = array(
89 'name' => 'A-team domain',
90 'description' => 'domain of chaos',
91 'domain_version' => '4.2',
92 'contact_id' => $domContact['id'],
93 );
94 }
95
96 ///////////////// civicrm_domain_get methods
97
98 /**
99 * Test civicrm_domain_get. Takes no params.
100 * Testing mainly for format.
101 */
102 public function testGet() {
103
104 $params = array('sequential' => 1,);
105 $result = $this->callAPIAndDocument('domain', 'get', $params, __FUNCTION__, __FILE__);
106
107 $this->assertType('array', $result);
108
109 $domain = $result['values'][0];
110 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email'], 'In line ' . __LINE__);
111 $this->assertEquals("FIXME", $domain['from_name']);
112 // checking other important parts of domain information
113 // test will fail if backward incompatible changes happen
114 $this->assertArrayHasKey('id', $domain);
115 $this->assertArrayHasKey('name', $domain);
116 $this->assertArrayHasKey('domain_email', $domain);
117 $this->assertArrayHasKey('domain_phone', $domain);
118 $this->assertArrayHasKey('domain_address', $domain);
119 }
120
121 public function testGetCurrentDomain() {
122 $params = array('current_domain' => 1);
123 $result = $this->callAPISuccess('domain', 'get', $params);
124
125 $this->assertType('array', $result);
126
127 foreach ($result['values'] as $key => $domain) {
128 if ($key == 'version') {
129 continue;
130 }
131
132 $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email'], 'In line ' . __LINE__);
133 $this->assertEquals("FIXME", $domain['from_name']);
134
135 // checking other important parts of domain information
136 // test will fail if backward incompatible changes happen
137 $this->assertArrayHasKey('id', $domain);
138 $this->assertArrayHasKey('name', $domain);
139 $this->assertArrayHasKey('domain_email', $domain);
140 $this->assertArrayHasKey('domain_phone', $domain);
141 $this->assertArrayHasKey('domain_address', $domain);
142 $this->assertEquals("my@email.com",$domain['domain_email']);
143 $this->assertEquals("456-456",$domain['domain_phone']['phone']);
144 $this->assertEquals("45 Penny Lane",$domain['domain_address']['street_address']);
145 }
146 }
147
148 ///////////////// civicrm_domain_create methods
149 /*
150 * This test checks for a memory leak observed when doing 2 gets on current domain
151 */
152
153
154
155 public function testGetCurrentDomainTwice() {
156 $domain = $this->callAPISuccess('domain', 'getvalue', array(
157 'current_domain' => 1,
158 'return' => 'name',
159 ));
160 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
161 $domain = $this->callAPISuccess('domain', 'getvalue', array(
162 'current_domain' => 1,
163 'return' => 'name',
164 ));
165 $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE));
166 }
167
168 /**
169 * Test civicrm_domain_create.
170 */
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']]['version'], $this->params['domain_version']);
177 }
178
179 /**
180 * Test civicrm_domain_create with empty params.
181 * Error expected.
182 */
183 public function testCreateWithEmptyParams() {
184 $result = $this->callAPIFailure('domain', 'create', array());
185 }
186 }
187