3 namespace Drupal\Tests\aggregator\Functional;
5 use Drupal\aggregator\Entity\Feed;
12 class ImportOpmlTest extends AggregatorTestBase {
19 public static $modules = ['block', 'help'];
24 protected function setUp() {
27 $admin_user = $this->drupalCreateUser(['administer news feeds', 'access news feeds', 'create article content', 'administer blocks']);
28 $this->drupalLogin($admin_user);
32 * Opens OPML import form.
34 public function openImportForm() {
35 // Enable the help block.
36 $this->drupalPlaceBlock('help_block', ['region' => 'help']);
38 $this->drupalGet('admin/config/services/aggregator/add/opml');
39 $this->assertText('A single OPML document may contain many feeds.', 'Found OPML help text.');
40 $this->assertField('files[upload]', 'Found file upload field.');
41 $this->assertField('remote', 'Found Remote URL field.');
42 $this->assertField('refresh', '', 'Found Refresh field.');
46 * Submits form filled with invalid fields.
48 public function validateImportFormFields() {
49 $count_query = \Drupal::entityQuery('aggregator_feed')->count();
50 $before = $count_query->execute();
53 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
54 $this->assertRaw(t('<em>Either</em> upload a file or enter a URL.'), 'Error if no fields are filled.');
56 $path = $this->getEmptyOpml();
58 'files[upload]' => $path,
59 'remote' => file_create_url($path),
61 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
62 $this->assertRaw(t('<em>Either</em> upload a file or enter a URL.'), 'Error if both fields are filled.');
64 $edit = ['remote' => 'invalidUrl://empty'];
65 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
66 $this->assertText(t('The URL invalidUrl://empty is not valid.'), 'Error if the URL is invalid.');
68 $after = $count_query->execute();
69 $this->assertEqual($before, $after, 'No feeds were added during the three last form submissions.');
73 * Submits form with invalid, empty, and valid OPML files.
75 protected function submitImportForm() {
76 $count_query = \Drupal::entityQuery('aggregator_feed')->count();
77 $before = $count_query->execute();
79 $form['files[upload]'] = $this->getInvalidOpml();
80 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $form, t('Import'));
81 $this->assertText(t('No new feed has been added.'), 'Attempting to upload invalid XML.');
83 $edit = ['remote' => file_create_url($this->getEmptyOpml())];
84 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
85 $this->assertText(t('No new feed has been added.'), 'Attempting to load empty OPML from remote URL.');
87 $after = $count_query->execute();
88 $this->assertEqual($before, $after, 'No feeds were added during the two last form submissions.');
90 foreach (Feed::loadMultiple() as $feed) {
94 $feeds[0] = $this->getFeedEditArray();
95 $feeds[1] = $this->getFeedEditArray();
96 $feeds[2] = $this->getFeedEditArray();
98 'files[upload]' => $this->getValidOpml($feeds),
101 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
102 $this->assertRaw(t('A feed with the URL %url already exists.', ['%url' => $feeds[0]['url[0][value]']]), 'Verifying that a duplicate URL was identified');
103 $this->assertRaw(t('A feed named %title already exists.', ['%title' => $feeds[1]['title[0][value]']]), 'Verifying that a duplicate title was identified');
105 $after = $count_query->execute();
106 $this->assertEqual($after, 2, 'Verifying that two distinct feeds were added.');
108 $feed_entities = Feed::loadMultiple();
110 foreach ($feed_entities as $feed_entity) {
111 $title[$feed_entity->getUrl()] = $feed_entity->label();
112 $url[$feed_entity->label()] = $feed_entity->getUrl();
113 $refresh = $refresh && $feed_entity->getRefreshRate() == 900;
116 $this->assertEqual($title[$feeds[0]['url[0][value]']], $feeds[0]['title[0][value]'], 'First feed was added correctly.');
117 $this->assertEqual($url[$feeds[1]['title[0][value]']], $feeds[1]['url[0][value]'], 'Second feed was added correctly.');
118 $this->assertTrue($refresh, 'Refresh times are correct.');
122 * Tests the import of an OPML file.
124 public function testOpmlImport() {
125 $this->openImportForm();
126 $this->validateImportFormFields();
127 $this->submitImportForm();