3 namespace Drupal\linkchecker\Tests;
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\simpletest\WebTestBase;
9 * Test Link checker module link extraction functionality.
13 class LinkCheckerLinkExtractionTest extends WebTestBase {
20 public static $modules = [
28 protected function setUp() {
31 $full_html_format = filter_format_load('full_html');
33 'create page content',
34 'edit own page content',
35 'administer url aliases',
37 filter_permission_name($full_html_format),
40 // User to set up google_analytics.
41 $this->admin_user = $this->drupalCreateUser($permissions);
42 $this->drupalLogin($this->admin_user);
45 public function testLinkCheckerCreateNodeWithLinks() {
47 // Enable all node type page for link extraction.
48 variable_set('linkchecker_scan_node_page', TRUE);
49 variable_set('linkchecker_scan_blocks', 1);
51 // Core enables the URL filter for "Full HTML" by default.
52 // -> Blacklist / Disable URL filter for testing.
53 variable_set('linkchecker_filter_blacklist', array('filter_url' => 'filter_url'));
55 // Extract from all link checker supported HTML tags.
56 variable_set('linkchecker_extract_from_a', 1);
57 variable_set('linkchecker_extract_from_audio', 1);
58 variable_set('linkchecker_extract_from_embed', 1);
59 variable_set('linkchecker_extract_from_iframe', 1);
60 variable_set('linkchecker_extract_from_img', 1);
61 variable_set('linkchecker_extract_from_object', 1);
62 variable_set('linkchecker_extract_from_video', 1);
65 <!-- UNSUPPORTED for link checking: -->
67 <a href="mailto:test@example.com">Send email</a>
68 <a href="javascript:foo()">Execute JavaScript</a>
70 <!-- SUPPORTED for link checking: -->
72 <!-- URL in HTML comment: http://example.com/test-if-url-filter-is-disabled -->
74 <!-- Relative URLs -->
75 <img src="test.png" alt="Test image 1" />
76 <img src="../foo1/test.png" alt="Test image 2" />
78 <a href="../foo1/bar1">../foo1/bar1</a>
79 <a href="./foo2/bar2">./foo2/bar2</a>
80 <a href="../foo3/../foo4/foo5">../foo3/../foo4/foo5</a>
81 <a href="./foo4/../foo5/foo6">./foo4/../foo5/foo6</a>
82 <a href="./foo4/./foo5/foo6">./foo4/./foo5/foo6</a>
83 <a href="./test/foo bar/is_valid-hack.test">./test/foo bar/is_valid-hack.test</a>
85 <!-- URL with uncommon chars that could potentially fail to extract. See http://drupal.org/node/465462. -->
86 <a href="http://www.lagrandeepicerie.fr/#e-boutique/Les_produits_du_moment,2/coffret_vins_doux_naturels,149">URL with uncommon chars</a>
87 <a href="http://example.com/foo bar/is_valid-hack.test">URL with space</a>
88 <a href="http://example.com/ajax.html#key1=value1&key2=value2">URL with ajax query params</a>
89 <a href="http://example.com/test.html#test">URL with standard anchor</a>
91 <!-- object tag: Embed SWF files -->
92 <object width="150" height="116"
93 type="application/x-shockwave-flash"
94 data="http://wetterservice.msn.de/phclip.swf?zip=60329&ort=Frankfurt">
95 <param name="movie" value="http://wetterservice.msn.de/phclip.swf?zip=60329&ort=Frankfurt" />
96 <img src="flash.png" width="150" height="116" alt="" /> <br />
97 No weather report visible? At <a href="http://www.msn.de/">MSN</a>
98 you are able to find the weather report missing here and the
99 Flash plugin can be found at <a href="http://www.adobe.com/">Adobe</a>.
102 <!-- object tag: Embed Quicktime Movies on HTML pages -->
103 <object width="420" height="282"
104 classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
105 codebase="http://www.apple.com/qtactivex/qtplugin.cab">
106 <param name="src" value="http://example.net/video/foo1.mov" />
107 <param name="href" value="http://example.net/video/foo2.mov" />
108 <param name="controller" value="true" />
109 <param name="autoplay" value="false" />
110 <param name="scale" value="aspect" />
111 <!--[if gte IE 7]> <!-->
112 <object type="video/quicktime" data="http://example.net/video/foo3.mov" width="420" height="282">
113 <param name="controller" value="true" />
114 <param name="autoplay" value="false" />
119 <!-- object tag: Play MP4 videos on HTML pages -->
120 <object data="http://example.org/video/foo1.mp4" type="video/mp4" width="420" height="288">
121 <param name="src" value="http://example.org/video/foo2.mp4" />
122 <param name="autoplay" value="false" />
123 <param name="autoStart" value="0" />
124 <a href="http://example.org/video/foo3.mp4">/video/foo3.mp4</a>
127 <!-- object tag: Play MP4 videos with Quicktime -->
128 <object width="420" height="282" codebase="http://www.apple.com/qtactivex/qtplugin.cab">
129 <param name="src" value="http://example.org/video/foo4.mp4" />
130 <param name="href" value="http://example.org/video/foo5.mp4" />
131 <param name="controller" value="true" />
132 <param name="autoplay" value="false" />
133 <param name="scale" value="aspect" />
134 <!--[if gte IE 7]> <!-->
135 <object type="video/quicktime" data="http://example.org/video/foo6.mp4" width="420" height="282">
136 <param name="controller" value="true" />
137 <param name="autoplay" value="false" />
142 <!-- object tag: Play flash videos on HTML pages -->
143 <object type="application/x-shockwave-flash" data="http://example.org/video/player1.swf" width="420" height="270">
144 <param name="movie" value="http://example.org/video/player2.swf" />
145 <param src="movie" value="http://example.org/video/player3.swf" />
146 <param name="flashvars" value="file=http://example.org/video/foo1.flv&width=420&height=270" />
149 <!-- Embed ActiveX control as objekt -->
150 <object width="267" height="175" classid="CLSID:05589FA1-C356-11CE-BF01-00AA0055595A">
151 <param name="filename" value="ritmo.mid">
154 <!-- Add inline frames -->
155 <iframe src="http://example.com/iframe/" name="ExampleIFrame" width="300" height="200">
156 <p>Your browser does not support inline frames.</p>
159 <!-- https://developer.mozilla.org/en/Using_audio_and_video_in_Firefox -->
161 <!-- http://www.theora.org/cortado/ -->
162 <video src="my_ogg_video.ogg" controls width="320" height="240">
163 <object type="application/x-java-applet" width="320" height="240">
164 <param name="archive" value="http://www.theora.org/cortado.jar">
165 <param name="code" value="com.fluendo.player.Cortado.class">
166 <param name="url" value="my_ogg_video.ogg">
167 <p>You need to install Java to play this file.</p>
171 <video src="video.ogv" controls>
172 <object data="flvplayer1.swf" type="application/x-shockwave-flash">
173 <param name="movie" value="flvplayer2.swf" />
178 <source src="http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.ogg" type="video/ogg">
179 <source src="http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.mov">
180 Your browser does not support the <code>video</code> element.
184 <source src="foo.ogg" type="video/ogg; codecs="dirac, speex"">
185 Your browser does not support the <code>video</code> element.
188 <video src="http://v2v.cc/~j/theora_testsuite/320x240.ogg" controls>
189 Your browser does not support the <code>video</code> element.
193 // Save folder names in variables for reuse.
194 $folder1 = $this->randomName(10);
195 $folder2 = $this->randomName(5);
198 $langcode = LANGUAGE_NONE;
200 $edit['title'] = $this->randomName(32);
201 $edit["body[$langcode][0][value]"] = $body;
202 $edit['path[alias]'] = $folder1 . '/' . $folder2;
203 $edit["body[$langcode][0][format]"] = 'full_html';
205 // Extract only full qualified URLs.
206 variable_set('linkchecker_check_links_types', 1);
208 // Verify path input field appears on add "Basic page" form.
209 $this->drupalGet('node/add/page');
210 // Verify path input is present.
211 $this->assertFieldByName('path[alias]', '', 'Path input field present on add Basic page form.');
214 $this->drupalPost('node/add/page', $edit, t('Save'));
215 $this->assertText(t('@type @title has been created.', array('@type' => 'Basic page', '@title' => $edit['title'])), 'Node was created.');
217 // Verify if the content links are extracted properly.
219 'http://www.lagrandeepicerie.fr/#e-boutique/Les_produits_du_moment,2/coffret_vins_doux_naturels,149',
220 'http://wetterservice.msn.de/phclip.swf?zip=60329&ort=Frankfurt',
221 'http://www.msn.de/',
222 'http://www.adobe.com/',
223 'http://www.apple.com/qtactivex/qtplugin.cab',
224 'http://example.net/video/foo1.mov',
225 'http://example.net/video/foo2.mov',
226 'http://example.net/video/foo3.mov',
227 'http://example.org/video/foo1.mp4',
228 'http://example.org/video/foo2.mp4',
229 'http://example.org/video/foo3.mp4',
230 'http://example.org/video/foo4.mp4',
231 'http://example.org/video/foo5.mp4',
232 'http://example.org/video/foo6.mp4',
233 'http://example.org/video/player1.swf',
234 'http://example.org/video/player2.swf',
235 'http://example.org/video/player3.swf',
236 'http://example.com/iframe/',
237 'http://www.theora.org/cortado.jar',
238 'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.ogg',
239 'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.mov',
240 'http://v2v.cc/~j/theora_testsuite/320x240.ogg',
241 'http://example.com/foo bar/is_valid-hack.test',
242 'http://example.com/ajax.html#key1=value1&key2=value2',
243 'http://example.com/test.html#test',
246 foreach ($urls_fqdn as $org_url => $check_url) {
247 $link = $this->getLinkCheckerLink($check_url);
249 $this->assertIdentical($link->url, $check_url, format_string('Absolute URL %org_url matches expected result %check_url.', array('%org_url' => $org_url, '%check_url' => $check_url)));
252 $this->fail(format_string('URL %check_url not found.', array('%check_url' => $check_url)));
256 // Check if the number of links is correct.
257 // - Verifies if all HTML tag regexes matched.
258 // - Verifies that the linkchecker filter blacklist works well.
259 $urls_in_database = $this->getLinkCheckerLinksCount();
260 $urls_expected_count = count($urls_fqdn);
261 $this->assertEqual($urls_in_database, $urls_expected_count, format_string('Found @urls_in_database URLs in database matches expected result of @urls_expected_count.', array('@urls_in_database' => $urls_in_database, '@urls_expected_count' => $urls_expected_count)));
263 // Extract all URLs including relative path.
264 variable_set('clean_url', 1);
265 variable_set('linkchecker_check_links_types', 0);
267 $node = $this->drupalGetNodeByTitle($edit['title']);
268 $this->assertTrue($node, 'Node found in database.');
269 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
270 $this->assertRaw(t('@type %title has been updated.', array('@type' => 'Basic page', '%title' => $edit['title'])));
272 // @todo Path alias seems not saved!???
273 // $this->assertIdentical($node->path, $edit['path'], format_string('URL alias "@node-path" matches path "@edit-path".', array('@node-path' => $node->path, '@edit-path' => $edit['path'])));
275 // Verify if the content links are extracted properly.
276 global $base_root, $base_path;
277 $urls_relative = array(
278 '../foo1/test.png' => $base_root . $base_path . 'foo1/test.png',
279 'test.png' => $base_root . $base_path . $folder1 . '/test.png',
280 '../foo1/bar1' => $base_root . $base_path . 'foo1/bar1',
281 './foo2/bar2' => $base_root . $base_path . $folder1 . '/foo2/bar2',
282 '../foo3/../foo4/foo5' => $base_root . $base_path . 'foo4/foo5',
283 './foo4/../foo5/foo6' => $base_root . $base_path . $folder1 . '/foo5/foo6',
284 './foo4/./foo5/foo6' => $base_root . $base_path . $folder1 . '/foo4/foo5/foo6',
285 './test/foo bar/is_valid-hack.test' => $base_root . $base_path . $folder1 . '/test/foo bar/is_valid-hack.test',
286 'flash.png' => $base_root . $base_path . $folder1 . '/flash.png',
287 'ritmo.mid' => $base_root . $base_path . $folder1 . '/ritmo.mid',
288 'my_ogg_video.ogg' => $base_root . $base_path . $folder1 . '/my_ogg_video.ogg',
289 'video.ogv' => $base_root . $base_path . $folder1 . '/video.ogv',
290 'flvplayer1.swf' => $base_root . $base_path . $folder1 . '/flvplayer1.swf',
291 'flvplayer2.swf' => $base_root . $base_path . $folder1 . '/flvplayer2.swf',
292 'foo.ogg' => $base_root . $base_path . $folder1 . '/foo.ogg',
294 $this->verbose(theme('item_list', array('items' => $urls_relative, 'title' => 'Verify if following relative URLs exists:')));
296 $links_debug = array();
297 $result = db_query('SELECT url FROM {linkchecker_link}');
298 foreach ($result as $row) {
299 $links_debug[] = $row->url;
301 $this->verbose(theme('item_list', array('items' => $links_debug, 'title' => 'Following URLs exists:')));
303 foreach ($urls_relative as $org_url => $check_url) {
304 $link = $this->getLinkCheckerLink($check_url);
306 $this->assertIdentical($link->url, $check_url, format_string('Relative URL %org_url matches expected result %check_url.', array('%org_url' => $org_url, '%check_url' => $check_url)));
309 $this->fail(format_string('URL %check_url not found.', array('%check_url' => $check_url)));
313 // Check if the number of links is correct.
314 $urls_in_database = $this->getLinkCheckerLinksCount();
315 $urls_expected_count = count($urls_fqdn + $urls_relative);
316 $this->assertEqual($urls_in_database, $urls_expected_count, format_string('Found @urls_in_database URLs in database matches expected result of @urls_expected_count.', array('@urls_in_database' => $urls_in_database, '@urls_expected_count' => $urls_expected_count)));
318 // Verify if link check has been enabled for normal URLs.
320 'http://www.lagrandeepicerie.fr/#e-boutique/Les_produits_du_moment,2/coffret_vins_doux_naturels,149',
321 'http://wetterservice.msn.de/phclip.swf?zip=60329&ort=Frankfurt',
322 'http://www.msn.de/',
323 'http://www.adobe.com/',
324 'http://www.apple.com/qtactivex/qtplugin.cab',
325 'http://www.theora.org/cortado.jar',
326 'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.ogg',
327 'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.mov',
328 'http://v2v.cc/~j/theora_testsuite/320x240.ogg',
329 $base_root . $base_path . 'foo1/test.png',
330 $base_root . $base_path . $folder1 . '/test.png',
331 $base_root . $base_path . 'foo1/bar1',
332 $base_root . $base_path . $folder1 . '/foo2/bar2',
333 $base_root . $base_path . 'foo4/foo5',
334 $base_root . $base_path . $folder1 . '/foo5/foo6',
335 $base_root . $base_path . $folder1 . '/foo4/foo5/foo6',
336 $base_root . $base_path . $folder1 . '/test/foo bar/is_valid-hack.test',
337 $base_root . $base_path . $folder1 . '/flash.png',
338 $base_root . $base_path . $folder1 . '/ritmo.mid',
339 $base_root . $base_path . $folder1 . '/my_ogg_video.ogg',
340 $base_root . $base_path . $folder1 . '/video.ogv',
341 $base_root . $base_path . $folder1 . '/flvplayer1.swf',
342 $base_root . $base_path . $folder1 . '/flvplayer2.swf',
343 $base_root . $base_path . $folder1 . '/foo.ogg',
346 foreach ($urls as $url) {
347 $this->assertTrue($this->getLinkcheckerLink($url)->status, format_string('Link check for %url is enabled.', array('%url' => $url)));
350 // Verify if link check has been disabled for example.com/net/org URLs.
351 $documentation_urls = array(
352 'http://example.net/video/foo1.mov',
353 'http://example.net/video/foo2.mov',
354 'http://example.net/video/foo3.mov',
355 'http://example.org/video/foo1.mp4',
356 'http://example.org/video/foo2.mp4',
357 'http://example.org/video/foo3.mp4',
358 'http://example.org/video/foo4.mp4',
359 'http://example.org/video/foo5.mp4',
360 'http://example.org/video/foo6.mp4',
361 'http://example.org/video/player1.swf',
362 'http://example.org/video/player2.swf',
363 'http://example.org/video/player3.swf',
364 'http://example.com/iframe/',
365 'http://example.com/foo bar/is_valid-hack.test',
366 'http://example.com/ajax.html#key1=value1&key2=value2',
367 'http://example.com/test.html#test',
370 foreach ($documentation_urls as $documentation_url) {
371 $this->assertFalse($this->getLinkcheckerLink($documentation_url)->status, format_string('Link check for %url is disabled.', array('%url' => $documentation_url)));
377 * Get linkchecker link by url.
380 * URL of the link to find.
385 function getLinkCheckerLink($url) {
386 return db_query('SELECT * FROM {linkchecker_link} WHERE urlhash = :urlhash', array(':urlhash' => drupal_hash_base64($url)))->fetchObject();
390 * Get the current number of links in linkchecker_links table.
392 function getLinkCheckerLinksCount() {
393 return db_query('SELECT COUNT(1) FROM {linkchecker_link}')->fetchField();