ImportOpmlTest.php 4.76 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\aggregator\Functional;
4

5
use Drupal\aggregator\Entity\Feed;
6

7
/**
8 9 10
 * Tests OPML import.
 *
 * @group aggregator
11
 */
12
class ImportOpmlTest extends AggregatorTestBase {
13 14

  /**
15
   * Modules to install.
16 17 18
   *
   * @var array
   */
19
  protected static $modules = ['block', 'help'];
20

21 22 23 24 25
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

26 27 28
  /**
   * {@inheritdoc}
   */
29
  protected function setUp(): void {
30 31
    parent::setUp();

32 33 34 35 36 37
    $admin_user = $this->drupalCreateUser([
      'administer news feeds',
      'access news feeds',
      'create article content',
      'administer blocks',
    ]);
38 39 40
    $this->drupalLogin($admin_user);
  }

41
  /**
42
   * Opens OPML import form.
43
   */
44
  public function openImportForm() {
45
    // Enable the help block.
46
    $this->drupalPlaceBlock('help_block', ['region' => 'help']);
47

48
    $this->drupalGet('admin/config/services/aggregator/add/opml');
49
    $this->assertText('A single OPML document may contain many feeds.');
50
    // Ensure that the file upload, remote URL, and refresh fields exist.
51 52 53
    $this->assertSession()->fieldExists('files[upload]');
    $this->assertSession()->fieldExists('remote');
    $this->assertSession()->fieldExists('refresh');
54 55 56
  }

  /**
57
   * Submits form filled with invalid fields.
58
   */
59
  public function validateImportFormFields() {
60
    $count_query = \Drupal::entityQuery('aggregator_feed')->accessCheck(FALSE)->count();
61
    $before = $count_query->execute();
62

63
    $edit = [];
64
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, 'Import');
65
    $this->assertRaw(t('<em>Either</em> upload a file or enter a URL.'));
66 67

    $path = $this->getEmptyOpml();
68
    $edit = [
69 70
      'files[upload]' => $path,
      'remote' => file_create_url($path),
71
    ];
72
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, 'Import');
73
    $this->assertRaw(t('<em>Either</em> upload a file or enter a URL.'));
74

75
    // Error if the URL is invalid.
76
    $edit = ['remote' => 'invalidUrl://empty'];
77
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, 'Import');
78
    $this->assertText('The URL invalidUrl://empty is not valid.');
79

80
    $after = $count_query->execute();
81
    $this->assertEqual($before, $after, 'No feeds were added during the three last form submissions.');
82 83 84
  }

  /**
85
   * Submits form with invalid, empty, and valid OPML files.
86
   */
87
  protected function submitImportForm() {
88
    $count_query = \Drupal::entityQuery('aggregator_feed')->accessCheck(FALSE)->count();
89
    $before = $count_query->execute();
90

91
    // Attempting to upload invalid XML.
92
    $form['files[upload]'] = $this->getInvalidOpml();
93
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $form, 'Import');
94
    $this->assertText('No new feed has been added.');
95

96
    // Attempting to load empty OPML from remote URL
97
    $edit = ['remote' => file_create_url($this->getEmptyOpml())];
98
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, 'Import');
99
    $this->assertText('No new feed has been added.');
100

101
    $after = $count_query->execute();
102
    $this->assertEqual($before, $after, 'No feeds were added during the two last form submissions.');
103

104 105 106
    foreach (Feed::loadMultiple() as $feed) {
      $feed->delete();
    }
107 108 109 110

    $feeds[0] = $this->getFeedEditArray();
    $feeds[1] = $this->getFeedEditArray();
    $feeds[2] = $this->getFeedEditArray();
111
    $edit = [
112 113
      'files[upload]' => $this->getValidOpml($feeds),
      'refresh'       => '900',
114
    ];
115
    $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, 'Import');
116 117 118 119
    // Verify that a duplicate URL was identified.
    $this->assertRaw(t('A feed with the URL %url already exists.', ['%url' => $feeds[0]['url[0][value]']]));
    // Verify that a duplicate title was identified.
    $this->assertRaw(t('A feed named %title already exists.', ['%title' => $feeds[1]['title[0][value]']]));
120

121
    $after = $count_query->execute();
122
    $this->assertEqual(2, $after, 'Verifying that two distinct feeds were added.');
123

124
    $feed_entities = Feed::loadMultiple();
125
    $refresh = TRUE;
126 127 128 129
    foreach ($feed_entities as $feed_entity) {
      $title[$feed_entity->getUrl()] = $feed_entity->label();
      $url[$feed_entity->label()] = $feed_entity->getUrl();
      $refresh = $refresh && $feed_entity->getRefreshRate() == 900;
130 131
    }

132 133
    $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.');
134
    $this->assertTrue($refresh, 'Refresh times are correct.');
135 136
  }

137 138 139
  /**
   * Tests the import of an OPML file.
   */
140
  public function testOpmlImport() {
141 142 143 144
    $this->openImportForm();
    $this->validateImportFormFields();
    $this->submitImportForm();
  }
145

146
}