You are here

public function LinkCheckerInterfaceTest::testLinkCheckerCreateBlockWithBrokenLinks in Link checker 7

File

./linkchecker.test, line 503
Test file for Link checker module.

Class

LinkCheckerInterfaceTest

Code

public function testLinkCheckerCreateBlockWithBrokenLinks() {

  // Enable all blocks for link extraction.
  variable_set('linkchecker_scan_blocks', 1);

  // Confirm that the add block link appears on block overview pages.
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->assertRaw(l(t('Add block'), 'admin/structure/block/add'), 'Add block link is present on block overview page for default theme.');
  $url1 = 'http://example.com/block/broken/link';
  $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';

  // Add a new custom block by filling out the input form on the admin/structure/block/add page.
  $custom_block = array();
  $custom_block['info'] = $this
    ->randomName(8);
  $custom_block['title'] = $this
    ->randomName(8);
  $custom_block['body[value]'] = $body;
  $custom_block['body[format]'] = 'full_html';
  $this
    ->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));

  // Confirm that the custom block has been created, and then query the created bid.
  $this
    ->assertText(t('The block has been created.'), 'Custom block successfully created.');
  $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(
    ':info' => $custom_block['info'],
  ))
    ->fetchField();

  // Check to see if the custom block was created by checking that it's in the database.
  $this
    ->assertNotNull($bid, 'Custom block found in database');

  // Verify if the content link is extracted properly.
  $link = $this
    ->getLinkCheckerLink($url1);
  if ($link) {
    $this
      ->assertIdentical($link->url, $url1, format_string('URL %url found.', array(
      '%url' => $url1,
    )));
  }
  else {
    $this
      ->fail(format_string('URL %url not found.', array(
      '%url' => $url1,
    )));
  }

  // Set link as failed once.
  $fail_count = 1;
  $status = '301';
  $this
    ->setLinkAsBroken($url1, $status, $fail_count);
  $this
    ->drupalGet('admin/structure/block/manage/block/' . $bid . '/configure');
  $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.');

  // Set link as failed multiple times.
  $fail_count = 4;
  $status = '404';
  $this
    ->setLinkAsBroken($url1, $status, $fail_count);
  $this
    ->drupalGet('admin/structure/block/manage/block/' . $bid . '/configure');
  $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.');
}