3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\Core\Field\FieldItemInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\file\Entity\File;
13 use Drupal\user\Entity\Role;
16 * Tests using entity fields of the file field type.
20 class FileItemTest extends FieldKernelTestBase {
27 public static $modules = ['file'];
30 * Created file entity.
32 * @var \Drupal\file\Entity\File
37 * Directory where the sample files are stored.
43 protected function setUp() {
46 $this->installEntitySchema('user');
47 $this->installConfig(['user']);
48 // Give anonymous users permission to access content, so they can view and
49 // download public files.
50 $anonymous_role = Role::load(Role::ANONYMOUS_ID);
51 $anonymous_role->grantPermission('access content');
52 $anonymous_role->save();
54 $this->installEntitySchema('file');
55 $this->installSchema('file', ['file_usage']);
57 FieldStorageConfig::create([
58 'field_name' => 'file_test',
59 'entity_type' => 'entity_test',
61 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
63 $this->directory = $this->getRandomGenerator()->name(8);
65 'entity_type' => 'entity_test',
66 'field_name' => 'file_test',
67 'bundle' => 'entity_test',
68 'settings' => ['file_directory' => $this->directory],
70 file_put_contents('public://example.txt', $this->randomMachineName());
71 $this->file = File::create([
72 'uri' => 'public://example.txt',
78 * Tests using entity fields of the file field type.
80 public function testFileItem() {
81 // Check that the selection handler was automatically assigned to
83 $field_definition = FieldConfig::load('entity_test.entity_test.file_test');
84 $handler_id = $field_definition->getSetting('handler');
85 $this->assertEqual($handler_id, 'default:file');
87 // Create a test entity with the
88 $entity = EntityTest::create();
89 $entity->file_test->target_id = $this->file->id();
90 $entity->file_test->display = 1;
91 $entity->file_test->description = $description = $this->randomMachineName();
92 $entity->name->value = $this->randomMachineName();
95 $entity = EntityTest::load($entity->id());
96 $this->assertTrue($entity->file_test instanceof FieldItemListInterface, 'Field implements interface.');
97 $this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
98 $this->assertEqual($entity->file_test->target_id, $this->file->id());
99 $this->assertEqual($entity->file_test->display, 1);
100 $this->assertEqual($entity->file_test->description, $description);
101 $this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
102 $this->assertEqual($entity->file_test->entity->url(), $url = file_create_url($this->file->getFileUri()));
103 $this->assertEqual($entity->file_test->entity->id(), $this->file->id());
104 $this->assertEqual($entity->file_test->entity->uuid(), $this->file->uuid());
106 // Make sure the computed files reflects updates to the file.
107 file_put_contents('public://example-2.txt', $this->randomMachineName());
108 $file2 = File::create([
109 'uri' => 'public://example-2.txt',
113 $entity->file_test->target_id = $file2->id();
114 $this->assertEqual($entity->file_test->entity->id(), $file2->id());
115 $this->assertEqual($entity->file_test->entity->getFileUri(), $file2->getFileUri());
117 // Test the deletion of an entity having an entity reference field targeting
118 // a non-existing entity.
122 // Test the generateSampleValue() method.
123 $entity = EntityTest::create();
124 $entity->file_test->generateSampleItems();
125 $this->entityValidateAndSave($entity);
126 // Verify that the sample file was stored in the correct directory.
127 $uri = $entity->file_test->entity->getFileUri();
128 $this->assertEqual($this->directory, dirname(file_uri_target($uri)));
130 // Make sure the computed files reflects updates to the file.
131 file_put_contents('public://example-3.txt', $this->randomMachineName());
132 // Test unsaved file entity.
133 $file3 = File::create([
134 'uri' => 'public://example-3.txt',
136 $display = entity_get_display('entity_test', 'entity_test', 'default');
137 $display->setComponent('file_test', [
139 'type' => 'file_default',
142 $entity = EntityTest::create();
143 $entity->file_test = ['entity' => $file3];
144 $uri = $file3->getFileUri();
145 $output = entity_view($entity, 'default');
146 \Drupal::service('renderer')->renderRoot($output);
147 $this->assertTrue(!empty($entity->file_test->entity));
148 $this->assertEqual($entity->file_test->entity->getFileUri(), $uri);