Merge pull request #4909 from pratikshad/broken-webtest
[civicrm-core.git] / tests / phpunit / api / v3 / ActionScheduleTest.php
1 <?php
2 /**
3 * File for the TestActionSchedule class
4 *
5 * (PHP 5)
6 *
7 * CiviCRM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation; either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * CiviCRM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 */
21
22 /**
23 * Include class definitions
24 */
25 require_once 'CiviTest/CiviUnitTestCase.php';
26
27
28 /**
29 * Test APIv3 civicrm_action_schedule functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_ActionSchedule
33 */
34 class api_v3_ActionScheduleTest extends CiviUnitTestCase {
35 protected $_params;
36 protected $_params2;
37 protected $_entity = 'action_schedule';
38 protected $_apiversion = 3;
39
40
41 /**
42 * Test setup for every test
43 *
44 * Connect to the database, truncate the tables that will be used
45 * and redirect stdin to a temporary file
46 */
47 public function setUp() {
48 // Connect to the database
49 parent::setUp();
50 $this->useTransaction(TRUE);
51 }
52
53 public function testSimpleActionScheduleCreate() {
54 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
55 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
56 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
57 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
58 $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
59 $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'activity_type', 'id', 'entity_value');
60 $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
61 $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
62 $params = array(
63 'title' => $title,
64 'recipient' => $assigneeID,
65 'limit_to' => 1,
66 'entity_value' => $activityTypeId,
67 'entity_status' => $scheduledStatus,
68 'is_active' => 1,
69 'record_activity' => 1,
70 'start_action_date' => 'activity_date_time',
71 'mapping_id' => $mappingId,
72 );
73 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
74 $this->assertTrue(is_numeric($actionSchedule['id']), "In line " . __LINE__);
75 $this->assertTrue($actionSchedule['id'] > 0, "In line " . __LINE__);
76 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
77 $this->assertEquals($oldCount + 1, $newCount);
78 }
79
80 /**
81 * Check if required fields are not passed
82 */
83 public function testActionScheduleCreateWithoutRequired() {
84 $params = array(
85 'subject' => 'this case should fail',
86 'scheduled_date_time' => date('Ymd'),
87 );
88 $result = $this->callAPIFailure('activity', 'create', $params);
89 }
90
91 public function testActionScheduleWithScheduledDatesCreate() {
92 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
93 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
94 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
95 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
96 $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
97 $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'activity_type', 'id', 'entity_value');
98 $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
99 $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
100 $params = array(
101 'title' => $title,
102 'recipient' => $assigneeID,
103 'limit_to' => 1,
104 'entity_value' => $activityTypeId,
105 'entity_status' => $scheduledStatus,
106 'is_active' => 1,
107 'record_activity' => 1,
108 'mapping_id' => $mappingId,
109 'start_action_offset' => 3,
110 'start_action_unit' => 'day',
111 'start_action_condition' => 'before',
112 'start_action_date' => 'activity_date_time',
113 'is_repeat' => 1,
114 'repetition_frequency_unit' => 'day',
115 'repetition_frequency_interval' => 3,
116 'end_frequency_unit' => 'hour',
117 'end_frequency_interval' => 0,
118 'end_action' => 'before',
119 'end_date' => 'activity_date_time',
120 'body_html' => 'Test description',
121 'subject' => 'Test subject',
122 );
123 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
124 $this->assertTrue(is_numeric($actionSchedule['id']), "In line " . __LINE__);
125 $this->assertTrue($actionSchedule['id'] > 0, "In line " . __LINE__);
126 $this->assertEquals($actionSchedule['values'][$actionSchedule['id']]['start_action_offset'][0], $params['start_action_offset']);
127 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
128 $this->assertEquals($oldCount + 1, $newCount);
129
130 }
131
132 }