View source
<?php
namespace Drupal\Tests\feeds\Functional\Commands;
use Drupal\node\Entity\Node;
use Drupal\Tests\feeds\Functional\FeedsBrowserTestBase;
use Drush\TestTraits\DrushTestTrait;
class FeedsDrushCommandsTest extends FeedsBrowserTestBase {
use DrushTestTrait {
getSimplifiedErrorOutput as traitGetSimplifiedErrorOutput;
}
protected $feedType;
public function setUp() {
parent::setUp();
$this->feedType = $this
->createFeedType([
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'rss2',
],
]);
}
public function testListFeeds() {
$feed = $this
->createFeed($this->feedType
->id());
$this
->drush('feeds:list-feeds');
$output = $this
->getOutputAsList();
$expected_columns = [
'Feed type',
'Feed ID',
'Title',
'Last imported',
'Next import',
'Feed source',
'Item count',
'Status',
];
foreach ($expected_columns as $column) {
$this
->assertStringContainsString($column, $output[1]);
}
$values = [
$this->feedType
->id(),
$feed
->label(),
];
foreach ($values as $value) {
$this
->assertStringContainsString($value, $output[3]);
}
}
public function testEnableFeed() {
$feed = $this
->createFeed($this->feedType
->id(), [
'title' => 'Foo',
'status' => FALSE,
]);
$this
->assertFalse((bool) $feed->status->value);
$this
->drush('feeds:enable', [
$feed
->id(),
]);
$feed = $this
->reloadEntity($feed);
$this
->assertTrue((bool) $feed->status->value);
$this
->assertStringContainsString('The feed "Foo" has been enabled.', $this
->getErrorOutput());
$this
->drush('feeds:enable', [
$feed
->id(),
]);
$this
->assertStringContainsString('This feed is already enabled.', $this
->getErrorOutput());
}
public function testDisableFeed() {
$feed = $this
->createFeed($this->feedType
->id(), [
'title' => 'Foo',
]);
$this
->drush('feeds:disable', [
$feed
->id(),
]);
$feed = $this
->reloadEntity($feed);
$this
->assertFalse((bool) $feed->status->value);
$this
->assertStringContainsString('The feed "Foo" has been disabled.', $this
->getErrorOutput());
$this
->drush('feeds:disable', [
$feed
->id(),
]);
$this
->assertStringContainsString('This feed is already disabled.', $this
->getErrorOutput());
}
public function testImportFeed() {
$feed = $this
->createFeed($this->feedType
->id(), [
'title' => 'Foo',
'source' => $this
->resourcesPath() . '/rss/drupalplanet.rss2',
]);
$this
->drush('feeds:import', [
$feed
->id(),
]);
$this
->assertStringContainsString('Created 25 Article items.', $this
->getErrorOutput());
$this
->assertNodeCount(25);
$node = Node::load(1);
$this
->assertEquals('Adaptivethemes: Why I killed Node, may it RIP', $node->title->value);
}
public function testImportFeedFailsWhenLocked() {
$feed = $this
->createFeed($this->feedType
->id(), [
'title' => 'Foo',
'source' => $this
->resourcesPath() . '/rss/drupalplanet.rss2',
]);
$feed
->lock();
$this
->drush('feeds:import', [
$feed
->id(),
]);
$this
->assertNodeCount(0);
$this
->assertStringContainsString('The feed became locked before the import could begin', $this
->getSimplifiedErrorOutput());
}
public function testImportDisabledFeed() {
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/drupalplanet.rss2',
'status' => FALSE,
]);
$this
->drush('feeds:import', [
$feed
->id(),
], [], NULL, NULL, 1);
$this
->assertNodeCount(0);
$this
->assertStringContainsString('The specified feed is disabled. If you want to force importing, specify --import-disabled.', $this
->getSimplifiedErrorOutput());
$this
->drush('feeds:import', [
$feed
->id(),
], [
'import-disabled' => NULL,
]);
$this
->assertNodeCount(25);
}
public function testLockFeed() {
$feed = $this
->createFeed($this->feedType
->id(), [
'title' => 'Foo',
]);
$this
->assertFalse($feed
->isLocked());
$this
->drush('feeds:lock', [
$feed
->id(),
]);
$this
->assertTrue($feed
->isLocked());
$this
->assertStringContainsString('The feed "Foo" has been locked.', $this
->getErrorOutput());
$this
->drush('feeds:lock', [
$feed
->id(),
]);
$this
->assertStringContainsString('This feed is already locked.', $this
->getErrorOutput());
}
public function testUnlockFeed() {
$feed = $this
->createFeed($this->feedType
->id(), [
'title' => 'Foo',
]);
$feed
->lock();
$this
->assertTrue($feed
->isLocked());
$this
->drush('feeds:unlock', [
$feed
->id(),
]);
$this
->assertFalse($feed
->isLocked());
$this
->assertStringContainsString('The feed "Foo" has been unlocked.', $this
->getErrorOutput());
$this
->drush('feeds:unlock', [
$feed
->id(),
]);
$this
->assertStringContainsString('This feed is already unlocked.', $this
->getErrorOutput());
}
public function testFeedCommandFailures($expected_output, $command, array $args = [], array $options = []) {
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/drupalplanet.rss2',
]);
$this
->drush($command, $args, $options, NULL, NULL, 1);
$this
->assertNodeCount(0);
$this
->assertStringContainsString($expected_output, $this
->getErrorOutput());
}
public function providerFeed() {
$return = [];
$commands = [
'feeds:enable',
'feeds:disable',
'feeds:import',
'feeds:lock',
'feeds:unlock',
];
foreach ($commands as $command) {
$return[$command . ':no-feed'] = [
'expected_output' => 'Please specify the ID of the feed',
'command' => $command,
];
$return[$command . ':non-existing-feed'] = [
'expected_output' => 'There is no feed with id 25',
'command' => $command,
'args' => [
25,
],
];
}
return $return;
}
protected function getSimplifiedErrorOutput() {
$output = $this
->traitGetSimplifiedErrorOutput();
return str_replace("\n", '', $output);
}
}