diff --git a/tests/drush/feedsDrushTest.php b/tests/drush/feedsDrushTest.php new file mode 100644 index 0000000..e760017 --- /dev/null +++ b/tests/drush/feedsDrushTest.php @@ -0,0 +1,138 @@ +markTestSkipped('This version of Feeds is for D7.'); + } + + // Install the standard install profile. + $site = $this->setUpDrupal(1, TRUE, UNISH_DRUPAL_MAJOR_VERSION, 'standard'); + $root = $this->webroot(); + $this->siteOptions = array( + 'root' => $root, + 'uri' => key($site), + 'yes' => NULL, + ); + + // Copy the local Feeds directory to the test directory. + $this->copyLocalFeedsDirToTestDir(); + + // Enable the feeds_import module. + $this->execDrush('pm-enable', array('feeds')); + $this->execDrush('pm-enable', array('feeds_import')); + } + + /** + * Copies the local Feeds directory to the directory used in the test. + */ + protected function copyLocalFeedsDirToTestDir() { + $source = dirname(dirname(dirname(__FILE__))); + $dest = $this->getFeedsDir(); + + mkdir($dest, 0755, TRUE); + foreach ( + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), + \RecursiveIteratorIterator::SELF_FIRST) as $item + ) { + if ($item->isDir()) { + mkdir($dest . "/" . $iterator->getSubPathName()); + } + else { + copy($item, $dest . "/" . $iterator->getSubPathName()); + } + } + } + + /** + * Wrapper around drush to always add the site options. + * + * @param string command + * The drush command to run. + * @param array args + * Command arguments. + * @param array $options + * An associative array containing options. + */ + protected function execDrush($command, array $args, array $options = array()) { + return $this->drush($command, $args, $this->siteOptions + $options); + } + + /** + * Returns path to Feeds directory. + */ + protected function getFeedsDir() { + return $this->webroot() . '/' . $this->drupalSitewideDirectory() . '/modules/feeds'; + } + + /** + * Tests an import using the file option. + */ + public function testImportUsingFileOption() { + // Perform an import. + $this->execDrush('feeds-import', array('node'), array( + 'file' => $this->getFeedsDir() . '/tests/feeds/content.csv', + )); + + // Ensure that two nodes were created. + $eval = "print db_query('SELECT COUNT(*) FROM {node}')->fetchField()"; + $this->execDrush('php-eval', array($eval)); + $this->assertEquals('2', $this->getOutput()); + } + + /** + * Tests if an importer can get disabled. + */ + public function testDisableFeedsImporter() { + // First check that the user importer is enabled. + $eval = "print feeds_importer('user')->disabled;"; + $this->execDrush('php-eval', array($eval)); + $this->assertEquals('', $this->getOutput()); + + // Disable the user importer. + $this->execDrush('feeds-disable', array('user')); + + // Ensure that the importer is now disabled. + $this->execDrush('php-eval', array($eval)); + $this->assertEquals('1', $this->getOutput()); + } + + /** + * Tests that no nodes get imported for a disabled importer. + */ + public function testNoImportForDisabledImporter() { + // Disable the node importer. + $this->execDrush('feeds-disable', array('node')); + + // Try to perform an import. Drush command should fail. + $this->drush('feeds-import', array('node'), $this->siteOptions + array( + 'file' => $this->getFeedsDir() . '/tests/feeds/content.csv', + ), NULL, NULL, static::EXIT_ERROR); + + // Ensure that no nodes were imported. + $eval = "print db_query('SELECT COUNT(*) FROM {node}')->fetchField()"; + $this->execDrush('php-eval', array($eval)); + $this->assertEquals('0', $this->getOutput()); + } + +}