annotate core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php @ 14:1fec387a4317

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