1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\StaticPublishQueue\Test; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\ORM\ArrayList; |
9
|
|
|
use SilverStripe\StaticPublishQueue\Extension\Engine\SiteTreePublishingEngine; |
10
|
|
|
use SilverStripe\StaticPublishQueue\Extension\Publisher\FilesystemPublisher; |
11
|
|
|
use SilverStripe\StaticPublishQueue\Model\StaticPagesQueue; |
12
|
|
|
use SilverStripe\StaticPublishQueue\Model\URLArrayObject; |
13
|
|
|
use SilverStripe\StaticPublishQueue\Test\SiteTreePublishingEngineTest\Model\StaticallyPublishablePage; |
14
|
|
|
use SilverStripe\StaticPublishQueue\Test\SiteTreePublishingEngineTest\Model\StaticPublishingTriggerPage; |
15
|
|
|
|
16
|
|
|
class SiteTreePublishingEngineTest extends SapphireTest |
17
|
|
|
{ |
18
|
|
|
protected static $required_extensions = array( |
19
|
|
|
StaticallyPublishablePage::class => array( |
20
|
|
|
SiteTreePublishingEngine::class, |
21
|
|
|
FilesystemPublisher::class, |
22
|
|
|
), |
23
|
|
|
StaticPublishingTriggerPage::class => array( |
24
|
|
|
SiteTreePublishingEngine::class, |
25
|
|
|
FilesystemPublisher::class, |
26
|
|
|
) |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
protected function setUp() |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
Config::modify()->set(StaticPagesQueue::class, 'realtime', true); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function testCollectChangesForPublishing() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$obj = StaticPublishingTriggerPage::create(); |
38
|
|
|
$obj->collectChanges(array('action' => 'publish')); |
39
|
|
|
|
40
|
|
|
$this->assertEquals( |
41
|
|
|
array('/updateOnPublish?_ID=1&_ClassName=StaticallyPublishableTest' => 10), |
42
|
|
|
$obj->getToUpdate() |
43
|
|
|
); |
44
|
|
|
$this->assertEquals( |
45
|
|
|
array('/deleteOnPublish?_ID=1&_ClassName=StaticallyPublishableTest' => 10), |
46
|
|
|
$obj->getToDelete() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function testCollectChangesForUnpublishing() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$obj = StaticPublishingTriggerPage::create(); |
53
|
|
|
$obj->collectChanges(array('action' => 'unpublish')); |
54
|
|
|
|
55
|
|
|
$this->assertEquals( |
56
|
|
|
array('/updateOnUnpublish?_ID=1&_ClassName=StaticallyPublishableTest' => 10), |
57
|
|
|
$obj->getToUpdate() |
58
|
|
|
); |
59
|
|
|
$this->assertEquals( |
60
|
|
|
array('/deleteOnUnpublish?_ID=1&_ClassName=StaticallyPublishableTest' => 10), |
61
|
|
|
$obj->getToDelete() |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testFlushChangesToUpdateEnqueuesAndDeletesRegular() |
66
|
|
|
{ |
67
|
|
|
|
68
|
|
|
$toUpdate = array('/toUpdate?_ID=1&_ClassName=StaticallyPublishableTest' => 10); |
69
|
|
|
|
70
|
|
|
$stub = $this->getMockBuilder(StaticPublishingTriggerPage::class) |
71
|
|
|
->setMethods(array('convertUrlsToPathMap', 'deleteRegularFiles', 'deleteStaleFiles')) |
72
|
|
|
->getMock(); |
73
|
|
|
|
74
|
|
|
$stub->expects($this->once()) |
75
|
|
|
->method('convertUrlsToPathMap') |
76
|
|
|
->will($this->returnValue(array('url' => 'file'))); |
77
|
|
|
|
78
|
|
|
// Test: enqueues the updated URLs |
79
|
|
|
$stub->setUrlArrayObject($this->getMockBuilder(URLArrayObject::class)->setMethods(array('addUrls'))->getMock()); |
80
|
|
|
$stub->getUrlArrayObject()->expects($this->once()) |
81
|
|
|
->method('addUrls') |
82
|
|
|
->with($this->equalTo($toUpdate)); |
83
|
|
|
|
84
|
|
|
// Test: deletes just the regular files |
85
|
|
|
$stub->expects($this->never()) |
86
|
|
|
->method('deleteStaleFiles'); |
87
|
|
|
$stub->expects($this->once()) |
88
|
|
|
->method('deleteRegularFiles') |
89
|
|
|
->with($this->equalTo(array('file'))); |
90
|
|
|
|
91
|
|
|
// Test: clears the update queue |
92
|
|
|
$stub->setToUpdate($toUpdate); |
93
|
|
|
$stub->flushChanges(); |
94
|
|
|
$this->assertEmpty($stub->getToUpdate()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testFlushChangesToDeleteDeletesRegularAndStale() |
98
|
|
|
{ |
99
|
|
|
$toDelete = array('/toDelete?_ID=1&_ClassName=StaticallyPublishableTest' => 10); |
100
|
|
|
|
101
|
|
|
$stub = $this->getMockBuilder(StaticPublishingTriggerPage::class) |
102
|
|
|
->setMethods(array('convertUrlsToPathMap', 'deleteRegularFiles', 'deleteStaleFiles')) |
103
|
|
|
->getMock(); |
104
|
|
|
|
105
|
|
|
$stub->expects($this->once()) |
106
|
|
|
->method('convertUrlsToPathMap') |
107
|
|
|
->will($this->returnValue(array('url' => 'file'))); |
108
|
|
|
|
109
|
|
|
// Test: deletes both regular and stale files |
110
|
|
|
$stub->expects($this->once()) |
111
|
|
|
->method('deleteRegularFiles') |
112
|
|
|
->with($this->equalTo(array('file'))); |
113
|
|
|
$stub->expects($this->once()) |
114
|
|
|
->method('deleteStaleFiles') |
115
|
|
|
->with($this->equalTo(array('file'))); |
116
|
|
|
|
117
|
|
|
// Test: clears the queue |
118
|
|
|
$stub->setToDelete($toDelete); |
119
|
|
|
$stub->flushChanges(); |
120
|
|
|
$this->assertEmpty($stub->getToDelete()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testConvertUrlsToPathMapNoObject() |
124
|
|
|
{ |
125
|
|
|
Config::modify()->set('FilesystemPublisher', 'static_base_url', 'http://foo'); |
126
|
|
|
Config::modify()->set('Director', 'alternate_base_url', '/'); |
127
|
|
|
|
128
|
|
|
$urls = array('/xyzzy'); |
129
|
|
|
|
130
|
|
|
$stub = StaticPublishingTriggerPage::create(); |
131
|
|
|
|
132
|
|
|
// Test (inclusively with urlsToPaths, these interfaces should be refactored together) |
133
|
|
|
$result = $stub->convertUrlsToPathMap($urls); |
134
|
|
|
$this->assertEquals( |
135
|
|
|
$result, |
136
|
|
|
array( |
137
|
|
|
'/xyzzy' => './xyzzy.html' |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testConvertUrlsToPathMapMainSite() |
143
|
|
|
{ |
144
|
|
|
Config::modify()->set(FilesystemPublisher::class, 'static_base_url', 'http://foo'); |
145
|
|
|
Config::modify()->set(Director::class, 'alternate_base_url', '/'); |
146
|
|
|
$urls = array('/xyzzy?_ID=1&_ClassName=SiteTreePublishingEngineTest_StaticallyPublishable'); |
147
|
|
|
|
148
|
|
|
// Pretend this object supports subsites, and is from the main site. |
149
|
|
|
$page = $this->getMockBuilder(StaticallyPublishablePage::class) |
150
|
|
|
->setMethods(array('Subsite', 'hasExtension')) |
151
|
|
|
->getMock(); |
152
|
|
|
|
153
|
|
|
$page->expects($this->any()) |
154
|
|
|
->method('Subsite') |
155
|
|
|
->will($this->returnValue(null)); |
156
|
|
|
$page->expects($this->any()) |
157
|
|
|
->method('hasExtension') |
158
|
|
|
->will($this->returnValue(true)); |
159
|
|
|
|
160
|
|
|
$stub = StaticPublishingTriggerPage::create(); |
161
|
|
|
$stub->setUrlArrayObject($this->getMockBuilder(URLArrayObject::class)->setMethods(array('getObject'))->getMock()); |
162
|
|
|
$stub->getUrlArrayObject()->expects($this->any()) |
163
|
|
|
->method('getObject') |
164
|
|
|
->will($this->returnValue($page)); |
165
|
|
|
|
166
|
|
|
// Test (inclusively with urlsToPaths, these interfaces should be refactored together) |
167
|
|
|
$result = $stub->convertUrlsToPathMap($urls); |
168
|
|
|
$this->assertEquals( |
169
|
|
|
array('http://foo/xyzzy?_ID=1&_ClassName=SiteTreePublishingEngineTest_StaticallyPublishable' => |
170
|
|
|
'foo/xyzzy.html'), |
171
|
|
|
$result |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testConvertUrlsToPathMapSubsite() |
176
|
|
|
{ |
177
|
|
|
Config::modify()->set(FilesystemPublisher::class, 'static_base_url', 'http://foo'); |
178
|
|
|
Config::modify()->set(Director::class, 'alternate_base_url', '/'); |
179
|
|
|
$urls = array('/xyzzy?_ID=1&_ClassName=SiteTreePublishingEngineTest_StaticallyPublishable'); |
180
|
|
|
|
181
|
|
|
// Mock a set of objects pretending to support Subsites. Subsites might not be installed. |
182
|
|
|
$domain1 = $this->createMock('SubsiteDomain_mock'); |
183
|
|
|
$domain1->Domain = 'subiste1.domain.org'; |
|
|
|
|
184
|
|
|
$domain2 = $this->createMock('SubsiteDomain_mock'); |
185
|
|
|
$domain2->Domain = 'subiste2.domain.org'; |
|
|
|
|
186
|
|
|
|
187
|
|
|
$domains = new ArrayList(array($domain1, $domain2)); |
188
|
|
|
|
189
|
|
|
$subsite = $this->createMock('Subsite_mock'); |
190
|
|
|
$subsite->expects($this->any()) |
191
|
|
|
->method('Domains') |
192
|
|
|
->will($this->returnValue($domains)); |
193
|
|
|
$subsite->ID = 1; |
|
|
|
|
194
|
|
|
|
195
|
|
|
$stub = $this->getMockBuilder(StaticallyPublishablePage::class) |
196
|
|
|
->setMethods(array('Subsite', 'hasExtension')) |
197
|
|
|
->getMock(); |
198
|
|
|
|
199
|
|
|
$stub->expects($this->any()) |
200
|
|
|
->method('Subsite') |
201
|
|
|
->will($this->returnValue($subsite)); |
202
|
|
|
$stub->expects($this->any()) |
203
|
|
|
->method('hasExtension') |
204
|
|
|
->will($this->returnValue(true)); |
205
|
|
|
|
206
|
|
|
// Prepare static mocks. |
207
|
|
|
$stub->setUrlArrayObject($this->getMockBuilder(URLArrayObject::class)->setMethods(array('getObject'))->getMock()); |
208
|
|
|
|
209
|
|
|
$stub->getUrlArrayObject()->expects($this->any()) |
210
|
|
|
->method('getObject') |
211
|
|
|
->will($this->returnValue($stub)); |
212
|
|
|
|
213
|
|
|
// Test (inclusively with urlsToPaths, these interfaces should be refactored together) |
214
|
|
|
$result = $stub->convertUrlsToPathMap($urls); |
215
|
|
|
$this->assertEquals( |
216
|
|
|
array( |
217
|
|
|
'http://subiste1.domain.org/xyzzy?_ID=1&_ClassName=SiteTreePublishingEngineTest_StaticallyPublishable' => |
218
|
|
|
"subiste1.domain.org/xyzzy.html", |
219
|
|
|
'http://subiste2.domain.org/xyzzy?_ID=1&_ClassName=SiteTreePublishingEngineTest_StaticallyPublishable' => |
220
|
|
|
"subiste2.domain.org/xyzzy.html" |
221
|
|
|
), |
222
|
|
|
$result |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function testDeleteStaleFiles() |
227
|
|
|
{ |
228
|
|
|
$stub = $this->getMockBuilder(StaticPublishingTriggerPage::class) |
229
|
|
|
->setMethods(array('deleteFromCacheDir')) |
230
|
|
|
->getMock(); |
231
|
|
|
|
232
|
|
|
$stub->expects($this->at(0)) |
233
|
|
|
->method('deleteFromCacheDir') |
234
|
|
|
->with($this->equalTo('xyzzy.stale.html')); |
235
|
|
|
|
236
|
|
|
$stub->expects($this->at(1)) |
237
|
|
|
->method('deleteFromCacheDir') |
238
|
|
|
->with($this->equalTo('foo/bar/baz.stale.html')); |
239
|
|
|
|
240
|
|
|
$stub->deleteStaleFiles(array('xyzzy.html', 'foo/bar/baz.html')); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.