Merge pull request #17641 from MegaphoneJon/core-1590
[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 $note = $this->callAPISuccess('note', 'create', $this->_params);
176 $apiResult = $this->callAPISuccess('note', 'get', ['id' => $note['id']]);
177 $this->assertAPISuccess($apiResult);
178 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($apiResult['values'][$apiResult['id']]['modified_date'])));
179 }
180
181 /**
182 * Check update with empty parameter array.
183 *
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
186 * @param int $version
187 * @dataProvider versionThreeAndFour
188 */
189 public function testUpdateWithEmptyParams($version) {
190 $this->_apiversion = $version;
191 $this->callAPIFailure('note', 'create', []);
192 }
193
194 /**
195 * Check update with missing parameter (contact id).
196 *
197 * Error expected
198 * @param int $version
199 * @dataProvider versionThreeAndFour
200 */
201 public function testUpdateWithoutContactId($version) {
202 $this->_apiversion = $version;
203 $params = [
204 'entity_id' => $this->_contactID,
205 'entity_table' => 'civicrm_contact',
206 ];
207 $this->callAPIFailure('note', 'create', $params,
208 'missing'
209 );
210 }
211
212 /**
213 * Check civicrm_note update.
214 * @param int $version
215 * @dataProvider versionThreeAndFour
216 */
217 public function testUpdate($version) {
218 $this->_apiversion = $version;
219 $params = [
220 'id' => $this->_noteID,
221 'contact_id' => $this->_contactID,
222 'note' => 'Note1',
223 'subject' => 'Hello World',
224 ];
225
226 // Update Note.
227 $this->callAPISuccess('note', 'create', $params);
228 $note = $this->callAPISuccess('Note', 'Get', []);
229 $this->assertEquals($note['id'], $this->_noteID);
230 $this->assertEquals($note['values'][$this->_noteID]['entity_id'], $this->_contactID);
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']);
234 }
235
236 /**
237 * Check delete with empty parameters array.
238 *
239 * Error expected.
240 */
241 public function testDeleteWithEmptyParams() {
242 $this->callAPIFailure('note', 'delete', [], 'Mandatory key(s) missing from params array: id');
243 }
244
245 /**
246 * Check delete with wrong id.
247 *
248 * Error expected
249 * @param int $version
250 * @dataProvider versionThreeAndFour
251 */
252 public function testDeleteWithWrongID($version) {
253 $this->_apiversion = $version;
254 $params = [
255 'id' => 99999,
256 ];
257 $this->callAPIFailure('note', 'delete', $params, 'Note');
258 }
259
260 /**
261 * Check civicrm_note delete.
262 * @param int $version
263 * @dataProvider versionThreeAndFour
264 */
265 public function testDelete($version) {
266 $this->_apiversion = $version;
267 $additionalNote = $this->noteCreate($this->_contactID);
268
269 $params = [
270 'id' => $additionalNote['id'],
271 ];
272
273 $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__, __FILE__);
274 }
275
276 public function testNoteJoin() {
277 $org = $this->callAPISuccess('Contact', 'create', [
278 'contact_type' => 'Organization',
279 'organization_name' => 'Org123',
280 'api.Note.create' => [
281 'note' => 'Hello join',
282 ],
283 ]);
284 // Fetch contact info via join
285 $result = $this->callAPISuccessGetSingle('Note', [
286 'return' => ["entity_id.organization_name", "note"],
287 'entity_id' => $org['id'],
288 'entity_table' => "civicrm_contact",
289 ]);
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
293 $result = $this->callAPISuccess('Note', 'get', [
294 'return' => ["entity_id.organization_name"],
295 'entity_id' => $org['id'],
296 'entity_table' => "civicrm_contact",
297 'entity_id.contact_type' => "Individual",
298 ]);
299 $this->assertEquals(0, $result['count']);
300 }
301
302 }
303
304 /**
305 * Test civicrm note create() using example code.
306 */
307 function testNoteCreateExample() {
308 require_once 'api/v3/examples/Note/Create.ex.php';
309 $result = Note_get_example();
310 $expectedResult = Note_get_expectedresult();
311 $this->assertEquals($result, $expectedResult);
312 }