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;
15 * Tests using entity fields of the file field type.
19 class FileItemTest extends FieldKernelTestBase {
26 public static $modules = ['file'];
29 * Created file entity.
31 * @var \Drupal\file\Entity\File
36 * Directory where the sample files are stored.
42 protected function setUp() {
45 $this->installEntitySchema('file');
46 $this->installSchema('file', ['file_usage']);
48 FieldStorageConfig::create([
49 'field_name' => 'file_test',
50 'entity_type' => 'entity_test',
52 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
54 $this->directory = $this->getRandomGenerator()->name(8);
56 'entity_type' => 'entity_test',
57 'field_name' => 'file_test',
58 'bundle' => 'entity_test',
59 'settings' => ['file_directory' => $this->directory],
61 file_put_contents('public://example.txt', $this->randomMachineName());
62 $this->file = File::create([
63 'uri' => 'public://example.txt',
69 * Tests using entity fields of the file field type.
71 public function testFileItem() {
72 // Check that the selection handler was automatically assigned to
74 $field_definition = FieldConfig::load('entity_test.entity_test.file_test');
75 $handler_id = $field_definition->getSetting('handler');
76 $this->assertEqual($handler_id, 'default:file');
78 // Create a test entity with the
79 $entity = EntityTest::create();
80 $entity->file_test->target_id = $this->file->id();
81 $entity->file_test->display = 1;
82 $entity->file_test->description = $description = $this->randomMachineName();
83 $entity->name->value = $this->randomMachineName();
86 $entity = EntityTest::load($entity->id());
87 $this->assertTrue($entity->file_test instanceof FieldItemListInterface, 'Field implements interface.');
88 $this->assertTrue($entity->file_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
89 $this->assertEqual($entity->file_test->target_id, $this->file->id());
90 $this->assertEqual($entity->file_test->display, 1);
91 $this->assertEqual($entity->file_test->description, $description);
92 $this->assertEqual($entity->file_test->entity->getFileUri(), $this->file->getFileUri());
93 $this->assertEqual($entity->file_test->entity->url(), $url = file_create_url($this->file->getFileUri()));
94 $this->assertEqual($entity->file_test->entity->id(), $this->file->id());
95 $this->assertEqual($entity->file_test->entity->uuid(), $this->file->uuid());
97 // Make sure the computed files reflects updates to the file.
98 file_put_contents('public://example-2.txt', $this->randomMachineName());
99 $file2 = File::create([
100 'uri' => 'public://example-2.txt',
104 $entity->file_test->target_id = $file2->id();
105 $this->assertEqual($entity->file_test->entity->id(), $file2->id());
106 $this->assertEqual($entity->file_test->entity->getFileUri(), $file2->getFileUri());
108 // Test the deletion of an entity having an entity reference field targeting
109 // a non-existing entity.
113 // Test the generateSampleValue() method.
114 $entity = EntityTest::create();
115 $entity->file_test->generateSampleItems();
116 $this->entityValidateAndSave($entity);
117 // Verify that the sample file was stored in the correct directory.
118 $uri = $entity->file_test->entity->getFileUri();
119 $this->assertEqual($this->directory, dirname(file_uri_target($uri)));
121 // Make sure the computed files reflects updates to the file.
122 file_put_contents('public://example-3.txt', $this->randomMachineName());
123 // Test unsaved file entity.
124 $file3 = File::create([
125 'uri' => 'public://example-3.txt',
127 $display = entity_get_display('entity_test', 'entity_test', 'default');
128 $display->setComponent('file_test', [
130 'type' => 'file_default',
133 $entity = EntityTest::create();
134 $entity->file_test = ['entity' => $file3];
135 $uri = $file3->getFileUri();
136 $output = entity_view($entity, 'default');
137 \Drupal::service('renderer')->renderRoot($output);
138 $this->assertTrue(!empty($entity->file_test->entity));
139 $this->assertEqual($entity->file_test->entity->getFileUri(), $uri);