|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class GridFieldRefreshButtonTest extends SapphireTest |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
protected static $fixture_file = 'GridFieldRefreshButtonTest.yml'; |
|
6
|
|
|
|
|
7
|
|
|
public function testHasRunningJob() |
|
8
|
|
|
{ |
|
9
|
|
|
$button = new GridFieldRefreshButton('test'); |
|
10
|
|
|
$this->assertTrue($button->hasActiveJob()); |
|
|
|
|
|
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
public function testDoesNotHaveCancelledCompletedOrBrokenJob() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->completeRunningJob(); |
|
16
|
|
|
|
|
17
|
|
|
$button = new GridFieldRefreshButton('test'); |
|
18
|
|
|
$this->assertFalse($button->hasActiveJob()); |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
public function testHandleRefreshDoesNotCreateJobWhenJobIsRunning() |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
$count = QueuedJobDescriptor::get()->count(); |
|
24
|
|
|
|
|
25
|
|
|
$button = new GridFieldRefreshButton('test'); |
|
26
|
|
|
$button->handleRefresh(); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertSame($count, QueuedJobDescriptor::get()->count()); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
View Code Duplication |
public function testHandleRefreshCreatesJobWhenNoJobIsRunning() |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$this->completeRunningJob(); |
|
34
|
|
|
|
|
35
|
|
|
$count = QueuedJobDescriptor::get()->count(); |
|
36
|
|
|
|
|
37
|
|
|
$button = new GridFieldRefreshButton('test'); |
|
38
|
|
|
$button->handleRefresh(); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertSame($count + 1, QueuedJobDescriptor::get()->count()); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testHandleCheckReturnsValidJson() |
|
44
|
|
|
{ |
|
45
|
|
|
$button = new GridFieldRefreshButton('test'); |
|
46
|
|
|
$this->assertSame('true', $button->handleCheck()); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testButtonIsDisabledWhenJobIsRunning() |
|
50
|
|
|
{ |
|
51
|
|
|
$button = new GridFieldRefreshButton('test'); |
|
52
|
|
|
|
|
53
|
|
|
$gridFieldMock = $this->getGridFieldMock(); |
|
54
|
|
|
|
|
55
|
|
|
$output = $button->getHTMLFragments($gridFieldMock); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertContains('disabled', $output['test']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testButtonIsEnabledWhenNoJobIsRunning() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->completeRunningJob(); |
|
63
|
|
|
|
|
64
|
|
|
$button = new GridFieldRefreshButton('test'); |
|
65
|
|
|
|
|
66
|
|
|
$gridFieldMock = $this->getGridFieldMock(); |
|
67
|
|
|
|
|
68
|
|
|
$output = $button->getHTMLFragments($gridFieldMock); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertNotContains('disabled', $output['test']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Turns the running job in the fixture file into a completed job |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function completeRunningJob() |
|
77
|
|
|
{ |
|
78
|
|
|
$runningJob = $this->objFromFixture('QueuedJobDescriptor', 'runningjob'); |
|
79
|
|
|
$runningJob->JobStatus = 'Complete'; |
|
80
|
|
|
$runningJob->write(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Mocks and returns a gridfield with name 'TestGridField' and 'Link' method, which returns a url |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getGridFieldMock() |
|
88
|
|
|
{ |
|
89
|
|
|
$gridFieldMock = $this |
|
|
|
|
|
|
90
|
|
|
->getMockBuilder(GridField::class) |
|
91
|
|
|
->setConstructorArgs(['TestGridField']) |
|
92
|
|
|
->setMethods(['Link']) |
|
93
|
|
|
->getMock(); |
|
94
|
|
|
|
|
95
|
|
|
$gridFieldMock |
|
96
|
|
|
->expects($this->any()) |
|
|
|
|
|
|
97
|
|
|
->method('Link') |
|
98
|
|
|
->will($this->returnValue('http://example.com')); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
return $gridFieldMock; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.