3 namespace Drupal\taxonomy\Tests;
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 $this->assertTrue(!empty($this->cssSelect('rss[version="2.0"]')), "Feed page is RSS.");
111 // Check that the "Exception value" is disabled by default.
112 $this->drupalGet('taxonomy/term/all/feed');
113 $this->assertResponse(404);
114 // Set the exception value to 'all'.
115 $view = Views::getView('taxonomy_term');
116 $arguments = $view->getDisplay()->getOption('arguments');
117 $arguments['tid']['exception']['value'] = 'all';
118 $view->getDisplay()->overrideOption('arguments', $arguments);
119 $view->storage->save();
120 // Check the article is shown in the feed.
121 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
122 $raw_xml = '<title>' . $node->label() . '</title>';
123 $this->drupalGet('taxonomy/term/all/feed');
124 $this->assertRaw($raw_xml, "Raw text '$raw_xml' is found.");
125 // Unpublish the article and check that it is not shown in the feed.
126 $node->setPublished(FALSE)->save();
127 $this->drupalGet('taxonomy/term/all/feed');
128 $this->assertNoRaw($raw_xml);