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
42 public static function setUpBeforeClass()
44 unset($GLOBALS['disable_date_format']);
45 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
46 $GLOBALS['current_user']->setPreference('datef', "m/d/Y");
47 $GLOBALS['current_user']->setPreference('timef', "h:ia");
48 $GLOBALS['current_user']->setPreference('timezone', "America/Los_Angeles");
51 public static function tearDownAfterClass()
53 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
54 unset($GLOBALS['current_user']);
57 public function setUp()
59 $this->scheduler = new TestScheduler(false);
60 $this->timedate = TimeDate::getInstance();
61 $this->now = $this->timedate->getNow();
64 public function tearDown()
66 $this->timedate->setNow($this->now);
70 * Test catch-up functionality
72 public function testCatchUp()
74 $this->scheduler->job_interval = "*::*::*::*::*";
75 $this->scheduler->catch_up = true;
76 $this->assertTrue($this->scheduler->fireQualified());
77 // we were late to the job
78 $this->timedate->setNow($this->timedate->fromDb("2011-02-01 14:45:00"));
79 $this->scheduler->job_interval = "30::3::*::*::*"; // 10:30 or 11:30 in UTC
80 $this->scheduler->last_run = null;
81 $this->scheduler->catch_up = 0;
82 $this->assertFalse($this->scheduler->fireQualified());
83 // but we can still catch up
84 $this->scheduler->catch_up = 1;
85 $this->assertTrue($this->scheduler->fireQualified());
86 // if already did it, don't catch up
87 $this->scheduler->last_run = $this->timedate->getNow(true)->setDate(2011, 2, 1)->setTime(3, 30)->asDb();
88 $this->assertFalse($this->scheduler->fireQualified());
89 // but if did it yesterday, do
90 $this->scheduler->last_run = $this->timedate->getNow(true)->setDate(2011, 1, 31)->setTime(3, 30)->asDb();
91 $this->assertTrue($this->scheduler->fireQualified());
95 * Test date start/finish
97 public function testDateFromTo()
99 $this->scheduler->job_interval = "*::*::*::*::*";
100 $this->scheduler->catch_up = 0;
101 $this->timedate->setNow($this->timedate->fromDb("2011-04-17 20:00:00"));
104 $this->assertTrue($this->scheduler->fireQualified());
105 // limit start, inclusive
106 $this->scheduler->date_time_start = "2011-01-01 20:00:00";
107 $this->assertTrue($this->scheduler->fireQualified(), "Inclusive start test failed");
109 $this->scheduler->date_time_start = "2011-04-17 20:00:00";
110 $this->assertTrue($this->scheduler->fireQualified(), "Start now test failed");
111 // limit start, exclusive
112 $this->scheduler->date_time_start = "2011-05-01 20:00:00";
113 $this->assertFalse($this->scheduler->fireQualified(), "Exclusive start test failed");
115 // limit end, inclusive
116 $this->scheduler->date_time_start = "2011-01-01 20:00:00";
117 $this->scheduler->date_time_end = "2011-05-01 20:00:00";
118 $this->assertTrue($this->scheduler->fireQualified(), "Inclusive start test failed");
120 $this->scheduler->date_time_end = "2011-04-17 20:00:00";
121 $this->assertTrue($this->scheduler->fireQualified(), "Start now test failed");
122 // limit start, exclusive
123 $this->scheduler->date_time_end = "2011-02-01 20:00:00";
124 $this->assertFalse($this->scheduler->fireQualified(), "Exclusive start test failed");
128 * Test date start/finish
130 public function testActiveFromTo()
132 $this->scheduler->job_interval = "*::*::*::*::*";
133 $this->scheduler->catch_up = 0;
134 $this->scheduler->time_from = "02:00:00";
135 $this->scheduler->time_to = "21:00:00";
137 $this->timedate->setNow($this->timedate->fromUser("1/17/2011 01:20am"));
138 $this->assertFalse($this->scheduler->fireQualified(), "Before start test failed");
139 $this->timedate->setNow($this->timedate->fromUser("2/17/2011 02:00am"));
140 $this->assertTrue($this->scheduler->fireQualified(), "Start test failed");
141 $this->timedate->setNow($this->timedate->fromUser("5/17/2011 10:00am"));
142 $this->assertTrue($this->scheduler->fireQualified(), "After start test failed");
143 $this->timedate->setNow($this->timedate->fromUser("7/17/2011 9:00pm"));
144 $this->assertTrue($this->scheduler->fireQualified(), "End test failed");
145 $this->timedate->setNow($this->timedate->fromUser("11/17/2011 11:30pm"));
146 $this->assertFalse($this->scheduler->fireQualified(), "After end test failed");
149 public function getSchedules()
152 // schedule - now point - last run - should be run?
153 array("*::*::*::*::*", "5/17/2011 1:00am", null, true),
154 array("*::*::*::*::*", "5/17/2011 11:20pm", null, true),
155 array("*::*::*::*::*", "5/17/2011 5:40pm", null, true),
156 array("*::*::*::*::*", "5/17/2011 7:43pm", null, true),
158 array("25::*::*::*::*", "5/17/2011 5:25pm", null, true),
159 array("25::*::*::*::*", "5/17/2011 7:27pm", null, false),
160 array("25::*::*::*::*", "5/17/2011 7:25pm", "5/17/2011 7:25pm", false),
161 array("25::*::*::*::*", "5/17/2011 8:25pm", "5/17/2011 7:25pm", true),
163 array("0::6::*::*::*", "5/17/2011 6:00pm", null, false),
164 array("0::6::*::*::*", "5/17/2011 6:00am", null, true),
165 array("0::6::*::*::*", "5/17/2011 1:00pm", null, false),
166 array("0::6::*::*::*", "5/17/2011 2:00pm", null, false),
168 array("0::2::1::*::*", "2/1/2011 2:00pm", null, false),
169 array("0::2::1::*::*", "2/1/2011 2:00am", null, true),
170 array("0::2::1::*::*", "2/17/2011 2:00am", null, false),
171 array("0::2::1::*::*", "1/31/2011 2:00am", null, false),
172 array("0::2::1::*::*", "2/2/2011 2:00am", null, false),
173 // Every 15 mins on Mon, Tue
174 array("*/15::*::*::*::0,1", "5/16/2011 2:00pm", null, true),
175 array("*/15::*::*::*::0,1", "5/17/2011 2:00pm", null, true),
176 array("*/15::*::*::*::0,1", "5/18/2011 2:00pm", null, false),
177 array("*/15::*::*::*::0,1", "5/17/2011 2:10pm", "5/17/2011 2:00pm", false),
178 array("*/15::*::*::*::0,1", "5/17/2011 2:15pm", "5/17/2011 2:00pm", true),
183 * @dataProvider getSchedules
184 * Test deriveDBDateTimes()
186 public function testDbTimes($sched, $time, $last, $run)
188 $time = $this->timedate->fromUser($time);
189 $time->setTime($time->hour, $time->min, rand(0, 59));
190 $this->timedate->setNow($time);
191 $this->scheduler->job_interval = $sched;
192 $this->scheduler->catch_up = false;
194 $this->scheduler->last_run = $this->timedate->fromUser($last)->asDb();
196 $this->scheduler->last_run = null;
199 $this->assertTrue($this->scheduler->fireQualified());
201 $this->assertFalse($this->scheduler->fireQualified());
206 class TestScheduler extends Scheduler
208 public $fired = false;
210 public $name = "testJob";
211 public $date_time_start = '2005-01-01 19:00:00';
213 public function fire() {