2e22a69c35381cf2800f5e6a0521e834a7ef09bf
[yaffs-website] / web / core / modules / file / tests / src / Functional / FileOnTranslatedEntityTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Uploads files to translated nodes.
9  *
10  * @group file
11  */
12 class FileOnTranslatedEntityTest extends FileFieldTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['language', 'content_translation'];
18
19   /**
20    * The name of the file field used in the test.
21    *
22    * @var string
23    */
24   protected $fieldName;
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31
32     // This test expects unused managed files to be marked as temporary a file.
33     $this->config('file.settings')
34       ->set('make_unused_managed_files_temporary', TRUE)
35       ->save();
36
37     // Create the "Basic page" node type.
38     // @todo Remove the disabling of new revision creation in
39     //   https://www.drupal.org/node/1239558.
40     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page', 'new_revision' => FALSE]);
41
42     // Create a file field on the "Basic page" node type.
43     $this->fieldName = strtolower($this->randomMachineName());
44     $this->createFileField($this->fieldName, 'node', 'page');
45
46     // Create and log in user.
47     $permissions = [
48       'access administration pages',
49       'administer content translation',
50       'administer content types',
51       'administer languages',
52       'create content translations',
53       'create page content',
54       'edit any page content',
55       'translate any entity',
56       'delete any page content',
57     ];
58     $admin_user = $this->drupalCreateUser($permissions);
59     $this->drupalLogin($admin_user);
60
61     // Add a second and third language.
62     $edit = [];
63     $edit['predefined_langcode'] = 'fr';
64     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
65
66     $edit = [];
67     $edit['predefined_langcode'] = 'nl';
68     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
69
70     // Enable translation for "Basic page" nodes.
71     $edit = [
72       'entity_types[node]' => 1,
73       'settings[node][page][translatable]' => 1,
74       "settings[node][page][fields][$this->fieldName]" => 1,
75     ];
76     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
77     \Drupal::entityManager()->clearCachedDefinitions();
78   }
79
80   /**
81    * Tests synced file fields on translated nodes.
82    */
83   public function testSyncedFiles() {
84     // Verify that the file field on the "Basic page" node type is translatable.
85     $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'page');
86     $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node file field is translatable.');
87
88     // Create a default language node.
89     $default_language_node = $this->drupalCreateNode(['type' => 'page', 'title' => 'Lost in translation']);
90
91     // Edit the node to upload a file.
92     $edit = [];
93     $name = 'files[' . $this->fieldName . '_0]';
94     $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[0]->uri);
95     $this->drupalPostForm('node/' . $default_language_node->id() . '/edit', $edit, t('Save'));
96     $first_fid = $this->getLastFileId();
97
98     // Translate the node into French: remove the existing file.
99     $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/fr', [], t('Remove'));
100
101     // Upload a different file.
102     $edit = [];
103     $edit['title[0][value]'] = 'Bill Murray';
104     $name = 'files[' . $this->fieldName . '_0]';
105     $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[1]->uri);
106     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
107     // This inspects the HTML after the post of the translation, the file
108     // should be displayed on the original node.
109     $this->assertRaw('file--mime-text-plain');
110     $second_fid = $this->getLastFileId();
111
112     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
113
114     /* @var $file \Drupal\file\FileInterface */
115
116     // Ensure the file status of the first file permanent.
117     $file = File::load($first_fid);
118     $this->assertTrue($file->isPermanent());
119
120     // Ensure the file status of the second file is permanent.
121     $file = File::load($second_fid);
122     $this->assertTrue($file->isPermanent());
123
124     // Translate the node into dutch: remove the existing file.
125     $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/nl', [], t('Remove'));
126
127     // Upload a different file.
128     $edit = [];
129     $edit['title[0][value]'] = 'Scarlett Johansson';
130     $name = 'files[' . $this->fieldName . '_0]';
131     $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[2]->uri);
132     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
133     $third_fid = $this->getLastFileId();
134
135     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
136
137     // Ensure the first file is untouched.
138     $file = File::load($first_fid);
139     $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
140     // This inspects the HTML after the post of the translation, the file
141     // should be displayed on the original node.
142     $this->assertRaw('file--mime-text-plain');
143
144     // Ensure the file status of the second file is permanent.
145     $file = File::load($second_fid);
146     $this->assertTrue($file->isPermanent());
147
148     // Ensure the file status of the third file is permanent.
149     $file = File::load($third_fid);
150     $this->assertTrue($file->isPermanent());
151
152     // Edit the second translation: remove the existing file.
153     $this->drupalPostForm('fr/node/' . $default_language_node->id() . '/edit', [], t('Remove'));
154
155     // Upload a different file.
156     $edit = [];
157     $edit['title[0][value]'] = 'David Bowie';
158     $name = 'files[' . $this->fieldName . '_0]';
159     $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[3]->uri);
160     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
161     $replaced_second_fid = $this->getLastFileId();
162
163     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
164
165     // Ensure the first and third files are untouched.
166     $file = File::load($first_fid);
167     $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
168
169     $file = File::load($third_fid);
170     $this->assertTrue($file->isPermanent());
171
172     // Ensure the file status of the replaced second file is permanent.
173     $file = File::load($replaced_second_fid);
174     $this->assertTrue($file->isPermanent());
175
176     // Delete the third translation.
177     $this->drupalPostForm('nl/node/' . $default_language_node->id() . '/delete', [], t('Delete Dutch translation'));
178
179     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
180
181     // Ensure the first and replaced second files are untouched.
182     $file = File::load($first_fid);
183     $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
184
185     $file = File::load($replaced_second_fid);
186     $this->assertTrue($file->isPermanent());
187
188     // Ensure the file status of the third file is now temporary.
189     $file = File::load($third_fid);
190     $this->assertTrue($file->isTemporary());
191
192     // Delete the all translations.
193     $this->drupalPostForm('node/' . $default_language_node->id() . '/delete', [], t('Delete all translations'));
194
195     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
196
197     // Ensure the file status of the all files are now temporary.
198     $file = File::load($first_fid);
199     $this->assertTrue($file->isTemporary(), 'First file still exists and is temporary.');
200
201     $file = File::load($replaced_second_fid);
202     $this->assertTrue($file->isTemporary());
203   }
204
205 }