]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/SchedulersJobs/Bug56573Test.php
Release 6.5.15
[Github/sugarcrm.git] / tests / modules / SchedulersJobs / Bug56573Test.php
1 <?php
2 /*********************************************************************************
3  * By installing or using this file, you are confirming on behalf of the entity
4  * subscribed to the SugarCRM Inc. product ("Company") that Company is bound by
5  * the SugarCRM Inc. Master Subscription Agreement (“MSA”), which is viewable at:
6  * http://www.sugarcrm.com/master-subscription-agreement
7  *
8  * If Company is not bound by the MSA, then by installing or using this file
9  * you are agreeing unconditionally that Company will be bound by the MSA and
10  * certifying that you have authority to bind Company accordingly.
11  *
12  * Copyright (C) 2004-2013 SugarCRM Inc.  All rights reserved.
13  ********************************************************************************/
14
15 require_once 'modules/SchedulersJobs/SchedulersJob.php';
16
17 /**
18  * Bug #56537 : Schedule Jobs don't work with classes
19  *
20  * @ticket 56537
21  */
22 class Bug56573Test extends Sugar_PHPUnit_Framework_TestCase
23 {
24     /**
25      * @var string job id
26      */
27     protected $id;
28
29     public function setUp()
30     {
31         $this->id = null;
32         SugarTestHelper::setUp('current_user');
33     }
34
35     public function tearDown()
36     {
37         if (!empty($this->id)) {
38             $job = new SchedulersJob();
39             $job->mark_deleted($this->id);
40         }
41         SugarTestHelper::tearDown();
42     }
43
44     protected function execJob($name, $data)
45     {
46         require_once('include/SugarQueue/SugarJobQueue.php');
47
48         $job = new SchedulersJob();
49         $job->name = "Bug56573Test Alert Job - '{$name}'";
50         $job->target = "class::Bug56573TestJob";
51         $job->data = $data;
52         $job->assigned_user_id = $GLOBALS['current_user']->id;
53
54         // Add the Job the the job Queue
55         $jq = new SugarJobQueue();
56         $jq->submitJob($job);
57         // Run the job
58         $job->runJob();
59
60         // Save id for cleaning
61         $this->id = $job->id;
62
63         return $job;
64     }
65
66     public static function provider()
67     {
68         return array(
69             array(
70                 'Success',
71                 implode(
72                     ',',
73                     array(
74                         'status' =>  SchedulersJob::JOB_STATUS_RUNNING,
75                         'resolution' => SchedulersJob::JOB_SUCCESS,
76                         'return' => true
77                     )
78                 ),
79                 SchedulersJob::JOB_STATUS_DONE,
80                 SchedulersJob::JOB_SUCCESS
81             ),
82             array(
83                 'Failure',
84                 implode(
85                     ',',
86                     array(
87                         'status' => SchedulersJob::JOB_STATUS_RUNNING,
88                         'resolution' => SchedulersJob::JOB_FAILURE,
89                         'return' => false
90                     )
91                 ),
92                 SchedulersJob::JOB_STATUS_DONE,
93                 SchedulersJob::JOB_FAILURE
94
95             ),
96             array(
97                 'Queue',
98                 implode(
99                     ',',
100                     array(
101                         'status' => SchedulersJob::JOB_STATUS_QUEUED,
102                         'resolution' => SchedulersJob::JOB_PARTIAL,
103                         'return' => false
104                     )
105                 ),
106                 SchedulersJob::JOB_STATUS_QUEUED,
107                 SchedulersJob::JOB_PARTIAL
108             ),
109         );
110     }
111
112     /**
113      * Test if runJob() sets proper values of status/resolution
114      *
115      * @dataProvider provider
116      * @group 56537
117      */
118     public function testJob($name, $data, $status, $resolution)
119     {
120         $job = $this->execJob($name, $data);
121         $this->assertEquals($resolution, $job->resolution, "Wrong resolution");
122         $this->assertEquals($status, $job->status, "Wrong status");
123     }
124 }
125
126 /**
127  * Job Class for testing SchedulersJob
128  */
129 class Bug56573TestJob implements RunnableSchedulerJob
130 {
131     public function run($data)
132     {
133         // Pull all the test data
134         $data = explode(',', $data);
135
136         // Set status and resolution
137         $this->job->status = $data[0];
138         $this->job->resolution = $data[1];
139
140         return $data[2];
141     }
142
143     public function setJob(SchedulersJob $job)
144     {
145         $this->job = $job;
146     }
147 }