Updated all the contrib modules to their latest versions.
[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 <a href="http://example.com/test.html#test%20ABC">URL with standard anchor and space</a>
91 <a name="test ABC">Anchor with space</a>
92
93 <!-- object tag: Embed SWF files -->
94 <object width="150" height="116"
95   type="application/x-shockwave-flash"
96   data="http://wetterservice.msn.de/phclip.swf?zip=60329&ort=Frankfurt">
97     <param name="movie" value="http://wetterservice.msn.de/phclip.swf?zip=60329&ort=Frankfurt" />
98     <img src="flash.png" width="150" height="116" alt="" /> <br />
99       No weather report visible? At <a href="http://www.msn.de/">MSN</a>
100       you are able to find the weather report missing here and the
101       Flash plugin can be found at <a href="http://www.adobe.com/">Adobe</a>.
102 </object>
103
104 <!-- object tag: Embed Quicktime Movies on HTML pages -->
105 <object width="420" height="282"
106   classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
107   codebase="http://www.apple.com/qtactivex/qtplugin.cab">
108   <param name="src" value="http://example.net/video/foo1.mov" />
109   <param name="href" value="http://example.net/video/foo2.mov" />
110   <param name="controller" value="true" />
111   <param name="autoplay" value="false" />
112   <param name="scale" value="aspect" />
113   <!--[if gte IE 7]> <!-->
114   <object type="video/quicktime" data="http://example.net/video/foo3.mov" width="420" height="282">
115     <param name="controller" value="true" />
116     <param name="autoplay" value="false" />
117   </object>
118   <!--<![endif]-->
119 </object>
120
121 <!-- object tag: Play MP4 videos on HTML pages -->
122 <object data="http://example.org/video/foo1.mp4" type="video/mp4" width="420" height="288">
123   <param name="src" value="http://example.org/video/foo2.mp4" />
124   <param name="autoplay" value="false" />
125   <param name="autoStart" value="0" />
126   <a href="http://example.org/video/foo3.mp4">/video/foo3.mp4</a>
127 </object>
128
129 <!-- object tag: Play MP4 videos with Quicktime -->
130 <object width="420" height="282" codebase="http://www.apple.com/qtactivex/qtplugin.cab">
131   <param name="src" value="http://example.org/video/foo4.mp4" />
132   <param name="href" value="http://example.org/video/foo5.mp4" />
133   <param name="controller" value="true" />
134   <param name="autoplay" value="false" />
135   <param name="scale" value="aspect" />
136   <!--[if gte IE 7]> <!-->
137   <object type="video/quicktime" data="http://example.org/video/foo6.mp4" width="420" height="282">
138     <param name="controller" value="true" />
139     <param name="autoplay" value="false" />
140   </object>
141   <!--<![endif]-->
142 </object>
143
144 <!-- object tag: Play flash videos on HTML pages -->
145 <object type="application/x-shockwave-flash" data="http://example.org/video/player1.swf" width="420" height="270">
146     <param name="movie" value="http://example.org/video/player2.swf" />
147     <param src="movie" value="http://example.org/video/player3.swf" />
148     <param name="flashvars" value="file=http://example.org/video/foo1.flv&width=420&height=270" />
149 </object>
150
151 <!-- Embed ActiveX control as objekt -->
152 <object width="267" height="175" classid="CLSID:05589FA1-C356-11CE-BF01-00AA0055595A">
153   <param name="filename" value="ritmo.mid">
154 </object>
155
156 <!-- Add inline frames -->
157 <iframe src="http://example.com/iframe/" name="ExampleIFrame" width="300" height="200">
158   <p>Your browser does not support inline frames.</p>
159 </iframe>
160
161 <!-- https://developer.mozilla.org/en/Using_audio_and_video_in_Firefox -->
162
163 <!-- http://www.theora.org/cortado/ -->
164 <video src="my_ogg_video.ogg" controls width="320" height="240">
165   <object type="application/x-java-applet" width="320" height="240">
166     <param name="archive" value="http://www.theora.org/cortado.jar">
167     <param name="code" value="com.fluendo.player.Cortado.class">
168     <param name="url" value="my_ogg_video.ogg">
169     <p>You need to install Java to play this file.</p>
170   </object>
171 </video>
172
173 <video src="video.ogv" controls>
174   <object data="flvplayer1.swf" type="application/x-shockwave-flash">
175     <param name="movie" value="flvplayer2.swf" />
176   </object>
177 </video>
178
179 <video controls>
180   <source src="http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.ogg" type="video/ogg">
181   <source src="http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.mov">
182   Your browser does not support the <code>video</code> element.
183 </video>
184
185 <video controls>
186   <source src="foo.ogg" type="video/ogg; codecs=&quot;dirac, speex&quot;">
187   Your browser does not support the <code>video</code> element.
188 </video>
189
190 <video src="http://v2v.cc/~j/theora_testsuite/320x240.ogg" controls>
191   Your browser does not support the <code>video</code> element.
192 </video>
193 EOT;
194
195     // Save folder names in variables for reuse.
196     $folder1 = $this->randomName(10);
197     $folder2 = $this->randomName(5);
198
199     // Fill node array.
200     $langcode = LANGUAGE_NONE;
201     $edit = array();
202     $edit['title'] = $this->randomName(32);
203     $edit["body[$langcode][0][value]"] = $body;
204     $edit['path[alias]'] = $folder1 . '/' . $folder2;
205     $edit["body[$langcode][0][format]"] = 'full_html';
206
207     // Extract only full qualified URLs.
208     variable_set('linkchecker_check_links_types', 1);
209
210     // Verify path input field appears on add "Basic page" form.
211     $this->drupalGet('node/add/page');
212     // Verify path input is present.
213     $this->assertFieldByName('path[alias]', '', 'Path input field present on add Basic page form.');
214
215     // Save node.
216     $this->drupalPost('node/add/page', $edit, t('Save'));
217     $this->assertText(t('@type @title has been created.', array('@type' => 'Basic page', '@title' => $edit['title'])), 'Node was created.');
218
219     // Verify if the content links are extracted properly.
220     $urls_fqdn = array(
221       'http://www.lagrandeepicerie.fr/#e-boutique/Les_produits_du_moment,2/coffret_vins_doux_naturels,149',
222       'http://wetterservice.msn.de/phclip.swf?zip=60329&ort=Frankfurt',
223       'http://www.msn.de/',
224       'http://www.adobe.com/',
225       'http://www.apple.com/qtactivex/qtplugin.cab',
226       'http://example.net/video/foo1.mov',
227       'http://example.net/video/foo2.mov',
228       'http://example.net/video/foo3.mov',
229       'http://example.org/video/foo1.mp4',
230       'http://example.org/video/foo2.mp4',
231       'http://example.org/video/foo3.mp4',
232       'http://example.org/video/foo4.mp4',
233       'http://example.org/video/foo5.mp4',
234       'http://example.org/video/foo6.mp4',
235       'http://example.org/video/player1.swf',
236       'http://example.org/video/player2.swf',
237       'http://example.org/video/player3.swf',
238       'http://example.com/iframe/',
239       'http://www.theora.org/cortado.jar',
240       'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.ogg',
241       'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.mov',
242       'http://v2v.cc/~j/theora_testsuite/320x240.ogg',
243       'http://example.com/foo bar/is_valid-hack.test',
244       'http://example.com/ajax.html#key1=value1&key2=value2',
245       'http://example.com/test.html#test',
246       'http://example.com/test.html#test%20ABC',
247     );
248
249     foreach ($urls_fqdn as $org_url => $check_url) {
250       $link = $this->getLinkCheckerLink($check_url);
251       if ($link) {
252         $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)));
253       }
254       else {
255         $this->fail(format_string('URL %check_url not found.', array('%check_url' => $check_url)));
256       }
257     }
258
259     // Check if the number of links is correct.
260     // - Verifies if all HTML tag regexes matched.
261     // - Verifies that the linkchecker filter blacklist works well.
262     $urls_in_database = $this->getLinkCheckerLinksCount();
263     $urls_expected_count = count($urls_fqdn);
264     $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)));
265
266     // Extract all URLs including relative path.
267     variable_set('clean_url', 1);
268     variable_set('linkchecker_check_links_types', 0);
269
270     $node = $this->drupalGetNodeByTitle($edit['title']);
271     $this->assertTrue($node, 'Node found in database.');
272     $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
273     $this->assertRaw(t('@type %title has been updated.', array('@type' => 'Basic page', '%title' => $edit['title'])));
274
275     // @todo Path alias seems not saved!???
276     // $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'])));
277
278     // Verify if the content links are extracted properly.
279     global $base_root, $base_path;
280     $urls_relative = array(
281       '../foo1/test.png' => $base_root . $base_path . 'foo1/test.png',
282       'test.png' => $base_root . $base_path . $folder1 . '/test.png',
283       '../foo1/bar1' => $base_root . $base_path . 'foo1/bar1',
284       './foo2/bar2' => $base_root . $base_path . $folder1 . '/foo2/bar2',
285       '../foo3/../foo4/foo5' => $base_root . $base_path . 'foo4/foo5',
286       './foo4/../foo5/foo6' => $base_root . $base_path . $folder1 . '/foo5/foo6',
287       './foo4/./foo5/foo6' => $base_root . $base_path . $folder1 . '/foo4/foo5/foo6',
288       './test/foo bar/is_valid-hack.test' => $base_root . $base_path . $folder1 . '/test/foo bar/is_valid-hack.test',
289       'flash.png' => $base_root . $base_path . $folder1 . '/flash.png',
290       'ritmo.mid' => $base_root . $base_path . $folder1 . '/ritmo.mid',
291       'my_ogg_video.ogg' => $base_root . $base_path . $folder1 . '/my_ogg_video.ogg',
292       'video.ogv' => $base_root . $base_path . $folder1 . '/video.ogv',
293       'flvplayer1.swf' => $base_root . $base_path . $folder1 . '/flvplayer1.swf',
294       'flvplayer2.swf' => $base_root . $base_path . $folder1 . '/flvplayer2.swf',
295       'foo.ogg' => $base_root . $base_path . $folder1 . '/foo.ogg',
296     );
297     $this->verbose(theme('item_list', array('items' => $urls_relative, 'title' => 'Verify if following relative URLs exists:')));
298
299     $links_debug = array();
300     $result = db_query('SELECT url FROM {linkchecker_link}');
301     foreach ($result as $row) {
302       $links_debug[] = $row->url;
303     }
304     $this->verbose(theme('item_list', array('items' => $links_debug, 'title' => 'Following URLs exists:')));
305
306     foreach ($urls_relative as $org_url => $check_url) {
307       $link = $this->getLinkCheckerLink($check_url);
308       if ($link) {
309         $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)));
310       }
311       else {
312         $this->fail(format_string('URL %check_url not found.', array('%check_url' => $check_url)));
313       }
314     }
315
316     // Check if the number of links is correct.
317     $urls_in_database = $this->getLinkCheckerLinksCount();
318     $urls_expected_count = count($urls_fqdn + $urls_relative);
319     $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)));
320
321     // Verify if link check has been enabled for normal URLs.
322     $urls = array(
323       'http://www.lagrandeepicerie.fr/#e-boutique/Les_produits_du_moment,2/coffret_vins_doux_naturels,149',
324       'http://wetterservice.msn.de/phclip.swf?zip=60329&ort=Frankfurt',
325       'http://www.msn.de/',
326       'http://www.adobe.com/',
327       'http://www.apple.com/qtactivex/qtplugin.cab',
328       'http://www.theora.org/cortado.jar',
329       'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.ogg',
330       'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.mov',
331       'http://v2v.cc/~j/theora_testsuite/320x240.ogg',
332       $base_root . $base_path . 'foo1/test.png',
333       $base_root . $base_path . $folder1 . '/test.png',
334       $base_root . $base_path . 'foo1/bar1',
335       $base_root . $base_path . $folder1 . '/foo2/bar2',
336       $base_root . $base_path . 'foo4/foo5',
337       $base_root . $base_path . $folder1 . '/foo5/foo6',
338       $base_root . $base_path . $folder1 . '/foo4/foo5/foo6',
339       $base_root . $base_path . $folder1 . '/test/foo bar/is_valid-hack.test',
340       $base_root . $base_path . $folder1 . '/flash.png',
341       $base_root . $base_path . $folder1 . '/ritmo.mid',
342       $base_root . $base_path . $folder1 . '/my_ogg_video.ogg',
343       $base_root . $base_path . $folder1 . '/video.ogv',
344       $base_root . $base_path . $folder1 . '/flvplayer1.swf',
345       $base_root . $base_path . $folder1 . '/flvplayer2.swf',
346       $base_root . $base_path . $folder1 . '/foo.ogg',
347     );
348
349     foreach ($urls as $url) {
350       $this->assertTrue($this->getLinkcheckerLink($url)->status, format_string('Link check for %url is enabled.', array('%url' => $url)));
351     }
352
353     // Verify if link check has been disabled for example.com/net/org URLs.
354     $documentation_urls = array(
355       'http://example.net/video/foo1.mov',
356       'http://example.net/video/foo2.mov',
357       'http://example.net/video/foo3.mov',
358       'http://example.org/video/foo1.mp4',
359       'http://example.org/video/foo2.mp4',
360       'http://example.org/video/foo3.mp4',
361       'http://example.org/video/foo4.mp4',
362       'http://example.org/video/foo5.mp4',
363       'http://example.org/video/foo6.mp4',
364       'http://example.org/video/player1.swf',
365       'http://example.org/video/player2.swf',
366       'http://example.org/video/player3.swf',
367       'http://example.com/iframe/',
368       'http://example.com/foo bar/is_valid-hack.test',
369       'http://example.com/ajax.html#key1=value1&key2=value2',
370       'http://example.com/test.html#test',
371       'http://example.com/test.html#test%20ABC',
372     );
373
374     foreach ($documentation_urls as $documentation_url) {
375       $this->assertFalse($this->getLinkcheckerLink($documentation_url)->status, format_string('Link check for %url is disabled.', array('%url' => $documentation_url)));
376     }
377
378   }
379
380   /**
381    * Get linkchecker link by url.
382    *
383    * @param string $url
384    *   URL of the link to find.
385    *
386    * @return object
387    *   The link object.
388    */
389   function getLinkCheckerLink($url) {
390     return db_query('SELECT * FROM {linkchecker_link} WHERE urlhash = :urlhash', array(':urlhash' => drupal_hash_base64($url)))->fetchObject();
391   }
392
393   /**
394    * Get the current number of links in linkchecker_links table.
395    */
396   function getLinkCheckerLinksCount() {
397     return db_query('SELECT COUNT(1) FROM {linkchecker_link}')->fetchField();
398   }
399 }