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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
<?php
declare(strict_types=1);
namespace Drupal\Tests\file\Kernel;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\user\Entity\Role;
/**
* Tests using entity fields of the file field type.
*
* @group file
*/
class FileItemTest extends FieldKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['file'];
/**
* Created file entity.
*
* @var \Drupal\file\Entity\File
*/
protected $file;
/**
* Directory where the sample files are stored.
*
* @var string
*/
protected $directory;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('user');
$this->installConfig(['user']);
// Give anonymous users permission to access content, so they can view and
// download public files.
$anonymous_role = Role::load(Role::ANONYMOUS_ID);
$anonymous_role->grantPermission('access content');
$anonymous_role->save();
$this->installEntitySchema('file');
$this->installSchema('file', ['file_usage']);
FieldStorageConfig::create([
'field_name' => 'file_test',
'entity_type' => 'entity_test',
'type' => 'file',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])->save();
$this->directory = $this->getRandomGenerator()->name(8);
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'file_test',
'bundle' => 'entity_test',
'settings' => ['file_directory' => $this->directory],
])->save();
file_put_contents('public://example.txt', $this->randomMachineName());
$this->file = File::create([
'uri' => 'public://example.txt',
]);
$this->file->save();
}
/**
* Tests using entity fields of the file field type.
*/
public function testFileItem(): void {
// Check that the selection handler was automatically assigned to
// 'default:file'.
$field_definition = FieldConfig::load('entity_test.entity_test.file_test');
$handler_id = $field_definition->getSetting('handler');
$this->assertEquals('default:file', $handler_id);
// Create a test entity with the
$entity = EntityTest::create();
$entity->file_test->target_id = $this->file->id();
$entity->file_test->display = 1;
$entity->file_test->description = $description = $this->randomMachineName();
$entity->name->value = $this->randomMachineName();
$entity->save();
$entity = EntityTest::load($entity->id());
$this->assertInstanceOf(FieldItemListInterface::class, $entity->file_test);
$this->assertInstanceOf(FieldItemInterface::class, $entity->file_test[0]);
$this->assertEquals($this->file->id(), $entity->file_test->target_id);
$this->assertEquals(1, $entity->file_test->display);
$this->assertEquals($description, $entity->file_test->description);
$this->assertEquals($this->file->getFileUri(), $entity->file_test->entity->getFileUri());
$this->assertEquals($this->file->id(), $entity->file_test->entity->id());
$this->assertEquals($this->file->uuid(), $entity->file_test->entity->uuid());
// Make sure the computed files reflects updates to the file.
file_put_contents('public://example-2.txt', $this->randomMachineName());
$file2 = File::create([
'uri' => 'public://example-2.txt',
]);
$file2->save();
$entity->file_test->target_id = $file2->id();
$this->assertEquals($entity->file_test->entity->id(), $file2->id());
$this->assertEquals($entity->file_test->entity->getFileUri(), $file2->getFileUri());
// Test the deletion of an entity having an entity reference field targeting
// a non-existing entity.
$file2->delete();
$entity->delete();
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->file_test->generateSampleItems();
$this->entityValidateAndSave($entity);
// Verify that the sample file was stored in the correct directory.
$uri = $entity->file_test->entity->getFileUri();
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
$this->assertEquals($this->directory, dirname($stream_wrapper_manager::getTarget($uri)));
// Make sure the computed files reflects updates to the file.
file_put_contents('public://example-3.txt', $this->randomMachineName());
// Test unsaved file entity.
$file3 = File::create([
'uri' => 'public://example-3.txt',
]);
$display = \Drupal::service('entity_display.repository')
->getViewDisplay('entity_test', 'entity_test');
$display->setComponent('file_test', [
'label' => 'above',
'type' => 'file_default',
'weight' => 1,
])->save();
$entity = EntityTest::create();
$entity->file_test = ['entity' => $file3];
$uri = $file3->getFileUri();
$output = \Drupal::entityTypeManager()
->getViewBuilder('entity_test')
->view($entity, 'default');
\Drupal::service('renderer')->renderRoot($output);
$this->assertTrue(!empty($entity->file_test->entity));
$this->assertEquals($uri, $entity->file_test->entity->getFileUri());
}
}
|