Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 TO |
28 | /** |
29 | * Class contains api test cases for "civicrm_note" | |
acb109b7 | 30 | * @group headless |
6a488035 TO |
31 | */ |
32 | class api_v3_NoteTest extends CiviUnitTestCase { | |
33 | ||
6a488035 TO |
34 | protected $_contactID; |
35 | protected $_params; | |
36 | protected $_noteID; | |
37 | protected $_note; | |
b7c9bc4c | 38 | |
00be9182 | 39 | public function setUp() { |
6a488035 | 40 | |
7fe37828 | 41 | // Connect to the database. |
6a488035 | 42 | parent::setUp(); |
bd772bfe | 43 | $this->useTransaction(TRUE); |
6a488035 TO |
44 | |
45 | $this->_contactID = $this->organizationCreate(NULL); | |
46 | ||
9099cab3 | 47 | $this->_params = [ |
6a488035 TO |
48 | 'entity_table' => 'civicrm_contact', |
49 | 'entity_id' => $this->_contactID, | |
50 | 'note' => 'Hello!!! m testing Note', | |
51 | 'contact_id' => $this->_contactID, | |
52 | 'modified_date' => '2011-01-31', | |
92915c55 | 53 | 'subject' => 'Test Note', |
9099cab3 | 54 | ]; |
6a488035 TO |
55 | $this->_note = $this->noteCreate($this->_contactID); |
56 | $this->_noteID = $this->_note['id']; | |
57 | } | |
58 | ||
6a488035 TO |
59 | ///////////////// civicrm_note_get methods |
60 | ||
6a488035 | 61 | /** |
7fe37828 EM |
62 | * Check retrieve note with empty parameter array. |
63 | * | |
6a488035 | 64 | * Error expected |
2d932085 CW |
65 | * @param int $version |
66 | * @dataProvider versionThreeAndFour | |
6a488035 | 67 | */ |
2d932085 CW |
68 | public function testGetWithEmptyParams($version) { |
69 | $this->_apiversion = $version; | |
9099cab3 | 70 | $this->callAPISuccess('note', 'get', []); |
6a488035 TO |
71 | } |
72 | ||
73 | /** | |
7fe37828 EM |
74 | * Check retrieve note with missing parameters. |
75 | * | |
6a488035 | 76 | * Error expected |
2d932085 CW |
77 | * @param int $version |
78 | * @dataProvider versionThreeAndFour | |
6a488035 | 79 | */ |
2d932085 CW |
80 | public function testGetWithoutEntityId($version) { |
81 | $this->_apiversion = $version; | |
9099cab3 | 82 | $params = [ |
6a488035 | 83 | 'entity_table' => 'civicrm_contact', |
9099cab3 | 84 | ]; |
7fe37828 | 85 | $this->callAPISuccess('note', 'get', $params); |
6a488035 TO |
86 | } |
87 | ||
88 | /** | |
7fe37828 | 89 | * Check civicrm_note get. |
2d932085 CW |
90 | * @param int $version |
91 | * @dataProvider versionThreeAndFour | |
6a488035 | 92 | */ |
2d932085 CW |
93 | public function testGet($version) { |
94 | $this->_apiversion = $version; | |
6a488035 | 95 | $entityId = $this->_noteID; |
9099cab3 | 96 | $params = [ |
6a488035 TO |
97 | 'entity_table' => 'civicrm_contact', |
98 | 'entity_id' => $entityId, | |
9099cab3 | 99 | ]; |
7fe37828 | 100 | $this->callAPIAndDocument('note', 'get', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
101 | } |
102 | ||
6a488035 | 103 | /** |
7fe37828 EM |
104 | * Check create with empty parameter array. |
105 | * | |
6a488035 | 106 | * Error Expected |
2d932085 CW |
107 | * @param int $version |
108 | * @dataProvider versionThreeAndFour | |
6a488035 | 109 | */ |
2d932085 CW |
110 | public function testCreateWithEmptyNoteField($version) { |
111 | $this->_apiversion = $version; | |
6a488035 | 112 | $this->_params['note'] = ""; |
7fe37828 | 113 | $this->callAPIFailure('note', 'create', $this->_params, |
2d932085 | 114 | 'missing' |
7fe37828 | 115 | ); |
6a488035 TO |
116 | } |
117 | ||
118 | /** | |
7fe37828 EM |
119 | * Check create with partial params. |
120 | * | |
6a488035 | 121 | * Error expected |
2d932085 CW |
122 | * @param int $version |
123 | * @dataProvider versionThreeAndFour | |
6a488035 | 124 | */ |
2d932085 CW |
125 | public function testCreateWithoutEntityId($version) { |
126 | $this->_apiversion = $version; | |
6a488035 | 127 | unset($this->_params['entity_id']); |
7fe37828 | 128 | $this->callAPIFailure('note', 'create', $this->_params, |
2d932085 | 129 | 'entity_id'); |
6a488035 TO |
130 | } |
131 | ||
132 | /** | |
7fe37828 EM |
133 | * Check create with partially empty params. |
134 | * | |
6a488035 | 135 | * Error expected |
2d932085 CW |
136 | * @param int $version |
137 | * @dataProvider versionThreeAndFour | |
6a488035 | 138 | */ |
2d932085 CW |
139 | public function testCreateWithEmptyEntityId($version) { |
140 | $this->_apiversion = $version; | |
6a488035 | 141 | $this->_params['entity_id'] = ""; |
7fe37828 | 142 | $this->callAPIFailure('note', 'create', $this->_params, |
2d932085 | 143 | 'entity_id'); |
6a488035 TO |
144 | } |
145 | ||
146 | /** | |
7fe37828 | 147 | * Check civicrm note create. |
2d932085 CW |
148 | * @param int $version |
149 | * @dataProvider versionThreeAndFour | |
6a488035 | 150 | */ |
2d932085 CW |
151 | public function testCreate($version) { |
152 | $this->_apiversion = $version; | |
6a488035 | 153 | |
b5ee049f | 154 | $result = $this->callAPIAndDocument('note', 'create', $this->_params, __FUNCTION__, __FILE__); |
ba4a1892 TM |
155 | $this->assertEquals($result['values'][$result['id']]['note'], 'Hello!!! m testing Note'); |
156 | $this->assertEquals(date('Y-m-d', strtotime($this->_params['modified_date'])), date('Y-m-d', strtotime($result['values'][$result['id']]['modified_date']))); | |
6a488035 | 157 | |
61ef23bd | 158 | $this->assertArrayHasKey('id', $result); |
6a488035 TO |
159 | } |
160 | ||
2d932085 CW |
161 | /** |
162 | * @param int $version | |
163 | * @dataProvider versionThreeAndFour | |
164 | */ | |
165 | public function testCreateWithApostropheInString($version) { | |
166 | $this->_apiversion = $version; | |
9099cab3 | 167 | $params = [ |
6a488035 TO |
168 | 'entity_table' => 'civicrm_contact', |
169 | 'entity_id' => $this->_contactID, | |
170 | 'note' => "Hello!!! ' testing Note", | |
171 | 'contact_id' => $this->_contactID, | |
172 | 'modified_date' => '2011-01-31', | |
173 | 'subject' => "With a '", | |
92915c55 | 174 | 'sequential' => 1, |
9099cab3 | 175 | ]; |
b5ee049f | 176 | $result = $this->callAPISuccess('Note', 'Create', $params); |
a15773db | 177 | $this->assertAPISuccess($result); |
ba4a1892 TM |
178 | $this->assertEquals($result['values'][0]['note'], "Hello!!! ' testing Note"); |
179 | $this->assertEquals($result['values'][0]['subject'], "With a '"); | |
a15773db | 180 | $this->assertArrayHasKey('id', $result); |
6a488035 TO |
181 | } |
182 | ||
183 | /** | |
7fe37828 | 184 | * Check civicrm_note_create - tests used of default set to . |
2d932085 CW |
185 | * @param int $version |
186 | * @dataProvider versionThreeAndFour | |
6a488035 | 187 | */ |
2d932085 CW |
188 | public function testCreateWithoutModifiedDate($version) { |
189 | $this->_apiversion = $version; | |
6a488035 | 190 | unset($this->_params['modified_date']); |
b5ee049f | 191 | $apiResult = $this->callAPISuccess('note', 'create', $this->_params); |
6a488035 TO |
192 | $this->assertAPISuccess($apiResult); |
193 | $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($apiResult['values'][$apiResult['id']]['modified_date']))); | |
6a488035 TO |
194 | } |
195 | ||
6a488035 | 196 | /** |
7fe37828 EM |
197 | * Check update with empty parameter array. |
198 | * | |
b5ee049f | 199 | * Please don't copy & paste this - is of marginal value |
200 | * better to put time into the function on Syntax Conformance class that tests this | |
2d932085 CW |
201 | * @param int $version |
202 | * @dataProvider versionThreeAndFour | |
6a488035 | 203 | */ |
2d932085 CW |
204 | public function testUpdateWithEmptyParams($version) { |
205 | $this->_apiversion = $version; | |
9099cab3 | 206 | $this->callAPIFailure('note', 'create', []); |
6a488035 TO |
207 | } |
208 | ||
209 | /** | |
7fe37828 EM |
210 | * Check update with missing parameter (contact id). |
211 | * | |
6a488035 | 212 | * Error expected |
2d932085 CW |
213 | * @param int $version |
214 | * @dataProvider versionThreeAndFour | |
6a488035 | 215 | */ |
2d932085 CW |
216 | public function testUpdateWithoutContactId($version) { |
217 | $this->_apiversion = $version; | |
9099cab3 | 218 | $params = [ |
6a488035 | 219 | 'entity_id' => $this->_contactID, |
92915c55 | 220 | 'entity_table' => 'civicrm_contact', |
9099cab3 | 221 | ]; |
7fe37828 | 222 | $this->callAPIFailure('note', 'create', $params, |
2d932085 | 223 | 'missing' |
6a488035 | 224 | ); |
6a488035 TO |
225 | } |
226 | ||
227 | /** | |
7fe37828 | 228 | * Check civicrm_note update. |
2d932085 CW |
229 | * @param int $version |
230 | * @dataProvider versionThreeAndFour | |
6a488035 | 231 | */ |
2d932085 CW |
232 | public function testUpdate($version) { |
233 | $this->_apiversion = $version; | |
9099cab3 | 234 | $params = [ |
6a488035 TO |
235 | 'id' => $this->_noteID, |
236 | 'contact_id' => $this->_contactID, | |
237 | 'note' => 'Note1', | |
92915c55 | 238 | 'subject' => 'Hello World', |
9099cab3 | 239 | ]; |
6a488035 | 240 | |
7fe37828 | 241 | // Update Note. |
b5ee049f | 242 | $this->callAPISuccess('note', 'create', $params); |
9099cab3 | 243 | $note = $this->callAPISuccess('Note', 'Get', []); |
a15773db TM |
244 | $this->assertEquals($note['id'], $this->_noteID); |
245 | $this->assertEquals($note['values'][$this->_noteID]['entity_id'], $this->_contactID); | |
ba4a1892 TM |
246 | $this->assertEquals($note['values'][$this->_noteID]['entity_table'], 'civicrm_contact'); |
247 | $this->assertEquals('Hello World', $note['values'][$this->_noteID]['subject']); | |
248 | $this->assertEquals('Note1', $note['values'][$this->_noteID]['note']); | |
6a488035 TO |
249 | } |
250 | ||
6a488035 | 251 | /** |
7fe37828 EM |
252 | * Check delete with empty parameters array. |
253 | * | |
254 | * Error expected. | |
6a488035 | 255 | */ |
00be9182 | 256 | public function testDeleteWithEmptyParams() { |
9099cab3 | 257 | $this->callAPIFailure('note', 'delete', [], 'Mandatory key(s) missing from params array: id'); |
6a488035 TO |
258 | } |
259 | ||
260 | /** | |
7fe37828 EM |
261 | * Check delete with wrong id. |
262 | * | |
6a488035 | 263 | * Error expected |
2d932085 CW |
264 | * @param int $version |
265 | * @dataProvider versionThreeAndFour | |
6a488035 | 266 | */ |
2d932085 CW |
267 | public function testDeleteWithWrongID($version) { |
268 | $this->_apiversion = $version; | |
9099cab3 | 269 | $params = [ |
4f94e3fa | 270 | 'id' => 99999, |
9099cab3 | 271 | ]; |
2d932085 | 272 | $this->callAPIFailure('note', 'delete', $params, 'Note'); |
6a488035 TO |
273 | } |
274 | ||
275 | /** | |
7fe37828 | 276 | * Check civicrm_note delete. |
2d932085 CW |
277 | * @param int $version |
278 | * @dataProvider versionThreeAndFour | |
6a488035 | 279 | */ |
2d932085 CW |
280 | public function testDelete($version) { |
281 | $this->_apiversion = $version; | |
6a488035 TO |
282 | $additionalNote = $this->noteCreate($this->_contactID); |
283 | ||
9099cab3 | 284 | $params = [ |
6a488035 | 285 | 'id' => $additionalNote['id'], |
9099cab3 | 286 | ]; |
6a488035 | 287 | |
7fe37828 | 288 | $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 | 289 | } |
96025800 | 290 | |
466fce54 | 291 | public function testNoteJoin() { |
9099cab3 | 292 | $org = $this->callAPISuccess('Contact', 'create', [ |
466fce54 CW |
293 | 'contact_type' => 'Organization', |
294 | 'organization_name' => 'Org123', | |
9099cab3 | 295 | 'api.Note.create' => [ |
466fce54 | 296 | 'note' => 'Hello join', |
9099cab3 CW |
297 | ], |
298 | ]); | |
466fce54 | 299 | // Fetch contact info via join |
9099cab3 CW |
300 | $result = $this->callAPISuccessGetSingle('Note', [ |
301 | 'return' => ["entity_id.organization_name", "note"], | |
466fce54 CW |
302 | 'entity_id' => $org['id'], |
303 | 'entity_table' => "civicrm_contact", | |
9099cab3 | 304 | ]); |
466fce54 CW |
305 | $this->assertEquals('Org123', $result['entity_id.organization_name']); |
306 | $this->assertEquals('Hello join', $result['note']); | |
307 | // This should return no results by restricting contact_type | |
9099cab3 CW |
308 | $result = $this->callAPISuccess('Note', 'get', [ |
309 | 'return' => ["entity_id.organization_name"], | |
466fce54 CW |
310 | 'entity_id' => $org['id'], |
311 | 'entity_table' => "civicrm_contact", | |
312 | 'entity_id.contact_type' => "Individual", | |
9099cab3 | 313 | ]); |
466fce54 CW |
314 | $this->assertEquals(0, $result['count']); |
315 | } | |
316 | ||
6a488035 TO |
317 | } |
318 | ||
319 | /** | |
7fe37828 | 320 | * Test civicrm note create() using example code. |
6a488035 TO |
321 | */ |
322 | function testNoteCreateExample() { | |
be44cfcb | 323 | require_once 'api/v3/examples/Note/Create.ex.php'; |
7fe37828 EM |
324 | $result = Note_get_example(); |
325 | $expectedResult = Note_get_expectedresult(); | |
6a488035 TO |
326 | $this->assertEquals($result, $expectedResult); |
327 | } |