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