Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / aggregator / tests / src / Functional / ImportOpmlTest.php
1 <?php
2
3 namespace Drupal\Tests\aggregator\Functional;
4
5 /**
6  * Tests OPML import.
7  *
8  * @group aggregator
9  */
10 class ImportOpmlTest extends AggregatorTestBase {
11
12   /**
13    * Modules to install.
14    *
15    * @var array
16    */
17   public static $modules = ['block', 'help'];
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     parent::setUp();
24
25     $admin_user = $this->drupalCreateUser(['administer news feeds', 'access news feeds', 'create article content', 'administer blocks']);
26     $this->drupalLogin($admin_user);
27   }
28
29   /**
30    * Opens OPML import form.
31    */
32   public function openImportForm() {
33     // Enable the help block.
34     $this->drupalPlaceBlock('help_block', ['region' => 'help']);
35
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.');
41   }
42
43   /**
44    * Submits form filled with invalid fields.
45    */
46   public function validateImportFormFields() {
47     $before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
48
49     $edit = [];
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.');
52
53     $path = $this->getEmptyOpml();
54     $edit = [
55       'files[upload]' => $path,
56       'remote' => file_create_url($path),
57     ];
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.');
60
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.');
64
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.');
67   }
68
69   /**
70    * Submits form with invalid, empty, and valid OPML files.
71    */
72   protected function submitImportForm() {
73     $before = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
74
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.');
78
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.');
82
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.');
85
86     db_delete('aggregator_feed')->execute();
87
88     $feeds[0] = $this->getFeedEditArray();
89     $feeds[1] = $this->getFeedEditArray();
90     $feeds[2] = $this->getFeedEditArray();
91     $edit = [
92       'files[upload]' => $this->getValidOpml($feeds),
93       'refresh'       => '900',
94     ];
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');
98
99     $after = db_query('SELECT COUNT(*) FROM {aggregator_feed}')->fetchField();
100     $this->assertEqual($after, 2, 'Verifying that two distinct feeds were added.');
101
102     $feeds_from_db = db_query("SELECT title, url, refresh FROM {aggregator_feed}");
103     $refresh = TRUE;
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;
108     }
109
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.');
113   }
114
115   /**
116    * Tests the import of an OPML file.
117    */
118   public function testOpmlImport() {
119     $this->openImportForm();
120     $this->validateImportFormFields();
121     $this->submitImportForm();
122   }
123
124 }