Version 1
[yaffs-website] / web / core / modules / file / tests / src / Kernel / FileItemTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
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
14 /**
15  * Tests using entity fields of the file field type.
16  *
17  * @group file
18  */
19 class FileItemTest extends FieldKernelTestBase {
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['file'];
27
28   /**
29    * Created file entity.
30    *
31    * @var \Drupal\file\Entity\File
32    */
33   protected $file;
34
35   /**
36    * Directory where the sample files are stored.
37    *
38    * @var string
39    */
40   protected $directory;
41
42   protected function setUp() {
43     parent::setUp();
44
45     $this->installEntitySchema('file');
46     $this->installSchema('file', ['file_usage']);
47
48     FieldStorageConfig::create([
49       'field_name' => 'file_test',
50       'entity_type' => 'entity_test',
51       'type' => 'file',
52       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
53     ])->save();
54     $this->directory = $this->getRandomGenerator()->name(8);
55     FieldConfig::create([
56       'entity_type' => 'entity_test',
57       'field_name' => 'file_test',
58       'bundle' => 'entity_test',
59       'settings' => ['file_directory' => $this->directory],
60     ])->save();
61     file_put_contents('public://example.txt', $this->randomMachineName());
62     $this->file = File::create([
63       'uri' => 'public://example.txt',
64     ]);
65     $this->file->save();
66   }
67
68   /**
69    * Tests using entity fields of the file field type.
70    */
71   public function testFileItem() {
72     // Check that the selection handler was automatically assigned to
73     // 'default:file'.
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');
77
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();
84     $entity->save();
85
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());
96
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',
101     ]);
102     $file2->save();
103
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());
107
108     // Test the deletion of an entity having an entity reference field targeting
109     // a non-existing entity.
110     $file2->delete();
111     $entity->delete();
112
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)));
120
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',
126     ]);
127     $display = entity_get_display('entity_test', 'entity_test', 'default');
128     $display->setComponent('file_test', [
129       'label' => 'above',
130       'type' => 'file_default',
131       'weight' => 1,
132     ])->save();
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);
140   }
141
142 }