| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
| 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 | /** |
| 29 | * Test class for Domain API - civicrm_domain_* |
| 30 | * |
| 31 | * @package CiviCRM_APIv3 |
| 32 | * @subpackage API_Domain |
| 33 | * @group headless |
| 34 | */ |
| 35 | class api_v3_DomainTest extends CiviUnitTestCase { |
| 36 | |
| 37 | /* This test case doesn't require DB reset - apart from |
| 38 | where cleanDB() is called. */ |
| 39 | |
| 40 | public $DBResetRequired = FALSE; |
| 41 | |
| 42 | protected $_apiversion = 3; |
| 43 | protected $params; |
| 44 | |
| 45 | /** |
| 46 | * Sets up the fixture, for example, opens a network connection. |
| 47 | * |
| 48 | * This method is called before a test is executed. |
| 49 | */ |
| 50 | protected function setUp() { |
| 51 | parent::setUp(); |
| 52 | $this->useTransaction(TRUE); |
| 53 | |
| 54 | // taken from form code - couldn't find good method to use |
| 55 | $params['entity_id'] = 1; |
| 56 | $params['entity_table'] = CRM_Core_BAO_Domain::getTableName(); |
| 57 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
| 58 | $domContact = $this->callAPISuccess('contact', 'create', array( |
| 59 | 'contact_type' => 'Organization', |
| 60 | 'organization_name' => 'new org', |
| 61 | 'api.phone.create' => array( |
| 62 | 'location_type_id' => $defaultLocationType->id, |
| 63 | 'phone_type_id' => 1, |
| 64 | 'phone' => '456-456', |
| 65 | ), |
| 66 | 'api.address.create' => array( |
| 67 | 'location_type_id' => $defaultLocationType->id, |
| 68 | 'street_address' => '45 Penny Lane', |
| 69 | ), |
| 70 | 'api.email.create' => array( |
| 71 | 'location_type_id' => $defaultLocationType->id, |
| 72 | 'email' => 'my@email.com', |
| 73 | ), |
| 74 | ) |
| 75 | ); |
| 76 | |
| 77 | $this->callAPISuccess('domain', 'create', array( |
| 78 | 'id' => 1, |
| 79 | 'contact_id' => $domContact['id'], |
| 80 | ) |
| 81 | ); |
| 82 | $this->params = array( |
| 83 | 'name' => 'A-team domain', |
| 84 | 'description' => 'domain of chaos', |
| 85 | 'domain_version' => '4.2', |
| 86 | 'contact_id' => $domContact['id'], |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Test civicrm_domain_get. |
| 92 | * |
| 93 | * Takes no params. |
| 94 | * Testing mainly for format. |
| 95 | */ |
| 96 | public function testGet() { |
| 97 | |
| 98 | $params = array('sequential' => 1); |
| 99 | $result = $this->callAPIAndDocument('domain', 'get', $params, __FUNCTION__, __FILE__); |
| 100 | |
| 101 | $this->assertType('array', $result); |
| 102 | |
| 103 | $domain = $result['values'][0]; |
| 104 | $this->assertEquals("info@EXAMPLE.ORG", $domain['from_email']); |
| 105 | $this->assertEquals("FIXME", $domain['from_name']); |
| 106 | // checking other important parts of domain information |
| 107 | // test will fail if backward incompatible changes happen |
| 108 | $this->assertArrayHasKey('id', $domain); |
| 109 | $this->assertArrayHasKey('name', $domain); |
| 110 | $this->assertArrayHasKey('domain_email', $domain); |
| 111 | $this->assertEquals(array( |
| 112 | 'phone_type' => 'Phone', |
| 113 | 'phone' => '456-456', |
| 114 | ), $domain['domain_phone']); |
| 115 | $this->assertArrayHasKey('domain_address', $domain); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Test get function with current domain. |
| 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']); |
| 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 | /** |
| 149 | * This test checks for a memory leak. |
| 150 | * |
| 151 | * The leak was observed when doing 2 gets on current domain. |
| 152 | */ |
| 153 | public function testGetCurrentDomainTwice() { |
| 154 | $domain = $this->callAPISuccess('domain', 'getvalue', array( |
| 155 | 'current_domain' => 1, |
| 156 | 'return' => 'name', |
| 157 | )); |
| 158 | $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE)); |
| 159 | $domain = $this->callAPISuccess('domain', 'getvalue', array( |
| 160 | 'current_domain' => 1, |
| 161 | 'return' => 'name', |
| 162 | )); |
| 163 | $this->assertEquals('Default Domain Name', $domain, print_r($domain, TRUE)); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Test civicrm_domain_create. |
| 168 | */ |
| 169 | public function testCreate() { |
| 170 | $result = $this->callAPIAndDocument('domain', 'create', $this->params, __FUNCTION__, __FILE__); |
| 171 | $this->assertEquals($result['count'], 1); |
| 172 | $this->assertNotNull($result['id']); |
| 173 | $this->assertEquals($result['values'][$result['id']]['name'], $this->params['name']); |
| 174 | $this->assertEquals($result['values'][$result['id']]['domain_version'], $this->params['domain_version']); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Test if Domain.create does not touch the version of the domain. |
| 179 | * |
| 180 | * See CRM-17430. |
| 181 | */ |
| 182 | public function testUpdateDomainName() { |
| 183 | // First create a domain. |
| 184 | $domain_result = $this->callAPISuccess('domain', 'create', $this->params); |
| 185 | $domain_before = $this->callAPISuccess('Domain', 'getsingle', array('id' => $domain_result['id'])); |
| 186 | |
| 187 | // Change domain name. |
| 188 | $this->callAPISuccess('Domain', 'create', array( |
| 189 | 'id' => $domain_result['id'], |
| 190 | 'name' => 'B-Team domain', |
| 191 | )); |
| 192 | |
| 193 | // Get domain again. |
| 194 | $domain_after = $this->callAPISuccess('Domain', 'getsingle', array('id' => $domain_result['id'])); |
| 195 | |
| 196 | // Version should still be the same. |
| 197 | $this->assertEquals($domain_before['version'], $domain_after['version']); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Test whether Domain.create returns a correct value for domain_version. |
| 202 | * |
| 203 | * See CRM-17430. |
| 204 | */ |
| 205 | public function testCreateDomainResult() { |
| 206 | // First create a domain. |
| 207 | $domain_result = $this->callAPISuccess('Domain', 'create', $this->params); |
| 208 | $result_value = CRM_Utils_Array::first($domain_result['values']); |
| 209 | |
| 210 | // Check for domain_version in create result. |
| 211 | $this->assertEquals($this->params['domain_version'], $result_value['domain_version']); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Test civicrm_domain_create with empty params. |
| 216 | * |
| 217 | * Error expected. |
| 218 | */ |
| 219 | public function testCreateWithEmptyParams() { |
| 220 | $this->callAPIFailure('domain', 'create', array()); |
| 221 | } |
| 222 | |
| 223 | } |