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