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