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