Skip to content
Snippets Groups Projects
Commit 949cde5e authored by David Cameron's avatar David Cameron
Browse files

Issue #1067532 by dcam, ParisLiakos, vineet.osscube, Pavan B S, larowlan:...

Issue #1067532 by dcam, ParisLiakos, vineet.osscube, Pavan B S, larowlan: Deleted and edited aggregator feeds are restored from queue
parent e08b17dd
Branches
Tags
No related merge requests found
......@@ -166,7 +166,7 @@ function aggregator_cron() {
$ids = \Drupal::entityTypeManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
foreach (Feed::loadMultiple($ids) as $feed) {
if ($queue->createItem($feed)) {
if ($queue->createItem($feed->id())) {
// Add timestamp to avoid queueing item more than once.
$feed->setQueuedTime(REQUEST_TIME);
$feed->save();
......
<?php
/**
* @file
* Post update functions for Aggregator.
*/
/**
* Delete the aggregator_feeds queue to eliminate old items containing entities.
*/
function aggregator_post_update_delete_queue_items(&$sandbox = NULL) {
\Drupal::queue('aggregator_feeds')->deleteQueue();
}
......@@ -2,7 +2,7 @@
namespace Drupal\aggregator\Plugin\QueueWorker;
use Drupal\aggregator\FeedInterface;
use Drupal\aggregator\Entity\Feed;
use Drupal\Core\Queue\QueueWorkerBase;
/**
......@@ -20,8 +20,9 @@ class AggregatorRefresh extends QueueWorkerBase {
* {@inheritdoc}
*/
public function processItem($data) {
if ($data instanceof FeedInterface) {
$data->refreshItems();
$feed = Feed::load($data);
if ($feed) {
$feed->refreshItems();
}
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\aggregator\Functional;
use Drupal\aggregator\Entity\Feed;
use Drupal\Tests\Traits\Core\CronRunTrait;
/**
......@@ -45,6 +46,32 @@ class AggregatorCronTest extends AggregatorTestBase {
$feed->setQueuedTime(0)->save();
$this->cronRun();
$this->assertEquals(5, $count_query->execute());
// Test that edited and deleted feeds are not restored from queued objects
$queue = \Drupal::queue('aggregator_feeds');
$queue->createItem($feed->id());
// Overwrite the existing feed values.
$edit = $this->getFeedEditArray();
$this->drupalGet('aggregator/sources/' . $feed->id() . '/configure');
$this->submitForm($edit, t('Save'));
$this->assertSession()->pageTextContains(t('The feed @name has been updated.', array('@name' => $edit['title[0][value]'])));
// Reload the feed with the new values.
\Drupal::entityTypeManager()->getStorage('aggregator_feed')->loadUnchanged($feed->id());
$feed = Feed::load($feed->id());
// Verify the feed still has the new title after cron runs.
$this->cronRun();
$this->drupalGet('aggregator/sources/' . $feed->id());
$this->assertSession()->responseContains($edit['title[0][value]']);
// Verify that queued feeds are not restored after being deleted and that
// cron has run the queue successfully.
$queue->createItem($feed->id());
$this->deleteFeed($feed);
$database = \Drupal::database();
$this->assertCount(0, Feed::loadMultiple(), 'There are no feeds in the database.');
$this->assertEquals(1, $queue->numberOfItems(), 'There is one queued feed in the database.');
$this->cronRun();
$this->assertCount(0, Feed::loadMultiple(), 'There are still no feeds in the database.');
$this->assertEquals(0, $queue->numberOfItems(), 'There are no queued feeds in the database.');
}
}
<?php
namespace Drupal\Tests\aggregator\Functional\Update;
use Drupal\aggregator\Entity\Feed;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\UpdatePathTestTrait;
/**
* @covers aggregator_post_update_delete_queue_items()
* @group Update
* @group aggregator
*/
class AggregatorUpdateDeleteQueueTest extends BrowserTestBase {
use UpdatePathTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'aggregator',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Since Aggregator was just installed Drupal thinks that it doesn't need to
// run the update. Remove it from the update list so it can be run by the
// test.
$key_value = \Drupal::service('keyvalue');
$existing_updates = $key_value->get('post_update')->get('existing_updates', []);
if ($key = array_search('aggregator_post_update_delete_queue_items', $existing_updates)) {
unset($existing_updates[$key]);
}
$key_value->get('post_update')->set('existing_updates', $existing_updates);
}
/**
* Ensure all items in the aggregator_feeds queue are deleted.
*/
public function testUpdateDeleteQueuePostUpdate(): void {
$queue = \Drupal::queue('aggregator_feeds');
// Any sample data would work. It isn't necessary to create a Feed entity
// here to mimic the old queuing behavior. But we do it anyway.
$feed = Feed::create([
'title' => 'test feed',
'url' => 'https://example.com/rss.xml',
'refresh' => '900',
]);
$queue->createItem($feed);
$this->assertEquals(1, $queue->numberOfItems());
$this->runUpdates();
$this->assertEquals(0, $queue->numberOfItems());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment