Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / src / Tests / RssTest.php
1 <?php
2
3 namespace Drupal\taxonomy\Tests;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use 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  */
14 class RssTest extends TaxonomyTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['node', 'field_ui', 'views'];
22
23   /**
24    * Vocabulary for testing.
25    *
26    * @var \Drupal\taxonomy\VocabularyInterface
27    */
28   protected $vocabulary;
29
30   /**
31    * Name of the taxonomy term reference field.
32    *
33    * @var string
34    */
35   protected $fieldName;
36
37   protected function setUp() {
38     parent::setUp();
39
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();
43
44     $handler_settings = [
45       'target_bundles' => [
46         $this->vocabulary->id() => $this->vocabulary->id(),
47       ],
48       'auto_create' => TRUE,
49     ];
50     $this->createEntityReferenceField('node', 'article', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
51
52     entity_get_form_display('node', 'article', 'default')
53       ->setComponent($this->fieldName, [
54         'type' => 'options_select',
55       ])
56       ->save();
57     entity_get_display('node', 'article', 'default')
58       ->setComponent($this->fieldName, [
59         'type' => 'entity_reference_label',
60       ])
61       ->save();
62   }
63
64   /**
65    * Tests that terms added to nodes are displayed in core RSS feed.
66    *
67    * Create a node and assert that taxonomy terms appear in rss.xml.
68    */
69   public function testTaxonomyRss() {
70     // Create two taxonomy terms.
71     $term1 = $this->createTerm($this->vocabulary);
72
73     // RSS display must be added manually.
74     $this->drupalGet("admin/structure/types/manage/article/display");
75     $edit = [
76       "display_modes_custom[rss]" => '1',
77     ];
78     $this->drupalPostForm(NULL, $edit, t('Save'));
79
80     // Change the format to 'RSS category'.
81     $this->drupalGet("admin/structure/types/manage/article/display/rss");
82     $edit = [
83       "fields[taxonomy_" . $this->vocabulary->id() . "][type]" => 'entity_reference_rss_category',
84       "fields[taxonomy_" . $this->vocabulary->id() . "][region]" => 'content',
85     ];
86     $this->drupalPostForm(NULL, $edit, t('Save'));
87
88     // Post an article.
89     $edit = [];
90     $edit['title[0][value]'] = $this->randomMachineName();
91     $edit[$this->fieldName . '[]'] = $term1->id();
92     $this->drupalPostForm('node/add/article', $edit, t('Save'));
93
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]) . '"',
99       $term1->getName()
100     );
101     $this->assertRaw($test_element, 'Term is displayed when viewing the rss feed.');
102
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");
106
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.");
110
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);
129   }
130
131 }