Skip to content
Snippets Groups Projects
Commit 4752a29a authored by Youri van Koppen's avatar Youri van Koppen
Browse files

Issue #3193092 by MegaChriz: Expire functionality broken

parent 8618a628
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,7 @@ class FeedExpireHandler extends FeedHandlerBase {
$feed->lock();
}
catch (LockException $e) {
$this->messenger()->addWarning(t('The feed became locked before the expiring could begin.'));
$this->messenger()->addWarning($this->t('The feed became locked before the expiring could begin.'));
return;
}
$feed->clearStates();
......@@ -39,7 +39,7 @@ class FeedExpireHandler extends FeedHandlerBase {
'title' => $this->t('Expiring: %title', ['%title' => $feed->label()]),
'init_message' => $this->t('Expiring: %title', ['%title' => $feed->label()]),
'progress_message' => $this->t('Expiring: %title', ['%title' => $feed->label()]),
'error_message' => $this->t('An error occored while expiring %title.', ['%title' => $feed->label()]),
'error_message' => $this->t('An error occurred while expiring %title.', ['%title' => $feed->label()]),
];
foreach ($ids as $id) {
......
......@@ -2,6 +2,8 @@
namespace Drupal\feeds;
use Drupal\feeds\Result\FetcherResultInterface;
/**
* Import feeds using the batch API.
*/
......@@ -14,4 +16,15 @@ class FeedsBatchExecutable extends FeedsExecutable {
return new FeedsBatchBatch($this, $feed, $stage);
}
/**
* {@inheritdoc}
*/
protected function finish(FeedInterface $feed, FetcherResultInterface $fetcher_result) {
$result = parent::finish($feed, $fetcher_result);
if ($result) {
// Start a batch for expiring items.
$feed->startBatchExpire();
}
}
}
......@@ -289,6 +289,14 @@ class FeedsExecutable implements FeedsExecutableInterface, ContainerInjectionInt
/**
* Finalizes the import.
*
* @param \Drupal\feeds\FeedInterface $feed
* The feed which import batch is about to be finished.
* @param \Drupal\feeds\Result\FetcherResultInterface $fetcher_result
* The last fetcher result.
*
* @return bool
* True if the last batch was done. False if the import is still ongoing.
*/
protected function finish(FeedInterface $feed, FetcherResultInterface $fetcher_result) {
// Update item count.
......@@ -298,11 +306,13 @@ class FeedsExecutable implements FeedsExecutableInterface, ContainerInjectionInt
$this->createBatch($feed, static::PARSE)
->addOperation(static::PARSE, ['fetcher_result' => $fetcher_result])
->run();
return FALSE;
}
elseif ($feed->progressFetching() !== StateInterface::BATCH_COMPLETE) {
$this->createBatch($feed, static::FETCH)
->addOperation(static::FETCH)
->run();
return FALSE;
}
elseif ($feed->progressCleaning() !== StateInterface::BATCH_COMPLETE) {
$clean_state = $feed->getState(StateInterface::CLEAN);
......@@ -315,9 +325,11 @@ class FeedsExecutable implements FeedsExecutableInterface, ContainerInjectionInt
// Add a final item that finalizes the import.
$batch->addOperation(static::FINISH, ['fetcher_result' => $fetcher_result]);
$batch->run();
return FALSE;
}
else {
$feed->finishImport();
return TRUE;
}
}
......
<?php
namespace Drupal\Tests\feeds\Functional;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
/**
* Tests the expire feature.
*
* @group feeds
*/
class ExpireTest extends FeedsBrowserTestBase {
/**
* The feed type entity.
*
* @var \Drupal\feeds\Entity\FeedType
*/
protected $feedType;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create a feed type.
$this->feedType = $this->createFeedType([
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'atom rss rss1 rss2 opml xml',
],
'processor_configuration' => [
'authorize' => FALSE,
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'values' => [
'type' => 'article',
],
'expire' => 3600,
'skip_hash_check' => TRUE,
],
]);
}
/**
* Tests expiring items when doing an import in the UI.
*/
public function testExpireItemsWithBatch() {
// Create a feed and import first file.
$feed = $this->createFeed($this->feedType->id(), [
'source' => $this->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this->batchImport($feed);
// Reload feed and assert that 6 nodes have been created.
$feed = $this->reloadFeed($feed);
static::assertEquals(6, $feed->getItemCount());
$this->assertNodeCount(6);
// Set import time to be about an hour in the past.
\Drupal::database()->update('node__feeds_item')
->fields([
'feeds_item_imported' => $this->container->get('datetime.time')->getRequestTime() - 3601,
])
->execute();
// Import an "updated" version of the file from which one item is removed.
$feed->setSource($this->resourcesPath() . '/rss/googlenewstz_missing.rss2');
$feed->save();
$this->batchImport($feed);
// Assert that one node is removed.
$feed = $this->reloadFeed($feed);
$this->assertText('Expired 1 items.');
static::assertEquals(5, $feed->getItemCount());
$this->assertNodeCount(5);
// And assert that the feed is no longer locked.
$this->assertFalse($feed->isLocked());
}
/**
* Tests expiring items using cron.
*
* @todo implement feature for expiring items using cron.
*/
public function testExpireItemsWithCron() {
$this->markTestIncomplete('Expiring items on cron runs is not implemented yet');
// Set the import period to run as often as possible.
$this->feedType->setImportPeriod(FeedTypeInterface::SCHEDULE_CONTINUOUSLY);
$this->feedType->save();
// Create a feed and import first file.
$feed = $this->createFeed($this->feedType->id(), [
'source' => $this->resourcesPath() . '/rss/googlenewstz.rss2',
]);
// Run cron to import.
$this->cronRun();
// Assert that 6 nodes have been created.
$feed = $this->reloadFeed($feed);
static::assertEquals(6, $feed->getItemCount());
$this->assertNodeCount(6);
// Set import time to be about an hour in the past.
\Drupal::database()->update('node__feeds_item')
->fields([
'feeds_item_imported' => $this->container->get('datetime.time')->getRequestTime() - 3601,
])
->execute();
// Run cron again.
$this->cronRun();
// Assert that all nodes have been expired.
$feed = $this->reloadFeed($feed);
static::assertEquals(0, $feed->getItemCount());
$this->assertNodeCount(0);
// And assert that the feed is no longer locked.
$this->assertFalse($feed->isLocked());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment