All Data Structures Functions Variables Pages
MonitorJobTest.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 testRunAndProcessStuckJobs()
47  {
48  Yii::app()->user->userModel = User::getByUsername('super');
49  $emailAddress = new Email();
50  $emailAddress->emailAddress = 'sometest@zurmoalerts.com';
51  Yii::app()->user->userModel->primaryEmail = $emailAddress;
52  $saved = Yii::app()->user->userModel->save();
53  $this->assertTrue($saved);
54 
55  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
56  $this->assertEquals(0, Yii::app()->emailHelper->getSentCount());
57 
58  $monitorJob = new MonitorJob();
59  $this->assertEquals(0, JobInProcess::getCount());
60  $this->assertEquals(0, StuckJob::getCount());
61  $this->assertEquals(0, Notification::getCount());
62  $this->assertEquals(0, EmailMessage::getCount());
63  $jobInProcess = new JobInProcess();
64  $jobInProcess->type = 'Test';
65  $this->assertTrue($jobInProcess->save());
66  //Should make createdDateTime long enough in past to trigger as stuck.
67  $createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 10000);
68  $sql = "Update item set createddatetime = '" . $createdDateTime . "' where id = " .
69  $jobInProcess->getClassId('Item');
70  ZurmoRedBean::exec($sql);
71  $jobInProcess->forget();
72  $monitorJob->run();
73  $this->assertEquals(0, JobInProcess::getCount());
74  //should still be 0 but the quantity should increase
75  $this->assertEquals(0, Notification::getCount());
76  //There should now be one stuck job with quantity 1
77  $stuckJobs = StuckJob::getAll();
78  $this->assertEquals(1, count($stuckJobs));
79  $this->assertEquals('Test', $stuckJobs[0]->type);
80  $this->assertEquals(1, $stuckJobs[0]->quantity);
81 
82  //Now it should increase to 2
83  $jobInProcess = new JobInProcess();
84  $jobInProcess->type = 'Test';
85  $this->assertTrue($jobInProcess->save());
86  //Should make createdDateTime long enough in past to trigger as stuck.
87  $createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 10000);
88  $sql = "Update item set createddatetime = '" . $createdDateTime . "' where id = " .
89  $jobInProcess->getClassId('Item');
90  ZurmoRedBean::exec($sql);
91  $jobInProcess->forget();
92  $monitorJob->run();
93  $this->assertEquals(0, JobInProcess::getCount());
94  //should still be 0 but the quantity should increase
95  $this->assertEquals(0, Notification::getCount());
96  //There should now be one stuck job with quantity 1
97  $stuckJobs = StuckJob::getAll();
98  $this->assertEquals(1, count($stuckJobs));
99  $this->assertEquals('Test', $stuckJobs[0]->type);
100  $this->assertEquals(2, $stuckJobs[0]->quantity);
101 
102  //Set quantity to 3, then run monitor again and notification should go out.
103  $stuckJobs[0]->quantity = 3;
104  $this->assertTrue($stuckJobs[0]->save());
105 
106  $jobInProcess = new JobInProcess();
107  $jobInProcess->type = 'Test';
108  $this->assertTrue($jobInProcess->save());
109  //Should make createdDateTime long enough in past to trigger as stuck.
110  $createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 10000);
111  $sql = "Update item set createddatetime = '" . $createdDateTime . "' where id = " .
112  $jobInProcess->getClassId('Item');
113  ZurmoRedBean::exec($sql);
114  $jobInProcess->forget();
115 
116  //Now the threshold of 4 should be reached and we should send a notification
117  $monitorJob->run();
118  ForgetAllCacheUtil::forgetAllCaches();
119  $stuckJobs = StuckJob::getAll();
120  $this->assertEquals(1, count($stuckJobs));
121  $this->assertEquals('Test', $stuckJobs[0]->type);
122  $this->assertEquals(4, $stuckJobs[0]->quantity);
123  $this->assertEquals(1, Notification::getCount());
124  //Confirm an email was sent
125  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
126  $this->assertEquals(1, EmailMessage::getCount());
127  $this->assertEquals(1, Yii::app()->emailHelper->getSentCount());
128  }
129  }
130 ?>
Definition: Email.php:37
static getByUsername($username)
Definition: User.php:49
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
Generated on Sat Feb 22 2020 07:10:31