5f1e7073df82c63f6b29922b433325dd7108dae4
[yaffs-website] / web / core / modules / file / src / Tests / FileOnTranslatedEntityTest.php
1 <?php
2
3 namespace Drupal\file\Tests;
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     // 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]);
36
37     // Create a file field on the "Basic page" node type.
38     $this->fieldName = strtolower($this->randomMachineName());
39     $this->createFileField($this->fieldName, 'node', 'page');
40
41     // Create and log in user.
42     $permissions = [
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',
52     ];
53     $admin_user = $this->drupalCreateUser($permissions);
54     $this->drupalLogin($admin_user);
55
56     // Add a second and third language.
57     $edit = [];
58     $edit['predefined_langcode'] = 'fr';
59     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
60
61     $edit = [];
62     $edit['predefined_langcode'] = 'nl';
63     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
64
65     // Enable translation for "Basic page" nodes.
66     $edit = [
67       'entity_types[node]' => 1,
68       'settings[node][page][translatable]' => 1,
69       "settings[node][page][fields][$this->fieldName]" => 1,
70     ];
71     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
72     \Drupal::entityManager()->clearCachedDefinitions();
73   }
74
75   /**
76    * Tests synced file fields on translated nodes.
77    */
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.');
82
83     // Create a default language node.
84     $default_language_node = $this->drupalCreateNode(['type' => 'page', 'title' => 'Lost in translation']);
85
86     // Edit the node to upload a file.
87     $edit = [];
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();
92
93     // Translate the node into French: remove the existing file.
94     $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/fr', [], t('Remove'));
95
96     // Upload a different file.
97     $edit = [];
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();
106
107     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
108
109     /* @var $file \Drupal\file\FileInterface */
110
111     // Ensure the file status of the first file permanent.
112     $file = File::load($first_fid);
113     $this->assertTrue($file->isPermanent());
114
115     // Ensure the file status of the second file is permanent.
116     $file = File::load($second_fid);
117     $this->assertTrue($file->isPermanent());
118
119     // Translate the node into dutch: remove the existing file.
120     $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/nl', [], t('Remove'));
121
122     // Upload a different file.
123     $edit = [];
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();
129
130     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
131
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');
138
139     // Ensure the file status of the second file is permanent.
140     $file = File::load($second_fid);
141     $this->assertTrue($file->isPermanent());
142
143     // Ensure the file status of the third file is permanent.
144     $file = File::load($third_fid);
145     $this->assertTrue($file->isPermanent());
146
147     // Edit the second translation: remove the existing file.
148     $this->drupalPostForm('fr/node/' . $default_language_node->id() . '/edit', [], t('Remove'));
149
150     // Upload a different file.
151     $edit = [];
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();
157
158     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
159
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.');
163
164     $file = File::load($third_fid);
165     $this->assertTrue($file->isPermanent());
166
167     // Ensure the file status of the replaced second file is permanent.
168     $file = File::load($replaced_second_fid);
169     $this->assertTrue($file->isPermanent());
170
171     // Delete the third translation.
172     $this->drupalPostForm('nl/node/' . $default_language_node->id() . '/delete', [], t('Delete Dutch translation'));
173
174     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
175
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.');
179
180     $file = File::load($replaced_second_fid);
181     $this->assertTrue($file->isPermanent());
182
183     // Ensure the file status of the third file is now temporary.
184     $file = File::load($third_fid);
185     $this->assertTrue($file->isTemporary());
186
187     // Delete the all translations.
188     $this->drupalPostForm('node/' . $default_language_node->id() . '/delete', [], t('Delete all translations'));
189
190     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
191
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.');
195
196     $file = File::load($replaced_second_fid);
197     $this->assertTrue($file->isTemporary());
198   }
199
200 }