]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarQueue/CronRemoteTest.php
Release 6.5.10
[Github/sugarcrm.git] / tests / include / SugarQueue / CronRemoteTest.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37 require_once 'include/SugarQueue/SugarJobQueue.php';
38 require_once 'include/SugarQueue/SugarCronRemoteJobs.php';
39
40 class CronRemoteTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     public static function setUpBeforeClass()
43     {
44         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
45         // clean up queue
46                 $GLOBALS['db']->query("DELETE FROM job_queue WHERE status='queued'");
47                 $GLOBALS['sugar_config']['job_server'] = "http://test.job.server/";
48     }
49
50     public static function tearDownAdterClass()
51     {
52         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
53         unset($GLOBALS['sugar_config']['job_server']);
54     }
55
56     public function setUp()
57     {
58         $this->jq = $jobq = new SugarCronRemoteJobs();
59         $this->client = new CronHttpMock();
60         $this->jq->setClient($this->client);
61     }
62
63     public function tearDown()
64     {
65         $GLOBALS['db']->query("DELETE FROM job_queue WHERE scheduler_id='unittest'");
66     }
67
68     public function testQueueJob()
69     {
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;
77         $job->save();
78         $jobid = $job->id;
79
80         $this->client->return = json_encode(array("ok" => $job->id));
81
82         $this->jq->min_interval = 0; // disable throttle
83         $this->jq->disable_schedulers = true;
84         $this->jq->runCycle();
85
86         $this->assertTrue($this->jq->runOk());
87
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']);
94
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");
99     }
100
101     public function testServerFailure()
102     {
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;
110         $job->save();
111         $jobid = $job->id;
112
113         $this->client->return = ''; // return nothing
114
115         $this->jq->min_interval = 0; // disable throttle
116         $this->jq->disable_schedulers = true;
117         $this->jq->runCycle();
118
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");
124     }
125
126     public function testServerFailureWithError()
127     {
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;
135         $job->save();
136         $jobid = $job->id;
137
138         $this->client->return = 'This is not the server you are looking for';
139
140         $this->jq->min_interval = 0; // disable throttle
141         $this->jq->disable_schedulers = true;
142         $this->jq->runCycle();
143
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");
150     }
151 }
152
153 class CronHttpMock extends SugarHttpClient
154 {
155      public $call_url;
156      public $call_data;
157      public $return;
158      public function callRest($url, $data) {
159          $this->call_url = $url;
160          $this->call_data = $data;
161          return $this->return;
162      }
163 }