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

StaticPagesQueueTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 150
rs 10
c 0
b 0
f 0

11 Methods

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