View source
<?php
namespace Drupal\file\Tests;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
class PrivateFileOnTranslatedEntityTest extends FileFieldTestBase {
public static $modules = array(
'language',
'content_translation',
);
protected $fieldName;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic page',
));
$this->fieldName = strtolower($this
->randomMachineName());
$this
->createFileField($this->fieldName, 'node', 'page', array(
'uri_scheme' => 'private',
));
$permissions = array(
'access administration pages',
'administer content translation',
'administer content types',
'administer languages',
'create content translations',
'create page content',
'edit any page content',
'translate any entity',
);
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$edit = array();
$edit['predefined_langcode'] = 'fr';
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
$edit = array(
'entity_types[node]' => 1,
'settings[node][page][translatable]' => 1,
"settings[node][page][fields][{$this->fieldName}]" => 1,
);
$this
->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
\Drupal::entityManager()
->clearCachedDefinitions();
}
public function testPrivateLanguageFile() {
$definitions = \Drupal::entityManager()
->getFieldDefinitions('node', 'page');
$this
->assertTrue($definitions[$this->fieldName]
->isTranslatable(), 'Node file field is translatable.');
$default_language_node = $this
->drupalCreateNode(array(
'type' => 'page',
));
$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();
$this
->rebuildContainer();
\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.');
$this
->drupalGet('node/' . $default_language_node
->id() . '/translations');
$this
->clickLink(t('Add'));
$this
->drupalPostForm(NULL, array(), t('Remove'));
$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();
\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.');
$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.');
}
}