Further modules included.
[yaffs-website] / web / modules / contrib / linkchecker / src / Tests / LinkCheckerInterfaceTest.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 case for interface tests.
10  *
11  * @group Link checker
12  */
13 class LinkCheckerInterfaceTest extends WebTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'block',
22     'linkchecker',
23     'path',
24   ];
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31
32     $full_html_format = filter_format_load('full_html');
33     $permissions = [
34       // Block permissions.
35       'administer blocks',
36       // Comment permissions.
37       'administer comments',
38       'access comments',
39       'post comments',
40       'skip comment approval',
41       'edit own comments',
42       // Node permissions.
43       'create page content',
44       'edit own page content',
45       // Path aliase permissions.
46       'administer url aliases',
47       'create url aliases',
48       // Content filter permissions.
49       filter_permission_name($full_html_format),
50     ];
51
52     // User to set up google_analytics.
53     $this->admin_user = $this->drupalCreateUser($permissions);
54     $this->drupalLogin($this->admin_user);
55   }
56
57   /**
58    * Test the interface functionality.
59    */
60   public function testLinkCheckerCreateNodeWithBrokenLinks() {
61     // Enable all node type page for link extraction.
62     variable_set('linkchecker_scan_node_page', TRUE);
63
64     // Core enables the URL filter for "Full HTML" by default.
65     // -> Blacklist / Disable URL filter for testing.
66     variable_set('linkchecker_filter_blacklist', array('filter_url' => 'filter_url'));
67
68     // Extract from all link checker supported HTML tags.
69     variable_set('linkchecker_extract_from_a', 1);
70     variable_set('linkchecker_extract_from_audio', 1);
71     variable_set('linkchecker_extract_from_embed', 1);
72     variable_set('linkchecker_extract_from_iframe', 1);
73     variable_set('linkchecker_extract_from_img', 1);
74     variable_set('linkchecker_extract_from_object', 1);
75     variable_set('linkchecker_extract_from_video', 1);
76
77     $url1 = 'http://example.com/node/broken/link';
78     $body = 'Lorem ipsum dolor sit amet <a href="' . $url1 . '">broken link</a> sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat';
79
80     // Save folder names in variables for reuse.
81     $folder1 = $this->randomName(10);
82     $folder2 = $this->randomName(5);
83
84     // Fill node array.
85     $langcode = LANGUAGE_NONE;
86     $edit = array();
87     $edit['title'] = $this->randomName(32);
88     $edit["body[$langcode][0][value]"] = $body;
89     $edit['path[alias]'] = $folder1 . '/' . $folder2;
90     $edit["body[$langcode][0][format]"] = 'full_html';
91
92     // Extract only full qualified URLs.
93     variable_set('linkchecker_check_links_types', 1);
94
95     // Verify path input field appears on add "Basic page" form.
96     $this->drupalGet('node/add/page');
97     // Verify path input is present.
98     $this->assertFieldByName('path[alias]', '', 'Path input field present on add Basic page form.');
99
100     // Save node.
101     $this->drupalPost('node/add/page', $edit, t('Save'));
102     $this->assertText(t('@type @title has been created.', array('@type' => 'Basic page', '@title' => $edit['title'])), 'Node was created.');
103
104     $node = $this->drupalGetNodeByTitle($edit['title']);
105     $this->assertTrue($node, 'Node found in database.');
106
107     // Verify if the content link is extracted properly.
108     $link = $this->getLinkCheckerLink($url1);
109     if ($link) {
110       $this->assertIdentical($link->url, $url1, format_string('URL %url found.', array('%url' => $url1)));
111     }
112     else {
113       $this->fail(format_string('URL %url not found.', array('%url' => $url1)));
114     }
115
116     // Set link as failed once.
117     $fail_count = 1;
118     $status = '301';
119     $this->setLinkAsBroken($url1, $status, $fail_count);
120     $this->drupalGet('node/' . $node->nid . '/edit');
121     $this->assertRaw(format_plural($fail_count, 'Link check of <a href="@url">@url</a> failed once (status code: @code).', 'Link check of <a href="@url">@url</a> failed @count times (status code: @code).', array('@url' => $url1, '@code' => $status)), 'Link check failed once found.');
122
123     // Set link as failed multiple times.
124     $fail_count = 4;
125     $status = '404';
126     $this->setLinkAsBroken($url1, $status, $fail_count);
127     $this->drupalGet('node/' . $node->nid . '/edit');
128     $this->assertRaw(format_plural($fail_count, 'Link check of <a href="@url">@url</a> failed once (status code: @code).', 'Link check of <a href="@url">@url</a> failed @count times (status code: @code).', array('@url' => $url1, '@code' => $status)), 'Link check failed multiple times found.');
129   }
130
131   public function testLinkCheckerCreateBlockWithBrokenLinks() {
132     // Enable all blocks for link extraction.
133     variable_set('linkchecker_scan_blocks', 1);
134
135     // Confirm that the add block link appears on block overview pages.
136     $this->drupalGet('admin/structure/block');
137     $this->assertRaw(l(t('Add block'), 'admin/structure/block/add'), 'Add block link is present on block overview page for default theme.');
138
139     $url1 = 'http://example.com/block/broken/link';
140     $body = 'Lorem ipsum dolor sit amet <a href="' . $url1 . '">broken link</a> sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat';
141
142     // Add a new custom block by filling out the input form on the admin/structure/block/add page.
143     $custom_block = array();
144     $custom_block['info'] = $this->randomName(8);
145     $custom_block['title'] = $this->randomName(8);
146     $custom_block['body[value]'] = $body;
147     $custom_block['body[format]'] = 'full_html';
148     $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
149
150     // Confirm that the custom block has been created, and then query the created bid.
151     $this->assertText(t('The block has been created.'), 'Custom block successfully created.');
152     $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
153
154     // Check to see if the custom block was created by checking that it's in the database.
155     $this->assertNotNull($bid, 'Custom block found in database');
156
157     // Verify if the content link is extracted properly.
158     $link = $this->getLinkCheckerLink($url1);
159     if ($link) {
160       $this->assertIdentical($link->url, $url1, format_string('URL %url found.', array('%url' => $url1)));
161     }
162     else {
163       $this->fail(format_string('URL %url not found.', array('%url' => $url1)));
164     }
165
166     // Set link as failed once.
167     $fail_count = 1;
168     $status = '301';
169     $this->setLinkAsBroken($url1, $status, $fail_count);
170     $this->drupalGet('admin/structure/block/manage/block/' . $bid . '/configure');
171     $this->assertRaw(format_plural($fail_count, 'Link check of <a href="@url">@url</a> failed once (status code: @code).', 'Link check of <a href="@url">@url</a> failed @count times (status code: @code).', array('@url' => $url1, '@code' => $status)), 'Link check failed once found.');
172
173     // Set link as failed multiple times.
174     $fail_count = 4;
175     $status = '404';
176     $this->setLinkAsBroken($url1, $status, $fail_count);
177     $this->drupalGet('admin/structure/block/manage/block/' . $bid . '/configure');
178     $this->assertRaw(format_plural($fail_count, 'Link check of <a href="@url">@url</a> failed once (status code: @code).', 'Link check of <a href="@url">@url</a> failed @count times (status code: @code).', array('@url' => $url1, '@code' => $status)), 'Link check failed multiple times found.');
179   }
180
181   /**
182    * Set an URL as broken.
183    *
184    * @param string $url
185    *   URL of the link to find.
186    * @param string $status
187    *   A fake HTTP code for testing.
188    */
189   function setLinkAsBroken($url = NULL, $status = '404', $fail_count = 0) {
190     db_update('linkchecker_link')
191     ->condition('urlhash', drupal_hash_base64($url))
192     ->fields(array(
193       'code' => $status,
194       'error' => 'Not available (test running)',
195       'fail_count' => $fail_count,
196       'last_checked' => time(),
197       'status' => 1,
198     ))
199     ->execute();
200   }
201
202   /**
203    * Get linkchecker link by url.
204    *
205    * @param string $url
206    *   URL of the link to find.
207    *
208    * @return object
209    *   The link object.
210    */
211   function getLinkCheckerLink($url) {
212     return db_query('SELECT * FROM {linkchecker_link} WHERE urlhash = :urlhash', array(':urlhash' => drupal_hash_base64($url)))->fetchObject();
213   }
214 }