2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
37 require_once 'modules/Schedulers/Scheduler.php';
39 class SchedulersTest extends Sugar_PHPUnit_Framework_TestCase
41 static protected $old_timedate;
43 public static function setUpBeforeClass()
45 self::$old_timedate = $GLOBALS['timedate'];
46 unset($GLOBALS['disable_date_format']);
47 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
48 $GLOBALS['current_user']->setPreference('datef', "m/d/Y");
49 $GLOBALS['current_user']->setPreference('timef', "h:ia");
50 $GLOBALS['current_user']->setPreference('timezone', "America/Los_Angeles");
53 public static function tearDownAfterClass()
55 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
56 unset($GLOBALS['current_user']);
57 $GLOBALS['timedate'] = self::$old_timedate;
60 public function setUp()
62 $this->scheduler = new TestScheduler(false);
63 $GLOBALS['timedate'] = $this->timedate = TimeDate::getInstance();
64 $this->timedate->allow_cache = true;
65 $this->now = $this->timedate->getNow();
68 public function tearDown()
70 $this->timedate->setNow($this->now);
74 * Test catch-up functionality
76 public function testCatchUp()
78 $this->scheduler->job_interval = "*::*::*::*::*";
79 $this->scheduler->catch_up = true;
80 $this->assertTrue($this->scheduler->fireQualified());
81 // we were late to the job
82 $this->timedate->setNow($this->timedate->fromDb("2011-02-01 14:45:00"));
83 $this->scheduler->job_interval = "30::3::*::*::*"; // 10:30 or 11:30 in UTC
84 $this->scheduler->last_run = null;
85 $this->scheduler->catch_up = 0;
86 $this->assertFalse($this->scheduler->fireQualified());
87 // but we can still catch up
88 $this->scheduler->catch_up = 1;
89 $this->assertTrue($this->scheduler->fireQualified());
90 // if already did it, don't catch up
91 $this->scheduler->last_run = $this->timedate->getNow(true)->setDate(2011, 2, 1)->setTime(3, 30)->asDb();
92 $this->assertFalse($this->scheduler->fireQualified());
93 // but if did it yesterday, do
94 $this->scheduler->last_run = $this->timedate->getNow(true)->setDate(2011, 1, 31)->setTime(3, 30)->asDb();
95 $this->assertTrue($this->scheduler->fireQualified());
99 * Test date start/finish
101 public function testDateFromTo()
103 $this->scheduler->job_interval = "*::*::*::*::*";
104 $this->scheduler->catch_up = 0;
105 $this->timedate->setNow($this->timedate->fromDb("2011-04-17 20:00:00"));
108 $this->assertTrue($this->scheduler->fireQualified());
109 // limit start, inclusive
110 $this->scheduler->date_time_start = "2011-01-01 20:00:00";
111 $this->assertTrue($this->scheduler->fireQualified(), "Inclusive start test failed");
113 $this->scheduler->date_time_start = "2011-04-17 20:00:00";
114 $this->assertTrue($this->scheduler->fireQualified(), "Start now test failed");
115 // limit start, exclusive
116 $this->scheduler->date_time_start = "2011-05-01 20:00:00";
117 $this->assertFalse($this->scheduler->fireQualified(), "Exclusive start test failed");
119 // limit end, inclusive
120 $this->scheduler->date_time_start = "2011-01-01 20:00:00";
121 $this->scheduler->date_time_end = "2011-05-01 20:00:00";
122 $this->assertTrue($this->scheduler->fireQualified(), "Inclusive start test failed");
124 $this->scheduler->date_time_end = "2011-04-17 20:00:00";
125 $this->assertTrue($this->scheduler->fireQualified(), "Start now test failed");
126 // limit start, exclusive
127 $this->scheduler->date_time_end = "2011-02-01 20:00:00";
128 $this->assertFalse($this->scheduler->fireQualified(), "Exclusive start test failed");
132 * Test date start/finish
134 public function testActiveFromTo()
136 $this->scheduler->job_interval = "*::*::*::*::*";
137 $this->scheduler->catch_up = 0;
138 $this->scheduler->time_from = "02:00:00";
139 $this->scheduler->time_to = "21:00:00";
141 $this->timedate->setNow($this->timedate->fromUser("1/17/2011 01:20am"));
142 $this->assertFalse($this->scheduler->fireQualified(), "Before start test failed");
143 $this->timedate->setNow($this->timedate->fromUser("2/17/2011 02:00am"));
144 $this->assertTrue($this->scheduler->fireQualified(), "Start test failed");
145 $this->timedate->setNow($this->timedate->fromUser("5/17/2011 10:00am"));
146 $this->assertTrue($this->scheduler->fireQualified(), "After start test failed");
147 $this->timedate->setNow($this->timedate->fromUser("7/17/2011 9:00pm"));
148 $this->assertTrue($this->scheduler->fireQualified(), "End test failed");
149 $this->timedate->setNow($this->timedate->fromUser("11/17/2011 11:30pm"));
150 $this->assertFalse($this->scheduler->fireQualified(), "After end test failed");
153 public function getSchedules()
156 // schedule - now point - last run - should be run?
157 array("*::*::*::*::*", "5/17/2011 1:00am", null, true),
158 array("*::*::*::*::*", "5/17/2011 11:20pm", null, true),
159 array("*::*::*::*::*", "5/17/2011 5:40pm", null, true),
160 array("*::*::*::*::*", "5/17/2011 7:43pm", null, true),
162 array("25::*::*::*::*", "5/17/2011 5:25pm", null, true),
163 array("25::*::*::*::*", "5/17/2011 7:27pm", null, false),
164 array("25::*::*::*::*", "5/17/2011 7:25pm", "5/17/2011 7:25pm", false),
165 array("25::*::*::*::*", "5/17/2011 8:25pm", "5/17/2011 7:25pm", true),
167 array("0::6::*::*::*", "5/17/2011 6:00pm", null, false),
168 array("0::6::*::*::*", "5/17/2011 6:00am", null, true),
169 array("0::6::*::*::*", "5/17/2011 1:00pm", null, false),
170 array("0::6::*::*::*", "5/17/2011 2:00pm", null, false),
172 array("0::2::1::*::*", "2/1/2011 2:00pm", null, false),
173 array("0::2::1::*::*", "2/1/2011 2:00am", null, true),
174 array("0::2::1::*::*", "2/17/2011 2:00am", null, false),
175 array("0::2::1::*::*", "1/31/2011 2:00am", null, false),
176 array("0::2::1::*::*", "2/2/2011 2:00am", null, false),
177 // Every 15 mins on Mon, Tue
178 array("*/15::*::*::*::0,1", "5/16/2011 2:00pm", null, true),
179 array("*/15::*::*::*::0,1", "5/17/2011 2:00pm", null, true),
180 array("*/15::*::*::*::0,1", "5/18/2011 2:00pm", null, false),
181 array("*/15::*::*::*::0,1", "5/17/2011 2:10pm", "5/17/2011 2:00pm", false),
182 array("*/15::*::*::*::0,1", "5/17/2011 2:15pm", "5/17/2011 2:00pm", true),
187 * @dataProvider getSchedules
188 * Test deriveDBDateTimes()
190 public function testDbTimes($sched, $time, $last, $run)
192 $time = $this->timedate->fromUser($time);
193 $time->setTime($time->hour, $time->min, rand(0, 59));
194 $this->timedate->setNow($time);
195 $this->scheduler->job_interval = $sched;
196 $this->scheduler->catch_up = false;
198 $this->scheduler->last_run = $this->timedate->fromUser($last)->asDb();
200 $this->scheduler->last_run = null;
203 $this->assertTrue($this->scheduler->fireQualified());
205 $this->assertFalse($this->scheduler->fireQualified());
210 class TestScheduler extends Scheduler
212 public $fired = false;
214 public $name = "testJob";
215 public $date_time_start = '2005-01-01 19:00:00';
217 public function fire() {