3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Class contains api test cases for "civicrm_note"
16 class api_v3_NoteTest
extends CiviUnitTestCase
{
18 protected $_contactID;
23 public function setUp() {
25 // Connect to the database.
27 $this->useTransaction(TRUE);
29 $this->_contactID
= $this->organizationCreate(NULL);
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',
39 $this->_note
= $this->noteCreate($this->_contactID
);
40 $this->_noteID
= $this->_note
['id'];
43 ///////////////// civicrm_note_get methods
46 * Check retrieve note with empty parameter array.
50 * @dataProvider versionThreeAndFour
52 public function testGetWithEmptyParams($version) {
53 $this->_apiversion
= $version;
54 $this->callAPISuccess('note', 'get', []);
58 * Check retrieve note with missing parameters.
62 * @dataProvider versionThreeAndFour
64 public function testGetWithoutEntityId($version) {
65 $this->_apiversion
= $version;
67 'entity_table' => 'civicrm_contact',
69 $this->callAPISuccess('note', 'get', $params);
73 * Check civicrm_note get.
75 * @dataProvider versionThreeAndFour
77 public function testGet($version) {
78 $this->_apiversion
= $version;
79 $entityId = $this->_noteID
;
81 'entity_table' => 'civicrm_contact',
82 'entity_id' => $entityId,
84 $this->callAPIAndDocument('note', 'get', $params, __FUNCTION__
, __FILE__
);
88 * Check create with empty parameter array.
92 * @dataProvider versionThreeAndFour
94 public function testCreateWithEmptyNoteField($version) {
95 $this->_apiversion
= $version;
96 $this->_params
['note'] = "";
97 $this->callAPIFailure('note', 'create', $this->_params
,
103 * Check create with partial params.
106 * @param int $version
107 * @dataProvider versionThreeAndFour
109 public function testCreateWithoutEntityId($version) {
110 $this->_apiversion
= $version;
111 unset($this->_params
['entity_id']);
112 $this->callAPIFailure('note', 'create', $this->_params
,
117 * Check create with partially empty params.
120 * @param int $version
121 * @dataProvider versionThreeAndFour
123 public function testCreateWithEmptyEntityId($version) {
124 $this->_apiversion
= $version;
125 $this->_params
['entity_id'] = "";
126 $this->callAPIFailure('note', 'create', $this->_params
,
131 * Check civicrm note create.
132 * @param int $version
133 * @dataProvider versionThreeAndFour
135 public function testCreate($version) {
136 $this->_apiversion
= $version;
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'])));
142 $this->assertArrayHasKey('id', $result);
146 * @param int $version
147 * @dataProvider versionThreeAndFour
149 public function testCreateWithApostropheInString($version) {
150 $this->_apiversion
= $version;
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 '",
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);
168 * Check civicrm_note_create - tests used of default set to .
169 * @param int $version
170 * @dataProvider versionThreeAndFour
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'])));
182 * Check update with empty parameter array.
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
189 public function testUpdateWithEmptyParams($version) {
190 $this->_apiversion
= $version;
191 $this->callAPIFailure('note', 'create', []);
195 * Check update with missing parameter (contact id).
198 * @param int $version
199 * @dataProvider versionThreeAndFour
201 public function testUpdateWithoutContactId($version) {
202 $this->_apiversion
= $version;
204 'entity_id' => $this->_contactID
,
205 'entity_table' => 'civicrm_contact',
207 $this->callAPIFailure('note', 'create', $params,
213 * Check civicrm_note update.
214 * @param int $version
215 * @dataProvider versionThreeAndFour
217 public function testUpdate($version) {
218 $this->_apiversion
= $version;
220 'id' => $this->_noteID
,
221 'contact_id' => $this->_contactID
,
223 'subject' => 'Hello World',
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']);
237 * Check delete with empty parameters array.
241 public function testDeleteWithEmptyParams() {
242 $this->callAPIFailure('note', 'delete', [], 'Mandatory key(s) missing from params array: id');
246 * Check delete with wrong id.
249 * @param int $version
250 * @dataProvider versionThreeAndFour
252 public function testDeleteWithWrongID($version) {
253 $this->_apiversion
= $version;
257 $this->callAPIFailure('note', 'delete', $params, 'Note');
261 * Check civicrm_note delete.
262 * @param int $version
263 * @dataProvider versionThreeAndFour
265 public function testDelete($version) {
266 $this->_apiversion
= $version;
267 $additionalNote = $this->noteCreate($this->_contactID
);
270 'id' => $additionalNote['id'],
273 $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__
, __FILE__
);
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',
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",
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",
299 $this->assertEquals(0, $result['count']);
305 * Test civicrm note create() using example code.
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);