1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
<?php
// $Id: page_test.php 1913 2009-07-29 16:50:56Z lastcraft $
require_once(dirname(__FILE__) . '/../autorun.php');
require_once(dirname(__FILE__) . '/../expectation.php');
require_once(dirname(__FILE__) . '/../http.php');
require_once(dirname(__FILE__) . '/../page.php');
Mock::generate('SimpleHttpHeaders');
Mock::generate('SimpleHttpResponse');
class TestOfPageInterface extends UnitTestCase {
function testInterfaceOnEmptyPage() {
$page = new SimplePage();
$this->assertEqual($page->getTransportError(), 'No page fetched yet');
$this->assertIdentical($page->getRaw(), false);
$this->assertIdentical($page->getHeaders(), false);
$this->assertIdentical($page->getMimeType(), false);
$this->assertIdentical($page->getResponseCode(), false);
$this->assertIdentical($page->getAuthentication(), false);
$this->assertIdentical($page->getRealm(), false);
$this->assertFalse($page->hasFrames());
$this->assertIdentical($page->getUrls(), array());
$this->assertIdentical($page->getTitle(), false);
}
}
class TestOfPageHeaders extends UnitTestCase {
function testUrlAccessor() {
$headers = new MockSimpleHttpHeaders();
$response = new MockSimpleHttpResponse();
$response->setReturnValue('getHeaders', $headers);
$response->setReturnValue('getMethod', 'POST');
$response->setReturnValue('getUrl', new SimpleUrl('here'));
$response->setReturnValue('getRequestData', array('a' => 'A'));
$page = new SimplePage($response);
$this->assertEqual($page->getMethod(), 'POST');
$this->assertEqual($page->getUrl(), new SimpleUrl('here'));
$this->assertEqual($page->getRequestData(), array('a' => 'A'));
}
function testTransportError() {
$response = new MockSimpleHttpResponse();
$response->setReturnValue('getError', 'Ouch');
$page = new SimplePage($response);
$this->assertEqual($page->getTransportError(), 'Ouch');
}
function testHeadersAccessor() {
$headers = new MockSimpleHttpHeaders();
$headers->setReturnValue('getRaw', 'My: Headers');
$response = new MockSimpleHttpResponse();
$response->setReturnValue('getHeaders', $headers);
$page = new SimplePage($response);
$this->assertEqual($page->getHeaders(), 'My: Headers');
}
function testMimeAccessor() {
$headers = new MockSimpleHttpHeaders();
$headers->setReturnValue('getMimeType', 'text/html');
$response = new MockSimpleHttpResponse();
$response->setReturnValue('getHeaders', $headers);
$page = new SimplePage($response);
$this->assertEqual($page->getMimeType(), 'text/html');
}
function testResponseAccessor() {
$headers = new MockSimpleHttpHeaders();
$headers->setReturnValue('getResponseCode', 301);
$response = new MockSimpleHttpResponse();
$response->setReturnValue('getHeaders', $headers);
$page = new SimplePage($response);
$this->assertIdentical($page->getResponseCode(), 301);
}
function testAuthenticationAccessors() {
$headers = new MockSimpleHttpHeaders();
$headers->setReturnValue('getAuthentication', 'Basic');
$headers->setReturnValue('getRealm', 'Secret stuff');
$response = new MockSimpleHttpResponse();
$response->setReturnValue('getHeaders', $headers);
$page = new SimplePage($response);
$this->assertEqual($page->getAuthentication(), 'Basic');
$this->assertEqual($page->getRealm(), 'Secret stuff');
}
}
class TestOfHtmlStrippingAndNormalisation extends UnitTestCase {
function testImageSuppressionWhileKeepingParagraphsAndAltText() {
$this->assertEqual(
SimplePage::normalise('<img src="foo.png" /><p>some text</p><img src="bar.png" alt="bar" />'),
'some text bar');
}
function testSpaceNormalisation() {
$this->assertEqual(
SimplePage::normalise("\nOne\tTwo \nThree\t"),
'One Two Three');
}
function testMultilinesCommentSuppression() {
$this->assertEqual(
SimplePage::normalise('<!--\n Hello \n-->'),
'');
}
function testCommentSuppression() {
$this->assertEqual(
SimplePage::normalise('<!--Hello-->'),
'');
}
function testJavascriptSuppression() {
$this->assertEqual(
SimplePage::normalise('<script attribute="test">\nHello\n</script>'),
'');
$this->assertEqual(
SimplePage::normalise('<script attribute="test">Hello</script>'),
'');
$this->assertEqual(
SimplePage::normalise('<script>Hello</script>'),
'');
}
function testTagSuppression() {
$this->assertEqual(
SimplePage::normalise('<b>Hello</b>'),
'Hello');
}
function testAdjoiningTagSuppression() {
$this->assertEqual(
SimplePage::normalise('<b>Hello</b><em>Goodbye</em>'),
'HelloGoodbye');
}
function testExtractImageAltTextWithDifferentQuotes() {
$this->assertEqual(
SimplePage::normalise('<img alt="One"><img alt=\'Two\'><img alt=Three>'),
'One Two Three');
}
function testExtractImageAltTextMultipleTimes() {
$this->assertEqual(
SimplePage::normalise('<img alt="One"><img alt="Two"><img alt="Three">'),
'One Two Three');
}
function testHtmlEntityTranslation() {
$this->assertEqual(
SimplePage::normalise('<>"&''),
'<>"&\'');
}
}
?>
|