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