Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / image / src / Tests / ImageOnTranslatedEntityTest.php
1 <?php
2
3 namespace Drupal\image\Tests;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Uploads images to translated nodes.
9  *
10  * @group image
11  */
12 class ImageOnTranslatedEntityTest extends ImageFieldTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['language', 'content_translation', 'field_ui'];
18
19   /**
20    * The name of the image 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 a temporary file.
33     $this->config('file.settings')->set('make_unused_managed_files_temporary', TRUE)->save();
34
35     // Create the "Basic page" node type.
36     // @todo Remove the disabling of new revision creation in
37     //   https://www.drupal.org/node/1239558.
38     $this->drupalCreateContentType(['type' => 'basicpage', 'name' => 'Basic page', 'new_revision' => FALSE]);
39
40     // Create a image field on the "Basic page" node type.
41     $this->fieldName = strtolower($this->randomMachineName());
42     $this->createImageField($this->fieldName, 'basicpage', [], ['title_field' => 1]);
43
44     // Create and log in user.
45     $permissions = [
46       'access administration pages',
47       'administer content translation',
48       'administer content types',
49       'administer languages',
50       'administer node fields',
51       'create content translations',
52       'create basicpage content',
53       'edit any basicpage content',
54       'translate any entity',
55       'delete any basicpage content',
56     ];
57     $admin_user = $this->drupalCreateUser($permissions);
58     $this->drupalLogin($admin_user);
59
60     // Add a second and third language.
61     $edit = [];
62     $edit['predefined_langcode'] = 'fr';
63     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
64
65     $edit = [];
66     $edit['predefined_langcode'] = 'nl';
67     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
68   }
69
70   /**
71    * Tests synced file fields on translated nodes.
72    */
73   public function testSyncedImages() {
74     // Enable translation for "Basic page" nodes.
75     $edit = [
76       'entity_types[node]' => 1,
77       'settings[node][basicpage][translatable]' => 1,
78       "settings[node][basicpage][fields][$this->fieldName]" => 1,
79       "settings[node][basicpage][columns][$this->fieldName][file]" => 1,
80       // Explicitly disable alt and title since the javascript disables the
81       // checkboxes on the form.
82       "settings[node][basicpage][columns][$this->fieldName][alt]" => FALSE,
83       "settings[node][basicpage][columns][$this->fieldName][title]" => FALSE,
84     ];
85     $this->drupalPostForm('admin/config/regional/content-language', $edit, 'Save configuration');
86
87     // Verify that the image field on the "Basic basic" node type is
88     // translatable.
89     $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'basicpage');
90     $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node image field is translatable.');
91
92     // Create a default language node.
93     $default_language_node = $this->drupalCreateNode(['type' => 'basicpage', 'title' => 'Lost in translation']);
94
95     // Edit the node to upload a file.
96     $edit = [];
97     $name = 'files[' . $this->fieldName . '_0]';
98     $edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[0]->uri);
99     $this->drupalPostForm('node/' . $default_language_node->id() . '/edit', $edit, t('Save'));
100     $edit = [$this->fieldName . '[0][alt]' => 'Lost in translation image', $this->fieldName . '[0][title]' => 'Lost in translation image title'];
101     $this->drupalPostForm(NULL, $edit, t('Save'));
102     $first_fid = $this->getLastFileId();
103
104     // Translate the node into French: remove the existing file.
105     $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/fr', [], t('Remove'));
106
107     // Upload a different file.
108     $edit = [];
109     $edit['title[0][value]'] = 'Scarlett Johansson';
110     $name = 'files[' . $this->fieldName . '_0]';
111     $edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[1]->uri);
112     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
113     $edit = [$this->fieldName . '[0][alt]' => 'Scarlett Johansson image', $this->fieldName . '[0][title]' => 'Scarlett Johansson image title'];
114     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
115     // This inspects the HTML after the post of the translation, the image
116     // should be displayed on the original node.
117     $this->assertRaw('alt="Lost in translation image"');
118     $this->assertRaw('title="Lost in translation image title"');
119     $second_fid = $this->getLastFileId();
120     // View the translated node.
121     $this->drupalGet('fr/node/' . $default_language_node->id());
122     $this->assertRaw('alt="Scarlett Johansson image"');
123
124     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
125
126     /* @var $file \Drupal\file\FileInterface */
127
128     // Ensure the file status of the first file permanent.
129     $file = File::load($first_fid);
130     $this->assertTrue($file->isPermanent());
131
132     // Ensure the file status of the second file is permanent.
133     $file = File::load($second_fid);
134     $this->assertTrue($file->isPermanent());
135
136     // Translate the node into dutch: remove the existing file.
137     $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/nl', [], t('Remove'));
138
139     // Upload a different file.
140     $edit = [];
141     $edit['title[0][value]'] = 'Akiko Takeshita';
142     $name = 'files[' . $this->fieldName . '_0]';
143     $edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[2]->uri);
144     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
145     $edit = [$this->fieldName . '[0][alt]' => 'Akiko Takeshita image', $this->fieldName . '[0][title]' => 'Akiko Takeshita image title'];
146     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
147     $third_fid = $this->getLastFileId();
148
149     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
150
151     // Ensure the first file is untouched.
152     $file = File::load($first_fid);
153     $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
154     // This inspects the HTML after the post of the translation, the image
155     // should be displayed on the original node.
156     $this->assertRaw('alt="Lost in translation image"');
157     $this->assertRaw('title="Lost in translation image title"');
158     // View the translated node.
159     $this->drupalGet('nl/node/' . $default_language_node->id());
160     $this->assertRaw('alt="Akiko Takeshita image"');
161     $this->assertRaw('title="Akiko Takeshita image title"');
162
163     // Ensure the file status of the second file is permanent.
164     $file = File::load($second_fid);
165     $this->assertTrue($file->isPermanent());
166
167     // Ensure the file status of the third file is permanent.
168     $file = File::load($third_fid);
169     $this->assertTrue($file->isPermanent());
170
171     // Edit the second translation: remove the existing file.
172     $this->drupalPostForm('fr/node/' . $default_language_node->id() . '/edit', [], t('Remove'));
173
174     // Upload a different file.
175     $edit = [];
176     $edit['title[0][value]'] = 'Giovanni Ribisi';
177     $name = 'files[' . $this->fieldName . '_0]';
178     $edit[$name] = drupal_realpath($this->drupalGetTestFiles('image')[3]->uri);
179     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
180     $name = $this->fieldName . '[0][alt]';
181
182     $edit = [$name => 'Giovanni Ribisi image'];
183     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
184     $replaced_second_fid = $this->getLastFileId();
185
186     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
187
188     // Ensure the first and third files are untouched.
189     $file = File::load($first_fid);
190     $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
191
192     $file = File::load($third_fid);
193     $this->assertTrue($file->isPermanent());
194
195     // Ensure the file status of the replaced second file is permanent.
196     $file = File::load($replaced_second_fid);
197     $this->assertTrue($file->isPermanent());
198
199     // Delete the third translation.
200     $this->drupalPostForm('nl/node/' . $default_language_node->id() . '/delete', [], t('Delete Dutch translation'));
201
202     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
203
204     // Ensure the first and replaced second files are untouched.
205     $file = File::load($first_fid);
206     $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.');
207
208     $file = File::load($replaced_second_fid);
209     $this->assertTrue($file->isPermanent());
210
211     // Ensure the file status of the third file is now temporary.
212     $file = File::load($third_fid);
213     $this->assertTrue($file->isTemporary());
214
215     // Delete the all translations.
216     $this->drupalPostForm('node/' . $default_language_node->id() . '/delete', [], t('Delete all translations'));
217
218     \Drupal::entityTypeManager()->getStorage('file')->resetCache();
219
220     // Ensure the file status of the all files are now temporary.
221     $file = File::load($first_fid);
222     $this->assertTrue($file->isTemporary(), 'First file still exists and is temporary.');
223
224     $file = File::load($replaced_second_fid);
225     $this->assertTrue($file->isTemporary());
226   }
227
228 }