ImportOpmlTest.php 4.92 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\aggregator\Tests\ImportOpmlTest.
 */

namespace Drupal\aggregator\Tests;

10 11 12
/**
 * Tests importing feeds from OPML functionality for the Aggregator module.
 */
13
class ImportOpmlTest extends AggregatorTestBase {
14 15 16 17 18 19 20 21

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('block');

22 23 24 25 26 27 28 29
  public static function getInfo() {
    return array(
      'name' => 'Import feeds from OPML functionality',
      'description' => 'Test OPML import.',
      'group' => 'Aggregator',
    );
  }

30 31 32 33 34 35 36
  function setUp() {
    parent::setUp();

    $admin_user = $this->drupalCreateUser(array('administer news feeds', 'access news feeds', 'create article content', 'administer blocks'));
    $this->drupalLogin($admin_user);
  }

37
  /**
38
   * Opens OPML import form.
39 40
   */
  function openImportForm() {
41
    // Enable the help block.
42
    $this->drupalPlaceBlock('system_help_block', array('region' => 'help'));
43

44
    $this->drupalGet('admin/config/services/aggregator/add/opml');
45
    $this->assertText('A single OPML document may contain many feeds.', 'Found OPML help text.');
46 47 48
    $this->assertField('files[upload]', 'Found file upload field.');
    $this->assertField('remote', 'Found Remote URL field.');
    $this->assertField('refresh', '', 'Found Refresh field.');
49 50 51
  }

  /**
52
   * Submits form filled with invalid fields.
53 54 55 56 57
   */
  function validateImportFormFields() {
    $before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();

    $edit = array();
58
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
59
    $this->assertRaw(t('<em>Either</em> upload a file or enter a URL.'), 'Error if no fields are filled.');
60 61 62 63 64 65

    $path = $this->getEmptyOpml();
    $edit = array(
      'files[upload]' => $path,
      'remote' => file_create_url($path),
    );
66
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
67
    $this->assertRaw(t('<em>Either</em> upload a file or enter a URL.'), 'Error if both fields are filled.');
68 69

    $edit = array('remote' => 'invalidUrl://empty');
70
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
71 72 73
    $this->assertText(t('The URL invalidUrl://empty is not valid.'), 'Error if the URL is invalid.');

    $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
74
    $this->assertEqual($before, $after, 'No feeds were added during the three last form submissions.');
75 76 77
  }

  /**
78
   * Submits form with invalid, empty, and valid OPML files.
79 80 81 82 83
   */
  function submitImportForm() {
    $before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();

    $form['files[upload]'] = $this->getInvalidOpml();
84
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $form, t('Import'));
85
    $this->assertText(t('No new feed has been added.'), 'Attempting to upload invalid XML.');
86 87

    $edit = array('remote' => file_create_url($this->getEmptyOpml()));
88
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
89
    $this->assertText(t('No new feed has been added.'), 'Attempting to load empty OPML from remote URL.');
90 91

    $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
92
    $this->assertEqual($before, $after, 'No feeds were added during the two last form submissions.');
93 94 95 96 97 98 99 100 101 102

    db_delete('aggregator_feed')->execute();

    $feeds[0] = $this->getFeedEditArray();
    $feeds[1] = $this->getFeedEditArray();
    $feeds[2] = $this->getFeedEditArray();
    $edit = array(
      'files[upload]' => $this->getValidOpml($feeds),
      'refresh'       => '900',
    );
103
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
104 105
    $this->assertRaw(t('A feed with the URL %url already exists.', array('%url' => $feeds[0]['url[0][value]'])), 'Verifying that a duplicate URL was identified');
    $this->assertRaw(t('A feed named %title already exists.', array('%title' => $feeds[1]['title[0][value]'])), 'Verifying that a duplicate title was identified');
106 107

    $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
108
    $this->assertEqual($after, 2, 'Verifying that two distinct feeds were added.');
109

110 111
    $feeds_from_db = db_query("SELECT title, url, refresh FROM {aggregator_feed}");
    $refresh = TRUE;
112 113 114 115 116 117
    foreach ($feeds_from_db as $feed) {
      $title[$feed->url] = $feed->title;
      $url[$feed->title] = $feed->url;
      $refresh = $refresh && $feed->refresh == 900;
    }

118 119
    $this->assertEqual($title[$feeds[0]['url[0][value]']], $feeds[0]['title[0][value]'], 'First feed was added correctly.');
    $this->assertEqual($url[$feeds[1]['title[0][value]']], $feeds[1]['url[0][value]'], 'Second feed was added correctly.');
120
    $this->assertTrue($refresh, 'Refresh times are correct.');
121 122
  }

123 124 125
  /**
   * Tests the import of an OPML file.
   */
126 127 128 129 130 131
  function testOpmlImport() {
    $this->openImportForm();
    $this->validateImportFormFields();
    $this->submitImportForm();
  }
}