Merge pull request #5536 from totten/4.5-httpclient
[civicrm-core.git] / tests / phpunit / api / v3 / ActionScheduleTest.php
CommitLineData
e790a8cb 1<?php
2/**
11e974ee
EM
3 * @file
4 * File for the TestActionSchedule class
e790a8cb 5 *
6 * (PHP 5)
7 *
8 * CiviCRM is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Affero General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * CiviCRM is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public
19 * License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 */
22
23/**
24 * Include class definitions
25 */
26require_once 'CiviTest/CiviUnitTestCase.php';
27
28
29/**
30 * Test APIv3 civicrm_action_schedule functions
31 *
6c6e6187
TO
32 * @package CiviCRM_APIv3
33 * @subpackage API_ActionSchedule
e790a8cb 34 */
e790a8cb 35class api_v3_ActionScheduleTest extends CiviUnitTestCase {
36 protected $_params;
37 protected $_params2;
38 protected $_entity = 'action_schedule';
39 protected $_apiversion = 3;
40
e790a8cb 41 /**
11e974ee 42 * Test setup for every test.
e790a8cb 43 */
44 public function setUp() {
e790a8cb 45 parent::setUp();
a12797ee 46 $this->useTransaction(TRUE);
e790a8cb 47 }
48
11e974ee
EM
49 /**
50 * Test simple create action schedule.
51 */
00be9182 52 public function testSimpleActionScheduleCreate() {
66908675
E
53 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
54 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
55 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
66908675
E
56 $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
57 $mappingId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_ActionMapping', 'activity_type', 'id', 'entity_value');
58 $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
92fcb95f 59 $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
66908675 60 $params = array(
f54c2beb 61 'title' => $title,
66908675
E
62 'recipient' => $assigneeID,
63 'limit_to' => 1,
64 'entity_value' => $activityTypeId,
65 'entity_status' => $scheduledStatus,
66 'is_active' => 1,
67 'record_activity' => 1,
ded34eb1 68 'start_action_date' => 'activity_date_time',
66908675 69 'mapping_id' => $mappingId,
e790a8cb 70 );
66908675 71 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
11e974ee
EM
72 $this->assertTrue(is_numeric($actionSchedule['id']));
73 $this->assertTrue($actionSchedule['id'] > 0);
66908675 74 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
6c6e6187 75 $this->assertEquals($oldCount + 1, $newCount);
e790a8cb 76 }
66908675 77
f54c2beb 78 /**
11e974ee 79 * Check if required fields are not passed.
f54c2beb 80 */
00be9182 81 public function testActionScheduleCreateWithoutRequired() {
66908675 82 $params = array(
92915c55
TO
83 'subject' => 'this case should fail',
84 'scheduled_date_time' => date('Ymd'),
66908675 85 );
f0be539a 86 $this->callAPIFailure('activity', 'create', $params);
f54c2beb 87 }
66908675 88
f0be539a 89 /**
11e974ee 90 * Test create with scheduled dates.
f0be539a 91 */
00be9182 92 public function testActionScheduleWithScheduledDatesCreate() {
66908675
E
93 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
94 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
95 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
66908675
E
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');
92fcb95f 99 $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
66908675 100 $params = array(
f54c2beb 101 'title' => $title,
66908675
E
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,
6c6e6187 114 'repetition_frequency_unit' => 'day',
66908675
E
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',
21dfd5f5 121 'subject' => 'Test subject',
f54c2beb 122 );
66908675 123 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
11e974ee
EM
124 $this->assertTrue(is_numeric($actionSchedule['id']));
125 $this->assertTrue($actionSchedule['id'] > 0);
66908675
E
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');
6c6e6187 128 $this->assertEquals($oldCount + 1, $newCount);
66908675 129
f54c2beb 130 }
e790a8cb 131
132}