Chris@16: config('file.settings')->set('make_unused_managed_files_temporary', TRUE)->save(); Chris@16: Chris@16: // Create the "Basic page" node type. Chris@16: // @todo Remove the disabling of new revision creation in Chris@16: // https://www.drupal.org/node/1239558. Chris@16: $this->drupalCreateContentType(['type' => 'basicpage', 'name' => 'Basic page', 'new_revision' => FALSE]); Chris@16: Chris@16: // Create a image field on the "Basic page" node type. Chris@16: $this->fieldName = strtolower($this->randomMachineName()); Chris@16: $this->createImageField($this->fieldName, 'basicpage', [], ['title_field' => 1]); Chris@16: Chris@16: // Create and log in user. Chris@16: $permissions = [ Chris@16: 'access administration pages', Chris@16: 'administer content translation', Chris@16: 'administer content types', Chris@16: 'administer languages', Chris@16: 'administer node fields', Chris@16: 'create content translations', Chris@16: 'create basicpage content', Chris@16: 'edit any basicpage content', Chris@16: 'translate any entity', Chris@16: 'delete any basicpage content', Chris@16: ]; Chris@16: $admin_user = $this->drupalCreateUser($permissions); Chris@16: $this->drupalLogin($admin_user); Chris@16: Chris@16: // Add a second and third language. Chris@16: $edit = []; Chris@16: $edit['predefined_langcode'] = 'fr'; Chris@16: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); Chris@16: Chris@16: $edit = []; Chris@16: $edit['predefined_langcode'] = 'nl'; Chris@16: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests synced file fields on translated nodes. Chris@16: */ Chris@16: public function testSyncedImages() { Chris@16: // Enable translation for "Basic page" nodes. Chris@16: $edit = [ Chris@16: 'entity_types[node]' => 1, Chris@16: 'settings[node][basicpage][translatable]' => 1, Chris@16: "settings[node][basicpage][fields][$this->fieldName]" => 1, Chris@16: "settings[node][basicpage][columns][$this->fieldName][file]" => 1, Chris@16: // Explicitly disable alt and title since the javascript disables the Chris@16: // checkboxes on the form. Chris@16: "settings[node][basicpage][columns][$this->fieldName][alt]" => FALSE, Chris@16: "settings[node][basicpage][columns][$this->fieldName][title]" => FALSE, Chris@16: ]; Chris@16: $this->drupalPostForm('admin/config/regional/content-language', $edit, 'Save configuration'); Chris@16: Chris@16: // Verify that the image field on the "Basic basic" node type is Chris@16: // translatable. Chris@16: $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'basicpage'); Chris@16: $this->assertTrue($definitions[$this->fieldName]->isTranslatable(), 'Node image field is translatable.'); Chris@16: Chris@16: // Create a default language node. Chris@16: $default_language_node = $this->drupalCreateNode(['type' => 'basicpage', 'title' => 'Lost in translation']); Chris@16: Chris@16: // Edit the node to upload a file. Chris@16: $edit = []; Chris@16: $name = 'files[' . $this->fieldName . '_0]'; Chris@16: $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('image')[0]->uri); Chris@16: $this->drupalPostForm('node/' . $default_language_node->id() . '/edit', $edit, t('Save')); Chris@16: $edit = [$this->fieldName . '[0][alt]' => 'Lost in translation image', $this->fieldName . '[0][title]' => 'Lost in translation image title']; Chris@16: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@16: $first_fid = $this->getLastFileId(); Chris@16: Chris@16: // Translate the node into French: remove the existing file. Chris@16: $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/fr', [], t('Remove')); Chris@16: Chris@16: // Upload a different file. Chris@16: $edit = []; Chris@16: $edit['title[0][value]'] = 'Scarlett Johansson'; Chris@16: $name = 'files[' . $this->fieldName . '_0]'; Chris@16: $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('image')[1]->uri); Chris@16: $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); Chris@16: $edit = [$this->fieldName . '[0][alt]' => 'Scarlett Johansson image', $this->fieldName . '[0][title]' => 'Scarlett Johansson image title']; Chris@16: $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); Chris@16: // This inspects the HTML after the post of the translation, the image Chris@16: // should be displayed on the original node. Chris@16: $this->assertRaw('alt="Lost in translation image"'); Chris@16: $this->assertRaw('title="Lost in translation image title"'); Chris@16: $second_fid = $this->getLastFileId(); Chris@16: // View the translated node. Chris@16: $this->drupalGet('fr/node/' . $default_language_node->id()); Chris@16: $this->assertRaw('alt="Scarlett Johansson image"'); Chris@16: Chris@16: \Drupal::entityTypeManager()->getStorage('file')->resetCache(); Chris@16: Chris@16: /* @var $file \Drupal\file\FileInterface */ Chris@16: Chris@16: // Ensure the file status of the first file permanent. Chris@16: $file = File::load($first_fid); Chris@16: $this->assertTrue($file->isPermanent()); Chris@16: Chris@16: // Ensure the file status of the second file is permanent. Chris@16: $file = File::load($second_fid); Chris@16: $this->assertTrue($file->isPermanent()); Chris@16: Chris@16: // Translate the node into dutch: remove the existing file. Chris@16: $this->drupalPostForm('node/' . $default_language_node->id() . '/translations/add/en/nl', [], t('Remove')); Chris@16: Chris@16: // Upload a different file. Chris@16: $edit = []; Chris@16: $edit['title[0][value]'] = 'Akiko Takeshita'; Chris@16: $name = 'files[' . $this->fieldName . '_0]'; Chris@16: $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('image')[2]->uri); Chris@16: $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); Chris@16: $edit = [$this->fieldName . '[0][alt]' => 'Akiko Takeshita image', $this->fieldName . '[0][title]' => 'Akiko Takeshita image title']; Chris@16: $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); Chris@16: $third_fid = $this->getLastFileId(); Chris@16: Chris@16: \Drupal::entityTypeManager()->getStorage('file')->resetCache(); Chris@16: Chris@16: // Ensure the first file is untouched. Chris@16: $file = File::load($first_fid); Chris@16: $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.'); Chris@16: // This inspects the HTML after the post of the translation, the image Chris@16: // should be displayed on the original node. Chris@16: $this->assertRaw('alt="Lost in translation image"'); Chris@16: $this->assertRaw('title="Lost in translation image title"'); Chris@16: // View the translated node. Chris@16: $this->drupalGet('nl/node/' . $default_language_node->id()); Chris@16: $this->assertRaw('alt="Akiko Takeshita image"'); Chris@16: $this->assertRaw('title="Akiko Takeshita image title"'); Chris@16: Chris@16: // Ensure the file status of the second file is permanent. Chris@16: $file = File::load($second_fid); Chris@16: $this->assertTrue($file->isPermanent()); Chris@16: Chris@16: // Ensure the file status of the third file is permanent. Chris@16: $file = File::load($third_fid); Chris@16: $this->assertTrue($file->isPermanent()); Chris@16: Chris@16: // Edit the second translation: remove the existing file. Chris@16: $this->drupalPostForm('fr/node/' . $default_language_node->id() . '/edit', [], t('Remove')); Chris@16: Chris@16: // Upload a different file. Chris@16: $edit = []; Chris@16: $edit['title[0][value]'] = 'Giovanni Ribisi'; Chris@16: $name = 'files[' . $this->fieldName . '_0]'; Chris@16: $edit[$name] = \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('image')[3]->uri); Chris@16: $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); Chris@16: $name = $this->fieldName . '[0][alt]'; Chris@16: Chris@16: $edit = [$name => 'Giovanni Ribisi image']; Chris@16: $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); Chris@16: $replaced_second_fid = $this->getLastFileId(); Chris@16: Chris@16: \Drupal::entityTypeManager()->getStorage('file')->resetCache(); Chris@16: Chris@16: // Ensure the first and third files are untouched. Chris@16: $file = File::load($first_fid); Chris@16: $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.'); Chris@16: Chris@16: $file = File::load($third_fid); Chris@16: $this->assertTrue($file->isPermanent()); Chris@16: Chris@16: // Ensure the file status of the replaced second file is permanent. Chris@16: $file = File::load($replaced_second_fid); Chris@16: $this->assertTrue($file->isPermanent()); Chris@16: Chris@16: // Delete the third translation. Chris@16: $this->drupalPostForm('nl/node/' . $default_language_node->id() . '/delete', [], t('Delete Dutch translation')); Chris@16: Chris@16: \Drupal::entityTypeManager()->getStorage('file')->resetCache(); Chris@16: Chris@16: // Ensure the first and replaced second files are untouched. Chris@16: $file = File::load($first_fid); Chris@16: $this->assertTrue($file->isPermanent(), 'First file still exists and is permanent.'); Chris@16: Chris@16: $file = File::load($replaced_second_fid); Chris@16: $this->assertTrue($file->isPermanent()); Chris@16: Chris@16: // Ensure the file status of the third file is now temporary. Chris@16: $file = File::load($third_fid); Chris@16: $this->assertTrue($file->isTemporary()); Chris@16: Chris@16: // Delete the all translations. Chris@16: $this->drupalPostForm('node/' . $default_language_node->id() . '/delete', [], t('Delete all translations')); Chris@16: Chris@16: \Drupal::entityTypeManager()->getStorage('file')->resetCache(); Chris@16: Chris@16: // Ensure the file status of the all files are now temporary. Chris@16: $file = File::load($first_fid); Chris@16: $this->assertTrue($file->isTemporary(), 'First file still exists and is temporary.'); Chris@16: Chris@16: $file = File::load($replaced_second_fid); Chris@16: $this->assertTrue($file->isTemporary()); Chris@16: } Chris@16: Chris@16: }