function PrivateFileOnTranslatedEntityTest::testPrivateLanguageFile

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php \Drupal\Tests\file\Functional\PrivateFileOnTranslatedEntityTest::testPrivateLanguageFile()
  2. 8.9.x core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php \Drupal\Tests\file\Functional\PrivateFileOnTranslatedEntityTest::testPrivateLanguageFile()
  3. 11.x core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php \Drupal\Tests\file\Functional\PrivateFileOnTranslatedEntityTest::testPrivateLanguageFile()

Tests private file fields on translated nodes.

File

core/modules/file/tests/src/Functional/PrivateFileOnTranslatedEntityTest.php, line 75

Class

PrivateFileOnTranslatedEntityTest
Uploads private files to translated node and checks access.

Namespace

Drupal\Tests\file\Functional

Code

public function testPrivateLanguageFile() : void {
  // Verify that the file field on the "Basic page" node type is translatable.
  $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'page');
  $this->assertTrue($definitions[$this->fieldName]
    ->isTranslatable(), 'Node file field is translatable.');
  // Create a default language node.
  $default_language_node = $this->drupalCreateNode([
    'type' => 'page',
  ]);
  // Edit the node to upload a file.
  $file = File::create([
    'uri' => $this->drupalGetTestFiles('text')[0]->uri,
  ]);
  $file->save();
  $default_language_node->set($this->fieldName, $file->id());
  $default_language_node->save();
  $last_fid_prior = $this->getLastFileId();
  // Languages are cached on many levels, and we need to clear those caches.
  $this->rebuildContainer();
  // Ensure the file can be downloaded.
  \Drupal::entityTypeManager()->getStorage('node')
    ->resetCache([
    $default_language_node->id(),
  ]);
  $node = Node::load($default_language_node->id());
  $node_file = File::load($node->{$this->fieldName}->target_id);
  $this->drupalGet($node_file->createFileUrl(FALSE));
  $this->assertSession()
    ->statusCodeEquals(200);
  // Translate the node into French.
  $node->addTranslation('fr', [
    'title' => $this->randomString(),
  ]);
  $node->save();
  // Remove the existing file.
  $existing_file = $node->{$this->fieldName}->entity;
  if ($existing_file) {
    $node->set($this->fieldName, NULL);
    $existing_file->delete();
    $node->save();
  }
  // Upload a different file.
  $default_language_node = $node->getTranslation('fr');
  $file = File::create([
    'uri' => $this->drupalGetTestFiles('text')[1]->uri,
  ]);
  $file->save();
  $default_language_node->set($this->fieldName, $file->id());
  $default_language_node->save();
  $last_fid = $this->getLastFileId();
  // Verify the translation was created.
  \Drupal::entityTypeManager()->getStorage('node')
    ->resetCache([
    $default_language_node->id(),
  ]);
  $default_language_node = Node::load($default_language_node->id());
  $this->assertTrue($default_language_node->hasTranslation('fr'), 'Node found in database.');
  // Verify that the new file got saved.
  $this->assertGreaterThan($last_fid_prior, $last_fid);
  // Ensure the file attached to the translated node can be downloaded.
  $french_node = $default_language_node->getTranslation('fr');
  $node_file = File::load($french_node->{$this->fieldName}->target_id);
  $this->drupalGet($node_file->createFileUrl(FALSE));
  $this->assertSession()
    ->statusCodeEquals(200);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.