Commit f036688c authored by catch's avatar catch
Browse files

Issue #1585944 by aspilicious, Rob Loach: Convert aggregator tests to PSR-0.

parent 1aee24df
......@@ -3,6 +3,5 @@ description = "Aggregates syndicated content (RSS, RDF, and Atom feeds) from ext
package = Core
version = VERSION
core = 8.x
files[] = aggregator.test
configure = admin/config/services/aggregator/settings
stylesheets[all][] = aggregator.theme.css
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\aggregator\Tests\AddFeedTest.
*/
namespace Drupal\aggregator\Tests;
/**
* Tests aggregator feed adding.
*/
class AddFeedTest extends AggregatorTestBase {
public static function getInfo() {
return array(
'name' => 'Add feed functionality',
'description' => 'Add feed test.',
'group' => 'Aggregator'
);
}
/**
* Create a feed, ensure that it is unique, check the source, and delete the feed.
*/
function testAddFeed() {
$feed = $this->createFeed();
// Check feed data.
$this->assertEqual($this->getUrl(), url('admin/config/services/aggregator/add/feed', array('absolute' => TRUE)), t('Directed to correct url.'));
$this->assertTrue($this->uniqueFeed($feed->title, $feed->url), t('The feed is unique.'));
// Check feed source.
$this->drupalGet('aggregator/sources/' . $feed->fid);
$this->assertResponse(200, t('Feed source exists.'));
$this->assertText($feed->title, t('Page title'));
$this->drupalGet('aggregator/sources/' . $feed->fid . '/categorize');
$this->assertResponse(200, t('Feed categorization page exists.'));
// Delete feed.
$this->deleteFeed($feed);
}
/**
* Tests feeds with very long URLs.
*/
function testAddLongFeed() {
// Create a feed with a URL of > 255 characters.
$long_url = "https://www.google.com/search?ix=heb&sourceid=chrome&ie=UTF-8&q=angie+byron#sclient=psy-ab&hl=en&safe=off&source=hp&q=angie+byron&pbx=1&oq=angie+byron&aq=f&aqi=&aql=&gs_sm=3&gs_upl=0l0l0l10534l0l0l0l0l0l0l0l0ll0l0&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=a70b6b1f0abe28d8&biw=1629&bih=889&ix=heb";
$feed = $this->createFeed($long_url);
// Create a second feed of > 255 characters, where the only difference is
// after the 255th character.
$long_url_2 = "https://www.google.com/search?ix=heb&sourceid=chrome&ie=UTF-8&q=angie+byron#sclient=psy-ab&hl=en&safe=off&source=hp&q=angie+byron&pbx=1&oq=angie+byron&aq=f&aqi=&aql=&gs_sm=3&gs_upl=0l0l0l10534l0l0l0l0l0l0l0l0ll0l0&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=a70b6b1f0abe28d8&biw=1629&bih=889";
$feed_2 = $this->createFeed($long_url_2);
// Check feed data.
$this->assertTrue($this->uniqueFeed($feed->title, $feed->url), 'The first long URL feed is unique.');
$this->assertTrue($this->uniqueFeed($feed_2->title, $feed_2->url), 'The second long URL feed is unique.');
// Check feed source.
$this->drupalGet('aggregator/sources/' . $feed->fid);
$this->assertResponse(200, 'Long URL feed source exists.');
$this->assertText($feed->title, 'Page title');
$this->drupalGet('aggregator/sources/' . $feed->fid . '/categorize');
$this->assertResponse(200, 'Long URL feed categorization page exists.');
// Delete feeds.
$this->deleteFeed($feed);
$this->deleteFeed($feed_2);
}
}
<?php
/**
* @file
* Definition of Drupal\aggregator\Tests\AggregatorConfigurationTest.
*/
namespace Drupal\aggregator\Tests;
/**
* Tests aggregator configuration settings.
*/
class AggregatorConfigurationTest extends AggregatorTestBase {
public static function getInfo() {
return array(
'name' => 'Aggregator configuration',
'description' => 'Test aggregator settings page.',
'group' => 'Aggregator',
);
}
/**
* Tests the settings form to ensure the correct default values are used.
*/
function testSettingsPage() {
$edit = array(
'aggregator_allowed_html_tags' => '<a>',
'aggregator_summary_items' => 10,
'aggregator_clear' => 3600,
'aggregator_category_selector' => 'select',
'aggregator_teaser_length' => 200,
);
$this->drupalPost('admin/config/services/aggregator/settings', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'));
foreach ($edit as $name => $value) {
$this->assertFieldByName($name, $value, t('"@name" has correct default value.', array('@name' => $name)));
}
}
}
<?php
/**
* @file
* Definition of Drupal\aggregator\Tests\AggregatorCronTest.
*/
namespace Drupal\aggregator\Tests;
class AggregatorCronTest extends AggregatorTestBase {
public static function getInfo() {
return array(
'name' => 'Update on cron functionality',
'description' => 'Update feeds on cron.',
'group' => 'Aggregator'
);
}
/**
* Add feeds update them on cron.
*/
public function testCron() {
// Create feed and test basic updating on cron.
global $base_url;
$key = config('system.cron')->get('cron_key');
$this->createSampleNodes();
$feed = $this->createFeed();
$this->cronRun();
$this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
$this->removeFeedItems($feed);
$this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
$this->cronRun();
$this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
// Test feed locking when queued for update.
$this->removeFeedItems($feed);
db_update('aggregator_feed')
->condition('fid', $feed->fid)
->fields(array(
'queued' => REQUEST_TIME,
))
->execute();
$this->cronRun();
$this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
db_update('aggregator_feed')
->condition('fid', $feed->fid)
->fields(array(
'queued' => 0,
))
->execute();
$this->cronRun();
$this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(), 'Expected number of items in database.');
}
}
<?php
/**
* @file
* Definition of Drupal\aggregator\Tests\AggregatorRenderingTest.
*/
namespace Drupal\aggregator\Tests;
class AggregatorRenderingTest extends AggregatorTestBase {
public static function getInfo() {
return array(
'name' => 'Checks display of aggregator items',
'description' => 'Checks display of aggregator items on the page.',
'group' => 'Aggregator'
);
}
/**
* Add a feed block to the page and checks its links.
*
* TODO: Test the category block as well.
*/
public function testBlockLinks() {
// Create feed.
$this->createSampleNodes();
$feed = $this->createFeed();
$this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
// Place block on page (@see block.test:moveBlockToRegion())
// Need admin user to be able to access block admin.
$this->admin_user = $this->drupalCreateUser(array(
'administer blocks',
'access administration pages',
'administer news feeds',
'access news feeds',
));
$this->drupalLogin($this->admin_user);
// Prepare to use the block admin form.
$block = array(
'module' => 'aggregator',
'delta' => 'feed-' . $feed->fid,
'title' => $feed->title,
);
$region = 'footer';
$edit = array();
$edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = $region;
// Check the feed block is available in the block list form.
$this->drupalGet('admin/structure/block');
$this->assertFieldByName('blocks[' . $block['module'] . '_' . $block['delta'] . '][region]', '', 'Aggregator feed block is available for positioning.');
// Position it.
$this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
$this->assertText(t('The block settings have been updated.'), t('Block successfully moved to %region_name region.', array( '%region_name' => $region)));
// Confirm that the block is now being displayed on pages.
$this->drupalGet('node');
$this->assertText(t($block['title']), t('Feed block is displayed on the page.'));
// Find the expected read_more link.
$href = 'aggregator/sources/' . $feed->fid;
$links = $this->xpath('//a[@href = :href]', array(':href' => url($href)));
$this->assert(isset($links[0]), t('Link to href %href found.', array('%href' => $href)));
// Visit that page.
$this->drupalGet($href);
$correct_titles = $this->xpath('//h1[normalize-space(text())=:title]', array(':title' => $feed->title));
$this->assertFalse(empty($correct_titles), t('Aggregator feed page is available and has the correct title.'));
// Set the number of news items to 0 to test that the block does not show
// up.
$feed->block = 0;
aggregator_save_feed((array) $feed);
// It is nescessary to flush the cache after saving the number of items.
$this->resetAll();
// Check that the block is no longer displayed.
$this->drupalGet('node');
$this->assertNoText(t($block['title']), 'Feed block is not displayed on the page when number of items is set to 0.');
}
/**
* Create a feed and check that feed's page.
*/
public function testFeedPage() {
// Increase the number of items published in the rss.xml feed so we have
// enough articles to test paging.
$config = config('system.rss-publishing');
$config->set('feed_default_items', 30);
$config->save();
// Create a feed with 30 items.
$this->createSampleNodes(30);
$feed = $this->createFeed();
$this->updateFeedItems($feed, 30);
// Check for the presence of a pager.
$this->drupalGet('aggregator/sources/' . $feed->fid);
$elements = $this->xpath("//ul[@class=:class]", array(':class' => 'pager'));
$this->assertTrue(!empty($elements), t('Individual source page contains a pager.'));
// Reset the number of items in rss.xml to the default value.
$config->set('feed_default_items', 10);
$config->save();
}
}
<?php
/**
* @file
* Definition of Drupal\aggregator\Tests\AggregatorTestBase.
*/
namespace Drupal\aggregator\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Defines a base class for testing aggregator.module.
*/
class AggregatorTestBase extends WebTestBase {
function setUp() {
parent::setUp(array('node', 'block', 'aggregator', 'aggregator_test'));
// Create an Article node type.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
}
$web_user = $this->drupalCreateUser(array('administer news feeds', 'access news feeds', 'create article content'));
$this->drupalLogin($web_user);
}
/**
* Create an aggregator feed (simulate form submission on admin/config/services/aggregator/add/feed).
*
* @param $feed_url
* If given, feed will be created with this URL, otherwise /rss.xml will be used.
* @return $feed
* Full feed object if possible.
*
* @see getFeedEditArray()
*/
function createFeed($feed_url = NULL) {
$edit = $this->getFeedEditArray($feed_url);
$this->drupalPost('admin/config/services/aggregator/add/feed', $edit, t('Save'));
$this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title'])), t('The feed !name has been added.', array('!name' => $edit['title'])));
$feed = db_query("SELECT * FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $edit['title'], ':url' => $edit['url']))->fetch();
$this->assertTrue(!empty($feed), t('The feed found in database.'));
return $feed;
}
/**
* Delete an aggregator feed.
*
* @param $feed
* Feed object representing the feed.
*/
function deleteFeed($feed) {
$this->drupalPost('admin/config/services/aggregator/edit/feed/' . $feed->fid, array(), t('Delete'));
$this->assertRaw(t('The feed %title has been deleted.', array('%title' => $feed->title)), t('Feed deleted successfully.'));
}
/**
* Return a randomly generated feed edit array.
*
* @param $feed_url
* If given, feed will be created with this URL, otherwise /rss.xml will be used.
* @return
* A feed array.
*/
function getFeedEditArray($feed_url = NULL) {
$feed_name = $this->randomName(10);
if (!$feed_url) {
$feed_url = url('rss.xml', array(
'query' => array('feed' => $feed_name),
'absolute' => TRUE,
));
}
$edit = array(
'title' => $feed_name,
'url' => $feed_url,
'refresh' => '900',
);
return $edit;
}
/**
* Return the count of the randomly created feed array.
*
* @return
* Number of feed items on default feed created by createFeed().
*/
function getDefaultFeedItemCount() {
// Our tests are based off of rss.xml, so let's find out how many elements should be related.
$feed_count = db_query_range('SELECT COUNT(*) FROM {node} n WHERE n.promote = 1 AND n.status = 1', 0, config('system.rss-publishing')->get('feed_default_items'))->fetchField();
return $feed_count > 10 ? 10 : $feed_count;
}
/**
* Update feed items (simulate click to admin/config/services/aggregator/update/$fid).
*
* @param $feed
* Feed object representing the feed.
* @param $expected_count
* Expected number of feed items.
*/
function updateFeedItems(&$feed, $expected_count) {
// First, let's ensure we can get to the rss xml.
$this->drupalGet($feed->url);
$this->assertResponse(200, t('!url is reachable.', array('!url' => $feed->url)));
// Attempt to access the update link directly without an access token.
$this->drupalGet('admin/config/services/aggregator/update/' . $feed->fid);
$this->assertResponse(403);
// Refresh the feed (simulated link click).
$this->drupalGet('admin/config/services/aggregator');
$this->clickLink('update items');
// Ensure we have the right number of items.
$result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid));
$items = array();
$feed->items = array();
foreach ($result as $item) {
$feed->items[] = $item->iid;
}
$feed->item_count = count($feed->items);
$this->assertEqual($expected_count, $feed->item_count, t('Total items in feed equal to the total items in database (!val1 != !val2)', array('!val1' => $expected_count, '!val2' => $feed->item_count)));
}
/**
* Confirm item removal from a feed.
*
* @param $feed
* Feed object representing the feed.
*/
function removeFeedItems($feed) {
$this->drupalPost('admin/config/services/aggregator/remove/' . $feed->fid, array(), t('Remove items'));
$this->assertRaw(t('The news items from %title have been removed.', array('%title' => $feed->title)), t('Feed items removed.'));
}
/**
* Add and remove feed items and ensure that the count is zero.
*
* @param $feed
* Feed object representing the feed.
* @param $expected_count
* Expected number of feed items.
*/
function updateAndRemove($feed, $expected_count) {
$this->updateFeedItems($feed, $expected_count);
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
$this->assertTrue($count);
$this->removeFeedItems($feed);
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
$this->assertTrue($count == 0);
}
/**
* Pull feed categories from aggregator_category_feed table.
*
* @param $feed
* Feed object representing the feed.
*/
function getFeedCategories($feed) {
// add the categories to the feed so we can use them
$result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $feed->fid));
foreach ($result as $category) {
$feed->categories[] = $category->cid;
}
}
/**
* Pull categories from aggregator_category table.
*/
function getCategories() {
$categories = array();
$result = db_query('SELECT * FROM {aggregator_category}');
foreach ($result as $category) {
$categories[$category->cid] = $category;
}
return $categories;
}
/**
* Check if the feed name and url is unique.
*
* @param $feed_name
* String containing the feed name to check.
* @param $feed_url
* String containing the feed url to check.
* @return
* TRUE if feed is unique.
*/
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);
}
/**
* Create a valid OPML file from an array of feeds.
*
* @param $feeds
* An array of feeds.
* @return
* Path to valid OPML file.
*/
function getValidOpml($feeds) {
// Properly escape URLs so that XML parsers don't choke on them.
foreach ($feeds as &$feed) {
$feed['url'] = htmlspecialchars($feed['url']);
}
/**
* Does not have an XML declaration, must pass the parser.
*/
$opml = <<<EOF
<opml version="1.0">
<head></head>
<body>
<!-- First feed to be imported. -->
<outline text="{$feeds[0]['title']}" xmlurl="{$feeds[0]['url']}" />
<!-- Second feed. Test string delimitation and attribute order. -->
<outline xmlurl='{$feeds[1]['url']}' text='{$feeds[1]['title']}'/>
<!-- Test for duplicate URL and title. -->
<outline xmlurl="{$feeds[0]['url']}" text="Duplicate URL"/>
<outline xmlurl="http://duplicate.title" text="{$feeds[1]['title']}"/>
<!-- Test that feeds are only added with required attributes. -->
<outline text="{$feeds[2]['title']}" />
<outline xmlurl="{$feeds[2]['url']}" />
</body>
</opml>
EOF;
$path = 'public://valid-opml.xml';
return file_unmanaged_save_data($opml, $path);
}
/**
* Create an invalid OPML file.
*
* @return
* Path to invalid OPML file.
*/
function getInvalidOpml() {
$opml = <<<EOF
<opml>
<invalid>
</opml>
EOF;
$path = 'public://invalid-opml.xml';
return file_unmanaged_save_data($opml, $path);
}
/**
* Create a valid but empty OPML file.
*
* @return
* Path to empty OPML file.
*/
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);
}
function getRSS091Sample() {
return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/aggregator_test_rss091.xml';
}
function getAtomSample() {
// The content of this sample ATOM feed is based directly off of the
// example provided in RFC 4287.
return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/aggregator_test_atom.xml';
}
function getHtmlEntitiesSample() {
return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/aggregator_test_title_entities.xml';
}
/**
* Creates sample article nodes.
*
* @param $count
* (optional) The number of nodes to generate.