2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 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 'include/SugarQueue/SugarJobQueue.php';
38 require_once 'include/SugarQueue/SugarCronRemoteJobs.php';
40 class CronRemoteTest extends Sugar_PHPUnit_Framework_TestCase
42 public static function setUpBeforeClass()
44 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
46 $GLOBALS['db']->query("DELETE FROM job_queue WHERE status='queued'");
47 $GLOBALS['sugar_config']['job_server'] = "http://test.job.server/";
50 public static function tearDownAdterClass()
52 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
53 unset($GLOBALS['sugar_config']['job_server']);
56 public function setUp()
58 $this->jq = $jobq = new SugarCronRemoteJobs();
59 $this->client = new CronHttpMock();
60 $this->jq->setClient($this->client);
63 public function tearDown()
65 $GLOBALS['db']->query("DELETE FROM job_queue WHERE scheduler_id='unittest'");
68 public function testQueueJob()
70 $job = new SchedulersJob();
71 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
72 $job->scheduler_id = 'unittest';
73 $job->execute_time = TimeDate::getInstance()->nowDb();
74 $job->name = "Unit test Job";
75 $job->target = "function::CronTest::cronJobFunction";
76 $job->assigned_user_id = $GLOBALS['current_user']->id;
80 $this->client->return = json_encode(array("ok" => $job->id));
82 $this->jq->min_interval = 0; // disable throttle
83 $this->jq->disable_schedulers = true;
84 $this->jq->runCycle();
86 $this->assertTrue($this->jq->runOk());
88 $this->assertEquals("http://test.job.server/submitJob", $this->client->call_url);
89 parse_str($this->client->call_data, $qdata);
90 $data = json_decode($qdata['data'], true);
91 $this->assertEquals($jobid, $data['job']);
92 $this->assertEquals($this->jq->getMyId(), $data['client']);
93 $this->assertEquals($GLOBALS['sugar_config']['site_url'], $data['instance']);
95 $job = new SchedulersJob();
96 $job->retrieve($jobid);
97 $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
98 $this->assertEquals($this->jq->getMyId(), $job->client, "Wrong client");
101 public function testServerFailure()
103 $job = new SchedulersJob();
104 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
105 $job->scheduler_id = 'unittest';
106 $job->execute_time = TimeDate::getInstance()->nowDb();
107 $job->name = "Unit test Job";
108 $job->target = "function::CronTest::cronJobFunction";
109 $job->assigned_user_id = $GLOBALS['current_user']->id;
113 $this->client->return = ''; // return nothing
115 $this->jq->min_interval = 0; // disable throttle
116 $this->jq->disable_schedulers = true;
117 $this->jq->runCycle();
119 $this->assertFalse($this->jq->runOk());
120 $job = new SchedulersJob();
121 $job->retrieve($jobid);
122 $this->assertEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");
123 $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
126 public function testServerFailureWithError()
128 $job = new SchedulersJob();
129 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
130 $job->scheduler_id = 'unittest';
131 $job->execute_time = TimeDate::getInstance()->nowDb();
132 $job->name = "Unit test Job";
133 $job->target = "function::CronTest::cronJobFunction";
134 $job->assigned_user_id = $GLOBALS['current_user']->id;
138 $this->client->return = 'This is not the server you are looking for';
140 $this->jq->min_interval = 0; // disable throttle
141 $this->jq->disable_schedulers = true;
142 $this->jq->runCycle();
144 $this->assertFalse($this->jq->runOk());
145 $job = new SchedulersJob();
146 $job->retrieve($jobid);
147 $this->assertEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");
148 $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
149 $this->assertContains('This is not the server you are looking for', $job->message, "Wrong message");
153 class CronHttpMock extends SugarHttpClient
158 public function callRest($url, $data) {
159 $this->call_url = $url;
160 $this->call_data = $data;
161 return $this->return;