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

StaticPagesQueueTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Test;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\StaticPublishQueue\Model\StaticPagesQueue;
9
use SilverStripe\StaticPublishQueue\Model\URLArrayObject;
10
11
class StaticPagesQueueTest extends SapphireTest
12
{
13
    protected function setUp()
14
    {
15
        parent::setUp();
16
        Config::modify()->set(StaticPagesQueue::class, 'realtime', true);
17
    }
18
19
    public function testAddOneURL()
20
    {
21
        $obj = StaticPagesQueue::get()->First();
22
        $this->assertNotInstanceOf(StaticPagesQueue::class, $obj);
23
24
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('test1' => 1));
25
26
        $obj = StaticPagesQueue::get()->first();
27
        $this->assertInstanceOf(StaticPagesQueue::class, $obj);
28
        $this->assertEquals('test1', $obj->URLSegment);
29
    }
30
31
    public function testAddManyURLs()
32
    {
33
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('test2-2' => 2, 'test2-10' => 10), true);
34
        $objSet = StaticPagesQueue::get();
35
        $this->assertCount(2, $objSet);
36
        $this->assertListContains(
0 ignored issues
show
Bug introduced by
The method assertListContains() does not exist on SilverStripe\StaticPubli...st\StaticPagesQueueTest. Did you maybe mean contains()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
37
            array(
38
                array('URLSegment' => 'test2-2'),
39
                array('URLSegment' => 'test2-10')
40
            ),
41
            $objSet
42
        );
43
    }
44
45
    public function testGetNextUrlInEmptyQueue()
46
    {
47
        $this->assertEmpty(StaticPagesQueue::get_next_url());
48
    }
49
50
    public function testGetNextUrlByPriority()
51
    {
52
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('monkey' => 1, 'stool' => 10), true);
53
        $this->assertEquals('stool', StaticPagesQueue::get_next_url());
54
        $this->assertEquals('monkey', StaticPagesQueue::get_next_url());
55
    }
56
57
    public function testBumpThePriority()
58
    {
59
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('foobar' => 1), true);
60
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('monkey' => 10), true);
61
        // Adding a duplicate
62
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('monkey' => 1), true);
63
        $this->assertCount(3, StaticPagesQueue::get());
64
        StaticPagesQueue::get_next_url();
65
        $this->assertEquals(10, StaticPagesQueue::get()->Last()->Priority);
66
    }
67
68
    public function testNothingToDelete()
69
    {
70
        $this->assertFalse(StaticPagesQueue::delete_by_link("Im not there"));
71
    }
72
73
    public function testGetNextUrlByPrioAndDate()
74
    {
75
        $oldObject = new StaticPagesQueue(array('URLSegment' => 'old/page', 'Priority' => 10), true);
76
        $oldObject->write();
77
        $oldObject->Created = "2010-01-01 10:00:01";
78
        $oldObject->write();
79
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('monkey/bites' => 1, 'stool/falls' => 10), true);
80
        $this->assertEquals('old/page', StaticPagesQueue::get_next_url());
81
        $this->assertEquals('stool/falls', StaticPagesQueue::get_next_url());
82
        $this->assertEquals('monkey/bites', StaticPagesQueue::get_next_url());
83
    }
84
85
    public function testMarkAsDone()
86
    {
87
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('monkey' => 1), true);
88
        $url = StaticPagesQueue::get_next_url();
89
        $this->assertTrue(StaticPagesQueue::delete_by_link($url));
90
    }
91
92
    /**
93
     * This tests that queueitems that are marked as regenerating, but are older
94
     * than 10 minutes actually gets another try
95
     */
96
    public function testDeadEntries()
97
    {
98
        $oldObject = new StaticPagesQueue(array('URLSegment' => 'old/page', 'Freshness' => 'regenerating'));
99
        $oldObject->write();
100
        // Update the entry directly, this is the only way to change LastEdited to a set value
101
        //@todo - look at using DBDatetime::set_mock_now()
102
        DB::query('UPDATE "StaticPagesQueue" SET "LastEdited" =\'' . date("Y-m-d H:i:s", time() - 60 * 11) . '\'');
103
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('monkey/bites' => 1), true);
104
        $this->assertEquals('monkey/bites', StaticPagesQueue::get_next_url());
105
        $this->assertEquals('old/page', StaticPagesQueue::get_next_url());
106
        Injector::inst()->get(URLArrayObject::class)->addUrls(array('should/be/next' => 1), true);
107
        $this->assertEquals('should/be/next', StaticPagesQueue::get_next_url());
108
    }
109
110
    public function testRemoveDuplicates()
111
    {
112
        Injector::inst()->get(URLArrayObject::class)->addUrls(
113
            array(
114
                'test1' => 1,
115
                'test2' => 1,
116
                'test3' => 1
117
            )
118
        );
119
        Injector::inst()->get(URLArrayObject::class)->addUrls(
120
            array(
121
                'test2' => 2, // duplicate
122
                'test3' => 2, // duplicate
123
            )
124
        );
125
        Injector::inst()->get(URLArrayObject::class)->addUrls(
126
            array(
127
                'test2' => 3, // duplicate
128
            )
129
        );
130
        $test1Objs = StaticPagesQueue::get()->filter(
131
            array(
132
                'URLSegment' => 'test1'
133
            )
134
        )->first();
135
        $test2Objs = StaticPagesQueue::get()->filter(
136
            array(
137
                'URLSegment' => 'test2'
138
            )
139
        )->first();
140
        $test3Objs = StaticPagesQueue::get()->filter(
141
            array(
142
                'URLSegment' => 'test3'
143
            )
144
        )->first();
145
        $this->assertCount(1, $test1Objs);
146
        $this->assertCount(3, $test2Objs);
147
        $this->assertCount(2, $test3Objs);
148
149
        StaticPagesQueue::remove_duplicates($test1Objs->first()->ID);
150
        $test1Objs = StaticPagesQueue::get()->filter(
151
            array(
152
                'URLSegment' => 'test1'
153
            )
154
        )->first();
155
        $test2Objs = StaticPagesQueue::get()->filter(
156
            array(
157
                'URLSegment' => 'test2'
158
            )
159
        )->first();
160
        $test3Objs = StaticPagesQueue::get()->filter(
161
            array(
162
                'URLSegment' => 'test3'
163
            )
164
        )->first();
165
        $this->assertCount(1, $test1Objs);
166
        $this->assertCount(3, $test2Objs);
167
        $this->assertCount(2, $test3Objs);
168
169
        StaticPagesQueue::remove_duplicates($test2Objs->first()->ID);
170
        $test2Objs = StaticPagesQueue::get()->filter(
171
            array(
172
                'URLSegment' => 'test2'
173
            )
174
        );
175
        $this->assertCount(1, $test2Objs);
176
177
        StaticPagesQueue::remove_duplicates($test3Objs->first()->ID);
178
        $test3Objs = StaticPagesQueue::get()->filter(
179
            array(
180
                'URLSegment' => 'test3'
181
            )
182
        );
183
        $this->assertCount(1, $test3Objs);
184
    }
185
}
186