| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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 'tests/phpunit/CiviTest/CiviUnitTestCase.php'; |
| 29 | |
| 30 | /** |
| 31 | * Class contains api test cases for "civicrm_note" |
| 32 | * |
| 33 | */ |
| 34 | class api_v3_NoteTest extends CiviUnitTestCase { |
| 35 | |
| 36 | protected $_apiversion; |
| 37 | protected $_contactID; |
| 38 | protected $_params; |
| 39 | protected $_noteID; |
| 40 | protected $_note; |
| 41 | public $_eNoticeCompliant = TRUE; |
| 42 | |
| 43 | function __construct() { |
| 44 | parent::__construct(); |
| 45 | } |
| 46 | |
| 47 | function get_info() { |
| 48 | return array( |
| 49 | 'name' => 'Note Create', |
| 50 | 'description' => 'Test all Note Create API methods.', |
| 51 | 'group' => 'CiviCRM API Tests', |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | function setUp() { |
| 56 | |
| 57 | $this->_apiversion = 3; |
| 58 | // Connect to the database |
| 59 | parent::setUp(); |
| 60 | |
| 61 | $this->_contactID = $this->organizationCreate(NULL); |
| 62 | |
| 63 | $this->_params = array( |
| 64 | 'entity_table' => 'civicrm_contact', |
| 65 | 'entity_id' => $this->_contactID, |
| 66 | 'note' => 'Hello!!! m testing Note', |
| 67 | 'contact_id' => $this->_contactID, |
| 68 | 'modified_date' => '2011-01-31', |
| 69 | 'subject' => 'Test Note', ); |
| 70 | $this->_note = $this->noteCreate($this->_contactID); |
| 71 | $this->_noteID = $this->_note['id']; |
| 72 | } |
| 73 | |
| 74 | function tearDown() { |
| 75 | $tablesToTruncate = array( |
| 76 | 'civicrm_note', 'civicrm_contact', |
| 77 | ); |
| 78 | $this->quickCleanup($tablesToTruncate); |
| 79 | } |
| 80 | |
| 81 | ///////////////// civicrm_note_get methods |
| 82 | |
| 83 | /** |
| 84 | * check retrieve note with empty parameter array |
| 85 | * Error expected |
| 86 | */ |
| 87 | function testGetWithEmptyParams() { |
| 88 | $this->callAPISuccess('note', 'get', array()); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * check retrieve note with missing patrameters |
| 93 | * Error expected |
| 94 | */ |
| 95 | function testGetWithoutEntityId() { |
| 96 | $params = array( |
| 97 | 'entity_table' => 'civicrm_contact', |
| 98 | ); |
| 99 | $note = $this->callAPISuccess('note', 'get', $params); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * check civicrm_note_get |
| 104 | */ |
| 105 | function testGet() { |
| 106 | $entityId = $this->_noteID; |
| 107 | $params = array( |
| 108 | 'entity_table' => 'civicrm_contact', |
| 109 | 'entity_id' => $entityId, |
| 110 | ); |
| 111 | $result = $this->callAPIAndDocument('note', 'get', $params, __FUNCTION__, __FILE__); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | ///////////////// civicrm_note_create methods |
| 116 | |
| 117 | /** |
| 118 | * Check create with empty parameter array |
| 119 | * Error Expected |
| 120 | */ |
| 121 | function testCreateWithEmptyNoteField() { |
| 122 | $this->_params['note'] = ""; |
| 123 | $result = $this->callAPIFailure('note', 'create', $this->_params, |
| 124 | 'Mandatory key(s) missing from params array: note'); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Check create with partial params |
| 129 | * Error expected |
| 130 | */ |
| 131 | function testCreateWithoutEntityId() { |
| 132 | unset($this->_params['entity_id']); |
| 133 | $result = $this->callAPIFailure('note', 'create', $this->_params, |
| 134 | 'Mandatory key(s) missing from params array: entity_id'); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Check create with partially empty params |
| 139 | * Error expected |
| 140 | */ |
| 141 | function testCreateWithEmptyEntityId() { |
| 142 | $this->_params['entity_id'] = ""; |
| 143 | $result = $this->callAPIFailure('note', 'create', $this->_params, |
| 144 | 'Mandatory key(s) missing from params array: entity_id'); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Check civicrm_note_create |
| 149 | */ |
| 150 | function testCreate() { |
| 151 | |
| 152 | $result = $this->callAPIAndDocument('note', 'create', $this->_params, __FUNCTION__, __FILE__); |
| 153 | $this->assertEquals($result['values'][$result['id']]['note'], 'Hello!!! m testing Note', 'in line ' . __LINE__); |
| 154 | $this->assertEquals(date('Y-m-d', strtotime($this->_params['modified_date'])), date('Y-m-d', strtotime($result['values'][$result['id']]['modified_date'])), 'in line ' . __LINE__); |
| 155 | |
| 156 | $this->assertArrayHasKey('id', $result, 'in line ' . __LINE__); |
| 157 | $this->assertAPISuccess($result, 'in line ' . __LINE__); |
| 158 | $note = array( |
| 159 | 'id' => $result['id'], ); |
| 160 | $this->noteDelete($note); |
| 161 | } |
| 162 | |
| 163 | function testCreateWithApostropheInString() { |
| 164 | $params = array( |
| 165 | 'entity_table' => 'civicrm_contact', |
| 166 | 'entity_id' => $this->_contactID, |
| 167 | 'note' => "Hello!!! ' testing Note", |
| 168 | 'contact_id' => $this->_contactID, |
| 169 | 'modified_date' => '2011-01-31', |
| 170 | 'subject' => "With a '", |
| 171 | 'sequential' => 1, ); |
| 172 | $result = $this->callAPISuccess('Note', 'Create', $params); |
| 173 | $this->assertAPISuccess($result, 'in line ' . __LINE__); |
| 174 | $this->assertEquals($result['values'][0]['note'], "Hello!!! ' testing Note", 'in line ' . __LINE__); |
| 175 | $this->assertEquals($result['values'][0]['subject'], "With a '", 'in line ' . __LINE__); |
| 176 | $this->assertArrayHasKey('id', $result, 'in line ' . __LINE__); |
| 177 | |
| 178 | //CleanUP |
| 179 | $note = array( |
| 180 | 'id' => $result['id'], ); |
| 181 | $this->noteDelete($note); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Check civicrm_note_create - tests used of default set to now |
| 186 | */ |
| 187 | function testCreateWithoutModifiedDate() { |
| 188 | unset($this->_params['modified_date']); |
| 189 | $apiResult = $this->callAPISuccess('note', 'create', $this->_params); |
| 190 | $this->assertAPISuccess($apiResult); |
| 191 | $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($apiResult['values'][$apiResult['id']]['modified_date']))); |
| 192 | $this->noteDelete(array( |
| 193 | 'id' => $apiResult['id'], )); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | ///////////////// civicrm_note_update methods |
| 198 | |
| 199 | /** |
| 200 | * Check update with empty parameter array |
| 201 | * Please don't copy & paste this - is of marginal value |
| 202 | * better to put time into the function on Syntax Conformance class that tests this |
| 203 | */ |
| 204 | function testUpdateWithEmptyParams() { |
| 205 | $note = $this->callAPIFailure('note', 'create', array()); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Check update with missing parameter (contact id) |
| 210 | * Error expected |
| 211 | */ |
| 212 | function testUpdateWithoutContactId() { |
| 213 | $params = array( |
| 214 | 'entity_id' => $this->_contactID, |
| 215 | 'entity_table' => 'civicrm_contact', ); |
| 216 | $note = $this->callAPIFailure('note', 'create', $params, |
| 217 | 'Mandatory key(s) missing from params array: note' |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Check civicrm_note_update |
| 223 | */ |
| 224 | function testUpdate() { |
| 225 | $params = array( |
| 226 | 'id' => $this->_noteID, |
| 227 | 'contact_id' => $this->_contactID, |
| 228 | 'note' => 'Note1', |
| 229 | 'subject' => 'Hello World', ); |
| 230 | |
| 231 | //Update Note |
| 232 | $this->callAPISuccess('note', 'create', $params); |
| 233 | $note = $this->callAPISuccess('Note', 'Get', array()); |
| 234 | $this->assertEquals($note['id'], $this->_noteID, 'in line ' . __LINE__); |
| 235 | $this->assertEquals($note['values'][$this->_noteID]['entity_id'], $this->_contactID, 'in line ' . __LINE__); |
| 236 | $this->assertEquals($note['values'][$this->_noteID]['entity_table'], 'civicrm_contact', 'in line ' . __LINE__); |
| 237 | $this->assertEquals('Hello World', $note['values'][$this->_noteID]['subject'], 'in line ' . __LINE__); |
| 238 | $this->assertEquals('Note1', $note['values'][$this->_noteID]['note'], 'in line ' . __LINE__); |
| 239 | } |
| 240 | |
| 241 | ///////////////// civicrm_note_delete methods |
| 242 | |
| 243 | |
| 244 | /** |
| 245 | * Check delete with empty parametes array |
| 246 | * Error expected |
| 247 | */ |
| 248 | function testDeleteWithEmptyParams() { |
| 249 | $deleteNote = $this->callAPIFailure('note', 'delete', array(), 'Mandatory key(s) missing from params array: id'); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Check delete with wrong id |
| 254 | * Error expected |
| 255 | */ |
| 256 | function testDeleteWithWrongID() { |
| 257 | $params = array( |
| 258 | 'id' => 0, |
| 259 | ); |
| 260 | $deleteNote = $this->callAPIFailure('note', 'delete', $params |
| 261 | , 'Mandatory key(s) missing from params array: id'); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Check civicrm_note_delete |
| 266 | */ |
| 267 | function testDelete() { |
| 268 | $additionalNote = $this->noteCreate($this->_contactID); |
| 269 | |
| 270 | $params = array( |
| 271 | 'id' => $additionalNote['id'], |
| 272 | ); |
| 273 | |
| 274 | $result = $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__, __FILE__); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Test civicrm_activity_create() using example code |
| 280 | */ |
| 281 | function testNoteCreateExample() { |
| 282 | require_once 'api/v3/examples/NoteCreate.php'; |
| 283 | $result = UF_match_get_example(); |
| 284 | $expectedResult = UF_match_get_expectedresult(); |
| 285 | $this->assertEquals($result, $expectedResult); |
| 286 | } |
| 287 | |