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