3 namespace Drupal\Tests\aggregator\Functional;
10 class ImportOpmlTest extends AggregatorTestBase {
17 public static $modules = ['block', 'help'];
22 protected function setUp() {
25 $admin_user = $this->drupalCreateUser(['administer news feeds', 'access news feeds', 'create article content', 'administer blocks']);
26 $this->drupalLogin($admin_user);
30 * Opens OPML import form.
32 public function openImportForm() {
33 // Enable the help block.
34 $this->drupalPlaceBlock('help_block', ['region' => 'help']);
36 $this->drupalGet('admin/config/services/aggregator/add/opml');
37 $this->assertText('A single OPML document may contain many feeds.', 'Found OPML help text.');
38 $this->assertField('files[upload]', 'Found file upload field.');
39 $this->assertField('remote', 'Found Remote URL field.');
40 $this->assertField('refresh', '', 'Found Refresh field.');
44 * Submits form filled with invalid fields.
46 public function validateImportFormFields() {
47 $before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
50 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
51 $this->assertRaw(t('<em>Either</em> upload a file or enter a URL.'), 'Error if no fields are filled.');
53 $path = $this->getEmptyOpml();
55 'files[upload]' => $path,
56 'remote' => file_create_url($path),
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 both fields are filled.');
61 $edit = ['remote' => 'invalidUrl://empty'];
62 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
63 $this->assertText(t('The URL invalidUrl://empty is not valid.'), 'Error if the URL is invalid.');
65 $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
66 $this->assertEqual($before, $after, 'No feeds were added during the three last form submissions.');
70 * Submits form with invalid, empty, and valid OPML files.
72 protected function submitImportForm() {
73 $before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
75 $form['files[upload]'] = $this->getInvalidOpml();
76 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $form, t('Import'));
77 $this->assertText(t('No new feed has been added.'), 'Attempting to upload invalid XML.');
79 $edit = ['remote' => file_create_url($this->getEmptyOpml())];
80 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
81 $this->assertText(t('No new feed has been added.'), 'Attempting to load empty OPML from remote URL.');
83 $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
84 $this->assertEqual($before, $after, 'No feeds were added during the two last form submissions.');
86 db_delete('aggregator_feed')->execute();
88 $feeds[0] = $this->getFeedEditArray();
89 $feeds[1] = $this->getFeedEditArray();
90 $feeds[2] = $this->getFeedEditArray();
92 'files[upload]' => $this->getValidOpml($feeds),
95 $this->drupalPostForm('admin/config/services/aggregator/add/opml', $edit, t('Import'));
96 $this->assertRaw(t('A feed with the URL %url already exists.', ['%url' => $feeds[0]['url[0][value]']]), 'Verifying that a duplicate URL was identified');
97 $this->assertRaw(t('A feed named %title already exists.', ['%title' => $feeds[1]['title[0][value]']]), 'Verifying that a duplicate title was identified');
99 $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
100 $this->assertEqual($after, 2, 'Verifying that two distinct feeds were added.');
102 $feeds_from_db = db_query("SELECT title, url, refresh FROM {aggregator_feed}");
104 foreach ($feeds_from_db as $feed) {
105 $title[$feed->url] = $feed->title;
106 $url[$feed->title] = $feed->url;
107 $refresh = $refresh && $feed->refresh == 900;
110 $this->assertEqual($title[$feeds[0]['url[0][value]']], $feeds[0]['title[0][value]'], 'First feed was added correctly.');
111 $this->assertEqual($url[$feeds[1]['title[0][value]']], $feeds[1]['url[0][value]'], 'Second feed was added correctly.');
112 $this->assertTrue($refresh, 'Refresh times are correct.');
116 * Tests the import of an OPML file.
118 public function testOpmlImport() {
119 $this->openImportForm();
120 $this->validateImportFormFields();
121 $this->submitImportForm();