You are here

public function PrivateFileOnTranslatedEntityTest::testPrivateLanguageFile in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Tests/PrivateFileOnTranslatedEntityTest.php \Drupal\file\Tests\PrivateFileOnTranslatedEntityTest::testPrivateLanguageFile()

Tests private file fields on translated nodes.

File

core/modules/file/src/Tests/PrivateFileOnTranslatedEntityTest.php, line 77
Contains \Drupal\file\Tests\PrivateFileOnTranslatedEntityTest.

Class

PrivateFileOnTranslatedEntityTest
Uploads private files to translated node and checks access.

Namespace

Drupal\file\Tests

Code

public function testPrivateLanguageFile() {

  // Verify that the file field on the "Basic page" node type is translatable.
  $definitions = \Drupal::entityManager()
    ->getFieldDefinitions('node', 'page');
  $this
    ->assertTrue($definitions[$this->fieldName]
    ->isTranslatable(), 'Node file field is translatable.');

  // Create a default language node.
  $default_language_node = $this
    ->drupalCreateNode(array(
    'type' => 'page',
  ));

  // Edit the node to upload a file.
  $edit = array();
  $name = 'files[' . $this->fieldName . '_0]';
  $edit[$name] = drupal_realpath($this
    ->drupalGetTestFiles('text')[0]->uri);
  $this
    ->drupalPostForm('node/' . $default_language_node
    ->id() . '/edit', $edit, t('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::entityManager()
    ->getStorage('node')
    ->resetCache(array(
    $default_language_node
      ->id(),
  ));
  $node = Node::load($default_language_node
    ->id());
  $node_file = File::load($node->{$this->fieldName}->target_id);
  $this
    ->drupalGet(file_create_url($node_file
    ->getFileUri()));
  $this
    ->assertResponse(200, 'Confirmed that the file attached to the English node can be downloaded.');

  // Translate the node into French.
  $this
    ->drupalGet('node/' . $default_language_node
    ->id() . '/translations');
  $this
    ->clickLink(t('Add'));

  // Remove the existing file.
  $this
    ->drupalPostForm(NULL, array(), t('Remove'));

  // Upload a different file.
  $edit = array();
  $edit['title[0][value]'] = $this
    ->randomMachineName();
  $name = 'files[' . $this->fieldName . '_0]';
  $edit[$name] = drupal_realpath($this
    ->drupalGetTestFiles('text')[1]->uri);
  $this
    ->drupalPostForm(NULL, $edit, t('Save (this translation)'));
  $last_fid = $this
    ->getLastFileId();

  // Verify the translation was created.
  \Drupal::entityManager()
    ->getStorage('node')
    ->resetCache(array(
    $default_language_node
      ->id(),
  ));
  $default_language_node = Node::load($default_language_node
    ->id());
  $this
    ->assertTrue($default_language_node
    ->hasTranslation('fr'), 'Node found in database.');
  $this
    ->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');

  // 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(file_create_url($node_file
    ->getFileUri()));
  $this
    ->assertResponse(200, 'Confirmed that the file attached to the French node can be downloaded.');
}