annotate core/modules/taxonomy/tests/src/Functional/RssTest.php @ 19:fa3358dc1485 tip

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