annotate core/modules/image/tests/src/Functional/ImageOnTranslatedEntityTest.php @ 19:fa3358dc1485 tip

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