3 namespace Drupal\linkchecker\Tests;
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\simpletest\WebTestBase;
9 * Test case for interface tests.
13 class LinkCheckerInterfaceTest extends WebTestBase {
20 public static $modules = [
29 protected function setUp() {
32 $full_html_format = filter_format_load('full_html');
36 // Comment permissions.
37 'administer comments',
40 'skip comment approval',
43 'create page content',
44 'edit own page content',
45 // Path aliase permissions.
46 'administer url aliases',
48 // Content filter permissions.
49 filter_permission_name($full_html_format),
52 // User to set up google_analytics.
53 $this->admin_user = $this->drupalCreateUser($permissions);
54 $this->drupalLogin($this->admin_user);
58 * Test the interface functionality.
60 public function testLinkCheckerCreateNodeWithBrokenLinks() {
61 // Enable all node type page for link extraction.
62 variable_set('linkchecker_scan_node_page', TRUE);
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'));
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);
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';
80 // Save folder names in variables for reuse.
81 $folder1 = $this->randomName(10);
82 $folder2 = $this->randomName(5);
85 $langcode = LANGUAGE_NONE;
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';
92 // Extract only full qualified URLs.
93 variable_set('linkchecker_check_links_types', 1);
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.');
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.');
104 $node = $this->drupalGetNodeByTitle($edit['title']);
105 $this->assertTrue($node, 'Node found in database.');
107 // Verify if the content link is extracted properly.
108 $link = $this->getLinkCheckerLink($url1);
110 $this->assertIdentical($link->url, $url1, format_string('URL %url found.', array('%url' => $url1)));
113 $this->fail(format_string('URL %url not found.', array('%url' => $url1)));
116 // Set link as failed once.
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.');
123 // Set link as failed multiple times.
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.');
131 public function testLinkCheckerCreateBlockWithBrokenLinks() {
132 // Enable all blocks for link extraction.
133 variable_set('linkchecker_scan_blocks', 1);
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.');
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';
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'));
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();
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');
157 // Verify if the content link is extracted properly.
158 $link = $this->getLinkCheckerLink($url1);
160 $this->assertIdentical($link->url, $url1, format_string('URL %url found.', array('%url' => $url1)));
163 $this->fail(format_string('URL %url not found.', array('%url' => $url1)));
166 // Set link as failed once.
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.');
173 // Set link as failed multiple times.
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.');
182 * Set an URL as broken.
185 * URL of the link to find.
186 * @param string $status
187 * A fake HTTP code for testing.
189 function setLinkAsBroken($url = NULL, $status = '404', $fail_count = 0) {
190 db_update('linkchecker_link')
191 ->condition('urlhash', drupal_hash_base64($url))
194 'error' => 'Not available (test running)',
195 'fail_count' => $fail_count,
196 'last_checked' => time(),
203 * Get linkchecker link by url.
206 * URL of the link to find.
211 function getLinkCheckerLink($url) {
212 return db_query('SELECT * FROM {linkchecker_link} WHERE urlhash = :urlhash', array(':urlhash' => drupal_hash_base64($url)))->fetchObject();