View source
<?php
namespace Drupal\Tests\aggregator\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\aggregator\Entity\Feed;
use Drupal\Component\Utility\Html;
use Drupal\node\NodeInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\aggregator\FeedInterface;
abstract class AggregatorTestBase extends BrowserTestBase {
protected $adminUser;
protected static $modules = [
'block',
'node',
'aggregator',
'aggregator_test',
'views',
];
protected function setUp() {
parent::setUp();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer news feeds',
'access news feeds',
'create article content',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalPlaceBlock('local_tasks_block');
}
public function createFeed($feed_url = NULL, array $edit = []) {
$edit = $this
->getFeedEditArray($feed_url, $edit);
$this
->drupalGet('aggregator/sources/add');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The feed ' . $edit['title[0][value]'] . ' has been added.');
$this
->assertSession()
->elementExists('xpath', '//div[@data-drupal-messages]//a[contains(@href, "aggregator/sources/")]');
$fids = \Drupal::entityQuery('aggregator_feed')
->accessCheck(FALSE)
->condition('title', $edit['title[0][value]'])
->condition('url', $edit['url[0][value]'])
->execute();
$this
->assertNotEmpty($fids, 'The feed found in database.');
return Feed::load(array_values($fids)[0]);
}
public function deleteFeed(FeedInterface $feed) {
$this
->drupalGet('aggregator/sources/' . $feed
->id() . '/delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('The feed ' . $feed
->label() . ' has been deleted.');
}
public function getFeedEditArray($feed_url = NULL, array $edit = []) {
$feed_name = $this
->randomMachineName(10);
if (!$feed_url) {
$feed_url = Url::fromRoute('view.frontpage.feed_1', [], [
'query' => [
'feed' => $feed_name,
],
'absolute' => TRUE,
])
->toString();
}
$edit += [
'title[0][value]' => $feed_name,
'url[0][value]' => $feed_url,
'refresh' => '900',
];
return $edit;
}
public function getFeedEditObject($feed_url = NULL, array $values = []) {
$feed_name = $this
->randomMachineName(10);
if (!$feed_url) {
$feed_url = Url::fromRoute('view.frontpage.feed_1', [
'query' => [
'feed' => $feed_name,
],
'absolute' => TRUE,
])
->toString();
}
$values += [
'title' => $feed_name,
'url' => $feed_url,
'refresh' => '900',
];
return Feed::create($values);
}
public function getDefaultFeedItemCount() {
return \Drupal::entityQuery('node')
->condition('promote', NodeInterface::PROMOTED)
->condition('status', NodeInterface::PUBLISHED)
->accessCheck(FALSE)
->range(0, 10)
->count()
->execute();
}
public function updateFeedItems(FeedInterface $feed, $expected_count = NULL) {
$this
->drupalGet($feed
->getUrl());
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/config/services/aggregator/update/' . $feed
->id());
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('admin/config/services/aggregator');
$this
->clickLink('Update items');
$item_ids = \Drupal::entityQuery('aggregator_item')
->accessCheck(FALSE)
->condition('fid', $feed
->id())
->execute();
$feed->items = array_values($item_ids);
if ($expected_count !== NULL) {
$feed->item_count = count($feed->items);
$this
->assertEquals($expected_count, $feed->item_count, new FormattableMarkup('Total items in feed equal to the total items in database (@val1 != @val2)', [
'@val1' => $expected_count,
'@val2' => $feed->item_count,
]));
}
}
public function deleteFeedItems(FeedInterface $feed) {
$this
->drupalGet('admin/config/services/aggregator/delete/' . $feed
->id());
$this
->submitForm([], 'Delete items');
$this
->assertSession()
->pageTextContains('The news items from ' . $feed
->label() . ' have been deleted.');
}
public function updateAndDelete(FeedInterface $feed, $expected_count) {
$count_query = \Drupal::entityQuery('aggregator_item')
->accessCheck(FALSE)
->condition('fid', $feed
->id())
->count();
$this
->updateFeedItems($feed, $expected_count);
$count = $count_query
->execute();
$this
->assertGreaterThan(0, $count);
$this
->deleteFeedItems($feed);
$count = $count_query
->execute();
$this
->assertEquals(0, $count);
}
public function uniqueFeed($feed_name, $feed_url) {
$result = \Drupal::entityQuery('aggregator_feed')
->accessCheck(FALSE)
->condition('title', $feed_name)
->condition('url', $feed_url)
->count()
->execute();
return 1 == $result;
}
public function getValidOpml(array $feeds) {
foreach ($feeds as &$feed) {
$feed['url[0][value]'] = Html::escape($feed['url[0][value]']);
}
$opml = <<<EOF
<opml version="1.0">
<head></head>
<body>
<!-- First feed to be imported. -->
<outline text="{<span class="php-variable">$feeds</span>[<span class="php-constant">0</span>][<span class="php-string">'title[0][value]'</span>]}" xmlurl="{<span class="php-variable">$feeds</span>[<span class="php-constant">0</span>][<span class="php-string">'url[0][value]'</span>]}" />
<!-- Second feed. Test string delimitation and attribute order. -->
<outline xmlurl='{<span class="php-variable">$feeds</span>[<span class="php-constant">1</span>][<span class="php-string">'url[0][value]'</span>]}' text='{<span class="php-variable">$feeds</span>[<span class="php-constant">1</span>][<span class="php-string">'title[0][value]'</span>]}'/>
<!-- Test for duplicate URL and title. -->
<outline xmlurl="{<span class="php-variable">$feeds</span>[<span class="php-constant">0</span>][<span class="php-string">'url[0][value]'</span>]}" text="Duplicate URL"/>
<outline xmlurl="http://duplicate.title" text="{<span class="php-variable">$feeds</span>[<span class="php-constant">1</span>][<span class="php-string">'title[0][value]'</span>]}"/>
<!-- Test that feeds are only added with required attributes. -->
<outline text="{<span class="php-variable">$feeds</span>[<span class="php-constant">2</span>][<span class="php-string">'title[0][value]'</span>]}" />
<outline xmlurl="{<span class="php-variable">$feeds</span>[<span class="php-constant">2</span>][<span class="php-string">'url[0][value]'</span>]}" />
</body>
</opml>
EOF;
$path = 'public://valid-opml.xml';
return \Drupal::service('file_system')
->saveData(chr(239) . chr(187) . chr(191) . $opml, $path);
}
public function getInvalidOpml() {
$opml = <<<EOF
<opml>
<invalid>
</opml>
EOF;
$path = 'public://invalid-opml.xml';
return \Drupal::service('file_system')
->saveData($opml, $path);
}
public function getEmptyOpml() {
$opml = <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<opml version="1.0">
<head></head>
<body>
<outline text="Sample text" />
<outline text="Sample text" url="Sample URL" />
</body>
</opml>
EOF;
$path = 'public://empty-opml.xml';
return \Drupal::service('file_system')
->saveData($opml, $path);
}
public function getRSS091Sample() {
return $GLOBALS['base_url'] . '/' . $this
->getModulePath('aggregator') . '/tests/modules/aggregator_test/aggregator_test_rss091.xml';
}
public function getAtomSample() {
return $GLOBALS['base_url'] . '/' . $this
->getModulePath('aggregator') . '/tests/modules/aggregator_test/aggregator_test_atom.xml';
}
public function getHtmlEntitiesSample() {
return $GLOBALS['base_url'] . '/' . $this
->getModulePath('aggregator') . '/tests/modules/aggregator_test/aggregator_test_title_entities.xml';
}
public function createSampleNodes($count = 5) {
for ($i = 0; $i < $count; $i++) {
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName();
$edit['body[0][value]'] = $this
->randomMachineName();
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
}
}
public function enableTestPlugins() {
$this
->config('aggregator.settings')
->set('fetcher', 'aggregator_test_fetcher')
->set('parser', 'aggregator_test_parser')
->set('processors', [
'aggregator_test_processor' => 'aggregator_test_processor',
'aggregator' => 'aggregator',
])
->save();
}
}