Merge pull request #995 from dpradeep/CRM-10146-commit
[civicrm-core.git] / tests / phpunit / api / v3 / JobTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * File for the CiviCRM APIv3 job functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_Job
34 *
35 * @copyright CiviCRM LLC (c) 2004-2013
36 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
37 *
38 */
39 require_once 'CiviTest/CiviUnitTestCase.php';
40 class api_v3_JobTest extends CiviUnitTestCase {
41 protected $_apiversion;
42
43 public $_eNoticeCompliant = TRUE;
44 public $DBResetRequired = FALSE;
45 public $_entity = 'Job';
46 public $_apiVersion = 3;
47
48 function setUp() {
49 parent::setUp();
50 $this->quickCleanup(array('civicrm_job'));
51 }
52
53 function tearDown() {
54 $this->quickCleanup(array('civicrm_job'));
55 parent::tearDown();
56 }
57
58 /**
59 * check with no name
60 */
61 function testCreateWithoutName() {
62 $params = array(
63 'is_active' => 1,
64 'version' => $this->_apiVersion,
65 );
66 $result = $this->callAPIFailure('job', 'create', $params);
67 $this->assertEquals($result['error_message'],
68 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
69 );
70 }
71
72 /**
73 * create job with an invalid "run_frequency" value
74 */
75 function testCreateWithInvalidFrequency() {
76 $params = array(
77 'version' => $this->_apiVersion,
78 'sequential' => 1,
79 'name' => 'API_Test_Job',
80 'description' => 'A long description written by hand in cursive',
81 'run_frequency' => 'Fortnightly',
82 'api_entity' => 'ApiTestEntity',
83 'api_action' => 'apitestaction',
84 'parameters' => 'Semi-formal explanation of runtime job parameters',
85 'is_active' => 1,
86 );
87 $result = $this->callAPIFailure('job', 'create', $params);
88 }
89
90 /**
91 * create job
92 */
93 function testCreate() {
94 $params = array(
95 'version' => $this->_apiVersion,
96 'sequential' => 1,
97 'name' => 'API_Test_Job',
98 'description' => 'A long description written by hand in cursive',
99 'run_frequency' => 'Daily',
100 'api_entity' => 'ApiTestEntity',
101 'api_action' => 'apitestaction',
102 'parameters' => 'Semi-formal explanation of runtime job parameters',
103 'is_active' => 1,
104 );
105 $result = civicrm_api('job', 'create', $params);
106 $this->assertAPISuccess($result);
107 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
108 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
109
110 // mutate $params to match expected return value
111 unset($params['version']);
112 unset($params['sequential']);
113 //assertDBState compares expected values in $result to actual values in the DB
114 $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $params);
115 }
116
117 /**
118 * check with empty array
119 */
120 function testDeleteEmpty() {
121 $params = array();
122 $result = $this->callAPIFailure('job', 'delete', $params);
123 }
124
125 /**
126 * check with No array
127 */
128 function testDeleteParamsNotArray() {
129 $result = $this->callAPIFailure('job', 'delete', 'string');
130 }
131
132 /**
133 * check if required fields are not passed
134 */
135 function testDeleteWithoutRequired() {
136 $params = array(
137 'name' => 'API_Test_PP',
138 'title' => 'API Test Payment Processor',
139 'class_name' => 'CRM_Core_Payment_APITest',
140 );
141
142 $result = $this->callAPIFailure('job', 'delete', $params);
143 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
144 }
145
146 /**
147 * check with incorrect required fields
148 */
149 function testDeleteWithIncorrectData() {
150 $params = array(
151 'id' => 'abcd',
152 'version' => $this->_apiVersion,
153 );
154
155 $result = $this->callAPIFailure('job', 'delete', $params);
156 $this->assertEquals($result['error_message'], 'Invalid value for job ID');
157 }
158
159 /**
160 * check job delete
161 */
162 function testDelete() {
163 $createParams = array(
164 'version' => $this->_apiVersion,
165 'sequential' => 1,
166 'name' => 'API_Test_Job',
167 'description' => 'A long description written by hand in cursive',
168 'run_frequency' => 'Daily',
169 'api_entity' => 'ApiTestEntity',
170 'api_action' => 'apitestaction',
171 'parameters' => 'Semi-formal explanation of runtime job parameters',
172 'is_active' => 1,
173 );
174 $createResult = civicrm_api('job', 'create', $createParams);
175 $this->assertAPISuccess($createResult);
176
177 $params = array(
178 'id' => $createResult['id'],
179 'version' => $this->_apiVersion,
180 );
181 $result = civicrm_api('job', 'delete', $params);
182 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
183 $this->assertAPISuccess($result);
184 }
185
186 /**
187
188 public function testCallUpdateGreetingMissingParams() {
189 $result = civicrm_api($this->_entity, 'update_greeting', array('gt' => 1, 'version' => $this->_apiVersion));
190 $this->assertEquals('Mandatory key(s) missing from params array: ct', $result['error_message']);
191 }
192
193 public function testCallUpdateGreetingIncorrectParams() {
194 $result = civicrm_api($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds', 'version' => $this->_apiVersion));
195 $this->assertEquals('ct `djkfhdskjfhds` is not valid.', $result['error_message']);
196 }
197 /*
198 * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function
199 */
200 public function testCallUpdateGreetingSuccess() {
201 $result = civicrm_api($this->_entity, 'update_greeting', array('gt' => 'postal_greeting', 'ct' => 'Individual', 'version' => $this->_apiVersion));
202 $this->assertAPISuccess($result);
203 }
204
205 public function testCallUpdateGreetingCommaSeparatedParamsSuccess() {
206 $gt = 'postal_greeting,email_greeting,addressee';
207 $ct = 'Individual,Household';
208 $result = civicrm_api($this->_entity, 'update_greeting', array('gt' => $gt, 'ct' => $ct, 'version' => $this->_apiVersion));
209 $this->assertAPISuccess($result);
210 }
211 }
212