INFRA-132 - tests/ - PHPStorm cleanup
[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 );
59 $this->_note = $this->noteCreate($this->_contactID);
60 $this->_noteID = $this->_note['id'];
61 }
62
63 ///////////////// civicrm_note_get methods
64
65 /**
66 * Check retrieve note with empty parameter array
67 * Error expected
68 */
69 public function testGetWithEmptyParams() {
70 $this->callAPISuccess('note', 'get', array());
71 }
72
73 /**
74 * Check retrieve note with missing patrameters
75 * Error expected
76 */
77 public function testGetWithoutEntityId() {
78 $params = array(
79 'entity_table' => 'civicrm_contact',
80 );
81 $note = $this->callAPISuccess('note', 'get', $params);
82 }
83
84 /**
85 * Check civicrm_note_get
86 */
87 public function testGet() {
88 $entityId = $this->_noteID;
89 $params = array(
90 'entity_table' => 'civicrm_contact',
91 'entity_id' => $entityId,
92 );
93 $result = $this->callAPIAndDocument('note', 'get', $params, __FUNCTION__, __FILE__);
94 }
95
96
97 ///////////////// civicrm_note_create methods
98
99 /**
100 * Check create with empty parameter array
101 * Error Expected
102 */
103 public function testCreateWithEmptyNoteField() {
104 $this->_params['note'] = "";
105 $result = $this->callAPIFailure('note', 'create', $this->_params,
106 'Mandatory key(s) missing from params array: note');
107 }
108
109 /**
110 * Check create with partial params
111 * Error expected
112 */
113 public function testCreateWithoutEntityId() {
114 unset($this->_params['entity_id']);
115 $result = $this->callAPIFailure('note', 'create', $this->_params,
116 'Mandatory key(s) missing from params array: entity_id');
117 }
118
119 /**
120 * Check create with partially empty params
121 * Error expected
122 */
123 public function testCreateWithEmptyEntityId() {
124 $this->_params['entity_id'] = "";
125 $result = $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', 'in line ' . __LINE__);
136 $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__);
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, 'in line ' . __LINE__);
157 $this->assertEquals($result['values'][0]['note'], "Hello!!! ' testing Note", 'in line ' . __LINE__);
158 $this->assertEquals($result['values'][0]['subject'], "With a '", 'in line ' . __LINE__);
159 $this->assertArrayHasKey('id', $result, 'in line ' . __LINE__);
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 now
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 ///////////////// civicrm_note_update methods
183
184 /**
185 * Check update with empty parameter array
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 $note = $this->callAPIFailure('note', 'create', array());
191 }
192
193 /**
194 * Check update with missing parameter (contact id)
195 * Error expected
196 */
197 public function testUpdateWithoutContactId() {
198 $params = array(
199 'entity_id' => $this->_contactID,
200 'entity_table' => 'civicrm_contact',
201 );
202 $note = $this->callAPIFailure('note', 'create', $params,
203 'Mandatory key(s) missing from params array: note'
204 );
205 }
206
207 /**
208 * Check civicrm_note_update
209 */
210 public function testUpdate() {
211 $params = array(
212 'id' => $this->_noteID,
213 'contact_id' => $this->_contactID,
214 'note' => 'Note1',
215 'subject' => 'Hello World',
216 );
217
218 //Update Note
219 $this->callAPISuccess('note', 'create', $params);
220 $note = $this->callAPISuccess('Note', 'Get', array());
221 $this->assertEquals($note['id'], $this->_noteID, 'in line ' . __LINE__);
222 $this->assertEquals($note['values'][$this->_noteID]['entity_id'], $this->_contactID, 'in line ' . __LINE__);
223 $this->assertEquals($note['values'][$this->_noteID]['entity_table'], 'civicrm_contact', 'in line ' . __LINE__);
224 $this->assertEquals('Hello World', $note['values'][$this->_noteID]['subject'], 'in line ' . __LINE__);
225 $this->assertEquals('Note1', $note['values'][$this->_noteID]['note'], 'in line ' . __LINE__);
226 }
227
228 ///////////////// civicrm_note_delete methods
229
230
231 /**
232 * Check delete with empty parametes array
233 * Error expected
234 */
235 public function testDeleteWithEmptyParams() {
236 $deleteNote = $this->callAPIFailure('note', 'delete', array(), 'Mandatory key(s) missing from params array: id');
237 }
238
239 /**
240 * Check delete with wrong id
241 * Error expected
242 */
243 public function testDeleteWithWrongID() {
244 $params = array(
245 'id' => 0,
246 );
247 $deleteNote = $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 $result = $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__, __FILE__);
261 }
262 }
263
264 /**
265 * Test civicrm_activity_create() using example code
266 */
267 function testNoteCreateExample() {
268 require_once 'api/v3/examples/Note/Create.php';
269 $result = UF_match_get_example();
270 $expectedResult = UF_match_get_expectedresult();
271 $this->assertEquals($result, $expectedResult);
272 }