3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\views\Views;
9 * Ensure that data added as terms appears in RSS feeds if "RSS Category" format
14 class RssTest extends TaxonomyTestBase {
21 public static $modules = ['node', 'field_ui', 'views'];
24 * Vocabulary for testing.
26 * @var \Drupal\taxonomy\VocabularyInterface
28 protected $vocabulary;
31 * Name of the taxonomy term reference field.
37 protected function setUp() {
40 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access', 'administer content types', 'administer node display']));
41 $this->vocabulary = $this->createVocabulary();
42 $this->fieldName = 'taxonomy_' . $this->vocabulary->id();
46 $this->vocabulary->id() => $this->vocabulary->id(),
48 'auto_create' => TRUE,
50 $this->createEntityReferenceField('node', 'article', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
52 entity_get_form_display('node', 'article', 'default')
53 ->setComponent($this->fieldName, [
54 'type' => 'options_select',
57 entity_get_display('node', 'article', 'default')
58 ->setComponent($this->fieldName, [
59 'type' => 'entity_reference_label',
65 * Tests that terms added to nodes are displayed in core RSS feed.
67 * Create a node and assert that taxonomy terms appear in rss.xml.
69 public function testTaxonomyRss() {
70 // Create two taxonomy terms.
71 $term1 = $this->createTerm($this->vocabulary);
73 // RSS display must be added manually.
74 $this->drupalGet("admin/structure/types/manage/article/display");
76 "display_modes_custom[rss]" => '1',
78 $this->drupalPostForm(NULL, $edit, t('Save'));
80 // Change the format to 'RSS category'.
81 $this->drupalGet("admin/structure/types/manage/article/display/rss");
83 "fields[taxonomy_" . $this->vocabulary->id() . "][type]" => 'entity_reference_rss_category',
84 "fields[taxonomy_" . $this->vocabulary->id() . "][region]" => 'content',
86 $this->drupalPostForm(NULL, $edit, t('Save'));
90 $edit['title[0][value]'] = $this->randomMachineName();
91 $edit[$this->fieldName . '[]'] = $term1->id();
92 $this->drupalPostForm('node/add/article', $edit, t('Save'));
94 // Check that the term is displayed when the RSS feed is viewed.
95 $this->drupalGet('rss.xml');
96 $test_element = sprintf(
97 '<category %s>%s</category>',
98 'domain="' . $term1->url('canonical', ['absolute' => TRUE]) . '"',
101 $this->assertRaw($test_element, 'Term is displayed when viewing the rss feed.');
103 // Test that the feed icon exists for the term.
104 $this->drupalGet("taxonomy/term/{$term1->id()}");
105 $this->assertLinkByHref("taxonomy/term/{$term1->id()}/feed");
107 // Test that the feed page exists for the term.
108 $this->drupalGet("taxonomy/term/{$term1->id()}/feed");
109 $assert = $this->assertSession();
110 $assert->responseHeaderContains('Content-Type', 'application/rss+xml');
111 // Ensure the RSS version is 2.0.
112 $rss_array = $this->getSession()->getDriver()->find('rss');
113 $this->assertEquals('2.0', reset($rss_array)->getAttribute('version'));
115 // Check that the "Exception value" is disabled by default.
116 $this->drupalGet('taxonomy/term/all/feed');
117 $this->assertResponse(404);
118 // Set the exception value to 'all'.
119 $view = Views::getView('taxonomy_term');
120 $arguments = $view->getDisplay()->getOption('arguments');
121 $arguments['tid']['exception']['value'] = 'all';
122 $view->getDisplay()->overrideOption('arguments', $arguments);
123 $view->storage->save();
124 // Check the article is shown in the feed.
125 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
126 $raw_xml = '<title>' . $node->label() . '</title>';
127 $this->drupalGet('taxonomy/term/all/feed');
128 $this->assertRaw($raw_xml, "Raw text '$raw_xml' is found.");
129 // Unpublish the article and check that it is not shown in the feed.
130 $node->setUnpublished()->save();
131 $this->drupalGet('taxonomy/term/all/feed');
132 $this->assertNoRaw($raw_xml);