| 1 |
<?php
|
| 2 |
// $Id: aggregator.test,v 1.34 2009/09/29 15:31:13 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Creates tests for aggregator module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
class AggregatorTestCase extends DrupalWebTestCase {
|
| 10 |
function setUp() {
|
| 11 |
parent::setUp('aggregator', 'aggregator_test');
|
| 12 |
$web_user = $this->drupalCreateUser(array('administer news feeds', 'access news feeds', 'create article content'));
|
| 13 |
$this->drupalLogin($web_user);
|
| 14 |
}
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Create an aggregator feed (simulate form submission on admin/config/services/aggregator/add/feed).
|
| 18 |
*
|
| 19 |
* @param $feed_url
|
| 20 |
* If given, feed will be created with this URL, otherwise /rss.xml will be used.
|
| 21 |
* @return $feed
|
| 22 |
* Full feed object if possible.
|
| 23 |
*
|
| 24 |
* @see getFeedEditArray()
|
| 25 |
*/
|
| 26 |
function createFeed($feed_url = NULL) {
|
| 27 |
$edit = $this->getFeedEditArray($feed_url);
|
| 28 |
$this->drupalPost('admin/config/services/aggregator/add/feed', $edit, t('Save'));
|
| 29 |
$this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title'])), t('The feed !name has been added.', array('!name' => $edit['title'])));
|
| 30 |
|
| 31 |
$feed = db_query("SELECT * FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $edit['title'], ':url' => $edit['url']))->fetch();
|
| 32 |
$this->assertTrue(!empty($feed), t('The feed found in database.'));
|
| 33 |
return $feed;
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Delete an aggregator feed.
|
| 38 |
*
|
| 39 |
* @param $feed
|
| 40 |
* Feed object representing the feed.
|
| 41 |
*/
|
| 42 |
function deleteFeed($feed) {
|
| 43 |
$this->drupalPost('admin/config/services/aggregator/edit/feed/' . $feed->fid, array(), t('Delete'));
|
| 44 |
$this->assertRaw(t('The feed %title has been deleted.', array('%title' => $feed->title)), t('Feed deleted successfully.'));
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Return a randomly generated feed edit array.
|
| 49 |
*
|
| 50 |
* @param $feed_url
|
| 51 |
* If given, feed will be created with this URL, otherwise /rss.xml will be used.
|
| 52 |
* @return
|
| 53 |
* A feed array.
|
| 54 |
*/
|
| 55 |
function getFeedEditArray($feed_url = NULL) {
|
| 56 |
$feed_name = $this->randomName(10);
|
| 57 |
if (!$feed_url) {
|
| 58 |
$feed_url = url('rss.xml', array(
|
| 59 |
'query' => array('feed' => $feed_name),
|
| 60 |
'absolute' => TRUE,
|
| 61 |
));
|
| 62 |
}
|
| 63 |
$edit = array(
|
| 64 |
'title' => $feed_name,
|
| 65 |
'url' => $feed_url,
|
| 66 |
'refresh' => '900',
|
| 67 |
);
|
| 68 |
return $edit;
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Return the count of the randomly created feed array.
|
| 73 |
*
|
| 74 |
* @return
|
| 75 |
* Number of feed items on default feed created by createFeed().
|
| 76 |
*/
|
| 77 |
function getDefaultFeedItemCount() {
|
| 78 |
// Our tests are based off of rss.xml, so let's find out how many elements should be related.
|
| 79 |
$feed_count = db_query_range('SELECT COUNT(*) FROM {node} n WHERE n.promote = 1 AND n.status = 1', 0, variable_get('feed_default_items', 10))->fetchField();
|
| 80 |
return $feed_count > 10 ? 10 : $feed_count;
|
| 81 |
}
|
| 82 |
|
| 83 |
/**
|
| 84 |
* Update feed items (simulate click to admin/config/services/aggregator/update/$fid).
|
| 85 |
*
|
| 86 |
* @param $feed
|
| 87 |
* Feed object representing the feed.
|
| 88 |
* @param $expected_count
|
| 89 |
* Expected number of feed items.
|
| 90 |
*/
|
| 91 |
function updateFeedItems(&$feed, $expected_count) {
|
| 92 |
// First, let's ensure we can get to the rss xml.
|
| 93 |
$this->drupalGet($feed->url);
|
| 94 |
$this->assertResponse(200, t('!url is reachable.', array('!url' => $feed->url)));
|
| 95 |
|
| 96 |
// Refresh the feed (simulated link click).
|
| 97 |
$this->drupalGet('admin/config/services/aggregator/update/' . $feed->fid);
|
| 98 |
|
| 99 |
// Ensure we have the right number of items.
|
| 100 |
$result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid));
|
| 101 |
$items = array();
|
| 102 |
$feed->items = array();
|
| 103 |
foreach ($result as $item) {
|
| 104 |
$feed->items[] = $item->iid;
|
| 105 |
}
|
| 106 |
$feed->item_count = count($feed->items);
|
| 107 |
$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)));
|
| 108 |
}
|
| 109 |
|
| 110 |
/**
|
| 111 |
* Confirm item removal from a feed.
|
| 112 |
*
|
| 113 |
* @param $feed
|
| 114 |
* Feed object representing the feed.
|
| 115 |
*/
|
| 116 |
function removeFeedItems($feed) {
|
| 117 |
$this->drupalPost('admin/config/services/aggregator/remove/' . $feed->fid, array(), t('Remove items'));
|
| 118 |
$this->assertRaw(t('The news items from %title have been removed.', array('%title' => $feed->title)), t('Feed items removed.'));
|
| 119 |
}
|
| 120 |
|
| 121 |
/**
|
| 122 |
* Add and remove feed items and ensure that the count is zero.
|
| 123 |
*
|
| 124 |
* @param $feed
|
| 125 |
* Feed object representing the feed.
|
| 126 |
* @param $expected_count
|
| 127 |
* Expected number of feed items.
|
| 128 |
*/
|
| 129 |
function updateAndRemove($feed, $expected_count) {
|
| 130 |
$this->updateFeedItems($feed, $expected_count);
|
| 131 |
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
|
| 132 |
$this->assertTrue($count);
|
| 133 |
$this->removeFeedItems($feed);
|
| 134 |
$count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
|
| 135 |
$this->assertTrue($count == 0);
|
| 136 |
}
|
| 137 |
|
| 138 |
/**
|
| 139 |
* Pull feed categories from aggregator_category_feed table.
|
| 140 |
*
|
| 141 |
* @param $feed
|
| 142 |
* Feed object representing the feed.
|
| 143 |
*/
|
| 144 |
function getFeedCategories($feed) {
|
| 145 |
// add the categories to the feed so we can use them
|
| 146 |
$result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $feed->fid));
|
| 147 |
foreach ($result as $category) {
|
| 148 |
$feed->categories[] = $category->cid;
|
| 149 |
}
|
| 150 |
}
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Check if the feed name and url is unique.
|
| 154 |
*
|
| 155 |
* @param $feed_name
|
| 156 |
* String containing the feed name to check.
|
| 157 |
* @param $feed_url
|
| 158 |
* String containing the feed url to check.
|
| 159 |
* @return
|
| 160 |
* TRUE if feed is unique.
|
| 161 |
*/
|
| 162 |
function uniqueFeed($feed_name, $feed_url) {
|
| 163 |
$result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed_name, ':url' => $feed_url))->fetchField();
|
| 164 |
return (1 == $result);
|
| 165 |
}
|
| 166 |
|
| 167 |
/**
|
| 168 |
* Create a valid OPML file from an array of feeds.
|
| 169 |
*
|
| 170 |
* @param $feeds
|
| 171 |
* An array of feeds.
|
| 172 |
* @return
|
| 173 |
* Path to valid OPML file.
|
| 174 |
*/
|
| 175 |
function getValidOpml($feeds) {
|
| 176 |
// Properly escape URLs so that XML parsers don't choke on them.
|
| 177 |
foreach ($feeds as &$feed) {
|
| 178 |
$feed['url'] = htmlspecialchars($feed['url']);
|
| 179 |
}
|
| 180 |
/**
|
| 181 |
* Does not have an XML declaration, must pass the parser.
|
| 182 |
*/
|
| 183 |
$opml = <<<EOF
|
| 184 |
<opml version="1.0">
|
| 185 |
<head></head>
|
| 186 |
<body>
|
| 187 |
<!-- First feed to be imported. -->
|
| 188 |
<outline text="{$feeds[0]['title']}" xmlurl="{$feeds[0]['url']}" />
|
| 189 |
|
| 190 |
<!-- Second feed. Test string delimitation and attribute order. -->
|
| 191 |
<outline xmlurl='{$feeds[1]['url']}' text='{$feeds[1]['title']}'/>
|
| 192 |
|
| 193 |
<!-- Test for duplicate URL and title. -->
|
| 194 |
<outline xmlurl="{$feeds[0]['url']}" text="Duplicate URL"/>
|
| 195 |
<outline xmlurl="http://duplicate.title" text="{$feeds[1]['title']}"/>
|
| 196 |
|
| 197 |
<!-- Test that feeds are only added with required attributes. -->
|
| 198 |
<outline text="{$feeds[2]['title']}" />
|
| 199 |
<outline xmlurl="{$feeds[2]['url']}" />
|
| 200 |
</body>
|
| 201 |
</opml>
|
| 202 |
EOF;
|
| 203 |
|
| 204 |
$path = 'public://valid-opml.xml';
|
| 205 |
return file_unmanaged_save_data($opml, $path);
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
| 209 |
* Create an invalid OPML file.
|
| 210 |
*
|
| 211 |
* @return
|
| 212 |
* Path to invalid OPML file.
|
| 213 |
*/
|
| 214 |
function getInvalidOpml() {
|
| 215 |
$opml = <<<EOF
|
| 216 |
<opml>
|
| 217 |
<invalid>
|
| 218 |
</opml>
|
| 219 |
EOF;
|
| 220 |
|
| 221 |
$path = 'public://invalid-opml.xml';
|
| 222 |
return file_unmanaged_save_data($opml, $path);
|
| 223 |
}
|
| 224 |
|
| 225 |
/**
|
| 226 |
* Create a valid but empty OPML file.
|
| 227 |
*
|
| 228 |
* @return
|
| 229 |
* Path to empty OPML file.
|
| 230 |
*/
|
| 231 |
function getEmptyOpml() {
|
| 232 |
$opml = <<<EOF
|
| 233 |
<?xml version="1.0" encoding="utf-8"?>
|
| 234 |
<opml version="1.0">
|
| 235 |
<head></head>
|
| 236 |
<body>
|
| 237 |
<outline text="Sample text" />
|
| 238 |
<outline text="Sample text" url="Sample URL" />
|
| 239 |
</body>
|
| 240 |
</opml>
|
| 241 |
EOF;
|
| 242 |
|
| 243 |
$path = 'public://empty-opml.xml';
|
| 244 |
return file_unmanaged_save_data($opml, $path);
|
| 245 |
}
|
| 246 |
|
| 247 |
function getRSS091Sample() {
|
| 248 |
return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/aggregator_test_rss091.xml';
|
| 249 |
}
|
| 250 |
|
| 251 |
function createSampleNodes() {
|
| 252 |
$langcode = FIELD_LANGUAGE_NONE;
|
| 253 |
// Post 5 articles.
|
| 254 |
for ($i = 0; $i < 5; $i++) {
|
| 255 |
$edit = array();
|
| 256 |
$edit["title[$langcode][0][value]"] = $this->randomName();
|
| 257 |
$edit["body[$langcode][0][value]"] = $this->randomName();
|
| 258 |
$this->drupalPost('node/add/article', $edit, t('Save'));
|
| 259 |
}
|
| 260 |
}
|
| 261 |
}
|
| 262 |
|
| 263 |
class AddFeedTestCase extends AggregatorTestCase {
|
| 264 |
public static function getInfo() {
|
| 265 |
return array(
|
| 266 |
'name' => 'Add feed functionality',
|
| 267 |
'description' => 'Add feed test.',
|
| 268 |
'group' => 'Aggregator'
|
| 269 |
);
|
| 270 |
}
|
| 271 |
|
| 272 |
/**
|
| 273 |
* Create a feed, ensure that it is unique, check the source, and delete the feed.
|
| 274 |
*/
|
| 275 |
function testAddFeed() {
|
| 276 |
$feed = $this->createFeed();
|
| 277 |
|
| 278 |
// Check feed data.
|
| 279 |
$this->assertEqual($this->getUrl(), url('admin/config/services/aggregator/add/feed', array('absolute' => TRUE)), t('Directed to correct url.'));
|
| 280 |
$this->assertTrue($this->uniqueFeed($feed->title, $feed->url), t('The feed is unique.'));
|
| 281 |
|
| 282 |
// Check feed source.
|
| 283 |
$this->drupalGet('aggregator/sources/' . $feed->fid);
|
| 284 |
$this->assertResponse(200, t('Feed source exists.'));
|
| 285 |
$this->assertText($feed->title, t('Page title'));
|
| 286 |
|
| 287 |
// Delete feed.
|
| 288 |
$this->deleteFeed($feed);
|
| 289 |
}
|
| 290 |
}
|
| 291 |
|
| 292 |
class UpdateFeedTestCase extends AggregatorTestCase {
|
| 293 |
public static function getInfo() {
|
| 294 |
return array(
|
| 295 |
'name' => 'Update feed functionality',
|
| 296 |
'description' => 'Update feed test.',
|
| 297 |
'group' => 'Aggregator'
|
| 298 |
);
|
| 299 |
}
|
| 300 |
|
| 301 |
/**
|
| 302 |
* Create a feed and attempt to update it.
|
| 303 |
*/
|
| 304 |
function testUpdateFeed() {
|
| 305 |
$remamining_fields = array('title', 'url', '');
|
| 306 |
foreach ($remamining_fields as $same_field) {
|
| 307 |
$feed = $this->createFeed();
|
| 308 |
|
| 309 |
// Get new feed data array and modify newly created feed.
|
| 310 |
$edit = $this->getFeedEditArray();
|
| 311 |
$edit['refresh'] = 1800; // Change refresh value.
|
| 312 |
if (isset($feed->{$same_field})) {
|
| 313 |
$edit[$same_field] = $feed->{$same_field};
|
| 314 |
}
|
| 315 |
$this->drupalPost('admin/config/services/aggregator/edit/feed/' . $feed->fid, $edit, t('Save'));
|
| 316 |
$this->assertRaw(t('The feed %name has been updated.', array('%name' => $edit['title'])), t('The feed %name has been updated.', array('%name' => $edit['title'])));
|
| 317 |
|
| 318 |
// Check feed data.
|
| 319 |
$this->assertEqual($this->getUrl(), url('admin/config/services/aggregator/', array('absolute' => TRUE)));
|
| 320 |
$this->assertTrue($this->uniqueFeed($edit['title'], $edit['url']), t('The feed is unique.'));
|
| 321 |
|
| 322 |
// Check feed source.
|
| 323 |
$this->drupalGet('aggregator/sources/' . $feed->fid);
|
| 324 |
$this->assertResponse(200, t('Feed source exists.'));
|
| 325 |
$this->assertText($edit['title'], t('Page title'));
|
| 326 |
|
| 327 |
// Delete feed.
|
| 328 |
$feed->title = $edit['title']; // Set correct title so deleteFeed() will work.
|
| 329 |
$this->deleteFeed($feed);
|
| 330 |
}
|
| 331 |
}
|
| 332 |
}
|
| 333 |
|
| 334 |
class RemoveFeedTestCase extends AggregatorTestCase {
|
| 335 |
public static function getInfo() {
|
| 336 |
return array(
|
| 337 |
'name' => 'Remove feed functionality',
|
| 338 |
'description' => 'Remove feed test.',
|
| 339 |
'group' => 'Aggregator'
|
| 340 |
);
|
| 341 |
}
|
| 342 |
|
| 343 |
/**
|
| 344 |
* Remove a feed and ensure that all it services are removed.
|
| 345 |
*/
|
| 346 |
function testRemoveFeed() {
|
| 347 |
$feed = $this->createFeed();
|
| 348 |
|
| 349 |
// Delete feed.
|
| 350 |
$this->deleteFeed($feed);
|
| 351 |
|
| 352 |
// Check feed source.
|
| 353 |
$this->drupalGet('aggregator/sources/' . $feed->fid);
|
| 354 |
$this->assertResponse(404, t('Deleted feed source does not exists.'));
|
| 355 |
|
| 356 |
// Check database for feed.
|
| 357 |
$result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed->title, ':url' => $feed->url))->fetchField();
|
| 358 |
$this->assertFalse($result, t('Feed not found in database'));
|
| 359 |
}
|
| 360 |
}
|
| 361 |
|
| 362 |
class UpdateFeedItemTestCase extends AggregatorTestCase {
|
| 363 |
public static function getInfo() {
|
| 364 |
return array(
|
| 365 |
'name' => 'Update feed item functionality',
|
| 366 |
'description' => 'Update feed items from a feed.',
|
| 367 |
'group' => 'Aggregator'
|
| 368 |
);
|
| 369 |
}
|
| 370 |
|
| 371 |
/**
|
| 372 |
* Test running "update items" from the 'admin/config/services/aggregator' page.
|
| 373 |
*/
|
| 374 |
function testUpdateFeedItem() {
|
| 375 |
$this->createSampleNodes();
|
| 376 |
|
| 377 |
// Create a feed and test updating feed items if possible.
|
| 378 |
$feed = $this->createFeed();
|
| 379 |
if (!empty($feed)) {
|
| 380 |
$this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
|
| 381 |
$this->removeFeedItems($feed);
|
| 382 |
}
|
| 383 |
|
| 384 |
// Delete feed.
|
| 385 |
$this->deleteFeed($feed);
|
| 386 |
|
| 387 |
// Test updating feed items without valid timestamp information.
|
| 388 |
$edit = array(
|
| 389 |
'title' => "Feed without publish timestamp",
|
| 390 |
'url' => $this->getRSS091Sample(),
|
| 391 |
);
|
| 392 |
|
| 393 |
$this->drupalGet($edit['url']);
|
| 394 |
$this->assertResponse(array(200), t('URL !url is accessible', array('!url' => $edit['url'])));
|
| 395 |
|
| 396 |
$this->drupalPost('admin/config/services/aggregator/add/feed', $edit, t('Save'));
|
| 397 |
$this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title'])), t('The feed !name has been added.', array('!name' => $edit['title'])));
|
| 398 |
|
| 399 |
$feed = db_query("SELECT * FROM {aggregator_feed} WHERE url = :url", array(':url' => $edit['url']))->fetchObject();
|
| 400 |
$this->drupalGet('admin/config/services/aggregator/update/' . $feed->fid);
|
| 401 |
|
| 402 |
$before = db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
|
| 403 |
|
| 404 |
// Sleep for 3 second.
|
| 405 |
sleep(3);
|
| 406 |
db_update('aggregator_feed')
|
| 407 |
->condition('fid', $feed->fid)
|
| 408 |
->fields(array(
|
| 409 |
'checked' => 0,
|
| 410 |
'hash' => '',
|
| 411 |
'etag' => '',
|
| 412 |
'modified' => 0,
|
| 413 |
))
|
| 414 |
->execute();
|
| 415 |
$this->drupalGet('admin/config/services/aggregator/update/' . $feed->fid);
|
| 416 |
|
| 417 |
$after = db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField();
|
| 418 |
|
| 419 |
$this->assertTrue($before === $after, t('Publish timestamp of feed item was not updated (!before === !after)', array('!before' => $before, '!after' => $after)));
|
| 420 |
}
|
| 421 |
}
|
| 422 |
|
| 423 |
class RemoveFeedItemTestCase extends AggregatorTestCase {
|
| 424 |
public static function getInfo() {
|
| 425 |
return array(
|
| 426 |
'name' => 'Remove feed item functionality',
|
| 427 |
'description' => 'Remove feed items from a feed.',
|
| 428 |
'group' => 'Aggregator'
|
| 429 |
);
|
| 430 |
}
|
| 431 |
|
| 432 |
/**
|
| 433 |
* Test running "remove items" from the 'admin/config/services/aggregator' page.
|
| 434 |
*/
|
| 435 |
function testRemoveFeedItem() {
|
| 436 |
// Create a bunch of test feeds.
|
| 437 |
$feed_urls = array();
|
| 438 |
// No last-modified, no etag.
|
| 439 |
$feed_urls[] = url('aggregator/test-feed', array('absolute' => TRUE));
|
| 440 |
// Last-modified, but no etag.
|
| 441 |
$feed_urls[] = url('aggregator/test-feed/1', array('absolute' => TRUE));
|
| 442 |
// No Last-modified, but etag.
|
| 443 |
$feed_urls[] = url('aggregator/test-feed/0/1', array('absolute' => TRUE));
|
| 444 |
// Last-modified and etag.
|
| 445 |
$feed_urls[] = url('aggregator/test-feed/1/1', array('absolute' => TRUE));
|
| 446 |
|
| 447 |
foreach ($feed_urls as $feed_url) {
|
| 448 |
$feed = $this->createFeed($feed_url);
|
| 449 |
// Update and remove items two times in a row to make sure that removal
|
| 450 |
// resets all 'modified' information (modified, etag, hash) and allows for
|
| 451 |
// immediate update.
|
| 452 |
$this->updateAndRemove($feed, 2);
|
| 453 |
$this->updateAndRemove($feed, 2);
|
| 454 |
$this->updateAndRemove($feed, 2);
|
| 455 |
// Delete feed.
|
| 456 |
$this->deleteFeed($feed);
|
| 457 |
}
|
| 458 |
}
|
| 459 |
}
|
| 460 |
|
| 461 |
class CategorizeFeedItemTestCase extends AggregatorTestCase {
|
| 462 |
public static function getInfo() {
|
| 463 |
return array(
|
| 464 |
'name' => 'Categorize feed item functionality',
|
| 465 |
'description' => 'Test feed item categorization.',
|
| 466 |
'group' => 'Aggregator'
|
| 467 |
);
|
| 468 |
}
|
| 469 |
|
| 470 |
/**
|
| 471 |
* If a feed has a category, make sure that the children inherit that
|
| 472 |
* categorization.
|
| 473 |
*/
|
| 474 |
function testCategorizeFeedItem() {
|
| 475 |
$this->createSampleNodes();
|
| 476 |
|
| 477 |
// Simulate form submission on "admin/config/services/aggregator/add/category".
|
| 478 |
$edit = array('title' => $this->randomName(10), 'description' => '');
|
| 479 |
$this->drupalPost('admin/config/services/aggregator/add/category', $edit, t('Save'));
|
| 480 |
$this->assertRaw(t('The category %title has been added.', array('%title' => $edit['title'])), t('The category %title has been added.', array('%title' => $edit['title'])));
|
| 481 |
|
| 482 |
$category = db_query("SELECT * FROM {aggregator_category} WHERE title = :title", array(':title' => $edit['title']))->fetch();
|
| 483 |
$this->assertTrue(!empty($category), t('The category found in database.'));
|
| 484 |
|
| 485 |
$link_path = 'aggregator/categories/' . $category->cid;
|
| 486 |
$menu_link = db_query("SELECT * FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $link_path))->fetch();
|
| 487 |
$this->assertTrue(!empty($menu_link), t('The menu link associated with the category found in database.'));
|
| 488 |
|
| 489 |
$feed = $this->createFeed();
|
| 490 |
db_insert('aggregator_category_feed')
|
| 491 |
->fields(array(
|
| 492 |
'cid' => $category->cid,
|
| 493 |
'fid' => $feed->fid,
|
| 494 |
))
|
| 495 |
->execute();
|
| 496 |
$this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
|
| 497 |
$this->getFeedCategories($feed);
|
| 498 |
$this->assertTrue(!empty($feed->categories), t('The category found in the feed.'));
|
| 499 |
|
| 500 |
// For each category of a feed, ensure feed items have that category, too.
|
| 501 |
if (!empty($feed->categories) && !empty($feed->items)) {
|
| 502 |
foreach ($feed->categories as $category) {
|
| 503 |
$categorized_count = db_select('aggregator_category_item')
|
| 504 |
->condition('iid', $feed->items, 'IN')
|
| 505 |
->countQuery()
|
| 506 |
->execute()
|
| 507 |
->fetchField();
|
| 508 |
|
| 509 |
$this->assertEqual($feed->item_count, $categorized_count, t('Total items in feed equal to the total categorized feed items in database'));
|
| 510 |
}
|
| 511 |
}
|
| 512 |
|
| 513 |
// Delete feed.
|
| 514 |
$this->deleteFeed($feed);
|
| 515 |
}
|
| 516 |
}
|
| 517 |
|
| 518 |
class ImportOPMLTestCase extends AggregatorTestCase {
|
| 519 |
public static function getInfo() {
|
| 520 |
return array(
|
| 521 |
'name' => 'Import feeds from OPML functionality',
|
| 522 |
'description' => 'Test OPML import.',
|
| 523 |
'group' => 'Aggregator',
|
| 524 |
);
|
| 525 |
}
|
| 526 |
|
| 527 |
/**
|
| 528 |
* Open OPML import form.
|
| 529 |
*/
|
| 530 |
function openImportForm() {
|
| 531 |
db_delete('aggregator_category')->execute();
|
| 532 |
|
| 533 |
$category = $this->randomName(10);
|
| 534 |
$cid = db_insert('aggregator_category')
|
| 535 |
->fields(array(
|
| 536 |
'title' => $category,
|
| 537 |
'description' => '',
|
| 538 |
))
|
| 539 |
->execute();
|
| 540 |
|
| 541 |
$this->drupalGet('admin/config/services/aggregator/add/opml');
|
| 542 |
$this->assertText('A single OPML document may contain a collection of many feeds.', t('Looking for help text.'));
|
| 543 |
$this->assertFieldByName('files[upload]', '', t('Looking for file upload field.'));
|
| 544 |
$this->assertFieldByName('remote', '', t('Looking for remote URL field.'));
|
| 545 |
$this->assertFieldByName('refresh', '', t('Looking for refresh field.'));
|
| 546 |
$this->assertFieldByName("category[$cid]", $cid, t('Looking for category field.'));
|
| 547 |
}
|
| 548 |
|
| 549 |
/**
|
| 550 |
* Submit form filled with invalid fields.
|
| 551 |
*/
|
| 552 |
function validateImportFormFields() {
|
| 553 |
$before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
|
| 554 |
|
| 555 |
$edit = array();
|
| 556 |
$this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
|
| 557 |
$this->assertRaw(t('You must <em>either</em> upload a file or enter a URL.'), t('Error if no fields are filled.'));
|
| 558 |
|
| 559 |
$path = $this->getEmptyOpml();
|
| 560 |
$edit = array(
|
| 561 |
'files[upload]' => $path,
|
| 562 |
'remote' => file_create_url($path),
|
| 563 |
);
|
| 564 |
$this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
|
| 565 |
$this->assertRaw(t('You must <em>either</em> upload a file or enter a URL.'), t('Error if both fields are filled.'));
|
| 566 |
|
| 567 |
$edit = array('remote' => 'invalidUrl://empty');
|
| 568 |
$this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
|
| 569 |
$this->assertText(t('This URL is not valid.'), t('Error if the URL is invalid.'));
|
| 570 |
|
| 571 |
$after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
|
| 572 |
$this->assertEqual($before, $after, t('No feeds were added during the three last form submissions.'));
|
| 573 |
}
|
| 574 |
|
| 575 |
/**
|
| 576 |
* Submit form with invalid, empty and valid OPML files.
|
| 577 |
*/
|
| 578 |
function submitImportForm() {
|
| 579 |
$before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
|
| 580 |
|
| 581 |
$form['files[upload]'] = $this->getInvalidOpml();
|
| 582 |
$this->drupalPost('admin/config/services/aggregator/add/opml', $form, t('Import'));
|
| 583 |
$this->assertText(t('No new feed has been added.'), t('Attempting to upload invalid XML.'));
|
| 584 |
|
| 585 |
$edit = array('remote' => file_create_url($this->getEmptyOpml()));
|
| 586 |
$this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
|
| 587 |
$this->assertText(t('No new feed has been added.'), t('Attempting to load empty OPML from remote URL.'));
|
| 588 |
|
| 589 |
$after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
|
| 590 |
$this->assertEqual($before, $after, t('No feeds were added during the two last form submissions.'));
|
| 591 |
|
| 592 |
db_delete('aggregator_feed')->execute();
|
| 593 |
db_delete('aggregator_category')->execute();
|
| 594 |
db_delete('aggregator_category_feed')->execute();
|
| 595 |
|
| 596 |
$category = $this->randomName(10);
|
| 597 |
db_insert('aggregator_category')
|
| 598 |
->fields(array(
|
| 599 |
'cid' => 1,
|
| 600 |
'title' => $category,
|
| 601 |
'description' => '',
|
| 602 |
))
|
| 603 |
->execute();
|
| 604 |
|
| 605 |
$feeds[0] = $this->getFeedEditArray();
|
| 606 |
$feeds[1] = $this->getFeedEditArray();
|
| 607 |
$feeds[2] = $this->getFeedEditArray();
|
| 608 |
$edit = array(
|
| 609 |
'files[upload]' => $this->getValidOpml($feeds),
|
| 610 |
'refresh' => '900',
|
| 611 |
'category[1]' => $category,
|
| 612 |
);
|
| 613 |
$this->drupalPost('admin/config/services/aggregator/add/opml', $edit, t('Import'));
|
| 614 |
$this->assertRaw(t('A feed with the URL %url already exists.', array('%url' => $feeds[0]['url'])), t('Verifying that a duplicate URL was identified'));
|
| 615 |
$this->assertRaw(t('A feed named %title already exists.', array('%title' => $feeds[1]['title'])), t('Verifying that a duplicate title was identified'));
|
| 616 |
|
| 617 |
$after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
|
| 618 |
$this->assertEqual($after, 2, t('Verifying that two distinct feeds were added.'));
|
| 619 |
|
| 620 |
$feeds_from_db = db_query("SELECT f.title, f.url, f.refresh, cf.cid FROM {aggregator_feed} f LEFT JOIN {aggregator_category_feed} cf ON f.fid = cf.fid");
|
| 621 |
$refresh = $category = TRUE;
|
| 622 |
foreach ($feeds_from_db as $feed) {
|
| 623 |
$title[$feed->url] = $feed->title;
|
| 624 |
$url[$feed->title] = $feed->url;
|
| 625 |
$category = $category && $feed->cid == 1;
|
| 626 |
$refresh = $refresh && $feed->refresh == 900;
|
| 627 |
}
|
| 628 |
|
| 629 |
$this->assertEqual($title[$feeds[0]['url']], $feeds[0]['title'], t('First feed was added correctly.'));
|
| 630 |
$this->assertEqual($url[$feeds[1]['title']], $feeds[1]['url'], t('Second feed was added correctly.'));
|
| 631 |
$this->assertTrue($refresh, t('Refresh times are correct.'));
|
| 632 |
$this->assertTrue($category, t('Categories are correct.'));
|
| 633 |
}
|
| 634 |
|
| 635 |
function testOPMLImport() {
|
| 636 |
$this->openImportForm();
|
| 637 |
$this->validateImportFormFields();
|
| 638 |
$this->submitImportForm();
|
| 639 |
}
|
| 640 |
}
|