Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[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 use Drupal\user\Entity\Role;
14
15 /**
16  * Tests using entity fields of the file field type.
17  *
18  * @group file
19  */
20 class FileItemTest extends FieldKernelTestBase {
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['file'];
28
29   /**
30    * Created file entity.
31    *
32    * @var \Drupal\file\Entity\File
33    */
34   protected $file;
35
36   /**
37    * Directory where the sample files are stored.
38    *
39    * @var string
40    */
41   protected $directory;
42
43   protected function setUp() {
44     parent::setUp();
45
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();
53
54     $this->installEntitySchema('file');
55     $this->installSchema('file', ['file_usage']);
56
57     FieldStorageConfig::create([
58       'field_name' => 'file_test',
59       'entity_type' => 'entity_test',
60       'type' => 'file',
61       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
62     ])->save();
63     $this->directory = $this->getRandomGenerator()->name(8);
64     FieldConfig::create([
65       'entity_type' => 'entity_test',
66       'field_name' => 'file_test',
67       'bundle' => 'entity_test',
68       'settings' => ['file_directory' => $this->directory],
69     ])->save();
70     file_put_contents('public://example.txt', $this->randomMachineName());
71     $this->file = File::create([
72       'uri' => 'public://example.txt',
73     ]);
74     $this->file->save();
75   }
76
77   /**
78    * Tests using entity fields of the file field type.
79    */
80   public function testFileItem() {
81     // Check that the selection handler was automatically assigned to
82     // 'default:file'.
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');
86
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();
93     $entity->save();
94
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());
105
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',
110     ]);
111     $file2->save();
112
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());
116
117     // Test the deletion of an entity having an entity reference field targeting
118     // a non-existing entity.
119     $file2->delete();
120     $entity->delete();
121
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)));
129
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',
135     ]);
136     $display = entity_get_display('entity_test', 'entity_test', 'default');
137     $display->setComponent('file_test', [
138       'label' => 'above',
139       'type' => 'file_default',
140       'weight' => 1,
141     ])->save();
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);
149   }
150
151 }