1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\StaticPublishQueue\Test; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\ORM\ArrayList; |
9
|
|
|
use SilverStripe\StaticPublishQueue\Extension\Engine\SiteTreePublishingEngine; |
10
|
|
|
use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree; |
11
|
|
|
use SilverStripe\StaticPublishQueue\Test\PublishableSiteTreeTest\Model\PublishablePage; |
12
|
|
|
|
13
|
|
|
class PublishableSiteTreeTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
protected $usesDatabase = true; |
16
|
|
|
|
17
|
|
|
protected static $required_extensions = [ |
18
|
|
|
PublishablePage::class => [PublishableSiteTree::class] |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
public function testObjectToUpdateOnSiteTreeReorganise() |
22
|
|
|
{ |
23
|
|
|
// build a mock of the extension overriding flushChanges to prevent writing to the queue |
24
|
|
|
$mockExtension = $this->getMockBuilder(SiteTreePublishingEngine::class) |
25
|
|
|
->setMethods([ |
26
|
|
|
'flushChanges', |
27
|
|
|
]) |
28
|
|
|
->getMock(); |
29
|
|
|
|
30
|
|
|
$getURL = function ($value) { |
31
|
|
|
return $value->URLSegment; |
32
|
|
|
}; |
33
|
|
|
|
34
|
|
|
// IF YOU'RE OF A NERVOUS DISPOSITION, LOOK AWAY NOT |
35
|
|
|
// stub the flushChanges method and make sure that each call is able to assert the correct items are in the |
36
|
|
|
$mockExtension->expects($this->exactly(3))->method('flushChanges')->willReturnOnConsecutiveCalls( |
37
|
|
View Code Duplication |
new \PHPUnit_Framework_MockObject_Stub_ReturnCallback(function () use ($mockExtension, $getURL) { |
|
|
|
|
38
|
|
|
$this->assertEmpty($mockExtension->getToDelete()); |
39
|
|
|
$mockExtension->setToDelete([]); |
40
|
|
|
$this->assertEquals(['stub'], array_map($getURL, $mockExtension->getToUpdate())); |
41
|
|
|
$mockExtension->setToUpdate([]); |
42
|
|
|
}), |
43
|
|
View Code Duplication |
new \PHPUnit_Framework_MockObject_Stub_ReturnCallback(function () use ($mockExtension, $getURL) { |
|
|
|
|
44
|
|
|
$this->assertEquals(['stub'], array_map($getURL, $mockExtension->getToDelete())); |
45
|
|
|
$mockExtension->setToDelete([]); |
46
|
|
|
$this->assertEmpty($mockExtension->getToUpdate()); |
47
|
|
|
$mockExtension->setToUpdate([]); |
48
|
|
|
}), |
49
|
|
View Code Duplication |
new \PHPUnit_Framework_MockObject_Stub_ReturnCallback(function () use ($mockExtension, $getURL) { |
|
|
|
|
50
|
|
|
$this->assertEmpty($mockExtension->getToDelete()); |
51
|
|
|
$mockExtension->setToDelete([]); |
52
|
|
|
$this->assertEquals(['stub-a-lub-a-dub-dub'], array_map($getURL, $mockExtension->getToUpdate())); |
53
|
|
|
$mockExtension->setToUpdate([]); |
54
|
|
|
}) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
// register our extension instance so it's applied to all SiteTree objects |
58
|
|
|
Injector::inst()->registerService($mockExtension, SiteTreePublishingEngine::class); |
59
|
|
|
|
60
|
|
|
$page = new PublishablePage; |
61
|
|
|
$page->URLSegment = 'stub'; |
62
|
|
|
|
63
|
|
|
// publish the page |
64
|
|
|
$page->write(); |
65
|
|
|
$page->publishRecursive(); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// change the URL and go again |
68
|
|
|
$page->URLSegment = 'stub-a-lub-a-dub-dub'; |
69
|
|
|
$page->write(); |
70
|
|
|
$page->publishRecursive(); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testObjectsToUpdateOnPublish() |
74
|
|
|
{ |
75
|
|
|
$parent = PublishablePage::create(); |
76
|
|
|
|
77
|
|
|
$stub = $this->getMockBuilder(PublishablePage::class) |
78
|
|
|
->setMethods( |
79
|
|
|
[ |
80
|
|
|
'getParentID', |
81
|
|
|
'Parent', |
82
|
|
|
] |
83
|
|
|
)->getMock(); |
84
|
|
|
|
85
|
|
|
$stub->expects($this->once()) |
86
|
|
|
->method('getParentID') |
87
|
|
|
->will($this->returnValue('2')); |
88
|
|
|
|
89
|
|
|
$stub->expects($this->once()) |
90
|
|
|
->method('Parent') |
91
|
|
|
->will($this->returnValue($parent)); |
92
|
|
|
|
93
|
|
|
$objects = $stub->objectsToUpdate(['action' => 'publish']); |
94
|
|
|
$this->assertContains($stub, $objects); |
95
|
|
|
$this->assertContains($parent, $objects); |
96
|
|
|
$this->assertCount(2, $objects); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testObjectsToUpdateOnUnpublish() |
100
|
|
|
{ |
101
|
|
|
$parent = PublishablePage::create(); |
102
|
|
|
|
103
|
|
|
$stub = $this->getMockBuilder(PublishablePage::class) |
104
|
|
|
->setMethods( |
105
|
|
|
[ |
106
|
|
|
'getParentID', |
107
|
|
|
'Parent', |
108
|
|
|
] |
109
|
|
|
)->getMock(); |
110
|
|
|
|
111
|
|
|
$stub->expects($this->once()) |
112
|
|
|
->method('getParentID') |
113
|
|
|
->will($this->returnValue('2')); |
114
|
|
|
|
115
|
|
|
$stub->expects($this->once()) |
116
|
|
|
->method('Parent') |
117
|
|
|
->will($this->returnValue($parent)); |
118
|
|
|
|
119
|
|
|
$updates = $stub->objectsToUpdate(['action' => 'unpublish']); |
120
|
|
|
$deletions = $stub->objectsToDelete(['action' => 'unpublish']); |
121
|
|
|
$this->assertContains($stub, $deletions); |
122
|
|
|
$this->assertNotContains($parent, $deletions); |
123
|
|
|
$this->assertContains($parent, $updates); |
124
|
|
|
$this->assertNotContains($stub, $updates); |
125
|
|
|
$this->assertCount(1, $deletions); |
126
|
|
|
$this->assertCount(1, $updates); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testObjectsToDeleteOnPublish() |
130
|
|
|
{ |
131
|
|
|
$stub = PublishablePage::create(); |
132
|
|
|
$objects = $stub->objectsToDelete(['action' => 'publish']); |
|
|
|
|
133
|
|
|
$this->assertEmpty($objects); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testObjectsToDeleteOnUnpublish() |
137
|
|
|
{ |
138
|
|
|
$stub = PublishablePage::create(); |
139
|
|
|
$stub->Title = 'stub'; |
140
|
|
|
$objects = $stub->objectsToDelete(['action' => 'unpublish']); |
|
|
|
|
141
|
|
|
$this->assertContains($stub, $objects); |
142
|
|
|
$this->assertCount(1, $objects); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
View Code Duplication |
public function testObjectsToUpdateOnPublishIfVirtualExists() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$redir = PublishablePage::create(); |
148
|
|
|
|
149
|
|
|
$stub = $this->getMockBuilder(PublishablePage::class) |
150
|
|
|
->setMethods(['getMyVirtualPages']) |
151
|
|
|
->getMock(); |
152
|
|
|
|
153
|
|
|
$stub->expects($this->once()) |
154
|
|
|
->method('getMyVirtualPages') |
155
|
|
|
->will( |
156
|
|
|
$this->returnValue( |
157
|
|
|
new ArrayList([$redir]) |
158
|
|
|
) |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$objects = $stub->objectsToUpdate(['action' => 'publish']); |
162
|
|
|
$this->assertContains($stub, $objects); |
163
|
|
|
$this->assertContains($redir, $objects); |
164
|
|
|
$this->assertCount(2, $objects); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
View Code Duplication |
public function testObjectsToDeleteOnUnpublishIfVirtualExists() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$redir = PublishablePage::create(); |
170
|
|
|
|
171
|
|
|
$stub = $this->getMockBuilder(PublishablePage::class) |
172
|
|
|
->setMethods(['getMyVirtualPages']) |
173
|
|
|
->getMock(); |
174
|
|
|
|
175
|
|
|
$stub->Title = 'stub'; |
176
|
|
|
|
177
|
|
|
$stub->expects($this->once()) |
178
|
|
|
->method('getMyVirtualPages') |
179
|
|
|
->will( |
180
|
|
|
$this->returnValue( |
181
|
|
|
new ArrayList([$redir]) |
182
|
|
|
) |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$objects = $stub->objectsToDelete(['action' => 'unpublish']); |
186
|
|
|
$this->assertContains($stub, $objects); |
187
|
|
|
$this->assertContains($redir, $objects); |
188
|
|
|
$this->assertCount(2, $objects); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
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.