1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class StaticPagesQueueTest extends SapphireTest { |
|
|
|
|
5
|
|
|
|
6
|
|
|
public function setUp() { |
7
|
|
|
parent::setUp(); |
8
|
|
|
Config::inst()->nest(); |
9
|
|
|
Config::inst()->update('StaticPagesQueue', 'realtime', true); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public function tearDown() { |
13
|
|
|
// Remove all StaticPagesQueue that might be left after running a testcase |
14
|
|
|
self::empty_temp_db(); |
15
|
|
|
|
16
|
|
|
Config::inst()->unnest(); |
17
|
|
|
parent::tearDown(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testAddOneURL() { |
21
|
|
|
$obj = StaticPagesQueue::get()->First(); |
22
|
|
|
$this->assertFalse( $obj instanceof StaticPagesQueue ); |
23
|
|
|
|
24
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('test1' => 1)); |
25
|
|
|
|
26
|
|
|
$obj = StaticPagesQueue::get()->First(); |
27
|
|
|
$this->assertTrue($obj instanceof StaticPagesQueue); |
28
|
|
|
$this->assertEquals('test1', $obj->URLSegment ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testAddManyURLs() { |
32
|
|
|
$objSet = DataObject::get('StaticPagesQueue'); |
|
|
|
|
33
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('test2-2' => 2 ,'test2-10' => 10),true); |
34
|
|
|
$objSet = DataObject::get('StaticPagesQueue'); |
35
|
|
|
$this->assertEquals( 2, $objSet->Count() ); |
36
|
|
|
$this->assertDOSContains(array( |
37
|
|
|
array('URLSegment' => 'test2-2'), |
38
|
|
|
array('URLSegment' => 'test2-10') |
39
|
|
|
), $objSet); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testGetNextUrlInEmptyQueue() { |
43
|
|
|
$this->assertEquals( '', StaticPagesQueue::get_next_url() ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testGetNextUrlByPriority() { |
47
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('monkey' => 1 ,'stool' => 10),true); |
48
|
|
|
$this->assertEquals( 'stool', StaticPagesQueue::get_next_url() ); |
49
|
|
|
$this->assertEquals( 'monkey', StaticPagesQueue::get_next_url() ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testBumpThePriority() { |
53
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('foobar' => 1),true); |
54
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('monkey' => 10),true); |
55
|
|
|
// Adding a duplicate |
56
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('monkey' => 1),true); |
57
|
|
|
$this->assertEquals(3, DataObject::get('StaticPagesQueue')->Count()); |
58
|
|
|
StaticPagesQueue::get_next_url(); |
59
|
|
|
$this->assertEquals(10, DataObject::get('StaticPagesQueue')->Last()->Priority); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testNothingToDelete() { |
63
|
|
|
$this->assertFalse( StaticPagesQueue::delete_by_link("Im not there")); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGetNextUrlByPrioAndDate() { |
67
|
|
|
$oldObject = new StaticPagesQueue(array('URLSegment'=>'old/page','Priority'=>10),true); |
68
|
|
|
$oldObject->write(); |
69
|
|
|
$oldObject->Created = "2010-01-01 10:00:01"; |
70
|
|
|
$oldObject->write(); |
71
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('monkey/bites' => 1 ,'stool/falls' => 10),true); |
72
|
|
|
$this->assertEquals( 'old/page', StaticPagesQueue::get_next_url()); |
73
|
|
|
$this->assertEquals( 'stool/falls', StaticPagesQueue::get_next_url()); |
74
|
|
|
$this->assertEquals( 'monkey/bites', StaticPagesQueue::get_next_url()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testMarkAsDone() { |
78
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('monkey' => 1),true); |
79
|
|
|
$url = StaticPagesQueue::get_next_url(); |
80
|
|
|
$this->assertTrue( StaticPagesQueue::delete_by_link($url) ); |
81
|
|
|
#$this->assertNull( DataObject::get('StaticPagesQueue')); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* This tests that queueitems that are marked as regenerating, but are older |
86
|
|
|
* than 10 minutes actually gets another try |
87
|
|
|
*/ |
88
|
|
|
public function testDeadEntries() { |
89
|
|
|
$oldObject = new StaticPagesQueue(array('URLSegment'=>'old/page','Freshness'=>'regenerating')); |
90
|
|
|
$oldObject->write(); |
91
|
|
|
// Update the entry directly, this is the only way to change LastEdited to a set value |
92
|
|
|
DB::query('UPDATE "StaticPagesQueue" SET "LastEdited" =\''.date("Y-m-d H:i:s", time()-60*11).'\''); |
93
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('monkey/bites' => 1),true); |
94
|
|
|
$this->assertEquals( 'monkey/bites', StaticPagesQueue::get_next_url()); |
95
|
|
|
$this->assertEquals( 'old/page', StaticPagesQueue::get_next_url()); |
96
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array('should/be/next' => 1),true); |
97
|
|
|
$this->assertEquals( 'should/be/next', StaticPagesQueue::get_next_url()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testRemoveDuplicates() { |
101
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array( |
102
|
|
|
'test1' => 1 , |
103
|
|
|
'test2' => 1, |
104
|
|
|
'test3' => 1 |
105
|
|
|
)); |
106
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array( |
107
|
|
|
'test2' => 2, // duplicate |
108
|
|
|
'test3' => 2, // duplicate |
109
|
|
|
)); |
110
|
|
|
Injector::inst()->get('URLArrayObject')->addUrls(array( |
111
|
|
|
'test2' => 3, // duplicate |
112
|
|
|
)); |
113
|
|
|
$test1Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test1\''); |
114
|
|
|
$test2Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test2\''); |
115
|
|
|
$test3Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test3\''); |
116
|
|
|
$this->assertEquals(1, $test1Objs->Count()); |
117
|
|
|
$this->assertEquals(3, $test2Objs->Count()); |
118
|
|
|
$this->assertEquals(2, $test3Objs->Count()); |
119
|
|
|
|
120
|
|
|
StaticPagesQueue::remove_duplicates($test1Objs->First()->ID); |
121
|
|
|
$test1Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test1\''); |
122
|
|
|
$test2Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test2\''); |
123
|
|
|
$test3Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test3\''); |
124
|
|
|
$this->assertEquals(1, $test1Objs->Count(), 'Keeps original instance without any duplicates found'); |
125
|
|
|
$this->assertEquals(3, $test2Objs->Count(), 'Doesnt remove unrelated duplicates'); |
126
|
|
|
$this->assertEquals(2, $test3Objs->Count(), 'Doesnt remove unrelated duplicates'); |
127
|
|
|
|
128
|
|
|
StaticPagesQueue::remove_duplicates($test2Objs->First()->ID); |
129
|
|
|
$test2Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test2\''); |
130
|
|
|
$this->assertEquals(1, $test2Objs->Count(), 'Removing a single duplicate'); |
131
|
|
|
|
132
|
|
|
StaticPagesQueue::remove_duplicates($test3Objs->First()->ID); |
133
|
|
|
$test3Objs = DataObject::get('StaticPagesQueue', '"URLSegment" = \'test3\''); |
134
|
|
|
$this->assertEquals(1, $test3Objs->Count(), 'Removing multiple duplicates'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* |
139
|
|
|
* Test takes about 22sec on my local environment |
140
|
|
|
*/ |
141
|
|
|
// public function testPerformance() { |
|
|
|
|
142
|
|
|
// $start_time = microtime(true); |
143
|
|
|
// $array = array(); |
144
|
|
|
// for($idx=0;$idx<1000;$idx++){ |
145
|
|
|
// $array["page-".$idx] = $idx; |
146
|
|
|
// } |
147
|
|
|
// StaticPagesQueue::add_urls($array); |
148
|
|
|
// while( $url = StaticPagesQueue::get_next_url() ) { |
149
|
|
|
// StaticPagesQueue::delete_by_link($url); |
150
|
|
|
// } |
151
|
|
|
// $time_end = microtime(true); |
152
|
|
|
// $end_time = $time_end - $start_time; |
153
|
|
|
// echo sprintf("%.3fs" ,$end_time); |
154
|
|
|
// } |
155
|
|
|
} |
156
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.