annotate core/modules/taxonomy/tests/src/Functional/RssTest.php @ 5:12f9dff5fda9 tip

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