View source
<?php
namespace Drupal\Tests\aggregator\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\views\Entity\View;
class AggregatorRenderingTest extends AggregatorTestBase {
public static $modules = [
'block',
'test_page_test',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
}
public function testBlockLinks() {
$this
->createSampleNodes();
$feed = $this
->createFeed();
$this
->updateFeedItems($feed, $this
->getDefaultFeedItemCount());
$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(),
]);
$block
->getPlugin()
->setConfigurationValue('feed', $feed
->id());
$block
->getPlugin()
->setConfigurationValue('block_count', 2);
$block
->save();
$this
->drupalGet('test-page');
$this
->assertText($block
->label(), 'Feed block is displayed on the page.');
$items = $this->container
->get('entity_type.manager')
->getStorage('aggregator_item')
->loadByFeed($feed
->id(), 1);
$links = $this
->xpath('//a[@href = :href]', [
':href' => reset($items)
->getLink(),
]);
$this
->assert(isset($links[0]), 'Item link found.');
$href = $feed
->toUrl()
->toString();
$links = $this
->xpath('//a[@href = :href]', [
':href' => $href,
]);
$this
->assert(isset($links[0]), new FormattableMarkup('Link to href %href found.', [
'%href' => $href,
]));
$cache_tags_header = $this
->drupalGetHeader('X-Drupal-Cache-Tags');
$cache_tags = explode(' ', $cache_tags_header);
$this
->assertContains('aggregator_feed:' . $feed
->id(), $cache_tags);
$this
->drupalGet($feed
->toUrl()
->getInternalPath());
$correct_titles = $this
->xpath('//h1[normalize-space(text())=:title]', [
':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
->assertContains('aggregator_feed:' . $feed
->id(), $cache_tags);
$this
->assertContains('aggregator_feed_view', $cache_tags);
$this
->assertContains('aggregator_item_view', $cache_tags);
$block
->getPlugin()
->setConfigurationValue('block_count', 0);
$block
->save();
$this
->drupalGet('test-page');
$this
->assertNoText($block
->label(), 'Feed block is not displayed on the page when number of items is set to 0.');
}
public function testFeedPage() {
$view = View::load('frontpage');
$display =& $view
->getDisplay('feed_1');
$display['display_options']['pager']['options']['items_per_page'] = 30;
$view
->save();
$this
->createSampleNodes(30);
$feed = $this
->createFeed();
$this
->updateFeedItems($feed, 30);
$this
->drupalGet('aggregator');
$elements = $this
->xpath("//ul[contains(@class, :class)]", [
':class' => 'pager__items',
]);
$this
->assertTrue(!empty($elements), 'Individual source page contains a pager.');
$this
->drupalGet('aggregator/sources');
$titles = $this
->xpath('//h1[normalize-space(text())=:title]', [
':title' => 'Sources',
]);
$this
->assertTrue(!empty($titles), 'Source page contains correct title.');
$href = $feed
->toUrl()
->toString();
$links = $this
->xpath('//a[@href = :href]', [
':href' => $href,
]);
$this
->assertTrue(isset($links[0]), new FormattableMarkup('Link to href %href found.', [
'%href' => $href,
]));
$cache_tags_header = $this
->drupalGetHeader('X-Drupal-Cache-Tags');
$cache_tags = explode(' ', $cache_tags_header);
$this
->assertContains('aggregator_feed:' . $feed
->id(), $cache_tags);
$this
->drupalLogout();
$this
->drupalGet('aggregator/rss');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('aggregator/rss');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertEqual($this
->drupalGetHeader('Content-type'), 'application/rss+xml; charset=utf-8');
$this
->drupalGet('aggregator/opml');
$content = $this
->getSession()
->getPage()
->getContent();
$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);
$this
->drupalGet('aggregator/sources/' . $feed
->id());
$elements = $this
->xpath("//ul[contains(@class, :class)]", [
':class' => 'pager__items',
]);
$this
->assertTrue(!empty($elements), 'Individual source page contains a pager.');
$cache_tags = explode(' ', $this
->drupalGetHeader('X-Drupal-Cache-Tags'));
$this
->assertContains('aggregator_feed:' . $feed
->id(), $cache_tags);
$this
->assertContains('aggregator_feed_view', $cache_tags);
$this
->assertContains('aggregator_item_view', $cache_tags);
}
}