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