Pull merge.
[yaffs-website] / web / core / modules / aggregator / tests / src / Functional / ImportOpmlTest.php
1 <?php
2
3 namespace Drupal\Tests\aggregator\Functional;
4
5 use Drupal\aggregator\Entity\Feed;
6
7 /**
8  * Tests OPML import.
9  *
10  * @group aggregator
11  */
12 class ImportOpmlTest extends AggregatorTestBase {
13
14   /**
15    * Modules to install.
16    *
17    * @var array
18    */
19   public static $modules = ['block', 'help'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26
27     $admin_user = $this->drupalCreateUser(['administer news feeds', 'access news feeds', 'create article content', 'administer blocks']);
28     $this->drupalLogin($admin_user);
29   }
30
31   /**
32    * Opens OPML import form.
33    */
34   public function openImportForm() {
35     // Enable the help block.
36     $this->drupalPlaceBlock('help_block', ['region' => 'help']);
37
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.');
43   }
44
45   /**
46    * Submits form filled with invalid fields.
47    */
48   public function validateImportFormFields() {
49     $count_query = \Drupal::entityQuery('aggregator_feed')->count();
50     $before = $count_query->execute();
51
52     $edit = [];
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.');
55
56     $path = $this->getEmptyOpml();
57     $edit = [
58       'files[upload]' => $path,
59       'remote' => file_create_url($path),
60     ];
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.');
63
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.');
67
68     $after = $count_query->execute();
69     $this->assertEqual($before, $after, 'No feeds were added during the three last form submissions.');
70   }
71
72   /**
73    * Submits form with invalid, empty, and valid OPML files.
74    */
75   protected function submitImportForm() {
76     $count_query = \Drupal::entityQuery('aggregator_feed')->count();
77     $before = $count_query->execute();
78
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.');
82
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.');
86
87     $after = $count_query->execute();
88     $this->assertEqual($before, $after, 'No feeds were added during the two last form submissions.');
89
90     foreach (Feed::loadMultiple() as $feed) {
91       $feed->delete();
92     }
93
94     $feeds[0] = $this->getFeedEditArray();
95     $feeds[1] = $this->getFeedEditArray();
96     $feeds[2] = $this->getFeedEditArray();
97     $edit = [
98       'files[upload]' => $this->getValidOpml($feeds),
99       'refresh'       => '900',
100     ];
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');
104
105     $after = $count_query->execute();
106     $this->assertEqual($after, 2, 'Verifying that two distinct feeds were added.');
107
108     $feed_entities = Feed::loadMultiple();
109     $refresh = TRUE;
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;
114     }
115
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.');
119   }
120
121   /**
122    * Tests the import of an OPML file.
123    */
124   public function testOpmlImport() {
125     $this->openImportForm();
126     $this->validateImportFormFields();
127     $this->submitImportForm();
128   }
129
130 }