1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\Maintenance\Tests\Forms; |
4
|
|
|
|
5
|
|
|
use BringYourOwnIdeas\Maintenance\Forms\GridFieldRefreshButton; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Core\Injector\Injector; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
use SilverStripe\Forms\GridField\GridField; |
10
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
11
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
12
|
|
|
|
13
|
|
|
class GridFieldRefreshButtonTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
protected static $fixture_file = 'GridFieldRefreshButtonTest.yml'; |
16
|
|
|
|
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
Config::modify()->set(QueuedJobService::class, 'use_shutdown_function', false); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testHasRunningJobReturnsTrueWhenJobIsRunning() |
25
|
|
|
{ |
26
|
|
|
$button = $this->getButton(); |
27
|
|
|
$this->assertTrue($button->hasPendingJob()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testHasRunningJobReturnsTrueForPendingJobsOnImmediateQueue() |
31
|
|
|
{ |
32
|
|
|
$runningJob = $this->objFromFixture(QueuedJobDescriptor::class, 'runningjob'); |
33
|
|
|
$runningJob->JobStatus = 'Complete'; |
34
|
|
|
$runningJob->write(); |
35
|
|
|
$this->assertTrue($this->getButton()->hasPendingJob()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testDoesNotHaveCancelledCompletedOrBrokenJob() |
39
|
|
|
{ |
40
|
|
|
$this->completeRunningJob(); |
41
|
|
|
|
42
|
|
|
$button = $this->getButton(); |
43
|
|
|
$this->assertFalse($button->hasPendingJob()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testHandleRefreshDoesNotCreateJobWhenJobIsRunning() |
47
|
|
|
{ |
48
|
|
|
$count = QueuedJobDescriptor::get()->count(); |
49
|
|
|
|
50
|
|
|
$button = $this->getButton(); |
51
|
|
|
$button->handleRefresh(); |
52
|
|
|
|
53
|
|
|
$this->assertSame($count, QueuedJobDescriptor::get()->count()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testHandleRefreshCreatesJobWhenNoJobIsRunning() |
57
|
|
|
{ |
58
|
|
|
$this->completeRunningJob(); |
59
|
|
|
|
60
|
|
|
$count = QueuedJobDescriptor::get()->count(); |
61
|
|
|
|
62
|
|
|
$button = $this->getButton(); |
63
|
|
|
$button->handleRefresh(); |
64
|
|
|
|
65
|
|
|
$this->assertSame($count + 1, QueuedJobDescriptor::get()->count()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testHandleCheckReturnsValidJson() |
69
|
|
|
{ |
70
|
|
|
$button = $this->getButton(); |
71
|
|
|
$this->assertSame('true', $button->handleCheck()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testButtonIsDisabledWhenJobIsRunning() |
75
|
|
|
{ |
76
|
|
|
$button = $this->getButton(); |
77
|
|
|
|
78
|
|
|
$gridFieldMock = $this->getGridFieldMock(); |
79
|
|
|
|
80
|
|
|
$output = $button->getHTMLFragments($gridFieldMock); |
81
|
|
|
|
82
|
|
|
$this->assertContains('disabled', $output['test']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testButtonIsEnabledWhenNoJobIsRunning() |
86
|
|
|
{ |
87
|
|
|
$this->completeRunningJob(); |
88
|
|
|
|
89
|
|
|
$button = $this->getButton(); |
90
|
|
|
|
91
|
|
|
$gridFieldMock = $this->getGridFieldMock(); |
92
|
|
|
|
93
|
|
|
$output = $button->getHTMLFragments($gridFieldMock); |
94
|
|
|
|
95
|
|
|
$this->assertNotContains('disabled', $output['test']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Turns the running job in the fixture file into a completed job |
100
|
|
|
*/ |
101
|
|
|
protected function completeRunningJob() |
102
|
|
|
{ |
103
|
|
|
$runningJob = $this->objFromFixture(QueuedJobDescriptor::class, 'runningjob'); |
104
|
|
|
$runningJob->JobStatus = 'Complete'; |
105
|
|
|
$runningJob->write(); |
106
|
|
|
|
107
|
|
|
$runningJob = $this->objFromFixture(QueuedJobDescriptor::class, 'immediatependingjob'); |
108
|
|
|
$runningJob->JobStatus = 'Complete'; |
109
|
|
|
$runningJob->write(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Mocks and returns a gridfield with name 'TestGridField' and 'Link' method, which returns a url |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
protected function getGridFieldMock() |
117
|
|
|
{ |
118
|
|
|
$gridFieldMock = $this |
119
|
|
|
->getMockBuilder(GridField::class) |
120
|
|
|
->setConstructorArgs(['TestGridField']) |
121
|
|
|
->setMethods(['Link']) |
122
|
|
|
->getMock(); |
123
|
|
|
|
124
|
|
|
$gridFieldMock |
125
|
|
|
->expects($this->any()) |
126
|
|
|
->method('Link') |
127
|
|
|
->will($this->returnValue('http://example.com')); |
128
|
|
|
|
129
|
|
|
return $gridFieldMock; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return GridFieldRefreshButton |
134
|
|
|
*/ |
135
|
|
|
protected function getButton() |
136
|
|
|
{ |
137
|
|
|
return Injector::inst()->create(GridFieldRefreshButton::class, 'test'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|