INFRA-132 - @param type fixes
[civicrm-core.git] / tests / phpunit / api / v3 / NoteTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Class contains api test cases for "civicrm_note"
32 *
33 */
34class api_v3_NoteTest extends CiviUnitTestCase {
35
36 protected $_apiversion;
37 protected $_contactID;
38 protected $_params;
39 protected $_noteID;
40 protected $_note;
b7c9bc4c 41
00be9182 42 public function setUp() {
6a488035
TO
43
44 $this->_apiversion = 3;
45 // Connect to the database
46 parent::setUp();
bd772bfe 47 $this->useTransaction(TRUE);
6a488035
TO
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',
481a74f4 57 'subject' => 'Test Note',);
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 /**
100fef9d 65 * Check retrieve note with empty parameter array
6a488035
TO
66 * Error expected
67 */
00be9182 68 public function testGetWithEmptyParams() {
d0e1eff2 69 $this->callAPISuccess('note', 'get', array());
6a488035
TO
70 }
71
72 /**
100fef9d 73 * Check retrieve note with missing patrameters
6a488035
TO
74 * Error expected
75 */
00be9182 76 public function testGetWithoutEntityId() {
6a488035
TO
77 $params = array(
78 'entity_table' => 'civicrm_contact',
6a488035 79 );
b5ee049f 80 $note = $this->callAPISuccess('note', 'get', $params);
6a488035
TO
81 }
82
83 /**
100fef9d 84 * Check civicrm_note_get
6a488035 85 */
00be9182 86 public function testGet() {
6a488035
TO
87 $entityId = $this->_noteID;
88 $params = array(
89 'entity_table' => 'civicrm_contact',
90 'entity_id' => $entityId,
6a488035 91 );
b5ee049f 92 $result = $this->callAPIAndDocument('note', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
93 }
94
95
96 ///////////////// civicrm_note_create methods
97
6a488035
TO
98 /**
99 * Check create with empty parameter array
100 * Error Expected
101 */
00be9182 102 public function testCreateWithEmptyNoteField() {
6a488035 103 $this->_params['note'] = "";
b5ee049f 104 $result = $this->callAPIFailure('note', 'create', $this->_params,
105 'Mandatory key(s) missing from params array: note');
6a488035
TO
106 }
107
108 /**
109 * Check create with partial params
110 * Error expected
111 */
00be9182 112 public function testCreateWithoutEntityId() {
6a488035 113 unset($this->_params['entity_id']);
b5ee049f 114 $result = $this->callAPIFailure('note', 'create', $this->_params,
115 'Mandatory key(s) missing from params array: entity_id');
6a488035
TO
116 }
117
118 /**
119 * Check create with partially empty params
120 * Error expected
121 */
00be9182 122 public function testCreateWithEmptyEntityId() {
6a488035 123 $this->_params['entity_id'] = "";
b5ee049f 124 $result = $this->callAPIFailure('note', 'create', $this->_params,
125 'Mandatory key(s) missing from params array: entity_id');
6a488035
TO
126 }
127
128 /**
129 * Check civicrm_note_create
130 */
00be9182 131 public function testCreate() {
6a488035 132
b5ee049f 133 $result = $this->callAPIAndDocument('note', 'create', $this->_params, __FUNCTION__, __FILE__);
6a488035
TO
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
61ef23bd 137 $this->assertArrayHasKey('id', $result);
6a488035 138 $note = array(
481a74f4 139 'id' => $result['id'],);
6a488035
TO
140 $this->noteDelete($note);
141 }
142
00be9182 143 public function testCreateWithApostropheInString() {
6a488035
TO
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 '",
481a74f4 151 'sequential' => 1,);
b5ee049f 152 $result = $this->callAPISuccess('Note', 'Create', $params);
791c263c 153 $this->assertAPISuccess($result, 'in line ' . __LINE__);
6a488035
TO
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(
481a74f4 160 'id' => $result['id'],);
6a488035
TO
161 $this->noteDelete($note);
162 }
163
164 /**
165 * Check civicrm_note_create - tests used of default set to now
166 */
00be9182 167 public function testCreateWithoutModifiedDate() {
6a488035 168 unset($this->_params['modified_date']);
b5ee049f 169 $apiResult = $this->callAPISuccess('note', 'create', $this->_params);
6a488035
TO
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(
481a74f4 173 'id' => $apiResult['id'],));
6a488035
TO
174 }
175
176
177 ///////////////// civicrm_note_update methods
178
6a488035
TO
179 /**
180 * Check update with empty parameter array
b5ee049f 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
6a488035 183 */
00be9182 184 public function testUpdateWithEmptyParams() {
b5ee049f 185 $note = $this->callAPIFailure('note', 'create', array());
6a488035
TO
186 }
187
188 /**
189 * Check update with missing parameter (contact id)
190 * Error expected
191 */
00be9182 192 public function testUpdateWithoutContactId() {
6a488035
TO
193 $params = array(
194 'entity_id' => $this->_contactID,
481a74f4 195 'entity_table' => 'civicrm_contact',);
b5ee049f 196 $note = $this->callAPIFailure('note', 'create', $params,
197 'Mandatory key(s) missing from params array: note'
6a488035 198 );
6a488035
TO
199 }
200
201 /**
202 * Check civicrm_note_update
203 */
00be9182 204 public function testUpdate() {
6a488035
TO
205 $params = array(
206 'id' => $this->_noteID,
207 'contact_id' => $this->_contactID,
208 'note' => 'Note1',
481a74f4 209 'subject' => 'Hello World',);
6a488035
TO
210
211 //Update Note
b5ee049f 212 $this->callAPISuccess('note', 'create', $params);
213 $note = $this->callAPISuccess('Note', 'Get', array());
6a488035 214 $this->assertEquals($note['id'], $this->_noteID, 'in line ' . __LINE__);
6a488035
TO
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
6a488035
TO
223
224 /**
225 * Check delete with empty parametes array
226 * Error expected
227 */
00be9182 228 public function testDeleteWithEmptyParams() {
b5ee049f 229 $deleteNote = $this->callAPIFailure('note', 'delete', array(), 'Mandatory key(s) missing from params array: id');
6a488035
TO
230 }
231
232 /**
233 * Check delete with wrong id
234 * Error expected
235 */
00be9182 236 public function testDeleteWithWrongID() {
6a488035
TO
237 $params = array(
238 'id' => 0,
6a488035 239 );
6c6e6187 240 $deleteNote = $this->callAPIFailure('note', 'delete', $params, 'Mandatory key(s) missing from params array: id');
6a488035
TO
241 }
242
243 /**
244 * Check civicrm_note_delete
245 */
00be9182 246 public function testDelete() {
6a488035
TO
247 $additionalNote = $this->noteCreate($this->_contactID);
248
249 $params = array(
250 'id' => $additionalNote['id'],
6a488035
TO
251 );
252
b5ee049f 253 $result = $this->callAPIAndDocument('note', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
254 }
255}
256
257/**
258 * Test civicrm_activity_create() using example code
259 */
260function testNoteCreateExample() {
3ec6e38d 261 require_once 'api/v3/examples/Note/Create.php';
6a488035
TO
262 $result = UF_match_get_example();
263 $expectedResult = UF_match_get_expectedresult();
264 $this->assertEquals($result, $expectedResult);
265}