AggregatorRenderingTest.php 6.17 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\aggregator\Functional;
4

5
use Drupal\Component\Render\FormattableMarkup;
6
use Drupal\views\Entity\View;
7

8
/**
9 10 11
 * Tests display of aggregator items on the page.
 *
 * @group aggregator
12
 */
13
class AggregatorRenderingTest extends AggregatorTestBase {
14 15

  /**
16
   * Modules to install.
17 18 19
   *
   * @var array
   */
20
  public static $modules = ['block', 'test_page_test'];
21

22 23 24 25 26 27
  protected function setUp() {
    parent::setUp();

    $this->drupalPlaceBlock('page_title_block');
  }

28
  /**
29
   * Adds a feed block to the page and checks its links.
30 31 32 33 34 35 36 37
   */
  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.
38
    $admin_user = $this->drupalCreateUser([
39 40 41 42
      'administer blocks',
      'access administration pages',
      'administer news feeds',
      'access news feeds',
43
    ]);
44
    $this->drupalLogin($admin_user);
45

46
    $block = $this->drupalPlaceBlock("aggregator_feed_block", ['label' => 'feed-' . $feed->label()]);
47 48 49 50 51

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

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

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

62
    // Find the expected read_more link.
63
    $href = $feed->url();
64 65
    $links = $this->xpath('//a[@href = :href]', [':href' => $href]);
    $this->assert(isset($links[0]), format_string('Link to href %href found.', ['%href' => $href]));
66 67 68
    $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));
69 70

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

    // Set the number of news items to 0 to test that the block does not show
    // up.
81 82
    $block->getPlugin()->setConfigurationValue('block_count', 0);
    $block->save();
83
    // Check that the block is no longer displayed.
84
    $this->drupalGet('test-page');
85
    $this->assertNoText($block->label(), 'Feed block is not displayed on the page when number of items is set to 0.');
86 87 88
  }

  /**
89
   * Creates a feed and checks that feed's page.
90 91 92 93
   */
  public function testFeedPage() {
    // Increase the number of items published in the rss.xml feed so we have
    // enough articles to test paging.
94
    $view = View::load('frontpage');
95 96 97
    $display = &$view->getDisplay('feed_1');
    $display['display_options']['pager']['options']['items_per_page'] = 30;
    $view->save();
98 99 100 101 102 103

    // Create a feed with 30 items.
    $this->createSampleNodes(30);
    $feed = $this->createFeed();
    $this->updateFeedItems($feed, 30);

104 105
    // Check for presence of an aggregator pager.
    $this->drupalGet('aggregator');
106
    $elements = $this->xpath("//ul[contains(@class, :class)]", [':class' => 'pager__items']);
107 108
    $this->assertTrue(!empty($elements), 'Individual source page contains a pager.');

109 110
    // Check for sources page title.
    $this->drupalGet('aggregator/sources');
111
    $titles = $this->xpath('//h1[normalize-space(text())=:title]', [':title' => 'Sources']);
112 113 114
    $this->assertTrue(!empty($titles), 'Source page contains correct title.');

    // Find the expected read_more link on the sources page.
115
    $href = $feed->url();
116
    $links = $this->xpath('//a[@href = :href]', [':href' => $href]);
117
    $this->assertTrue(isset($links[0]), new FormattableMarkup('Link to href %href found.', ['%href' => $href]));
118 119 120
    $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));
121

122 123 124 125 126 127 128
    // Check the rss aggregator page as anonymous user.
    $this->drupalLogout();
    $this->drupalGet('aggregator/rss');
    $this->assertResponse(403);

    // Check the rss aggregator page as admin.
    $this->drupalLogin($this->adminUser);
129 130 131 132 133 134
    $this->drupalGet('aggregator/rss');
    $this->assertResponse(200);
    $this->assertEqual($this->drupalGetHeader('Content-type'), 'application/rss+xml; charset=utf-8');

    // Check the opml aggregator page.
    $this->drupalGet('aggregator/opml');
135 136 137 138 139 140 141 142
    $content = $this->getSession()->getPage()->getContent();
    // We can't use Mink xpath queries here because it only supports HTML pages,
    // but we are dealing with XML here.
    $xml = simplexml_load_string($content);
    $attributes = $xml->xpath('//outline[1]')[0]->attributes();
    $this->assertEquals('rss', $attributes->type);
    $this->assertEquals($feed->label(), $attributes->text);
    $this->assertEquals($feed->getUrl(), $attributes->xmlUrl);
143

144
    // Check for the presence of a pager.
145
    $this->drupalGet('aggregator/sources/' . $feed->id());
146
    $elements = $this->xpath("//ul[contains(@class, :class)]", [':class' => 'pager__items']);
147
    $this->assertTrue(!empty($elements), 'Individual source page contains a pager.');
148 149
    $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
    $this->assertTrue(in_array('aggregator_feed:' . $feed->id(), $cache_tags));
150 151
    $this->assertTrue(in_array('aggregator_feed_view', $cache_tags));
    $this->assertTrue(in_array('aggregator_item_view', $cache_tags));
152
  }
153

154
}