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