1 |
bad4e148
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Validate the link_validate_url() validation API.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Validate the link_validate_url() validation API.
|
10 |
|
|
*
|
11 |
|
|
* Validation is guided by the rules in http://tools.ietf.org/html/rfc1738.
|
12 |
|
|
*/
|
13 |
|
|
class LinkValidationApiTest extends LinkBaseTestClass {
|
14 |
|
|
|
15 |
|
|
/**
|
16 |
|
|
* Get Info.
|
17 |
|
|
*/
|
18 |
|
|
public static function getInfo() {
|
19 |
|
|
return array(
|
20 |
|
|
'name' => 'Link Validation API Tests',
|
21 |
|
|
'description' => 'Tests the link_validate_url() function by itself, without invoking the full drupal/cck lifecycle.',
|
22 |
|
|
'group' => 'Link',
|
23 |
|
|
);
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
/**
|
27 |
|
|
* {@inheritdoc}
|
28 |
|
|
*/
|
29 |
|
|
public function setUp(array $modules = array()) {
|
30 |
|
|
$modules[] = 'link';
|
31 |
|
|
parent::setUp($modules);
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
/**
|
35 |
|
|
* Name Link Type.
|
36 |
|
|
*
|
37 |
|
|
* Translates the LINK type constants to english for display and debugging of
|
38 |
|
|
* tests.
|
39 |
|
|
*
|
40 |
|
|
* @todo What is this for? Can it be removed?
|
41 |
|
|
*
|
42 |
|
|
* @codingStandardsIgnoreStart
|
43 |
|
|
*/
|
44 |
|
|
public function name_Link_Type($type) {
|
45 |
|
|
// @codingStandardsIgnoreEnd
|
46 |
|
|
switch ($type) {
|
47 |
|
|
case LINK_FRONT:
|
48 |
|
|
return "Front";
|
49 |
|
|
|
50 |
|
|
case LINK_EMAIL:
|
51 |
|
|
return "Email";
|
52 |
|
|
|
53 |
|
|
case LINK_TEL:
|
54 |
|
|
return "Telephone";
|
55 |
|
|
|
56 |
|
|
case LINK_NEWS:
|
57 |
|
|
return "Newsgroup";
|
58 |
|
|
|
59 |
|
|
case LINK_INTERNAL:
|
60 |
|
|
return "Internal Link";
|
61 |
|
|
|
62 |
|
|
case LINK_EXTERNAL:
|
63 |
|
|
return "External Link";
|
64 |
|
|
|
65 |
|
|
case FALSE:
|
66 |
|
|
return "Invalid Link";
|
67 |
|
|
|
68 |
|
|
default:
|
69 |
|
|
return "Bad Value:" . $type;
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/**
|
74 |
|
|
* Make sure that a link labeled <front> works.
|
75 |
|
|
*/
|
76 |
|
|
public function testValidateFrontLink() {
|
77 |
|
|
$valid = link_validate_url('<front>');
|
78 |
|
|
$this->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verified and identified');
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
|
|
* Validate Email Link.
|
83 |
|
|
*/
|
84 |
|
|
public function testValidateEmailLink() {
|
85 |
|
|
$valid = link_validate_url('mailto:bob@example.com');
|
86 |
|
|
$this->assertEqual(LINK_EMAIL, $valid, "Make sure a basic mailto is verified and identified");
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
/**
|
90 |
|
|
* Validate Email Link Bad.
|
91 |
|
|
*/
|
92 |
|
|
public function testValidateEmailLinkBad() {
|
93 |
|
|
$valid = link_validate_url(':bob@example.com');
|
94 |
|
|
$this->assertEqual(FALSE, $valid, 'Make sure just a bad address is correctly failed');
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
/**
|
98 |
|
|
* Confirm that valid tel: links work as expected.
|
99 |
|
|
*/
|
100 |
|
|
public function testValidateTelLinks() {
|
101 |
|
|
$links = array(
|
102 |
|
|
'tel:01',
|
103 |
|
|
'tel:123456789012345',
|
104 |
|
|
'tel:+123456789012345',
|
105 |
|
|
);
|
106 |
|
|
foreach ($links as $link) {
|
107 |
|
|
$type = link_url_type($link);
|
108 |
|
|
$this->assertEqual(LINK_TEL, $type, 'Test ' . $link . ' is a tel link.');
|
109 |
|
|
$valid = link_validate_url($link);
|
110 |
|
|
$this->assertTrue($valid, 'Test ' . $link . ' is valid tel link.');
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
/**
|
115 |
|
|
* Confirm that invalid tel: links work as expected.
|
116 |
|
|
*/
|
117 |
|
|
public function testValidateTelLinksBad() {
|
118 |
|
|
$links = array(
|
119 |
|
|
'tel:0',
|
120 |
|
|
'tel:1234567890123456',
|
121 |
|
|
'tel:+1',
|
122 |
|
|
'tel:+0123456789',
|
123 |
|
|
'tel:+1234567890123456',
|
124 |
|
|
':12345678',
|
125 |
|
|
);
|
126 |
|
|
foreach ($links as $link) {
|
127 |
|
|
$type = link_url_type($link);
|
128 |
|
|
$this->assertFalse($type, 'Test ' . $link . ' is not a tel link.');
|
129 |
|
|
$valid = link_validate_url($link);
|
130 |
|
|
$this->assertFalse($valid, 'Test ' . $link . ' is not a valid tel link.');
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
|
|
* Validate Newsgroup Link.
|
136 |
|
|
*/
|
137 |
|
|
public function testValidateNewsgroupLink() {
|
138 |
|
|
$valid = link_validate_url('news:comp.infosystems.www.misc');
|
139 |
|
|
$this->assertEqual(LINK_NEWS, $valid, 'Make sure link to newsgroup validates as news.');
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* Validate News Article Link.
|
144 |
|
|
*/
|
145 |
|
|
public function testValidateNewsArticleLink() {
|
146 |
|
|
$valid = link_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
|
147 |
|
|
$this->assertEqual(LINK_NEWS, $valid, 'Make sure link to specific article validates as news.');
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
/**
|
151 |
|
|
* Validate Bad Newsgroup Link.
|
152 |
|
|
*/
|
153 |
|
|
public function testValidateBadNewsgroupLink() {
|
154 |
|
|
$valid = link_validate_url('news:comp.bad_name.misc');
|
155 |
|
|
$this->assertEqual(FALSE, $valid, 'newsgroup names can\'t contain underscores, so it should come back as invalid.');
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
/**
|
159 |
|
|
* Validate Internal Links.
|
160 |
|
|
*/
|
161 |
|
|
public function testValidateInternalLinks() {
|
162 |
|
|
$tempfile = drupal_tempnam('public://files', 'test');
|
163 |
|
|
$links = array(
|
164 |
|
|
'rss.xml',
|
165 |
|
|
'foo#bar',
|
166 |
|
|
file_uri_target($tempfile),
|
167 |
|
|
drupal_realpath($tempfile),
|
168 |
|
|
);
|
169 |
|
|
|
170 |
|
|
foreach ($links as $link) {
|
171 |
|
|
$type = link_url_type($link);
|
172 |
|
|
$this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
|
173 |
|
|
$valid = link_validate_url($link);
|
174 |
|
|
$this->assertTrue($valid, 'Test ' . $link . ' is valid internal link.');
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
/**
|
179 |
|
|
* Validate External Links.
|
180 |
|
|
*/
|
181 |
|
|
public function testValidateExternalLinks() {
|
182 |
|
|
$links = array(
|
183 |
|
|
'http://localhost:8080/',
|
184 |
|
|
'www.example.com',
|
185 |
|
|
'www.example.com/',
|
186 |
|
|
'http://username:p%40ssw0rd!@www.example.com/',
|
187 |
|
|
'http://@www.example.com/',
|
188 |
|
|
'http://username:@www.example.com/',
|
189 |
|
|
'http://username:password@www.example.com:8080/',
|
190 |
|
|
'http://127.0.0.1:80/',
|
191 |
|
|
'http://127.173.24.255:4723/',
|
192 |
|
|
'127.173.24.255:4723/',
|
193 |
|
|
'http://255.255.255.255:4823/',
|
194 |
|
|
'www.test-site.com',
|
195 |
|
|
'http://example.com/index.php?q=node/123',
|
196 |
|
|
'http://example.com/?first_name=Joe Bob&last_name=Smith',
|
197 |
|
|
// Anchors.
|
198 |
|
|
'http://www.example.com/index.php#test',
|
199 |
|
|
'http://www.example.com/index.php#this@that.',
|
200 |
|
|
'http://www.example.com/index.php#',
|
201 |
|
|
'http://www.cnn.com/video/#/video/politics/2008/12/09/intv.madeleine.albright.cnn',
|
202 |
|
|
'http://www.archive.org/stream/aesopsfables00aesorich#page/n7/mode/2up',
|
203 |
|
|
'http://www.example.com/blah/#this@that?',
|
204 |
|
|
);
|
205 |
|
|
|
206 |
|
|
// Test all of the protocols.
|
207 |
|
|
$allowed_protocols = variable_get('filter_allowed_protocols', array(
|
208 |
|
|
'http',
|
209 |
|
|
'https',
|
210 |
|
|
'ftp',
|
211 |
|
|
'news',
|
212 |
|
|
'nntp',
|
213 |
|
|
'telnet',
|
214 |
|
|
'mailto',
|
215 |
|
|
'irc',
|
216 |
|
|
'ssh',
|
217 |
|
|
'sftp',
|
218 |
|
|
'webcal',
|
219 |
|
|
));
|
220 |
|
|
|
221 |
|
|
foreach ($allowed_protocols as $protocol) {
|
222 |
|
|
if ($protocol !== 'news' && $protocol !== 'mailto') {
|
223 |
|
|
$links[] = $protocol . '://www.example.com';
|
224 |
|
|
}
|
225 |
|
|
}
|
226 |
|
|
foreach ($links as $link) {
|
227 |
|
|
$type = link_url_type($link);
|
228 |
|
|
$this->assertEqual(LINK_EXTERNAL, $type, 'Testing that ' . $link . ' is an external link.');
|
229 |
|
|
$valid = link_validate_url($link);
|
230 |
|
|
$this->assertTrue($valid, 'Test ' . $link . ' is valid external link.');
|
231 |
|
|
// The following two lines are commented out and only used for
|
232 |
|
|
// comparisons.
|
233 |
|
|
// @code
|
234 |
|
|
// $valid2 = valid_url($link, TRUE);
|
235 |
|
|
// $this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");.
|
236 |
|
|
// @endcode
|
237 |
|
|
}
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
/**
|
241 |
|
|
* Check Invalid External Links.
|
242 |
|
|
*/
|
243 |
|
|
public function testInvalidExternalLinks() {
|
244 |
|
|
$links = array(
|
245 |
|
|
'http://www.ex ample.com/',
|
246 |
|
|
// Bad ip!
|
247 |
|
|
'http://25.0.0/',
|
248 |
|
|
'http://4827.0.0.2/',
|
249 |
|
|
// ß not allowed in domain names!
|
250 |
|
|
'http://www.testß.com/',
|
251 |
|
|
// Bad TLD.
|
252 |
|
|
'http://.www.foo.bar./',
|
253 |
|
|
// Domains can't have sections starting with a dash.
|
254 |
|
|
// 'http://www.-fudge.com/',
|
255 |
|
|
'http://example.com/index.php?page=this\that',
|
256 |
|
|
'example@example.com',
|
257 |
|
|
);
|
258 |
|
|
foreach ($links as $link) {
|
259 |
|
|
$valid = link_validate_url($link);
|
260 |
|
|
$this->assertEqual(FALSE, $valid, 'Testing that ' . $link . ' is not a valid link.');
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
|
264 |
|
|
} |