Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / ActionScheduleTest.php
1 <?php
2 /**
3 * @file
4 * File for the TestActionSchedule class
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 */
26
27 /**
28 * Test APIv3 civicrm_action_schedule functions
29 *
30 * @package CiviCRM_APIv3
31 * @subpackage API_ActionSchedule
32 * @group headless
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 * Test setup for every test.
42 */
43 public function setUp() {
44 parent::setUp();
45 $this->useTransaction(TRUE);
46 }
47
48 /**
49 * Test simple create action schedule.
50 */
51 public function testSimpleActionScheduleCreate() {
52 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
53 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
54 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
55 $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
56 $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
57 $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
58 $params = array(
59 'title' => $title,
60 'recipient' => $assigneeID,
61 'limit_to' => 1,
62 'entity_value' => $activityTypeId,
63 'entity_status' => $scheduledStatus,
64 'is_active' => 1,
65 'record_activity' => 1,
66 'start_action_date' => 'activity_date_time',
67 'mapping_id' => CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID,
68 );
69 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
70 $this->assertTrue(is_numeric($actionSchedule['id']));
71 $this->assertTrue($actionSchedule['id'] > 0);
72 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
73 $this->assertEquals($oldCount + 1, $newCount);
74 }
75
76 /**
77 * Check if required fields are not passed.
78 */
79 public function testActionScheduleCreateWithoutRequired() {
80 $params = array(
81 'subject' => 'this case should fail',
82 'scheduled_date_time' => date('Ymd'),
83 );
84 $this->callAPIFailure('activity', 'create', $params);
85 }
86
87 /**
88 * Test create with scheduled dates.
89 */
90 public function testActionScheduleWithScheduledDatesCreate() {
91 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
92 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
93 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
94 $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
95 $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
96 $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
97 $params = array(
98 'title' => $title,
99 'recipient' => $assigneeID,
100 'limit_to' => 1,
101 'entity_value' => $activityTypeId,
102 'entity_status' => $scheduledStatus,
103 'is_active' => 1,
104 'record_activity' => 1,
105 'mapping_id' => CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID,
106 'start_action_offset' => 3,
107 'start_action_unit' => 'day',
108 'start_action_condition' => 'before',
109 'start_action_date' => 'activity_date_time',
110 'is_repeat' => 1,
111 'repetition_frequency_unit' => 'day',
112 'repetition_frequency_interval' => 3,
113 'end_frequency_unit' => 'hour',
114 'end_frequency_interval' => 0,
115 'end_action' => 'before',
116 'end_date' => 'activity_date_time',
117 'body_html' => 'Test description',
118 'subject' => 'Test subject',
119 );
120 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
121 $this->assertTrue(is_numeric($actionSchedule['id']));
122 $this->assertTrue($actionSchedule['id'] > 0);
123 $this->assertEquals($actionSchedule['values'][$actionSchedule['id']]['start_action_offset'][0], $params['start_action_offset']);
124 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
125 $this->assertEquals($oldCount + 1, $newCount);
126
127 }
128
129 }