View source
<?php
namespace Drupal\Tests\feeds\Kernel\Entity;
use Drupal\feeds\StateInterface;
use Drupal\feeds\Entity\Feed;
use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Event\ImportFinishedEvent;
use Drupal\feeds\Exception\LockException;
use Drupal\feeds\Feeds\State\CleanStateInterface;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds\Plugin\Type\FeedsPluginInterface;
use Drupal\feeds\Plugin\Type\Fetcher\FetcherInterface;
use Drupal\feeds\Plugin\Type\Parser\ParserInterface;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
use Drupal\node\Entity\Node;
use Drupal\Tests\feeds\Kernel\FeedsKernelTestBase;
use Drupal\Tests\feeds\Kernel\TestLogger;
use Exception;
use Symfony\Component\EventDispatcher\EventDispatcher;
class FeedTest extends FeedsKernelTestBase {
protected $feedType;
public function setUp() {
parent::setUp();
$this->feedType = $this
->createFeedType([
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'atom rss rss1 rss2 opml xml',
],
]);
}
public function testGetSource() {
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => 'http://www.example.com',
]);
$this
->assertEquals('http://www.example.com', $feed
->getSource());
}
public function testSetSource() {
$feed = $this
->createFeed($this->feedType
->id());
$feed
->setSource('http://www.example.com');
$this
->assertEquals('http://www.example.com', $feed
->getSource());
}
public function testGetType() {
$feed = $this
->createFeed($this->feedType
->id());
$feed_type = $feed
->getType();
$this
->assertInstanceOf(FeedTypeInterface::class, $feed_type);
$this
->assertSame($this->feedType
->id(), $feed_type
->id());
}
public function testGetCreatedTime() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->assertTrue(is_int($feed
->getCreatedTime()));
}
public function testSetCreatedTime() {
$feed = $this
->createFeed($this->feedType
->id());
$timestamp = time();
$feed
->setCreatedTime($timestamp);
$this
->assertSame($timestamp, $feed
->getCreatedTime());
}
public function testGetImportedTime() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->assertSame(0, $feed
->getImportedTime());
$this
->assertSame(-1, $feed
->getNextImportTime());
$this->feedType
->set('import_period', 3600);
$this->feedType
->save();
$feed = $this
->reloadFeed($feed);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz.rss2');
$feed
->import();
$this
->assertGreaterThanOrEqual(\Drupal::time()
->getRequestTime(), $feed
->getImportedTime());
$this
->assertSame($feed
->getImportedTime() + 3600, $feed
->getNextImportTime());
}
public function testStartBatchImport() {
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this
->assertEquals([], batch_get());
$feed
->startBatchImport();
$batch = batch_get();
$this
->assertCount(1, $batch['sets']);
}
public function testStartCronImport() {
$this
->installSchema('system', [
'key_value_expire',
]);
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this
->assertEquals(0, $feed
->getQueuedTime());
$queue = \Drupal::service('queue')
->get('feeds_feed_refresh:' . $feed
->bundle());
$this
->assertEquals(0, $queue
->numberOfItems());
$feed
->startCronImport();
$this
->assertGreaterThanOrEqual(\Drupal::time()
->getRequestTime(), $feed
->getQueuedTime());
$this
->assertEquals(1, $queue
->numberOfItems());
}
public function testStartCronImportFailsOnLockedFeed() {
$this
->installSchema('system', [
'key_value_expire',
]);
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$feed
->lock();
$this
->expectException(LockException::class);
$feed
->startCronImport();
}
public function testStartBatchClear() {
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$feed
->import();
$this
->assertEquals([], batch_get());
$feed
->startBatchClear();
$batch = batch_get();
$this
->assertCount(1, $batch['sets']);
}
public function testPushImport() {
$feed = $this
->createFeed($this->feedType
->id());
$feed
->pushImport(file_get_contents($this
->resourcesPath() . '/rss/googlenewstz.rss2'));
$this
->runCompleteQueue('feeds_feed_refresh:' . $this->feedType
->id());
$this
->assertNodeCount(6);
}
public function testStartBatchExpire() {
$config = $this->feedType
->getProcessor()
->getConfiguration();
$config['expire'] = 3600;
$this->feedType
->getProcessor()
->setConfiguration($config);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$feed
->import();
$this
->assertEquals([], batch_get());
$feed
->startBatchExpire();
$this
->assertEquals([], batch_get());
$node = Node::load(1);
$node->feeds_item->imported = \Drupal::time()
->getRequestTime() - 3601;
$node
->save();
$feed
->startBatchExpire();
$batch = batch_get();
$this
->assertCount(1, $batch['sets']);
}
public function testFinishImport() {
$feed = $this
->createFeed($this->feedType
->id());
$feed
->finishImport();
$this
->assertGreaterThanOrEqual(\Drupal::time()
->getRequestTime(), $feed
->getImportedTime());
}
public function testDispatchImportFinishedEvent() {
$dispatcher = new EventDispatcher();
$feed = $this
->getMockBuilder(Feed::class)
->setMethods([
'eventDispatcher',
'getType',
])
->setConstructorArgs([
[
'type' => $this->feedType
->id(),
],
'feeds_feed',
$this->feedType
->id(),
])
->getMock();
$feed
->expects($this
->once())
->method('getType')
->willReturn($this->feedType);
$feed
->expects($this
->once())
->method('eventDispatcher')
->willReturn($dispatcher);
$dispatcher
->addListener(FeedsEvents::IMPORT_FINISHED, function (ImportFinishedEvent $event) {
throw new Exception();
});
$this
->expectException(Exception::class);
$feed
->finishImport();
}
public function testFinishClear() {
$feed = $this
->createFeed($this->feedType
->id());
$feed
->finishClear();
}
public function testProgressFetching() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->assertTrue(is_float($feed
->progressFetching()));
}
public function testProgressParsing() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->assertTrue(is_float($feed
->progressParsing()));
}
public function testProgressImporting() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->assertTrue(is_float($feed
->progressImporting()));
}
public function testProgressCleaning() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->assertTrue(is_float($feed
->progressCleaning()));
}
public function testProgressClearing() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->assertTrue(is_float($feed
->progressClearing()));
}
public function testProgressExpiring() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->assertTrue(is_float($feed
->progressExpiring()));
}
public function testGetState() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->assertInstanceOf(StateInterface::class, $feed
->getState(StateInterface::FETCH));
$this
->assertInstanceOf(StateInterface::class, $feed
->getState(StateInterface::PARSE));
$this
->assertInstanceOf(StateInterface::class, $feed
->getState(StateInterface::PROCESS));
$this
->assertInstanceOf(CleanStateInterface::class, $feed
->getState(StateInterface::CLEAN));
$this
->assertInstanceOf(StateInterface::class, $feed
->getState(StateInterface::CLEAR));
}
public function testGetStateAfterSettingStateToNull() {
$feed = $this
->createFeed($this->feedType
->id());
$feed
->setState(StateInterface::PARSE, NULL);
$feed
->saveStates();
$this
->assertInstanceOf(StateInterface::class, $feed
->getState(StateInterface::PARSE));
}
public function testSetState() {
$feed = $this
->createFeed($this->feedType
->id());
$state = $this
->createMock(StateInterface::class);
$feed
->setState(StateInterface::FETCH, $state);
$this
->assertSame($state, $feed
->getState(StateInterface::FETCH));
$feed
->setState(StateInterface::FETCH, NULL);
$this
->assertNotSame($state, $feed
->getState(StateInterface::FETCH));
$this
->assertInstanceOf(StateInterface::class, $feed
->getState(StateInterface::FETCH));
}
public function testClearStates() {
$feed = $this
->createFeed($this->feedType
->id());
$state = $this
->createMock(StateInterface::class);
$feed
->setState(StateInterface::FETCH, $state);
$this
->assertSame($state, $feed
->getState(StateInterface::FETCH));
$feed
->clearStates();
$this
->assertNotSame($state, $feed
->getState(StateInterface::FETCH));
}
public function testSaveStates() {
$feed = $this
->createFeed($this->feedType
->id());
$state = $this
->createMock(StateInterface::class);
$feed
->setState(StateInterface::FETCH, $state);
$feed
->saveStates();
}
public function testGetItemCount() {
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this
->assertSame(0, $feed
->getItemCount());
$feed
->import();
$this
->assertSame(6, $feed
->getItemCount());
}
public function testGetConfigurationFor() {
$feed = $this
->createFeed($this->feedType
->id());
$classes = [
FeedsPluginInterface::class,
FetcherInterface::class,
ParserInterface::class,
ProcessorInterface::class,
];
foreach ($classes as $class) {
$plugin = $this
->createMock($class);
$plugin
->expects($this
->atLeastOnce())
->method('defaultFeedConfiguration')
->will($this
->returnValue([]));
$this
->assertIsArray($feed
->getConfigurationFor($plugin));
}
}
public function testSetConfigurationFor() {
$feed = $this
->createFeed($this->feedType
->id());
$classes = [
FeedsPluginInterface::class,
FetcherInterface::class,
ParserInterface::class,
ProcessorInterface::class,
];
foreach ($classes as $class) {
$plugin = $this
->createMock($class);
$plugin
->expects($this
->atLeastOnce())
->method('defaultFeedConfiguration')
->will($this
->returnValue([]));
$feed
->setConfigurationFor($plugin, [
'foo' => 'bar',
]);
}
}
public function testPostDeleteWithFeedTypeMissing() {
$feed = $this
->createFeed($this->feedType
->id());
$feed_label = $feed
->label();
$feed_type_id = $this->feedType
->id();
$test_logger = new TestLogger();
$this->container
->get('logger.factory')
->addLogger($test_logger);
$this->feedType
->delete();
$feed = $this
->reloadEntity($feed);
$feed
->postDelete($this->container
->get('entity_type.manager')
->getStorage('feeds_feed'), [
$feed,
]);
$logs = $test_logger
->getMessages();
$expected_logs = [
'Could not perform some post cleanups for feed ' . $feed_label . ' because of the following error: The feed type "' . $feed_type_id . '" for feed 1 no longer exists.',
];
$this
->assertEquals($expected_logs, $logs);
}
public function testSetActive() {
$feed = $this
->createFeed($this->feedType
->id());
$feed
->setActive(TRUE);
$this
->assertSame(TRUE, $feed
->isActive());
$feed
->setActive(FALSE);
$this
->assertSame(FALSE, $feed
->isActive());
$feed
->setActive(TRUE);
$this
->assertSame(TRUE, $feed
->isActive());
}
public function testLock() {
$feed = $this
->createFeed($this->feedType
->id());
$feed
->lock();
$this
->assertSame(TRUE, $feed
->isLocked());
$feed
->unlock();
$this
->assertSame(FALSE, $feed
->isLocked());
$feed
->lock();
$this
->assertSame(TRUE, $feed
->isLocked());
}
}