3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class contains api test cases for "civicrm_note"
32 class api_v3_NoteTest
extends CiviUnitTestCase
{
34 protected $_apiversion;
35 protected $_contactID;
40 public function setUp() {
42 $this->_apiversion
= 3;
43 // Connect to the database.
45 $this->useTransaction(TRUE);
47 $this->_contactID
= $this->organizationCreate(NULL);
49 $this->_params
= array(
50 'entity_table' => 'civicrm_contact',
51 'entity_id' => $this->_contactID
,
52 'note' => 'Hello!!! m testing Note',
53 'contact_id' => $this->_contactID
,
54 'modified_date' => '2011-01-31',
55 'subject' => 'Test Note',
57 $this->_note
= $this->noteCreate($this->_contactID
);
58 $this->_noteID
= $this->_note
['id'];
61 ///////////////// civicrm_note_get methods
64 * Check retrieve note with empty parameter array.
68 public function testGetWithEmptyParams() {
69 $this->callAPISuccess('note', 'get', array());
73 * Check retrieve note with missing parameters.
77 public function testGetWithoutEntityId() {
79 'entity_table' => 'civicrm_contact',
81 $this->callAPISuccess('note', 'get', $params);
85 * Check civicrm_note get.
87 public function testGet() {
88 $entityId = $this->_noteID
;
90 'entity_table' => 'civicrm_contact',
91 'entity_id' => $entityId,
93 $this->callAPIAndDocument('note', 'get', $params, __FUNCTION__
, __FILE__
);
97 * Check create with empty parameter array.
101 public function testCreateWithEmptyNoteField() {
102 $this->_params
['note'] = "";
103 $this->callAPIFailure('note', 'create', $this->_params
,
104 'Mandatory key(s) missing from params array: note'
109 * Check create with partial params.
113 public function testCreateWithoutEntityId() {
114 unset($this->_params
['entity_id']);
115 $this->callAPIFailure('note', 'create', $this->_params
,
116 'Mandatory key(s) missing from params array: entity_id');
120 * Check create with partially empty params.
124 public function testCreateWithEmptyEntityId() {
125 $this->_params
['entity_id'] = "";
126 $this->callAPIFailure('note', 'create', $this->_params
,
127 'Mandatory key(s) missing from params array: entity_id');
131 * Check civicrm note create.
133 public function testCreate() {
135 $result = $this->callAPIAndDocument('note', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
136 $this->assertEquals($result['values'][$result['id']]['note'], 'Hello!!! m testing Note');
137 $this->assertEquals(date('Y-m-d', strtotime($this->_params
['modified_date'])), date('Y-m-d', strtotime($result['values'][$result['id']]['modified_date'])));
139 $this->assertArrayHasKey('id', $result);
142 public function testCreateWithApostropheInString() {
144 'entity_table' => 'civicrm_contact',
145 'entity_id' => $this->_contactID
,
146 'note' => "Hello!!! ' testing Note",
147 'contact_id' => $this->_contactID
,
148 'modified_date' => '2011-01-31',
149 'subject' => "With a '",
152 $result = $this->callAPISuccess('Note', 'Create', $params);
153 $this->assertAPISuccess($result);
154 $this->assertEquals($result['values'][0]['note'], "Hello!!! ' testing Note");
155 $this->assertEquals($result['values'][0]['subject'], "With a '");
156 $this->assertArrayHasKey('id', $result);
160 * Check civicrm_note_create - tests used of default set to .
162 public function testCreateWithoutModifiedDate() {
163 unset($this->_params
['modified_date']);
164 $apiResult = $this->callAPISuccess('note', 'create', $this->_params
);
165 $this->assertAPISuccess($apiResult);
166 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($apiResult['values'][$apiResult['id']]['modified_date'])));
170 * Check update with empty parameter array.
172 * Please don't copy & paste this - is of marginal value
173 * better to put time into the function on Syntax Conformance class that tests this
175 public function testUpdateWithEmptyParams() {
176 $this->callAPIFailure('note', 'create', array());
180 * Check update with missing parameter (contact id).
184 public function testUpdateWithoutContactId() {
186 'entity_id' => $this->_contactID
,
187 'entity_table' => 'civicrm_contact',
189 $this->callAPIFailure('note', 'create', $params,
190 'Mandatory key(s) missing from params array: note'
195 * Check civicrm_note update.
197 public function testUpdate() {
199 'id' => $this->_noteID
,
200 'contact_id' => $this->_contactID
,
202 'subject' => 'Hello World',
206 $this->callAPISuccess('note', 'create', $params);
207 $note = $this->callAPISuccess('Note', 'Get', array());
208 $this->assertEquals($note['id'], $this->_noteID
);
209 $this->assertEquals($note['values'][$this->_noteID
]['entity_id'], $this->_contactID
);
210 $this->assertEquals($note['values'][$this->_noteID
]['entity_table'], 'civicrm_contact');
211 $this->assertEquals('Hello World', $note['values'][$this->_noteID
]['subject']);
212 $this->assertEquals('Note1', $note['values'][$this->_noteID
]['note']);
216 * Check delete with empty parameters array.
220 public function testDeleteWithEmptyParams() {
221 $this->callAPIFailure('note', 'delete', array(), 'Mandatory key(s) missing from params array: id');
225 * Check delete with wrong id.
229 public function testDeleteWithWrongID() {
233 $this->callAPIFailure('note', 'delete', $params, 'Error while deleting Note');
237 * Check civicrm_note delete.
239 public function testDelete() {
240 $additionalNote = $this->noteCreate($this->_contactID
);
243 'id' => $additionalNote['id'],
246 $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__
, __FILE__
);
249 public function testNoteJoin() {
250 $org = $this->callAPISuccess('Contact', 'create', array(
251 'contact_type' => 'Organization',
252 'organization_name' => 'Org123',
253 'api.Note.create' => array(
254 'note' => 'Hello join',
257 // Fetch contact info via join
258 $result = $this->callAPISuccessGetSingle('Note', array(
259 'return' => array("entity_id.organization_name", "note"),
260 'entity_id' => $org['id'],
261 'entity_table' => "civicrm_contact",
263 $this->assertEquals('Org123', $result['entity_id.organization_name']);
264 $this->assertEquals('Hello join', $result['note']);
265 // This should return no results by restricting contact_type
266 $result = $this->callAPISuccess('Note', 'get', array(
267 'return' => array("entity_id.organization_name"),
268 'entity_id' => $org['id'],
269 'entity_table' => "civicrm_contact",
270 'entity_id.contact_type' => "Individual",
272 $this->assertEquals(0, $result['count']);
278 * Test civicrm note create() using example code.
280 function testNoteCreateExample() {
281 require_once 'api/v3/examples/Note/Create.php';
282 $result = Note_get_example();
283 $expectedResult = Note_get_expectedresult();
284 $this->assertEquals($result, $expectedResult);