| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 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 | |
| 29 | |
| 30 | |
| 31 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 32 | |
| 33 | /** |
| 34 | * Test class for UFGroup API - civicrm_uf_* |
| 35 | * @todo Split UFGroup and UFJoin tests |
| 36 | * |
| 37 | * @package CiviCRM |
| 38 | */ |
| 39 | class api_v3_UFJoinTest extends CiviUnitTestCase { |
| 40 | // ids from the uf_group_test.xml fixture |
| 41 | protected $_ufGroupId = 11; |
| 42 | protected $_ufFieldId; |
| 43 | protected $_contactId = 69; |
| 44 | protected $_apiversion; |
| 45 | |
| 46 | protected function setUp() { |
| 47 | parent::setUp(); |
| 48 | // Truncate the tables |
| 49 | $this->quickCleanup( |
| 50 | array( |
| 51 | 'civicrm_group', |
| 52 | 'civicrm_contact', |
| 53 | 'civicrm_uf_group', |
| 54 | 'civicrm_uf_join', |
| 55 | 'civicrm_uf_match', |
| 56 | ) |
| 57 | ); |
| 58 | $this->_apiversion = 3; |
| 59 | $op = new PHPUnit_Extensions_Database_Operation_Insert; |
| 60 | $op->execute( |
| 61 | $this->_dbconn, |
| 62 | $this->createFlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml') |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | function tearDown() { |
| 67 | // Truncate the tables |
| 68 | $this->quickCleanup( |
| 69 | array( |
| 70 | 'civicrm_group', |
| 71 | 'civicrm_contact', |
| 72 | 'civicrm_uf_group', |
| 73 | 'civicrm_uf_join', |
| 74 | 'civicrm_uf_match', |
| 75 | ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Find uf join group id |
| 81 | */ |
| 82 | public function testFindUFGroupId() { |
| 83 | $params = array( |
| 84 | 'module' => 'CiviContribute', |
| 85 | 'entity_table' => 'civicrm_contribution_page', |
| 86 | 'entity_id' => 1, |
| 87 | 'weight' => 1, |
| 88 | 'uf_group_id' => $this->_ufGroupId, |
| 89 | 'is_active' => 1, ); |
| 90 | $ufJoin = $this->callAPISuccess('uf_join', 'create', $params); |
| 91 | |
| 92 | $searchParams = array( |
| 93 | 'entity_table' => 'civicrm_contribution_page', |
| 94 | 'entity_id' => 1, ); |
| 95 | $result = $this->callAPISuccess('uf_join', 'get', $searchParams); |
| 96 | |
| 97 | foreach ($result['values'] as $key => $value) { |
| 98 | $this->assertEquals($value['uf_group_id'], $this->_ufGroupId, 'In line ' . __LINE__); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | public function testUFJoinEditWrongParamsType() { |
| 104 | $params = 'a string'; |
| 105 | $result = $this->callAPIFailure('uf_join', 'create', $params); |
| 106 | $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__); |
| 107 | } |
| 108 | |
| 109 | public function testUFJoinEditEmptyParams() { |
| 110 | $params = array(); |
| 111 | $result = $this->callAPIFailure('uf_join', 'create', $params); |
| 112 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: module, weight, uf_group_id', 'In line ' . __LINE__); |
| 113 | } |
| 114 | |
| 115 | public function testUFJoinEditWithoutUFGroupId() { |
| 116 | $params = array( |
| 117 | 'module' => 'CiviContribute', |
| 118 | 'entity_table' => 'civicrm_contribution_page', |
| 119 | 'entity_id' => 1, |
| 120 | 'weight' => 1, |
| 121 | 'is_active' => 1, ); |
| 122 | $result = $this->callAPIFailure('uf_join', 'create', $params); |
| 123 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id', 'In line ' . __LINE__); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Create/update uf join |
| 128 | */ |
| 129 | public function testCreateUFJoin() { |
| 130 | $params = array( |
| 131 | 'module' => 'CiviContribute', |
| 132 | 'entity_table' => 'civicrm_contribution_page', |
| 133 | 'entity_id' => 1, |
| 134 | 'weight' => 1, |
| 135 | 'uf_group_id' => $this->_ufGroupId, |
| 136 | 'is_active' => 1, 'sequential' => 1, |
| 137 | ); |
| 138 | $ufJoin = $this->callAPIAndDocument('uf_join', 'create', $params, __FUNCTION__, __FILE__); |
| 139 | $this->assertEquals($ufJoin['values'][0]['module'], $params['module'], 'In line ' . __LINE__); |
| 140 | $this->assertEquals($ufJoin['values'][0]['uf_group_id'], $params['uf_group_id'], 'In line ' . __LINE__); |
| 141 | $this->assertEquals($ufJoin['values'][0]['is_active'], $params['is_active'], 'In line ' . __LINE__); |
| 142 | |
| 143 | $params = array( |
| 144 | 'id' => $ufJoin['id'], |
| 145 | 'module' => 'CiviContribute', |
| 146 | 'entity_table' => 'civicrm_contribution_page', |
| 147 | 'entity_id' => 1, |
| 148 | 'weight' => 1, |
| 149 | 'uf_group_id' => $this->_ufGroupId, |
| 150 | 'is_active' => 0, 'sequential' => 1, |
| 151 | ); |
| 152 | $ufJoinUpdated = $this->callAPISuccess('uf_join', 'create', $params); |
| 153 | $this->assertEquals($ufJoinUpdated['values'][0]['module'], $params['module'], 'In line ' . __LINE__); |
| 154 | $this->assertEquals($ufJoinUpdated['values'][0]['uf_group_id'], $params['uf_group_id'], 'In line ' . __LINE__); |
| 155 | $this->assertEquals($ufJoinUpdated['values'][0]['is_active'], $params['is_active'], 'In line ' . __LINE__); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | public function testFindUFJoinWrongParamsType() { |
| 160 | $params = 'a string'; |
| 161 | $result = $this->callAPIFailure('uf_join', 'create', $params); |
| 162 | $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__); |
| 163 | } |
| 164 | |
| 165 | public function testFindUFJoinEmptyParams() { |
| 166 | $result = $this->callAPIFailure('uf_join', 'create', array()); |
| 167 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: module, weight, uf_group_id', 'In line ' . __LINE__); |
| 168 | } |
| 169 | |
| 170 | public function testFindUFJoinWithoutUFGroupId() { |
| 171 | $params = array( |
| 172 | 'module' => 'CiviContribute', |
| 173 | 'entity_table' => 'civicrm_contribution_page', |
| 174 | 'entity_id' => 1, |
| 175 | 'weight' => 1, |
| 176 | 'is_active' => 1, |
| 177 | ); |
| 178 | $result = $this->callAPIFailure('uf_join', 'create', $params); |
| 179 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id', 'In line ' . __LINE__); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Find uf join id |
| 184 | */ |
| 185 | public function testGetUFJoinId() { |
| 186 | $params = array( |
| 187 | 'module' => 'CiviContribute', |
| 188 | 'entity_table' => 'civicrm_contribution_page', |
| 189 | 'entity_id' => 1, |
| 190 | 'weight' => 1, |
| 191 | 'uf_group_id' => $this->_ufGroupId, |
| 192 | 'is_active' => 1, ); |
| 193 | |
| 194 | $ufJoin = $this->callAPISuccess('uf_join', 'create', $params); |
| 195 | $searchParams = array( |
| 196 | 'entity_table' => 'civicrm_contribution_page', |
| 197 | 'entity_id' => 1, 'sequential' => 1, |
| 198 | ); |
| 199 | |
| 200 | $result = $this->callAPIAndDocument('uf_join', 'get', $searchParams, __FUNCTION__, __FILE__); |
| 201 | $this->assertEquals($result['values'][0]['module'], $params['module'], 'In line ' . __LINE__); |
| 202 | $this->assertEquals($result['values'][0]['uf_group_id'], $params['uf_group_id'], 'In line ' . __LINE__); |
| 203 | $this->assertEquals($result['values'][0]['entity_id'], $params['entity_id'], 'In line ' . __LINE__); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Test civicrm_activity_create() using example code |
| 208 | */ |
| 209 | function testUFJoinCreateExample() { |
| 210 | require_once 'api/v3/examples/UFJoin/Create.php'; |
| 211 | $result = UF_join_create_example(); |
| 212 | $expectedResult = UF_join_create_expectedresult(); |
| 213 | $this->assertEquals($result, $expectedResult); |
| 214 | } |
| 215 | } |
| 216 | |