7228e97a9151cae11d53f51f6113fb48851275ce
[yaffs-website] / web / core / modules / file / tests / src / Functional / PrivateFileOnTranslatedEntityTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional;
4
5 use Drupal\file\Entity\File;
6 use Drupal\node\Entity\Node;
7
8 /**
9  * Uploads private files to translated node and checks access.
10  *
11  * @group file
12  */
13 class PrivateFileOnTranslatedEntityTest extends FileFieldTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['language', 'content_translation'];
19
20   /**
21    * The name of the file field used in the test.
22    *
23    * @var string
24    */
25   protected $fieldName;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     // Create the "Basic page" node type.
34     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
35
36     // Create a file field on the "Basic page" node type.
37     $this->fieldName = strtolower($this->randomMachineName());
38     $this->createFileField($this->fieldName, 'node', 'page', ['uri_scheme' => 'private']);
39
40     // Create and log in user.
41     $permissions = [
42       'access administration pages',
43       'administer content translation',
44       'administer content types',
45       'administer languages',
46       'create content translations',
47       'create page content',
48       'edit any page content',
49       'translate any entity',
50     ];
51     $admin_user = $this->drupalCreateUser($permissions);
52     $this->drupalLogin($admin_user);
53
54     // Add a second language.
55     $edit = [];
56     $edit['predefined_langcode'] = 'fr';
57     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
58
59     // Enable translation for "Basic page" nodes.
60     $edit = [
61       'entity_types[node]' => 1,
62       'settings[node][page][translatable]' => 1,
63       "settings[node][page][fields][$this->fieldName]" => 1,
64     ];
65     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
66     \Drupal::entityManager()->clearCachedDefinitions();
67   }
68
69   /**
70    * Tests private file fields on translated nodes.
71    */
72   public function testPrivateLanguageFile() {
73     // Verify that the file field on the "Basic page" node type is translatable.
74     $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'page');
75     $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node file field is translatable.');
76
77     // Create a default language node.
78     $default_language_node = $this->drupalCreateNode(['type' => 'page']);
79
80     // Edit the node to upload a file.
81     $edit = [];
82     $name = 'files[' . $this->fieldName . '_0]';
83     $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[0]->uri);
84     $this->drupalPostForm('node/' . $default_language_node->id() . '/edit', $edit, t('Save'));
85     $last_fid_prior = $this->getLastFileId();
86
87     // Languages are cached on many levels, and we need to clear those caches.
88     $this->rebuildContainer();
89
90     // Ensure the file can be downloaded.
91     \Drupal::entityManager()->getStorage('node')->resetCache([$default_language_node->id()]);
92     $node = Node::load($default_language_node->id());
93     $node_file = File::load($node->{$this->fieldName}->target_id);
94     $this->drupalGet(file_create_url($node_file->getFileUri()));
95     $this->assertResponse(200, 'Confirmed that the file attached to the English node can be downloaded.');
96
97     // Translate the node into French.
98     $this->drupalGet('node/' . $default_language_node->id() . '/translations');
99     $this->clickLink(t('Add'));
100
101     // Remove the existing file.
102     $this->drupalPostForm(NULL, [], t('Remove'));
103
104     // Upload a different file.
105     $edit = [];
106     $edit['title[0][value]'] = $this->randomMachineName();
107     $name = 'files[' . $this->fieldName . '_0]';
108     $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[1]->uri);
109     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
110     $last_fid = $this->getLastFileId();
111
112     // Verify the translation was created.
113     \Drupal::entityManager()->getStorage('node')->resetCache([$default_language_node->id()]);
114     $default_language_node = Node::load($default_language_node->id());
115     $this->assertTrue($default_language_node->hasTranslation('fr'), 'Node found in database.');
116     $this->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
117
118     // Ensure the file attached to the translated node can be downloaded.
119     $french_node = $default_language_node->getTranslation('fr');
120     $node_file = File::load($french_node->{$this->fieldName}->target_id);
121     $this->drupalGet(file_create_url($node_file->getFileUri()));
122     $this->assertResponse(200, 'Confirmed that the file attached to the French node can be downloaded.');
123   }
124
125 }