Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
JobsManagerUtilTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo 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 ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo 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 Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified 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 Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
38  {
39  public static function setUpBeforeClass()
40  {
41  parent::setUpBeforeClass();
42  SecurityTestHelper::createSuperAdmin();
43  Yii::import('application.modules.jobsManager.tests.unit.jobs.*');
44  }
45 
46  public function testRunNonMonitorJob()
47  {
48  Yii::app()->user->userModel = User::getByUsername('super');
49  //Test running a TestJob that it creates a JobLog and does not leave a JobInProcess
50  $this->assertEquals(0, JobInProcess::getCount());
51  $this->assertEquals(0, JobLog::getCount());
52 
53  $isJobInProgress = false;
54  JobsManagerUtil::runNonMonitorJob('Test', new MessageLogger(), $isJobInProgress);
55  $this->assertFalse($isJobInProgress);
56  $this->assertEquals(0, JobInProcess::getCount());
57  $jobLogs = JobLog::getAll();
58  $this->assertEquals(1, count($jobLogs));
59  $this->assertEquals('Test', $jobLogs[0]->type);
60  $this->assertEquals(JobLog::STATUS_COMPLETE_WITHOUT_ERROR, $jobLogs[0]->status);
61  $this->assertEquals(0, $jobLogs[0]->isProcessed);
62 
63  //Now test a job that always fails
64  $isJobInProgress = false;
65  JobsManagerUtil::runNonMonitorJob('TestAlwaysFails', new MessageLogger(), $isJobInProgress);
66  $this->assertFalse($isJobInProgress);
67  $this->assertEquals(0, JobInProcess::getCount());
68  $jobLogs = JobLog::getAll();
69  $this->assertEquals(2, count($jobLogs));
70  $this->assertEquals('TestAlwaysFails', $jobLogs[1]->type);
71  $this->assertEquals(JobLog::STATUS_COMPLETE_WITH_ERROR, $jobLogs[1]->status);
72  $this->assertEquals('The test job failed', $jobLogs[1]->message);
73  $this->assertEquals(0, $jobLogs[1]->isProcessed);
74  }
75 
76  public function testIsJobInProcessOverThreashold()
77  {
78  Yii::app()->user->userModel = User::getByUsername('super');
79  $jobInProcess = new JobInProcess();
80  $jobInProcess->type = 'Test';
81  $this->assertTrue($jobInProcess->save());
82  //Set the createdDateTime as way in the past, so that it is over the threshold
83  $sql = "update " . Item::getTableName(). " set createddatetime = '1980-06-03 18:33:03' where id = " .
84  $jobInProcess->getClassId('Item');
85  ZurmoRedBean::exec($sql);
86  $jobInProcessId = $jobInProcess->id;
87  $jobInProcess->forget();
88  $jobInProcess = JobInProcess::getById($jobInProcessId);
89  $this->assertTrue(JobsManagerUtil::isJobInProcessOverThreshold($jobInProcess, $jobInProcess->type));
90  $jobInProcess->delete();
91 
92  //Test when a job is not over the threshold.
93  $jobInProcess = new JobInProcess();
94  $jobInProcess->type = 'Test';
95  $this->assertTrue($jobInProcess->save());
96  $this->assertFalse(JobsManagerUtil::isJobInProcessOverThreshold($jobInProcess, $jobInProcess->type));
97  $jobInProcess->delete();
98  }
99 
100  public function testRunMonitorJob()
101  {
102  Yii::app()->user->userModel = User::getByUsername('super');
103  foreach (JobLog::getAll() as $jobLog)
104  {
105  $jobLog->delete();
106  }
107  $isJobInProgress = false;
108  JobsManagerUtil::runNonMonitorJob('Test', new MessageLogger(), $isJobInProgress);
109  $this->assertFalse($isJobInProgress);
110  $jobLogs = JobLog::getAll();
111  $this->assertEquals(1, count($jobLogs));
112  $this->assertEquals(0, $jobLogs[0]->isProcessed);
113  $jobLogId = $jobLogs[0]->id;
114  $jobLogs[0]->forget(); //to ensure cache is cleared before running monitor job
115  $isJobInProgress = false;
116  JobsManagerUtil::runMonitorJob(new MessageLogger(), $isJobInProgress);
117  $this->assertFalse($isJobInProgress);
118  $jobLogs = JobLog::getAll();
119  $this->assertEquals(2, count($jobLogs));
120  $this->assertEquals($jobLogId, $jobLogs[0]->id);
121  $this->assertEquals(1, $jobLogs[0]->isProcessed);
122  $this->assertEquals(0, $jobLogs[1]->isProcessed);
123  }
124 
125  public function testMakeMonitorStuckJobNotification()
126  {
127  $super = User::getByUsername('super');
128  Yii::app()->user->userModel = $super;
129  $super->primaryEmail->emailAddress = 'super@zurmo.com';
130  $this->assertTrue($super->save());
131 
132  $notificationInitalCount = Notification::getCount();
133  $emailMessageInitialCount = EmailMessage::getCount();
134 
135  JobsManagerUtil::makeMonitorStuckJobNotification();
136 
137  $notifications = Notification::getAll();
138  $emailMessages = EmailMessage::getAll();
139  $this->assertCount($emailMessageInitialCount + 1, $notifications);
140  $this->assertCount($notificationInitalCount + 1, $emailMessages);
141  $this->assertEquals('The monitor job is stuck.', $notifications[0]->notificationMessage->htmlContent);
142  $this->assertEquals('The monitor job is stuck.', $notifications[0]->notificationMessage->textContent);
143  $this->assertContains('The monitor job is stuck.', $emailMessages[0]->content->htmlContent);
144  $this->assertContains('The monitor job is stuck.', $emailMessages[0]->content->textContent);
145  }
146  }
147 ?>
static getByUsername($username)
Definition: User.php:49
static runNonMonitorJob($type, MessageLogger $messageLogger, &$isJobInProgress)
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getById($id, $modelClassName=null)
static isJobInProcessOverThreshold(JobInProcess $jobInProcess, $type)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static getTableName()
static runMonitorJob(MessageLogger $messageLogger, &$isJobInProgress)
Generated on Sat Jul 4 2020 07:10:33
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.