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