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