26d14dbfb4983278f41f250359289dfef3906e62
[yaffs-website] / web / core / modules / file / tests / src / Functional / FileFieldRSSContentTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Ensure that files added to nodes appear correctly in RSS feeds.
9  *
10  * @group file
11  */
12 class FileFieldRSSContentTest extends FileFieldTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['node', 'views'];
20
21   /**
22    * Tests RSS enclosure formatter display for RSS feeds.
23    */
24   public function testFileFieldRSSContent() {
25     $node_storage = $this->container->get('entity.manager')->getStorage('node');
26     $field_name = strtolower($this->randomMachineName());
27     $type_name = 'article';
28
29     $this->createFileField($field_name, 'node', $type_name);
30
31     // RSS display must be added manually.
32     $this->drupalGet("admin/structure/types/manage/$type_name/display");
33     $edit = [
34       "display_modes_custom[rss]" => '1',
35     ];
36     $this->drupalPostForm(NULL, $edit, t('Save'));
37
38     // Change the format to 'RSS enclosure'.
39     $this->drupalGet("admin/structure/types/manage/$type_name/display/rss");
40     $edit = [
41       "fields[$field_name][type]" => 'file_rss_enclosure',
42       "fields[$field_name][region]" => 'content',
43     ];
44     $this->drupalPostForm(NULL, $edit, t('Save'));
45
46     // Create a new node with a file field set. Promote to frontpage
47     // needs to be set so this node will appear in the RSS feed.
48     $node = $this->drupalCreateNode(['type' => $type_name, 'promote' => 1]);
49     $test_file = $this->getTestFile('text');
50
51     // Create a new node with the uploaded file.
52     $nid = $this->uploadNodeFile($test_file, $field_name, $node->id());
53
54     // Get the uploaded file from the node.
55     $node_storage->resetCache([$nid]);
56     $node = $node_storage->load($nid);
57     $node_file = File::load($node->{$field_name}->target_id);
58
59     // Check that the RSS enclosure appears in the RSS feed.
60     $this->drupalGet('rss.xml');
61     $uploaded_filename = str_replace('public://', '', $node_file->getFileUri());
62     $selector = sprintf(
63       'enclosure[@url="%s"][@length="%s"][@type="%s"]',
64       file_create_url("public://$uploaded_filename", ['absolute' => TRUE]),
65       $node_file->getSize(),
66       $node_file->getMimeType()
67     );
68     $this->assertNotNull($this->getSession()->getDriver()->find('xpath', $selector), 'File field RSS enclosure is displayed when viewing the RSS feed.');
69   }
70
71 }