View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\views\Views;
class RssTest extends TaxonomyTestBase {
protected static $modules = [
'node',
'field_ui',
'views',
];
protected $defaultTheme = 'stark';
protected $vocabulary;
protected $fieldName;
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer taxonomy',
'bypass node access',
'administer content types',
'administer node display',
]));
$this->vocabulary = $this
->createVocabulary();
$this->fieldName = 'taxonomy_' . $this->vocabulary
->id();
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', 'article', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', 'article')
->setComponent($this->fieldName, [
'type' => 'options_select',
])
->save();
$display_repository
->getViewDisplay('node', 'article')
->setComponent($this->fieldName, [
'type' => 'entity_reference_label',
])
->save();
}
public function testTaxonomyRss() {
$term1 = $this
->createTerm($this->vocabulary);
$this
->drupalGet("admin/structure/types/manage/article/display");
$edit = [
"display_modes_custom[rss]" => '1',
];
$this
->submitForm($edit, 'Save');
$this
->drupalGet("admin/structure/types/manage/article/display/rss");
$edit = [
"fields[taxonomy_" . $this->vocabulary
->id() . "][type]" => 'entity_reference_rss_category',
"fields[taxonomy_" . $this->vocabulary
->id() . "][region]" => 'content',
];
$this
->submitForm($edit, 'Save');
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName();
$edit[$this->fieldName . '[]'] = $term1
->id();
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('rss.xml');
$test_element = sprintf('<category %s>%s</category>', 'domain="' . $term1
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString() . '"', $term1
->getName());
$this
->assertSession()
->responseContains($test_element);
$this
->drupalGet("taxonomy/term/{$term1->id()}");
$this
->assertSession()
->linkByHrefExists("taxonomy/term/{$term1->id()}/feed");
$this
->drupalGet("taxonomy/term/{$term1->id()}/feed");
$assert = $this
->assertSession();
$assert
->responseHeaderContains('Content-Type', 'application/rss+xml');
$rss_array = $this
->getSession()
->getDriver()
->find('rss');
$this
->assertEquals('2.0', reset($rss_array)
->getAttribute('version'));
$this
->drupalGet('taxonomy/term/all/feed');
$this
->assertSession()
->statusCodeEquals(404);
$view = Views::getView('taxonomy_term');
$arguments = $view
->getDisplay()
->getOption('arguments');
$arguments['tid']['exception']['value'] = 'all';
$view
->getDisplay()
->overrideOption('arguments', $arguments);
$view->storage
->save();
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$raw_xml = '<title>' . $node
->label() . '</title>';
$this
->drupalGet('taxonomy/term/all/feed');
$this
->assertSession()
->responseContains($raw_xml);
$node
->setUnpublished()
->save();
$this
->drupalGet('taxonomy/term/all/feed');
$this
->assertSession()
->responseNotContains($raw_xml);
}
}