CRM-13244 - ActionSchedule - Extract entity-specific logic for tokens and dropdowns.
[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 require_once 'CiviTest/CiviUnitTestCase.php';
27
28
29 /**
30 * Test APIv3 civicrm_action_schedule functions
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_ActionSchedule
34 */
35 class api_v3_ActionScheduleTest extends CiviUnitTestCase {
36 protected $_params;
37 protected $_params2;
38 protected $_entity = 'action_schedule';
39 protected $_apiversion = 3;
40
41 /**
42 * Test setup for every test.
43 */
44 public function setUp() {
45 parent::setUp();
46 $this->useTransaction(TRUE);
47 }
48
49 /**
50 * Test simple create action schedule.
51 */
52 public function testSimpleActionScheduleCreate() {
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);
56 $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
57 $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array(
58 'entity_value' => 'activity_type',
59 )));
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' => $mapping->id,
72 );
73 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
74 $this->assertTrue(is_numeric($actionSchedule['id']));
75 $this->assertTrue($actionSchedule['id'] > 0);
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 $this->callAPIFailure('activity', 'create', $params);
89 }
90
91 /**
92 * Test create with scheduled dates.
93 */
94 public function testActionScheduleWithScheduledDatesCreate() {
95 $oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
96 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
97 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
98 $scheduledStatus = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name');
99 $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings(array(
100 'entity_value' => 'activity_type',
101 )));
102 $activityTypeId = CRM_Core_OptionGroup::getValue('activity_type', "Meeting", 'name');
103 $title = "simpleActionSchedule" . substr(sha1(rand()), 0, 7);
104 $params = array(
105 'title' => $title,
106 'recipient' => $assigneeID,
107 'limit_to' => 1,
108 'entity_value' => $activityTypeId,
109 'entity_status' => $scheduledStatus,
110 'is_active' => 1,
111 'record_activity' => 1,
112 'mapping_id' => $mapping->id,
113 'start_action_offset' => 3,
114 'start_action_unit' => 'day',
115 'start_action_condition' => 'before',
116 'start_action_date' => 'activity_date_time',
117 'is_repeat' => 1,
118 'repetition_frequency_unit' => 'day',
119 'repetition_frequency_interval' => 3,
120 'end_frequency_unit' => 'hour',
121 'end_frequency_interval' => 0,
122 'end_action' => 'before',
123 'end_date' => 'activity_date_time',
124 'body_html' => 'Test description',
125 'subject' => 'Test subject',
126 );
127 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', $params);
128 $this->assertTrue(is_numeric($actionSchedule['id']));
129 $this->assertTrue($actionSchedule['id'] > 0);
130 $this->assertEquals($actionSchedule['values'][$actionSchedule['id']]['start_action_offset'][0], $params['start_action_offset']);
131 $newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_action_schedule');
132 $this->assertEquals($oldCount + 1, $newCount);
133
134 }
135
136 }