1<?php
2
3namespace Drupal\Tests\taxonomy\Functional;
4
5use Drupal\Core\Field\FieldStorageDefinitionInterface;
6use Drupal\views\Views;
7
8/**
9 * Ensure that data added as terms appears in RSS feeds if "RSS Category" format
10 * is selected.
11 *
12 * @group taxonomy
13 */
14class RssTest extends TaxonomyTestBase {
15
16  /**
17   * Modules to enable.
18   *
19   * @var array
20   */
21  protected static $modules = ['node', 'field_ui', 'views'];
22
23  /**
24   * {@inheritdoc}
25   */
26  protected $defaultTheme = 'stark';
27
28  /**
29   * Vocabulary for testing.
30   *
31   * @var \Drupal\taxonomy\VocabularyInterface
32   */
33  protected $vocabulary;
34
35  /**
36   * Name of the taxonomy term reference field.
37   *
38   * @var string
39   */
40  protected $fieldName;
41
42  protected function setUp(): void {
43    parent::setUp();
44
45    $this->drupalLogin($this->drupalCreateUser([
46      'administer taxonomy',
47      'bypass node access',
48      'administer content types',
49      'administer node display',
50    ]));
51    $this->vocabulary = $this->createVocabulary();
52    $this->fieldName = 'taxonomy_' . $this->vocabulary->id();
53
54    $handler_settings = [
55      'target_bundles' => [
56        $this->vocabulary->id() => $this->vocabulary->id(),
57      ],
58      'auto_create' => TRUE,
59    ];
60    $this->createEntityReferenceField('node', 'article', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
61
62    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
63    $display_repository = \Drupal::service('entity_display.repository');
64
65    $display_repository->getFormDisplay('node', 'article')
66      ->setComponent($this->fieldName, [
67        'type' => 'options_select',
68      ])
69      ->save();
70    $display_repository->getViewDisplay('node', 'article')
71      ->setComponent($this->fieldName, [
72        'type' => 'entity_reference_label',
73      ])
74      ->save();
75  }
76
77  /**
78   * Tests that terms added to nodes are displayed in core RSS feed.
79   *
80   * Create a node and assert that taxonomy terms appear in rss.xml.
81   */
82  public function testTaxonomyRss() {
83    // Create two taxonomy terms.
84    $term1 = $this->createTerm($this->vocabulary);
85
86    // RSS display must be added manually.
87    $this->drupalGet("admin/structure/types/manage/article/display");
88    $edit = [
89      "display_modes_custom[rss]" => '1',
90    ];
91    $this->submitForm($edit, 'Save');
92
93    // Change the format to 'RSS category'.
94    $this->drupalGet("admin/structure/types/manage/article/display/rss");
95    $edit = [
96      "fields[taxonomy_" . $this->vocabulary->id() . "][type]" => 'entity_reference_rss_category',
97      "fields[taxonomy_" . $this->vocabulary->id() . "][region]" => 'content',
98    ];
99    $this->submitForm($edit, 'Save');
100
101    // Post an article.
102    $edit = [];
103    $edit['title[0][value]'] = $this->randomMachineName();
104    $edit[$this->fieldName . '[]'] = $term1->id();
105    $this->drupalGet('node/add/article');
106    $this->submitForm($edit, 'Save');
107
108    // Check that the term is displayed when the RSS feed is viewed.
109    $this->drupalGet('rss.xml');
110    $test_element = sprintf(
111      '<category %s>%s</category>',
112      'domain="' . $term1->toUrl('canonical', ['absolute' => TRUE])->toString() . '"',
113      $term1->getName()
114    );
115    $this->assertSession()->responseContains($test_element);
116
117    // Test that the feed icon exists for the term.
118    $this->drupalGet("taxonomy/term/{$term1->id()}");
119    $this->assertSession()->linkByHrefExists("taxonomy/term/{$term1->id()}/feed");
120
121    // Test that the feed page exists for the term.
122    $this->drupalGet("taxonomy/term/{$term1->id()}/feed");
123    $assert = $this->assertSession();
124    $assert->responseHeaderContains('Content-Type', 'application/rss+xml');
125    // Ensure the RSS version is 2.0.
126    $rss_array = $this->getSession()->getDriver()->find('rss');
127    $this->assertEquals('2.0', reset($rss_array)->getAttribute('version'));
128
129    // Check that the "Exception value" is disabled by default.
130    $this->drupalGet('taxonomy/term/all/feed');
131    $this->assertSession()->statusCodeEquals(404);
132    // Set the exception value to 'all'.
133    $view = Views::getView('taxonomy_term');
134    $arguments = $view->getDisplay()->getOption('arguments');
135    $arguments['tid']['exception']['value'] = 'all';
136    $view->getDisplay()->overrideOption('arguments', $arguments);
137    $view->storage->save();
138    // Check the article is shown in the feed.
139    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
140    $raw_xml = '<title>' . $node->label() . '</title>';
141    $this->drupalGet('taxonomy/term/all/feed');
142    $this->assertSession()->responseContains($raw_xml);
143    // Unpublish the article and check that it is not shown in the feed.
144    $node->setUnpublished()->save();
145    $this->drupalGet('taxonomy/term/all/feed');
146    $this->assertSession()->responseNotContains($raw_xml);
147  }
148
149}
150