You are here

public function AggregatorRenderingTest::testBlockLinks in Drupal 9

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

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

File

core/modules/aggregator/tests/src/Functional/AggregatorRenderingTest.php, line 35

Class

AggregatorRenderingTest
Tests display of aggregator items on the page.

Namespace

Drupal\Tests\aggregator\Functional

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([
    'administer blocks',
    'access administration pages',
    'administer news feeds',
    'access news feeds',
  ]);
  $this
    ->drupalLogin($admin_user);
  $block = $this
    ->drupalPlaceBlock("aggregator_feed_block", [
    '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
    ->assertSession()
    ->pageTextContains($block
    ->label());

  // Confirm items appear as links.
  $items = $this->container
    ->get('entity_type.manager')
    ->getStorage('aggregator_item')
    ->loadByFeed($feed
    ->id(), 1);
  $this
    ->assertSession()
    ->linkByHrefExists(reset($items)
    ->getLink());

  // Find the expected read_more link.
  $this
    ->assertSession()
    ->linkByHrefExists($feed
    ->toUrl()
    ->toString());
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'aggregator_feed:' . $feed
    ->id());

  // Visit that page.
  $this
    ->drupalGet($feed
    ->toUrl()
    ->getInternalPath());

  // Verify that aggregator feed page is available and has the correct title.
  $this
    ->assertSession()
    ->elementTextContains('xpath', '//h1', $feed
    ->label());
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'aggregator_feed:' . $feed
    ->id());
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'aggregator_feed_view');
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'aggregator_item_view');

  // 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
    ->assertSession()
    ->pageTextNotContains($block
    ->label());
}