View source
<?php
namespace Drupal\Tests\feeds\Functional;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\feeds\FeedTypeInterface;
class CronTest extends FeedsBrowserTestBase {
public function test() {
$feed_type = $this
->createFeedType();
$feed_type
->setImportPeriod(3600);
$mappings = $feed_type
->getMappings();
unset($mappings[0]['unique']);
$feed_type
->setMappings($mappings);
$feed_type
->save();
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesUrl() . '/rss/googlenewstz.rss2',
]);
$feed = $this
->reloadEntity($feed);
$this
->assertEquals(0, $feed
->getImportedTime());
$this
->assertEquals(0, $feed
->getNextImportTime());
$this
->assertEquals(0, $feed
->getItemCount());
\Drupal::cache('feeds_download')
->deleteAll();
sleep(1);
$this
->cronRun();
$feed = $this
->reloadEntity($feed);
$this
->assertEquals(6, $feed
->getItemCount());
$imported = $feed
->getImportedTime();
$this
->assertTrue($imported > 0);
$this
->assertEquals($imported + 3600, $feed
->getNextImportTime());
\Drupal::cache('feeds_download')
->deleteAll();
sleep(1);
$this
->cronRun();
$feed = $this
->reloadEntity($feed);
$this
->assertEquals(6, $feed
->getItemCount());
$this
->assertEquals($imported, $feed
->getImportedTime());
$this
->assertEquals($imported + 3600, $feed
->getNextImportTime());
\Drupal::cache('feeds_download')
->deleteAll();
sleep(1);
$this
->drupalPostForm('feed/' . $feed
->id() . '/import', [], t('Import'));
$feed = $this
->reloadEntity($feed);
$manual_imported_time = $feed
->getImportedTime();
$this
->assertEquals(12, $feed
->getItemCount());
$this
->assertTrue($manual_imported_time > $imported);
$this
->assertEquals($feed
->getImportedTime() + 3600, $feed
->getNextImportTime());
$feed = $this
->reloadEntity($feed);
$feed
->set('next', 0);
$feed
->setActive(FALSE);
$feed
->save();
\Drupal::cache('feeds_download')
->deleteAll();
sleep(1);
$this
->cronRun();
$feed = $this
->reloadEntity($feed);
$this
->assertEquals(12, $feed
->getItemCount());
$this
->assertEquals($manual_imported_time, $feed
->getImportedTime());
$this
->assertEquals(0, $feed
->getNextImportTime());
}
public function testImportSourceWithMultipleCronRuns() {
$this->container
->get('module_installer')
->install([
'feeds_test_files',
'feeds_test_multiple_cron_runs',
]);
$this
->rebuildContainer();
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'GUID',
'title' => 'Title',
], [
'fetcher' => 'http',
'fetcher_configuration' => [],
'mappings' => [
[
'target' => 'feeds_item',
'map' => [
'guid' => 'guid',
],
],
[
'target' => 'title',
'map' => [
'value' => 'title',
],
],
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => \Drupal::request()
->getSchemeAndHttpHost() . '/testing/feeds/nodes.csv',
]);
$feed
->startCronImport();
$this
->cronRun();
$this
->assertNodeCount(5);
\Drupal::state()
->set('feeds_test_nodes_last_modified', strtotime('Sun, 30 Mar 2016 10:19:55 GMT'));
\Drupal::cache('feeds_download')
->deleteAll();
$this
->cronRun();
$this
->assertNodeCount(9);
}
public function testDeleteFeedTypeForWhichImportIsScheduled() {
$feed_type = $this
->createFeedType([
'id' => 'foo',
]);
$feed_type
->setImportPeriod(FeedTypeInterface::SCHEDULE_CONTINUOUSLY);
$feed_type
->save();
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesUrl() . '/rss/googlenewstz.rss2',
]);
$feed_type
->delete();
$this
->cronRun();
$this
->expectException(EntityStorageException::class);
$this
->expectExceptionMessage('The feed type "foo" for feed 1 no longer exists.');
$feed
->startCronImport();
}
}