Further modules included.
[yaffs-website] / web / modules / contrib / linkchecker / src / Tests / LinkCheckerLinkExtractionTest.php
1 <?php
2
3 namespace Drupal\linkchecker\Tests;
4
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Test Link checker module link extraction functionality.
10  *
11  * @group Link checker
12  */
13 class LinkCheckerLinkExtractionTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'linkchecker',
22     'path',
23   ];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30
31     $full_html_format = filter_format_load('full_html');
32     $permissions = [
33       'create page content',
34       'edit own page content',
35       'administer url aliases',
36       'create url aliases',
37       filter_permission_name($full_html_format),
38     ];
39
40     // User to set up google_analytics.
41     $this->admin_user = $this->drupalCreateUser($permissions);
42     $this->drupalLogin($this->admin_user);
43   }
44
45   public function testLinkCheckerCreateNodeWithLinks() {
46
47     // Enable all node type page for link extraction.
48     variable_set('linkchecker_scan_node_page', TRUE);
49     variable_set('linkchecker_scan_blocks', 1);
50
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'));
54
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);
63
64     $body = <<<EOT
65 <!-- UNSUPPORTED for link checking: -->
66
67 <a href="mailto:test@example.com">Send email</a>
68 <a href="javascript:foo()">Execute JavaScript</a>
69
70 <!-- SUPPORTED for link checking: -->
71
72 <!-- URL in HTML comment: http://example.com/test-if-url-filter-is-disabled -->
73
74 <!-- Relative URLs -->
75 <img src="test.png" alt="Test image 1" />
76 <img src="../foo1/test.png" alt="Test image 2" />
77
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>
84
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>
90
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>.
100 </object>
101
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" />
115   </object>
116   <!--<![endif]-->
117 </object>
118
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>
125 </object>
126
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" />
138   </object>
139   <!--<![endif]-->
140 </object>
141
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" />
147 </object>
148
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">
152 </object>
153
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>
157 </iframe>
158
159 <!-- https://developer.mozilla.org/en/Using_audio_and_video_in_Firefox -->
160
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>
168   </object>
169 </video>
170
171 <video src="video.ogv" controls>
172   <object data="flvplayer1.swf" type="application/x-shockwave-flash">
173     <param name="movie" value="flvplayer2.swf" />
174   </object>
175 </video>
176
177 <video controls>
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.
181 </video>
182
183 <video controls>
184   <source src="foo.ogg" type="video/ogg; codecs=&quot;dirac, speex&quot;">
185   Your browser does not support the <code>video</code> element.
186 </video>
187
188 <video src="http://v2v.cc/~j/theora_testsuite/320x240.ogg" controls>
189   Your browser does not support the <code>video</code> element.
190 </video>
191 EOT;
192
193     // Save folder names in variables for reuse.
194     $folder1 = $this->randomName(10);
195     $folder2 = $this->randomName(5);
196
197     // Fill node array.
198     $langcode = LANGUAGE_NONE;
199     $edit = array();
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';
204
205     // Extract only full qualified URLs.
206     variable_set('linkchecker_check_links_types', 1);
207
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.');
212
213     // Save node.
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.');
216
217     // Verify if the content links are extracted properly.
218     $urls_fqdn = array(
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',
244     );
245
246     foreach ($urls_fqdn as $org_url => $check_url) {
247       $link = $this->getLinkCheckerLink($check_url);
248       if ($link) {
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)));
250       }
251       else {
252         $this->fail(format_string('URL %check_url not found.', array('%check_url' => $check_url)));
253       }
254     }
255
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)));
262
263     // Extract all URLs including relative path.
264     variable_set('clean_url', 1);
265     variable_set('linkchecker_check_links_types', 0);
266
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'])));
271
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'])));
274
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',
293     );
294     $this->verbose(theme('item_list', array('items' => $urls_relative, 'title' => 'Verify if following relative URLs exists:')));
295
296     $links_debug = array();
297     $result = db_query('SELECT url FROM {linkchecker_link}');
298     foreach ($result as $row) {
299       $links_debug[] = $row->url;
300     }
301     $this->verbose(theme('item_list', array('items' => $links_debug, 'title' => 'Following URLs exists:')));
302
303     foreach ($urls_relative as $org_url => $check_url) {
304       $link = $this->getLinkCheckerLink($check_url);
305       if ($link) {
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)));
307       }
308       else {
309         $this->fail(format_string('URL %check_url not found.', array('%check_url' => $check_url)));
310       }
311     }
312
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)));
317
318     // Verify if link check has been enabled for normal URLs.
319     $urls = array(
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',
344     );
345
346     foreach ($urls as $url) {
347       $this->assertTrue($this->getLinkcheckerLink($url)->status, format_string('Link check for %url is enabled.', array('%url' => $url)));
348     }
349
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',
368     );
369
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)));
372     }
373
374   }
375
376   /**
377    * Get linkchecker link by url.
378    *
379    * @param string $url
380    *   URL of the link to find.
381    *
382    * @return object
383    *   The link object.
384    */
385   function getLinkCheckerLink($url) {
386     return db_query('SELECT * FROM {linkchecker_link} WHERE urlhash = :urlhash', array(':urlhash' => drupal_hash_base64($url)))->fetchObject();
387   }
388
389   /**
390    * Get the current number of links in linkchecker_links table.
391    */
392   function getLinkCheckerLinksCount() {
393     return db_query('SELECT COUNT(1) FROM {linkchecker_link}')->fetchField();
394   }
395 }