Merge pull request #16469 from civicrm/5.22
[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 */
e790a8cb 26
27/**
28 * Test APIv3 civicrm_action_schedule functions
29 *
6c6e6187
TO
30 * @package CiviCRM_APIv3
31 * @subpackage API_ActionSchedule
acb109b7 32 * @group headless
e790a8cb 33 */
e790a8cb 34class api_v3_ActionScheduleTest extends CiviUnitTestCase {
35 protected $_params;
36 protected $_params2;
37 protected $_entity = 'action_schedule';
e790a8cb 38
e790a8cb 39 /**
11e974ee 40 * Test setup for every test.
e790a8cb 41 */
42 public function setUp() {
e790a8cb 43 parent::setUp();
a12797ee 44 $this->useTransaction(TRUE);
e790a8cb 45 }
46
11e974ee
EM
47 /**
48 * Test simple create action schedule.
2d932085
CW
49 * @param int $version
50 * @dataProvider versionThreeAndFour
11e974ee 51 */
2d932085
CW
52 public function testSimpleActionScheduleCreate($version) {
53 $this->_apiversion = $version;
66908675 54 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
44f817d4 55 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
66908675 56 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
92fcb95f 57 $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
9099cab3 58 $params = [
f54c2beb 59 'title' => $title,
66908675
E
60 'recipient' => $assigneeID,
61 'limit_to' => 1,
d66c61b6 62 'entity_value' => 'Meeting',
63 'entity_status' => 'Scheduled',
66908675
E
64 'is_active' => 1,
65 'record_activity' => 1,
ded34eb1 66 'start_action_date' => 'activity_date_time',
46f5566c 67 'mapping_id' => CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID,
9099cab3 68 ];
66908675 69 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
11e974ee
EM
70 $this->assertTrue(is_numeric($actionSchedule['id']));
71 $this->assertTrue($actionSchedule['id'] > 0);
66908675 72 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
6c6e6187 73 $this->assertEquals($oldCount + 1, $newCount);
e790a8cb 74 }
66908675 75
f54c2beb 76 /**
11e974ee 77 * Check if required fields are not passed.
2d932085
CW
78 * @param int $version
79 * @dataProvider versionThreeAndFour
f54c2beb 80 */
2d932085
CW
81 public function testActionScheduleCreateWithoutRequired($version) {
82 $this->_apiversion = $version;
9099cab3 83 $params = [
92915c55
TO
84 'subject' => 'this case should fail',
85 'scheduled_date_time' => date('Ymd'),
9099cab3 86 ];
f0be539a 87 $this->callAPIFailure('activity', 'create', $params);
f54c2beb 88 }
66908675 89
f0be539a 90 /**
11e974ee 91 * Test create with scheduled dates.
2d932085
CW
92 * @param int $version
93 * @dataProvider versionThreeAndFour
f0be539a 94 */
2d932085
CW
95 public function testActionScheduleWithScheduledDatesCreate($version) {
96 $this->_apiversion = $version;
66908675 97 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
44f817d4 98 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
66908675 99 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
92fcb95f 100 $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
9099cab3 101 $params = [
f54c2beb 102 'title' => $title,
66908675
E
103 'recipient' => $assigneeID,
104 'limit_to' => 1,
d66c61b6 105 'entity_value' => 'Meeting',
106 'entity_status' => 'Scheduled',
66908675
E
107 'is_active' => 1,
108 'record_activity' => 1,
46f5566c 109 'mapping_id' => CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID,
66908675
E
110 'start_action_offset' => 3,
111 'start_action_unit' => 'day',
112 'start_action_condition' => 'before',
113 'start_action_date' => 'activity_date_time',
114 'is_repeat' => 1,
6c6e6187 115 'repetition_frequency_unit' => 'day',
66908675
E
116 'repetition_frequency_interval' => 3,
117 'end_frequency_unit' => 'hour',
118 'end_frequency_interval' => 0,
119 'end_action' => 'before',
120 'end_date' => 'activity_date_time',
121 'body_html' => 'Test description',
21dfd5f5 122 'subject' => 'Test subject',
9099cab3 123 ];
66908675 124 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
11e974ee
EM
125 $this->assertTrue(is_numeric($actionSchedule['id']));
126 $this->assertTrue($actionSchedule['id'] > 0);
2d932085 127 $this->assertEquals($actionSchedule['values'][$actionSchedule['id']]['start_action_offset'], $params['start_action_offset']);
66908675 128 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
6c6e6187 129 $this->assertEquals($oldCount + 1, $newCount);
66908675 130
f54c2beb 131 }
e790a8cb 132
133}