Merge pull request #16469 from civicrm/5.22
[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']);
b5ee049f 175 $apiResult = $this->callAPISuccess('note', 'create', $this->_params);
6a488035
TO
176 $this->assertAPISuccess($apiResult);
177 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($apiResult['values'][$apiResult['id']]['modified_date'])));
6a488035
TO
178 }
179
6a488035 180 /**
7fe37828
EM
181 * Check update with empty parameter array.
182 *
b5ee049f 183 * Please don't copy & paste this - is of marginal value
184 * better to put time into the function on Syntax Conformance class that tests this
2d932085
CW
185 * @param int $version
186 * @dataProvider versionThreeAndFour
6a488035 187 */
2d932085
CW
188 public function testUpdateWithEmptyParams($version) {
189 $this->_apiversion = $version;
9099cab3 190 $this->callAPIFailure('note', 'create', []);
6a488035
TO
191 }
192
193 /**
7fe37828
EM
194 * Check update with missing parameter (contact id).
195 *
6a488035 196 * Error expected
2d932085
CW
197 * @param int $version
198 * @dataProvider versionThreeAndFour
6a488035 199 */
2d932085
CW
200 public function testUpdateWithoutContactId($version) {
201 $this->_apiversion = $version;
9099cab3 202 $params = [
6a488035 203 'entity_id' => $this->_contactID,
92915c55 204 'entity_table' => 'civicrm_contact',
9099cab3 205 ];
7fe37828 206 $this->callAPIFailure('note', 'create', $params,
2d932085 207 'missing'
6a488035 208 );
6a488035
TO
209 }
210
211 /**
7fe37828 212 * Check civicrm_note update.
2d932085
CW
213 * @param int $version
214 * @dataProvider versionThreeAndFour
6a488035 215 */
2d932085
CW
216 public function testUpdate($version) {
217 $this->_apiversion = $version;
9099cab3 218 $params = [
6a488035
TO
219 'id' => $this->_noteID,
220 'contact_id' => $this->_contactID,
221 'note' => 'Note1',
92915c55 222 'subject' => 'Hello World',
9099cab3 223 ];
6a488035 224
7fe37828 225 // Update Note.
b5ee049f 226 $this->callAPISuccess('note', 'create', $params);
9099cab3 227 $note = $this->callAPISuccess('Note', 'Get', []);
a15773db
TM
228 $this->assertEquals($note['id'], $this->_noteID);
229 $this->assertEquals($note['values'][$this->_noteID]['entity_id'], $this->_contactID);
ba4a1892
TM
230 $this->assertEquals($note['values'][$this->_noteID]['entity_table'], 'civicrm_contact');
231 $this->assertEquals('Hello World', $note['values'][$this->_noteID]['subject']);
232 $this->assertEquals('Note1', $note['values'][$this->_noteID]['note']);
6a488035
TO
233 }
234
6a488035 235 /**
7fe37828
EM
236 * Check delete with empty parameters array.
237 *
238 * Error expected.
6a488035 239 */
00be9182 240 public function testDeleteWithEmptyParams() {
9099cab3 241 $this->callAPIFailure('note', 'delete', [], 'Mandatory key(s) missing from params array: id');
6a488035
TO
242 }
243
244 /**
7fe37828
EM
245 * Check delete with wrong id.
246 *
6a488035 247 * Error expected
2d932085
CW
248 * @param int $version
249 * @dataProvider versionThreeAndFour
6a488035 250 */
2d932085
CW
251 public function testDeleteWithWrongID($version) {
252 $this->_apiversion = $version;
9099cab3 253 $params = [
4f94e3fa 254 'id' => 99999,
9099cab3 255 ];
2d932085 256 $this->callAPIFailure('note', 'delete', $params, 'Note');
6a488035
TO
257 }
258
259 /**
7fe37828 260 * Check civicrm_note delete.
2d932085
CW
261 * @param int $version
262 * @dataProvider versionThreeAndFour
6a488035 263 */
2d932085
CW
264 public function testDelete($version) {
265 $this->_apiversion = $version;
6a488035
TO
266 $additionalNote = $this->noteCreate($this->_contactID);
267
9099cab3 268 $params = [
6a488035 269 'id' => $additionalNote['id'],
9099cab3 270 ];
6a488035 271
7fe37828 272 $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__, __FILE__);
6a488035 273 }
96025800 274
466fce54 275 public function testNoteJoin() {
9099cab3 276 $org = $this->callAPISuccess('Contact', 'create', [
466fce54
CW
277 'contact_type' => 'Organization',
278 'organization_name' => 'Org123',
9099cab3 279 'api.Note.create' => [
466fce54 280 'note' => 'Hello join',
9099cab3
CW
281 ],
282 ]);
466fce54 283 // Fetch contact info via join
9099cab3
CW
284 $result = $this->callAPISuccessGetSingle('Note', [
285 'return' => ["entity_id.organization_name", "note"],
466fce54
CW
286 'entity_id' => $org['id'],
287 'entity_table' => "civicrm_contact",
9099cab3 288 ]);
466fce54
CW
289 $this->assertEquals('Org123', $result['entity_id.organization_name']);
290 $this->assertEquals('Hello join', $result['note']);
291 // This should return no results by restricting contact_type
9099cab3
CW
292 $result = $this->callAPISuccess('Note', 'get', [
293 'return' => ["entity_id.organization_name"],
466fce54
CW
294 'entity_id' => $org['id'],
295 'entity_table' => "civicrm_contact",
296 'entity_id.contact_type' => "Individual",
9099cab3 297 ]);
466fce54
CW
298 $this->assertEquals(0, $result['count']);
299 }
300
6a488035
TO
301}
302
303/**
7fe37828 304 * Test civicrm note create() using example code.
6a488035
TO
305 */
306function testNoteCreateExample() {
be44cfcb 307 require_once 'api/v3/examples/Note/Create.ex.php';
7fe37828
EM
308 $result = Note_get_example();
309 $expectedResult = Note_get_expectedresult();
6a488035
TO
310 $this->assertEquals($result, $expectedResult);
311}