1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\VersionedAdmin\Tests\Controllers; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\Admin\LeftAndMainFormRequestHandler; |
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
8
|
|
|
use SilverStripe\Control\HTTPResponse; |
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
10
|
|
|
use SilverStripe\Forms\Form; |
11
|
|
|
use SilverStripe\VersionedAdmin\Controllers\HistoryViewerController; |
12
|
|
|
use SilverStripe\VersionedAdmin\Tests\Controllers\HistoryViewerControllerTest\UnviewableVersionedObject; |
13
|
|
|
use SilverStripe\VersionedAdmin\Tests\Controllers\HistoryViewerControllerTest\ViewableVersionedObject; |
14
|
|
|
|
15
|
|
|
class HistoryViewerControllerTest extends SapphireTest |
16
|
|
|
{ |
17
|
|
|
protected static $fixture_file = 'HistoryViewerControllerTest.yml'; |
18
|
|
|
|
19
|
|
|
protected static $extra_dataobjects = [ |
20
|
|
|
ViewableVersionedObject::class, |
21
|
|
|
UnviewableVersionedObject::class, |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
public function testGetClientConfig() |
25
|
|
|
{ |
26
|
|
|
$controller = new HistoryViewerController(); |
27
|
|
|
|
28
|
|
|
$clientConfig = $controller->getClientConfig(); |
29
|
|
|
|
30
|
|
|
$this->assertArrayHasKey('versionForm', $clientConfig['form']); |
31
|
|
|
$this->assertArrayHasKey('compareForm', $clientConfig['form']); |
32
|
|
|
$this->assertArrayHasKey('schemaUrl', $clientConfig['form']['versionForm']); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testSchema() |
36
|
|
|
{ |
37
|
|
|
$controllerMock = $this->getMockBuilder(HistoryViewerController::class) |
|
|
|
|
38
|
|
|
->setMethods(['getVersionForm', 'getCompareForm', 'getSchemaResponse']) |
39
|
|
|
->getMock(); |
40
|
|
|
|
41
|
|
|
$controllerMock->expects($this->once())->method('getVersionForm')->with([ |
42
|
|
|
'RecordClass' => 'Page', |
43
|
|
|
'RecordID' => 123, |
44
|
|
|
'RecordVersion' => 234, |
45
|
|
|
'RecordDate' => null |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
$controllerMock->expects($this->once())->method('getCompareForm')->with([ |
49
|
|
|
'RecordClass' => 'Page', |
50
|
|
|
'RecordID' => 123, |
51
|
|
|
'RecordVersionFrom' => 234, |
52
|
|
|
'RecordVersionTo' => 236, |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$controllerMock->expects($this->exactly(2))->method('getSchemaResponse')->willReturn(true); |
56
|
|
|
|
57
|
|
|
$request = $this->getMockBuilder(HTTPRequest::class) |
|
|
|
|
58
|
|
|
->setConstructorArgs(['GET', '/']) |
59
|
|
|
->setMethods(['param']) |
60
|
|
|
->getMock(); |
61
|
|
|
$request->expects($this->once())->method('param')->with('FormName')->willReturn('versionForm'); |
62
|
|
|
$request->offsetSet('RecordClass', 'Page'); |
63
|
|
|
$request->offsetSet('RecordID', 123); |
64
|
|
|
$request->offsetSet('RecordVersion', 234); |
65
|
|
|
|
66
|
|
|
$controllerMock->schema($request); |
67
|
|
|
/** @var HTTPResponse $result */ |
68
|
|
|
$result = $controllerMock->getResponse(); |
69
|
|
|
$this->assertSame('application/json', $result->getHeader('Content-Type')); |
70
|
|
|
|
71
|
|
|
$request = $this->getMockBuilder(HTTPRequest::class) |
|
|
|
|
72
|
|
|
->setConstructorArgs(['GET', '/']) |
73
|
|
|
->setMethods(['param']) |
74
|
|
|
->getMock(); |
75
|
|
|
$request->expects($this->once())->method('param')->with('FormName')->willReturn('compareForm'); |
76
|
|
|
$request->offsetSet('RecordClass', 'Page'); |
77
|
|
|
$request->offsetSet('RecordID', 123); |
78
|
|
|
$request->offsetSet('RecordVersion', 234); |
79
|
|
|
$request->offsetSet('RecordVersionFrom', 234); |
80
|
|
|
$request->offsetSet('RecordVersionTo', 236); |
81
|
|
|
|
82
|
|
|
$controllerMock->schema($request); |
83
|
|
|
/** @var HTTPResponse $result */ |
84
|
|
|
$result = $controllerMock->getResponse(); |
85
|
|
|
$this->assertSame('application/json', $result->getHeader('Content-Type')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGetVersionFormThrowsExceptionWhenArgsAreMissing() |
89
|
|
|
{ |
90
|
|
|
$this->expectException(InvalidArgumentException::class); |
91
|
|
|
$this->expectExceptionMessageMatches('/Missing required field/'); |
92
|
|
|
$controller = new HistoryViewerController(); |
93
|
|
|
$controller->getVersionForm([]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testGetVersionFormThrowsExceptionWhenArgsAreFalsy() |
97
|
|
|
{ |
98
|
|
|
$this->expectException(InvalidArgumentException::class); |
99
|
|
|
$this->expectExceptionMessageMatches('/Missing required field/'); |
100
|
|
|
$controller = new HistoryViewerController(); |
101
|
|
|
$controller->getVersionForm([ |
102
|
|
|
'RecordClass' => 'Page', |
103
|
|
|
'RecordID' => 0, |
104
|
|
|
'RecordVersion' => 0, |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testGetVersionFormThrowsExceptionWhenRecordVersionDoesntExist() |
109
|
|
|
{ |
110
|
|
|
$this->expectException(\SilverStripe\Control\HTTPResponse_Exception::class); |
111
|
|
|
$this->expectExceptionCode('404'); |
112
|
|
|
$controller = new HistoryViewerController(); |
113
|
|
|
$controller->getVersionForm([ |
114
|
|
|
'RecordClass' => UnviewableVersionedObject::class, |
115
|
|
|
'RecordID' => 99999, // doesn't exist |
116
|
|
|
'RecordVersion' => 1, |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testGetVersionFormThrowsExceptionWhenCanViewIsFalse() |
121
|
|
|
{ |
122
|
|
|
$this->expectException(\SilverStripe\Control\HTTPResponse_Exception::class); |
123
|
|
|
$this->expectExceptionCode('403'); |
124
|
|
|
$this->expectExceptionMessage('You don\'t have the necessary permissions to view Unviewable Versioned Object'); |
125
|
|
|
$controller = new HistoryViewerController(); |
126
|
|
|
$controller->getVersionForm([ |
127
|
|
|
'RecordClass' => UnviewableVersionedObject::class, |
128
|
|
|
'RecordID' => $this->idFromFixture(UnviewableVersionedObject::class, 'record_one'), |
129
|
|
|
'RecordVersion' => 1, |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testGetVersionFormReturnsForm() |
134
|
|
|
{ |
135
|
|
|
$controller = new HistoryViewerController(); |
136
|
|
|
$result = $controller->getVersionForm([ |
137
|
|
|
'RecordClass' => UnviewableVersionedObject::class, |
138
|
|
|
'RecordID' => $this->idFromFixture(ViewableVersionedObject::class, 'record_one'), |
139
|
|
|
'RecordVersion' => 1, |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
$this->assertInstanceOf(Form::class, $result); |
143
|
|
|
$this->assertInstanceOf(LeftAndMainFormRequestHandler::class, $result->getRequestHandler()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testVersionFormThrowsExceptionWithoutRequest() |
147
|
|
|
{ |
148
|
|
|
$this->expectException(\SilverStripe\Control\HTTPResponse_Exception::class); |
149
|
|
|
$this->expectExceptionCode('400'); |
150
|
|
|
$controller = new HistoryViewerController(); |
151
|
|
|
$controller->versionForm(null); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testCompareFormThrowsExceptionWithoutRequest() |
155
|
|
|
{ |
156
|
|
|
$this->expectException(\SilverStripe\Control\HTTPResponse_Exception::class); |
157
|
|
|
$this->expectExceptionCode('400'); |
158
|
|
|
$controller = new HistoryViewerController(); |
159
|
|
|
$controller->compareForm(null); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testVersionFormThrowsExceptionWhenArgsAreFalsy() |
163
|
|
|
{ |
164
|
|
|
$this->expectException(InvalidArgumentException::class); |
165
|
|
|
$this->expectExceptionMessageMatches('/Missing required field/'); |
166
|
|
|
$controller = new HistoryViewerController(); |
167
|
|
|
$controller->getVersionForm([ |
168
|
|
|
'RecordClass' => 'Page', |
169
|
|
|
'RecordID' => 0, |
170
|
|
|
'RecordVersion' => 0, |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testVersionFormReturnsVersionForm() |
175
|
|
|
{ |
176
|
|
|
$controllerMock = $this->getMockBuilder(HistoryViewerController::class) |
|
|
|
|
177
|
|
|
->setMethods(['getVersionForm']) |
178
|
|
|
->getMock(); |
179
|
|
|
|
180
|
|
|
$mockData = [ |
181
|
|
|
'RecordClass' => 'Page', |
182
|
|
|
'RecordID' => 123, |
183
|
|
|
'RecordVersion' => 234, |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
$controllerMock->expects($this->once())->method('getVersionForm') |
187
|
|
|
->with($mockData) |
188
|
|
|
->willReturn('mocked'); |
189
|
|
|
|
190
|
|
|
$result = $controllerMock->versionForm(new HTTPRequest('GET', '/', $mockData)); |
191
|
|
|
|
192
|
|
|
$this->assertSame('mocked', $result); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.