Skip to content
Snippets Groups Projects
feeds_fetcher_file.test 6.27 KiB
Newer Older
<?php

/**
 * @file
 * File fetcher tests.
 */

/**
 * File fetcher test class.
 */
class FeedsFileFetcherTestCase extends FeedsWebTestCase {
    return array(
      'name' => 'File fetcher',
      'description' => 'Tests for file fetcher plugin.',
      'group' => 'Feeds',
    // Set up an importer.
    $this->createImporterConfiguration('Node import', 'node');
    // Set and configure plugins and mappings.
    $this->setSettings('node', NULL, array('content_type' => ''));
    $this->setPlugin('node', 'FeedsFileFetcher');
    $this->setPlugin('node', 'FeedsCSVParser');
      '0' => array(
        'source' => 'title',
        'target' => 'title',
      ),
    ));
  }

  /**
   * Test scheduling on cron.
   */
  public function testPublicFiles() {
    // Straight up upload is covered in other tests, focus on direct mode and
    // file batching here.
    $settings = array(
      'direct' => TRUE,
      'directory' => 'public://feeds',
    );
    $this->setSettings('node', 'FeedsFileFetcher', $settings);

    // Verify that invalid paths are not accepted.
    foreach (array('/tmp/') as $path) {
      $edit = array(
        'feeds[FeedsFileFetcher][source]' => $path,
      );
      $this->drupalPost('import/node', $edit, t('Import'));
      $this->assertText("The file needs to reside within the site's files directory, its path needs to start with scheme://. Available schemes:");
      $count = db_query("SELECT COUNT(*) FROM {feeds_source} WHERE feed_nid = 0")->fetchField();
      $this->assertEqual($count, 0);
    }

    // Verify batching through directories.
    // Copy directory of files.
    $dir = 'public://batchtest';
    $this->copyDir($this->absolutePath() . '/tests/feeds/batch', $dir);

    // Ingest directory of files. Set limit to 5 to force processor to batch,
    // too.
    variable_set('feeds_process_limit', 5);
    $edit = array(
      'feeds[FeedsFileFetcher][source]' => $dir,
    );
    $this->drupalPost('import/node', $edit, t('Import'));
    $this->assertText('Created 18 nodes');

  /**
   * Test uploading private files.
   */
  public function testPrivateFiles() {
    // Straight up upload is covered in other tests, focus on direct mode and
    // file batching here.
    $settings = array(
      'direct' => TRUE,
      'directory' => 'private://feeds',
    );
    $this->setSettings('node', 'FeedsFileFetcher', $settings);

    // Verify batching through directories.
    // Copy directory of files.
    $dir = 'private://batchtest';
    $this->copyDir($this->absolutePath() . '/tests/feeds/batch', $dir);

    // Ingest directory of files. Set limit to 5 to force processor to batch,
    // too.
    variable_set('feeds_process_limit', 5);
    $edit = array(
      'feeds[FeedsFileFetcher][source]' => $dir,
    );
    $this->drupalPost('import/node', $edit, t('Import'));
    $this->assertText('Created 18 nodes');
  }

  /**
   * Tests if files can be removed after the import has finished.
   */
  public function testRemoveFileAfterImport() {
    $this->setSettings('node', 'FeedsFileFetcher', array(
      'delete_uploaded_file' => TRUE,
      'directory' => 'private://feeds',
    ));

    // Import the file.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv');
    $this->assertText('Created 2 nodes');

    // Assert that the file no longer exists.
    $this->assertFalse(file_exists('private://feeds/content.csv'), 'The imported file no longer exists.');

    // Assert that the file is no longer shown on the import form.
    $this->drupalGet('import/node');
    $this->assertNoText('nodes.csv');
  }

  /**
   * Tests if files can be removed after import when running the import in
   * background.
   */
  public function testRemoveFileAfterImportInBackground() {
    // Configure to import in background and import as often as possible.
    $this->setSettings('node', NULL, array(
      'import_period' => 0,
      'import_on_create' => FALSE,
      'process_in_background' => TRUE,
    ));
    $this->setSettings('node', 'FeedsFileFetcher', array(
      'delete_uploaded_file' => TRUE,
      'directory' => 'private://feeds',
    ));

    // Make sure that the import cannot be completed in one run.
    variable_set('feeds_process_limit', 5);

    // Set variable to enforce that only five items get imported per cron run.
    // @see feeds_tests_cron_queue_alter()
    // @see feeds_tests_feeds_after_save()
    variable_set('feeds_tests_feeds_source_import_queue_time', 5);
    variable_set('feeds_tests_feeds_after_save_sleep', 1);

    // Import a file with 9 nodes.
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/nodes.csv');

    // Assert that the file has been created.
    $this->assertTrue(file_exists('private://feeds/nodes.csv'), 'The imported file is created.');

    // Run cron and assert that five nodes have been created.
    $this->cronRun();
    $node_count = db_select('node')
      ->fields('node', array())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(5, $node_count, format_string('Five nodes have been created (actual: @count).', array(
      '@count' => $node_count,
    )));

    // Assert that the file to import still exists as the import hasn't finished
    // yet.
    drupal_flush_all_caches();
    $this->assertTrue(file_exists('private://feeds/nodes.csv'), 'The imported file still exists.');

    // Run cron again to import the remaining 4 nodes and assert that 9 nodes
    // exist in total.
    $this->cronRun();
    $node_count = db_select('node')
      ->fields('node', array())
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual(9, $node_count, format_string('Nine nodes have been created (actual: @count).', array(
      '@count' => $node_count,
    )));

    // Assert that the file to import finally has been removed now.
    drupal_flush_all_caches();
    $this->assertFalse(file_exists('private://feeds/nodes.csv'), 'The imported file no longer exists.');

    // Assert that running a second import does not result into errors.
    $this->cronRun();

    // Assert that the file is no longer shown on the import form.
    $this->drupalGet('import/node');
    $this->assertNoText('nodes.csv');
  }