View source
<?php
namespace Drupal\aggregator\Tests;
use Drupal\aggregator\Entity\Feed;
use Drupal\Component\Utility\Html;
use Drupal\simpletest\WebTestBase;
use Drupal\aggregator\FeedInterface;
abstract class AggregatorTestBase extends WebTestBase {
protected $adminUser;
public static $modules = [
'block',
'node',
'aggregator',
'aggregator_test',
'views',
];
protected function setUp() {
parent::setUp();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
}
$this->adminUser = $this
->drupalCreateUser(array(
'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 = array()) {
$edit = $this
->getFeedEditArray($feed_url, $edit);
$this
->drupalPostForm('aggregator/sources/add', $edit, t('Save'));
$this
->assertRaw(t('The feed %name has been added.', array(
'%name' => $edit['title[0][value]'],
)), format_string('The feed @name has been added.', array(
'@name' => $edit['title[0][value]'],
)));
$fid = db_query("SELECT fid FROM {aggregator_feed} WHERE title = :title AND url = :url", array(
':title' => $edit['title[0][value]'],
':url' => $edit['url[0][value]'],
))
->fetchField();
$this
->assertTrue(!empty($fid), 'The feed found in database.');
return Feed::load($fid);
}
public function deleteFeed(FeedInterface $feed) {
$this
->drupalPostForm('aggregator/sources/' . $feed
->id() . '/delete', array(), t('Delete'));
$this
->assertRaw(t('The feed %title has been deleted.', array(
'%title' => $feed
->label(),
)), 'Feed deleted successfully.');
}
public function getFeedEditArray($feed_url = NULL, array $edit = array()) {
$feed_name = $this
->randomMachineName(10);
if (!$feed_url) {
$feed_url = \Drupal::url('view.frontpage.feed_1', array(), array(
'query' => array(
'feed' => $feed_name,
),
'absolute' => TRUE,
));
}
$edit += array(
'title[0][value]' => $feed_name,
'url[0][value]' => $feed_url,
'refresh' => '900',
);
return $edit;
}
public function getFeedEditObject($feed_url = NULL, array $values = array()) {
$feed_name = $this
->randomMachineName(10);
if (!$feed_url) {
$feed_url = \Drupal::url('view.frontpage.feed_1', array(
'query' => array(
'feed' => $feed_name,
),
'absolute' => TRUE,
));
}
$values += array(
'title' => $feed_name,
'url' => $feed_url,
'refresh' => '900',
);
return entity_create('aggregator_feed', $values);
}
public function getDefaultFeedItemCount() {
$feed_count = db_query_range('SELECT COUNT(DISTINCT nid) FROM {node_field_data} n WHERE n.promote = 1 AND n.status = 1', 0, $this
->config('system.rss')
->get('items.limit'))
->fetchField();
return $feed_count > 10 ? 10 : $feed_count;
}
public function updateFeedItems(FeedInterface $feed, $expected_count = NULL) {
$this
->drupalGet($feed
->getUrl());
$this
->assertResponse(200, format_string(':url is reachable.', array(
':url' => $feed
->getUrl(),
)));
$this
->drupalGet('admin/config/services/aggregator/update/' . $feed
->id());
$this
->assertResponse(403);
$this
->drupalGet('admin/config/services/aggregator');
$this
->clickLink('Update items');
$result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(
':fid' => $feed
->id(),
));
$feed->items = array();
foreach ($result as $item) {
$feed->items[] = $item->iid;
}
if ($expected_count !== NULL) {
$feed->item_count = count($feed->items);
$this
->assertEqual($expected_count, $feed->item_count, format_string('Total items in feed equal to the total items in database (@val1 != @val2)', array(
'@val1' => $expected_count,
'@val2' => $feed->item_count,
)));
}
}
public function deleteFeedItems(FeedInterface $feed) {
$this
->drupalPostForm('admin/config/services/aggregator/delete/' . $feed
->id(), array(), t('Delete items'));
$this
->assertRaw(t('The news items from %title have been deleted.', array(
'%title' => $feed
->label(),
)), 'Feed items deleted.');
}
public function updateAndDelete(FeedInterface $feed, $expected_count) {
$this
->updateFeedItems($feed, $expected_count);
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(
':fid' => $feed
->id(),
))
->fetchField();
$this
->assertTrue($count);
$this
->deleteFeedItems($feed);
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(
':fid' => $feed
->id(),
))
->fetchField();
$this
->assertTrue($count == 0);
}
public function uniqueFeed($feed_name, $feed_url) {
$result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(
':title' => $feed_name,
':url' => $feed_url,
))
->fetchField();
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 file_unmanaged_save_data($opml, $path);
}
public function getInvalidOpml() {
$opml = <<<EOF
<opml>
<invalid>
</opml>
EOF;
$path = 'public://invalid-opml.xml';
return file_unmanaged_save_data($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 file_unmanaged_save_data($opml, $path);
}
public function getRSS091Sample() {
return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/modules/aggregator_test/aggregator_test_rss091.xml';
}
public function getAtomSample() {
return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/modules/aggregator_test/aggregator_test_atom.xml';
}
public function getHtmlEntitiesSample() {
return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/modules/aggregator_test/aggregator_test_title_entities.xml';
}
public function createSampleNodes($count = 5) {
for ($i = 0; $i < $count; $i++) {
$edit = array();
$edit['title[0][value]'] = $this
->randomMachineName();
$edit['body[0][value]'] = $this
->randomMachineName();
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
}
}
public function enableTestPlugins() {
$this
->config('aggregator.settings')
->set('fetcher', 'aggregator_test_fetcher')
->set('parser', 'aggregator_test_parser')
->set('processors', array(
'aggregator_test_processor' => 'aggregator_test_processor',
'aggregator' => 'aggregator',
))
->save();
}
}