1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<?php
declare(strict_types=1);
namespace Drupal\Tests\file\Functional;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
use Drupal\Tests\content_translation\Traits\ContentTranslationTestTrait;
/**
* Uploads private files to translated node and checks access.
*
* @group file
*/
class PrivateFileOnTranslatedEntityTest extends FileFieldTestBase {
use ContentTranslationTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['language', 'content_translation'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The name of the file field used in the test.
*
* @var string
*/
protected $fieldName;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create the "Basic page" node type.
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
// Create a file field on the "Basic page" node type.
$this->fieldName = $this->randomMachineName();
$this->createFileField($this->fieldName, 'node', 'page', ['uri_scheme' => 'private']);
// Create and log in user.
$permissions = [
'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);
// Add a second language.
static::createLanguageFromLangcode('fr');
// Enable translation for "Basic page" nodes.
static::enableContentTranslation('node', 'page');
static::setFieldTranslatable('node', 'page', $this->fieldName, TRUE);
}
/**
* Tests private file fields on translated nodes.
*/
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.
$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.
$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);
}
}
|