Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-11-23-22-46-27
[civicrm-core.git] / tests / phpunit / api / v3 / NoteTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class contains api test cases for "civicrm_note"
32 */
33 class api_v3_NoteTest extends CiviUnitTestCase {
34
35 protected $_apiversion;
36 protected $_contactID;
37 protected $_params;
38 protected $_noteID;
39 protected $_note;
40
41 public function setUp() {
42
43 $this->_apiversion = 3;
44 // Connect to the database.
45 parent::setUp();
46 $this->useTransaction(TRUE);
47
48 $this->_contactID = $this->organizationCreate(NULL);
49
50 $this->_params = array(
51 'entity_table' => 'civicrm_contact',
52 'entity_id' => $this->_contactID,
53 'note' => 'Hello!!! m testing Note',
54 'contact_id' => $this->_contactID,
55 'modified_date' => '2011-01-31',
56 'subject' => 'Test Note',
57 );
58 $this->_note = $this->noteCreate($this->_contactID);
59 $this->_noteID = $this->_note['id'];
60 }
61
62 ///////////////// civicrm_note_get methods
63
64 /**
65 * Check retrieve note with empty parameter array.
66 *
67 * Error expected
68 */
69 public function testGetWithEmptyParams() {
70 $this->callAPISuccess('note', 'get', array());
71 }
72
73 /**
74 * Check retrieve note with missing parameters.
75 *
76 * Error expected
77 */
78 public function testGetWithoutEntityId() {
79 $params = array(
80 'entity_table' => 'civicrm_contact',
81 );
82 $this->callAPISuccess('note', 'get', $params);
83 }
84
85 /**
86 * Check civicrm_note get.
87 */
88 public function testGet() {
89 $entityId = $this->_noteID;
90 $params = array(
91 'entity_table' => 'civicrm_contact',
92 'entity_id' => $entityId,
93 );
94 $this->callAPIAndDocument('note', 'get', $params, __FUNCTION__, __FILE__);
95 }
96
97 /**
98 * Check create with empty parameter array.
99 *
100 * Error Expected
101 */
102 public function testCreateWithEmptyNoteField() {
103 $this->_params['note'] = "";
104 $this->callAPIFailure('note', 'create', $this->_params,
105 'Mandatory key(s) missing from params array: note'
106 );
107 }
108
109 /**
110 * Check create with partial params.
111 *
112 * Error expected
113 */
114 public function testCreateWithoutEntityId() {
115 unset($this->_params['entity_id']);
116 $this->callAPIFailure('note', 'create', $this->_params,
117 'Mandatory key(s) missing from params array: entity_id');
118 }
119
120 /**
121 * Check create with partially empty params.
122 *
123 * Error expected
124 */
125 public function testCreateWithEmptyEntityId() {
126 $this->_params['entity_id'] = "";
127 $this->callAPIFailure('note', 'create', $this->_params,
128 'Mandatory key(s) missing from params array: entity_id');
129 }
130
131 /**
132 * Check civicrm note create.
133 */
134 public function testCreate() {
135
136 $result = $this->callAPIAndDocument('note', 'create', $this->_params, __FUNCTION__, __FILE__);
137 $this->assertEquals($result['values'][$result['id']]['note'], 'Hello!!! m testing Note');
138 $this->assertEquals(date('Y-m-d', strtotime($this->_params['modified_date'])), date('Y-m-d', strtotime($result['values'][$result['id']]['modified_date'])));
139
140 $this->assertArrayHasKey('id', $result);
141 $note = array(
142 'id' => $result['id'],
143 );
144 $this->noteDelete($note);
145 }
146
147 public function testCreateWithApostropheInString() {
148 $params = array(
149 'entity_table' => 'civicrm_contact',
150 'entity_id' => $this->_contactID,
151 'note' => "Hello!!! ' testing Note",
152 'contact_id' => $this->_contactID,
153 'modified_date' => '2011-01-31',
154 'subject' => "With a '",
155 'sequential' => 1,
156 );
157 $result = $this->callAPISuccess('Note', 'Create', $params);
158 $this->assertAPISuccess($result);
159 $this->assertEquals($result['values'][0]['note'], "Hello!!! ' testing Note");
160 $this->assertEquals($result['values'][0]['subject'], "With a '");
161 $this->assertArrayHasKey('id', $result);
162
163 //CleanUP
164 $note = array(
165 'id' => $result['id'],
166 );
167 $this->noteDelete($note);
168 }
169
170 /**
171 * Check civicrm_note_create - tests used of default set to .
172 */
173 public function testCreateWithoutModifiedDate() {
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 $this->noteDelete(array(
179 'id' => $apiResult['id'],
180 ));
181 }
182
183 /**
184 * Check update with empty parameter array.
185 *
186 * Please don't copy & paste this - is of marginal value
187 * better to put time into the function on Syntax Conformance class that tests this
188 */
189 public function testUpdateWithEmptyParams() {
190 $this->callAPIFailure('note', 'create', array());
191 }
192
193 /**
194 * Check update with missing parameter (contact id).
195 *
196 * Error expected
197 */
198 public function testUpdateWithoutContactId() {
199 $params = array(
200 'entity_id' => $this->_contactID,
201 'entity_table' => 'civicrm_contact',
202 );
203 $this->callAPIFailure('note', 'create', $params,
204 'Mandatory key(s) missing from params array: note'
205 );
206 }
207
208 /**
209 * Check civicrm_note update.
210 */
211 public function testUpdate() {
212 $params = array(
213 'id' => $this->_noteID,
214 'contact_id' => $this->_contactID,
215 'note' => 'Note1',
216 'subject' => 'Hello World',
217 );
218
219 // Update Note.
220 $this->callAPISuccess('note', 'create', $params);
221 $note = $this->callAPISuccess('Note', 'Get', array());
222 $this->assertEquals($note['id'], $this->_noteID);
223 $this->assertEquals($note['values'][$this->_noteID]['entity_id'], $this->_contactID);
224 $this->assertEquals($note['values'][$this->_noteID]['entity_table'], 'civicrm_contact');
225 $this->assertEquals('Hello World', $note['values'][$this->_noteID]['subject']);
226 $this->assertEquals('Note1', $note['values'][$this->_noteID]['note']);
227 }
228
229 /**
230 * Check delete with empty parameters array.
231 *
232 * Error expected.
233 */
234 public function testDeleteWithEmptyParams() {
235 $this->callAPIFailure('note', 'delete', array(), 'Mandatory key(s) missing from params array: id');
236 }
237
238 /**
239 * Check delete with wrong id.
240 *
241 * Error expected
242 */
243 public function testDeleteWithWrongID() {
244 $params = array(
245 'id' => 0,
246 );
247 $this->callAPIFailure('note', 'delete', $params, 'Mandatory key(s) missing from params array: id');
248 }
249
250 /**
251 * Check civicrm_note delete.
252 */
253 public function testDelete() {
254 $additionalNote = $this->noteCreate($this->_contactID);
255
256 $params = array(
257 'id' => $additionalNote['id'],
258 );
259
260 $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__, __FILE__);
261 }
262
263 }
264
265 /**
266 * Test civicrm note create() using example code.
267 */
268 function testNoteCreateExample() {
269 require_once 'api/v3/examples/Note/Create.php';
270 $result = Note_get_example();
271 $expectedResult = Note_get_expectedresult();
272 $this->assertEquals($result, $expectedResult);
273 }