You are here

public function AggregatorRenderingTest::testBlockLinks in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/aggregator/src/Tests/AggregatorRenderingTest.php \Drupal\aggregator\Tests\AggregatorRenderingTest::testBlockLinks()

Adds a feed block to the page and checks its links.

File

core/modules/aggregator/src/Tests/AggregatorRenderingTest.php, line 35
Contains \Drupal\aggregator\Tests\AggregatorRenderingTest.

Class

AggregatorRenderingTest
Tests display of aggregator items on the page.

Namespace

Drupal\aggregator\Tests

Code

public function testBlockLinks() {

  // Create feed.
  $this
    ->createSampleNodes();
  $feed = $this
    ->createFeed();
  $this
    ->updateFeedItems($feed, $this
    ->getDefaultFeedItemCount());

  // Need admin user to be able to access block admin.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer blocks',
    'access administration pages',
    'administer news feeds',
    'access news feeds',
  ));
  $this
    ->drupalLogin($admin_user);
  $block = $this
    ->drupalPlaceBlock("aggregator_feed_block", array(
    'label' => 'feed-' . $feed
      ->label(),
  ));

  // Configure the feed that should be displayed.
  $block
    ->getPlugin()
    ->setConfigurationValue('feed', $feed
    ->id());
  $block
    ->getPlugin()
    ->setConfigurationValue('block_count', 2);
  $block
    ->save();

  // Confirm that the block is now being displayed on pages.
  $this
    ->drupalGet('test-page');
  $this
    ->assertText($block
    ->label(), 'Feed block is displayed on the page.');

  // Confirm items appear as links.
  $items = $this->container
    ->get('entity.manager')
    ->getStorage('aggregator_item')
    ->loadByFeed($feed
    ->id(), 1);
  $links = $this
    ->xpath('//a[@href = :href]', array(
    ':href' => reset($items)
      ->getLink(),
  ));
  $this
    ->assert(isset($links[0]), 'Item link found.');

  // Find the expected read_more link.
  $href = $feed
    ->url();
  $links = $this
    ->xpath('//a[@href = :href]', array(
    ':href' => $href,
  ));
  $this
    ->assert(isset($links[0]), format_string('Link to href %href found.', array(
    '%href' => $href,
  )));
  $cache_tags_header = $this
    ->drupalGetHeader('X-Drupal-Cache-Tags');
  $cache_tags = explode(' ', $cache_tags_header);
  $this
    ->assertTrue(in_array('aggregator_feed:' . $feed
    ->id(), $cache_tags));

  // Visit that page.
  $this
    ->drupalGet($feed
    ->urlInfo()
    ->getInternalPath());
  $correct_titles = $this
    ->xpath('//h1[normalize-space(text())=:title]', array(
    ':title' => $feed
      ->label(),
  ));
  $this
    ->assertFalse(empty($correct_titles), 'Aggregator feed page is available and has the correct title.');
  $cache_tags = explode(' ', $this
    ->drupalGetHeader('X-Drupal-Cache-Tags'));
  $this
    ->assertTrue(in_array('aggregator_feed:' . $feed
    ->id(), $cache_tags));
  $this
    ->assertTrue(in_array('aggregator_feed_view', $cache_tags));
  $this
    ->assertTrue(in_array('aggregator_item_view', $cache_tags));

  // Set the number of news items to 0 to test that the block does not show
  // up.
  $block
    ->getPlugin()
    ->setConfigurationValue('block_count', 0);
  $block
    ->save();

  // Check that the block is no longer displayed.
  $this
    ->drupalGet('test-page');
  $this
    ->assertNoText($block
    ->label(), 'Feed block is not displayed on the page when number of items is set to 0.');
}