3 namespace Drupal\file\Tests;
5 use Drupal\file\Entity\File;
8 * Uploads files to translated nodes.
12 class FileOnTranslatedEntityTest extends FileFieldTestBase {
17 public static $modules = ['language', 'content_translation'];
20 * The name of the file field used in the test.
29 protected function setUp() {
32 // Create the "Basic page" node type.
33 // @todo Remove the disabling of new revision creation in
34 // https://www.drupal.org/node/1239558.
35 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page', 'new_revision' => FALSE]);
37 // Create a file field on the "Basic page" node type.
38 $this->fieldName = strtolower($this->randomMachineName());
39 $this->createFileField($this->fieldName, 'node', 'page');
41 // Create and log in user.
43 'access administration pages',
44 'administer content translation',
45 'administer content types',
46 'administer languages',
47 'create content translations',
48 'create page content',
49 'edit any page content',
50 'translate any entity',
51 'delete any page content',
53 $admin_user = $this->drupalCreateUser($permissions);
54 $this->drupalLogin($admin_user);
56 // Add a second and third language.
58 $edit['predefined_langcode'] = 'fr';
59 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
62 $edit['predefined_langcode'] = 'nl';
63 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
65 // Enable translation for "Basic page" nodes.
67 'entity_types[node]' => 1,
68 'settings[node][page][translatable]' => 1,
69 "settings[node][page][fields][$this->fieldName]" => 1,
71 $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
72 \Drupal::entityManager()->clearCachedDefinitions();
76 * Tests synced file fields on translated nodes.
78 public function testSyncedFiles() {
79 // Verify that the file field on the "Basic page" node type is translatable.
80 $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'page');
81 $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node file field is translatable.');
83 // Create a default language node.
84 $default_language_node = $this->drupalCreateNode(['type' => 'page', 'title' => 'Lost in translation']);
86 // Edit the node to upload a file.
88 $name = 'files[' . $this->fieldName . '_0]';
89 $edit[$name] = drupal_realpath($this->drupalGetTestFiles('text')[0]->uri);
90 $this->drupalPostForm('node/' . $default_language_node->id() . '/edit', $edit, t('Save'));
91 $first_fid = $this->getLastFileId();
93 // Translate the node into French: remove the existing file.
94 $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/fr', [], t('Remove'));
96 // Upload a different file.
98 $edit['title[0][value]'] = 'Bill Murray';
99 $name = 'files[' . $this->fieldName . '_0]';
100 $edit[$name] = drupal_realpath($this->drupalGetTestFiles('text')[1]->uri);
101 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
102 // This inspects the HTML after the post of the translation, the file
103 // should be displayed on the original node.
104 $this->assertRaw('file--mime-text-plain');
105 $second_fid = $this->getLastFileId();
107 \Drupal::entityTypeManager()->getStorage('file')->resetCache();
109 /* @var $file \Drupal\file\FileInterface */
111 // Ensure the file status of the first file permanent.
112 $file = File::load($first_fid);
113 $this->assertTrue($file->isPermanent());
115 // Ensure the file status of the second file is permanent.
116 $file = File::load($second_fid);
117 $this->assertTrue($file->isPermanent());
119 // Translate the node into dutch: remove the existing file.
120 $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/nl', [], t('Remove'));
122 // Upload a different file.
124 $edit['title[0][value]'] = 'Scarlett Johansson';
125 $name = 'files[' . $this->fieldName . '_0]';
126 $edit[$name] = drupal_realpath($this->drupalGetTestFiles('text')[2]->uri);
127 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
128 $third_fid = $this->getLastFileId();
130 \Drupal::entityTypeManager()->getStorage('file')->resetCache();
132 // Ensure the first file is untouched.
133 $file = File::load($first_fid);
134 $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
135 // This inspects the HTML after the post of the translation, the file
136 // should be displayed on the original node.
137 $this->assertRaw('file--mime-text-plain');
139 // Ensure the file status of the second file is permanent.
140 $file = File::load($second_fid);
141 $this->assertTrue($file->isPermanent());
143 // Ensure the file status of the third file is permanent.
144 $file = File::load($third_fid);
145 $this->assertTrue($file->isPermanent());
147 // Edit the second translation: remove the existing file.
148 $this->drupalPostForm('fr/node/' . $default_language_node->id() . '/edit', [], t('Remove'));
150 // Upload a different file.
152 $edit['title[0][value]'] = 'David Bowie';
153 $name = 'files[' . $this->fieldName . '_0]';
154 $edit[$name] = drupal_realpath($this->drupalGetTestFiles('text')[3]->uri);
155 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
156 $replaced_second_fid = $this->getLastFileId();
158 \Drupal::entityTypeManager()->getStorage('file')->resetCache();
160 // Ensure the first and third files are untouched.
161 $file = File::load($first_fid);
162 $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
164 $file = File::load($third_fid);
165 $this->assertTrue($file->isPermanent());
167 // Ensure the file status of the replaced second file is permanent.
168 $file = File::load($replaced_second_fid);
169 $this->assertTrue($file->isPermanent());
171 // Delete the third translation.
172 $this->drupalPostForm('nl/node/' . $default_language_node->id() . '/delete', [], t('Delete Dutch translation'));
174 \Drupal::entityTypeManager()->getStorage('file')->resetCache();
176 // Ensure the first and replaced second files are untouched.
177 $file = File::load($first_fid);
178 $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
180 $file = File::load($replaced_second_fid);
181 $this->assertTrue($file->isPermanent());
183 // Ensure the file status of the third file is now temporary.
184 $file = File::load($third_fid);
185 $this->assertTrue($file->isTemporary());
187 // Delete the all translations.
188 $this->drupalPostForm('node/' . $default_language_node->id() . '/delete', [], t('Delete all translations'));
190 \Drupal::entityTypeManager()->getStorage('file')->resetCache();
192 // Ensure the file status of the all files are now temporary.
193 $file = File::load($first_fid);
194 $this->assertTrue($file->isTemporary(), 'First file still exists and is temporary.');
196 $file = File::load($replaced_second_fid);
197 $this->assertTrue($file->isTemporary());