Completed
Pull Request — master (#70)
by Daniel
04:09
created

testFlushChangesToUpdateEnqueuesAndDeletesRegular()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 0
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
51 View Code Duplication
    public function testCollectChangesForUnpublishing()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
52
    {
53
        $obj = StaticPublishingTriggerPage::create();
54
        $obj->collectChanges(array('action' => 'unpublish'));
55
56
        $this->assertEquals(
57
            array('/updateOnUnpublish?_ID=1&_ClassName=StaticallyPublishableTest' => 10),
58
            $obj->getToUpdate()
59
        );
60
        $this->assertEquals(
61
            array('/deleteOnUnpublish?_ID=1&_ClassName=StaticallyPublishableTest' => 10),
62
            $obj->getToDelete()
63
        );
64
65
    }
66
67
    public function testFlushChangesToUpdateEnqueuesAndDeletesRegular()
68
    {
69
70
        $toUpdate = array('/toUpdate?_ID=1&_ClassName=StaticallyPublishableTest' => 10);
71
72
        $stub = $this->getMockBuilder(StaticPublishingTriggerPage::class)
73
            ->setMethods(array('convertUrlsToPathMap', 'deleteRegularFiles', 'deleteStaleFiles'))
74
            ->getMock();
75
76
        $stub->expects($this->once())
77
            ->method('convertUrlsToPathMap')
78
            ->will($this->returnValue(array('url' => 'file')));
79
80
        // Test: enqueues the updated URLs
81
        $stub->setUrlArrayObject($this->getMockBuilder(URLArrayObject::class)->setMethods(array('addUrls'))->getMock());
82
        $stub->getUrlArrayObject()->expects($this->once())
83
            ->method('addUrls')
84
            ->with($this->equalTo($toUpdate));
85
86
        // Test: deletes just the regular files
87
        $stub->expects($this->never())
88
            ->method('deleteStaleFiles');
89
        $stub->expects($this->once())
90
            ->method('deleteRegularFiles')
91
            ->with($this->equalTo(array('file')));
92
93
        // Test: clears the update queue
94
        $stub->setToUpdate($toUpdate);
95
        $stub->flushChanges();
96
        $this->assertEmpty($stub->getToUpdate());
97
    }
98
99
    public function testFlushChangesToDeleteDeletesRegularAndStale()
100
    {
101
        $toDelete = array('/toDelete?_ID=1&_ClassName=StaticallyPublishableTest' => 10);
102
103
        $stub = $this->getMockBuilder(StaticPublishingTriggerPage::class)
104
            ->setMethods(array('convertUrlsToPathMap', 'deleteRegularFiles', 'deleteStaleFiles'))
105
            ->getMock();
106
107
        $stub->expects($this->once())
108
            ->method('convertUrlsToPathMap')
109
            ->will($this->returnValue(array('url' => 'file')));
110
111
        // Test: deletes both regular and stale files
112
        $stub->expects($this->once())
113
            ->method('deleteRegularFiles')
114
            ->with($this->equalTo(array('file')));
115
        $stub->expects($this->once())
116
            ->method('deleteStaleFiles')
117
            ->with($this->equalTo(array('file')));
118
119
        // Test: clears the queue
120
        $stub->setToDelete($toDelete);
121
        $stub->flushChanges();
122
        $this->assertEmpty($stub->getToDelete());
123
124
    }
125
126
    public function testConvertUrlsToPathMapNoObject()
127
    {
128
        Config::modify()->set('FilesystemPublisher', 'static_base_url', 'http://foo');
129
        Config::modify()->set('Director', 'alternate_base_url', '/');
130
131
        $urls = array('/xyzzy');
132
133
        $stub = StaticPublishingTriggerPage::create();
134
135
        // Test (inclusively with urlsToPaths, these interfaces should be refactored together)
136
        $result = $stub->convertUrlsToPathMap($urls);
137
        $this->assertEquals($result, array(
138
            '/xyzzy' => './xyzzy.html'
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(
165
                $page
166
            ));
167
168
        // Test (inclusively with urlsToPaths, these interfaces should be refactored together)
169
        $result = $stub->convertUrlsToPathMap($urls);
170
        $this->assertEquals(
171
            array('http://foo/xyzzy?_ID=1&_ClassName=SiteTreePublishingEngineTest_StaticallyPublishable' =>
172
                'foo/xyzzy.html'),
173
            $result
174
        );
175
    }
176
177
    public function testConvertUrlsToPathMapSubsite()
178
    {
179
        Config::modify()->set(FilesystemPublisher::class, 'static_base_url', 'http://foo');
180
        Config::modify()->set(Director::class, 'alternate_base_url', '/');
181
        $urls = array('/xyzzy?_ID=1&_ClassName=SiteTreePublishingEngineTest_StaticallyPublishable');
182
183
        // Mock a set of objects pretending to support Subsites. Subsites might not be installed.
184
        $domain1 = $this->createMock('SubsiteDomain_mock');
185
        $domain1->Domain = 'subiste1.domain.org';
0 ignored issues
show
Bug introduced by
Accessing Domain on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
186
        $domain2 = $this->createMock('SubsiteDomain_mock');
187
        $domain2->Domain = 'subiste2.domain.org';
0 ignored issues
show
Bug introduced by
Accessing Domain on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
188
189
        $domains = new ArrayList(array($domain1, $domain2));
190
191
        $subsite = $this->createMock('Subsite_mock');
192
        $subsite->expects($this->any())
193
            ->method('Domains')
194
            ->will($this->returnValue($domains));
195
        $subsite->ID = 1;
0 ignored issues
show
Bug introduced by
Accessing ID on the interface PHPUnit_Framework_MockObject_MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
196
197
        $stub = $this->getMockBuilder(StaticallyPublishablePage::class)
198
            ->setMethods(array('Subsite', 'hasExtension'))
199
            ->getMock();
200
201
        $stub->expects($this->any())
202
            ->method('Subsite')
203
            ->will($this->returnValue($subsite));
204
        $stub->expects($this->any())
205
            ->method('hasExtension')
206
            ->will($this->returnValue(true));
207
208
        // Prepare static mocks.
209
        $stub->setUrlArrayObject($this->getMockBuilder(URLArrayObject::class)->setMethods(array('getObject'))->getMock());
210
211
        $stub->getUrlArrayObject()->expects($this->any())
212
            ->method('getObject')
213
            ->will($this->returnValue($stub));
214
215
        // Test (inclusively with urlsToPaths, these interfaces should be refactored together)
216
        $result = $stub->convertUrlsToPathMap($urls);
217
        $this->assertEquals(
218
            array(
219
                'http://subiste1.domain.org/xyzzy?_ID=1&_ClassName=SiteTreePublishingEngineTest_StaticallyPublishable' =>
220
                    "subiste1.domain.org/xyzzy.html",
221
                'http://subiste2.domain.org/xyzzy?_ID=1&_ClassName=SiteTreePublishingEngineTest_StaticallyPublishable' =>
222
                    "subiste2.domain.org/xyzzy.html"
223
            ),
224
            $result
225
        );
226
    }
227
228
    public function testDeleteStaleFiles()
229
    {
230
        $stub = $this->getMockBuilder(StaticPublishingTriggerPage::class)
231
            ->setMethods(array('deleteFromCacheDir'))
232
            ->getMock();
233
234
        $stub->expects($this->at(0))
235
            ->method('deleteFromCacheDir')
236
            ->with($this->equalTo('xyzzy.stale.html'));
237
238
        $stub->expects($this->at(1))
239
            ->method('deleteFromCacheDir')
240
            ->with($this->equalTo('foo/bar/baz.stale.html'));
241
242
        $stub->deleteStaleFiles(array('xyzzy.html', 'foo/bar/baz.html'));
243
    }
244
}
245