3 namespace Drupal\file\Tests;
5 @trigger_error('The ' . __NAMESPACE__ . '\FileFieldTestBase is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\file\Functional\FileFieldTestBase. See https://www.drupal.org/node/2969361.', E_USER_DEPRECATED);
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\file\FileInterface;
10 use Drupal\simpletest\WebTestBase;
11 use Drupal\file\Entity\File;
14 * Provides methods specifically for testing File module's field handling.
16 * @deprecated Scheduled for removal in Drupal 9.0.0.
17 * Use \Drupal\Tests\file\Functional\FileFieldTestBase instead.
19 abstract class FileFieldTestBase extends WebTestBase {
26 public static $modules = ['node', 'file', 'file_module_test', 'field_ui'];
29 * An user with administration permissions.
31 * @var \Drupal\user\UserInterface
35 protected function setUp() {
37 $this->adminUser = $this->drupalCreateUser(['access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer node fields', 'administer node display', 'administer nodes', 'bypass node access']);
38 $this->drupalLogin($this->adminUser);
39 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
43 * Retrieves a sample file of the specified type.
45 * @return \Drupal\file\FileInterface
47 public function getTestFile($type_name, $size = NULL) {
48 // Get a file to upload.
49 $file = current($this->drupalGetTestFiles($type_name, $size));
51 // Add a filesize property to files as would be read by
52 // \Drupal\file\Entity\File::load().
53 $file->filesize = filesize($file->uri);
55 return File::create((array) $file);
59 * Retrieves the fid of the last inserted file.
61 public function getLastFileId() {
62 return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
66 * Creates a new file field.
69 * The name of the new field (all lowercase), exclude the "field_" prefix.
70 * @param string $entity_type
72 * @param string $bundle
73 * The bundle that this field will be added to.
74 * @param array $storage_settings
75 * A list of field storage settings that will be added to the defaults.
76 * @param array $field_settings
77 * A list of instance settings that will be added to the instance defaults.
78 * @param array $widget_settings
79 * A list of widget settings that will be added to the widget defaults.
81 public function createFileField($name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $widget_settings = []) {
82 $field_storage = FieldStorageConfig::create([
83 'entity_type' => $entity_type,
84 'field_name' => $name,
86 'settings' => $storage_settings,
87 'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
89 $field_storage->save();
91 $this->attachFileField($name, $entity_type, $bundle, $field_settings, $widget_settings);
92 return $field_storage;
96 * Attaches a file field to an entity.
99 * The name of the new field (all lowercase), exclude the "field_" prefix.
100 * @param string $entity_type
101 * The entity type this field will be added to.
102 * @param string $bundle
103 * The bundle this field will be added to.
104 * @param array $field_settings
105 * A list of field settings that will be added to the defaults.
106 * @param array $widget_settings
107 * A list of widget settings that will be added to the widget defaults.
109 public function attachFileField($name, $entity_type, $bundle, $field_settings = [], $widget_settings = []) {
111 'field_name' => $name,
113 'entity_type' => $entity_type,
115 'required' => !empty($field_settings['required']),
116 'settings' => $field_settings,
118 FieldConfig::create($field)->save();
120 entity_get_form_display($entity_type, $bundle, 'default')
121 ->setComponent($name, [
122 'type' => 'file_generic',
123 'settings' => $widget_settings,
126 // Assign display settings.
127 entity_get_display($entity_type, $bundle, 'default')
128 ->setComponent($name, [
130 'type' => 'file_default',
136 * Updates an existing file field with new settings.
138 public function updateFileField($name, $type_name, $field_settings = [], $widget_settings = []) {
139 $field = FieldConfig::loadByName('node', $type_name, $name);
140 $field->setSettings(array_merge($field->getSettings(), $field_settings));
143 entity_get_form_display('node', $type_name, 'default')
144 ->setComponent($name, [
145 'settings' => $widget_settings,
151 * Uploads a file to a node.
153 * @param \Drupal\file\FileInterface $file
154 * The File to be uploaded.
155 * @param string $field_name
156 * The name of the field on which the files should be saved.
157 * @param $nid_or_type
158 * A numeric node id to upload files to an existing node, or a string
159 * indicating the desired bundle for a new node.
160 * @param bool $new_revision
161 * The revision number.
162 * @param array $extras
163 * Additional values when a new node is created.
168 public function uploadNodeFile(FileInterface $file, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = []) {
169 return $this->uploadNodeFiles([$file], $field_name, $nid_or_type, $new_revision, $extras);
173 * Uploads multiple files to a node.
175 * @param \Drupal\file\FileInterface[] $files
176 * The files to be uploaded.
177 * @param string $field_name
178 * The name of the field on which the files should be saved.
179 * @param $nid_or_type
180 * A numeric node id to upload files to an existing node, or a string
181 * indicating the desired bundle for a new node.
182 * @param bool $new_revision
183 * The revision number.
184 * @param array $extras
185 * Additional values when a new node is created.
190 public function uploadNodeFiles(array $files, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = []) {
192 'title[0][value]' => $this->randomMachineName(),
193 'revision' => (string) (int) $new_revision,
196 $node_storage = $this->container->get('entity.manager')->getStorage('node');
197 if (is_numeric($nid_or_type)) {
199 $node_storage->resetCache([$nid]);
200 $node = $node_storage->load($nid);
204 $extras['type'] = $nid_or_type;
205 $node = $this->drupalCreateNode($extras);
207 // Save at least one revision to better simulate a real site.
208 $node->setNewRevision();
210 $node_storage->resetCache([$nid]);
211 $node = $node_storage->load($nid);
212 $this->assertNotEqual($nid, $node->getRevisionId(), 'Node revision exists.');
215 // Attach files to the node.
216 $field_storage = FieldStorageConfig::loadByName('node', $field_name);
217 // File input name depends on number of files already uploaded.
218 $field_num = count($node->{$field_name});
219 $name = 'files[' . $field_name . "_$field_num]";
220 if ($field_storage->getCardinality() != 1) {
223 foreach ($files as $file) {
224 $file_path = $this->container->get('file_system')->realpath($file->getFileUri());
225 if (count($files) == 1) {
226 $edit[$name] = $file_path;
229 $edit[$name][] = $file_path;
232 $this->drupalPostForm("node/$nid/edit", $edit, t('Save'));
238 * Removes a file from a node.
240 * Note that if replacing a file, it must first be removed then added again.
242 public function removeNodeFile($nid, $new_revision = TRUE) {
244 'revision' => (string) (int) $new_revision,
247 $this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove'));
248 $this->drupalPostForm(NULL, $edit, t('Save'));
252 * Replaces a file within a node.
254 public function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) {
256 'files[' . $field_name . '_0]' => \Drupal::service('file_system')->realpath($file->getFileUri()),
257 'revision' => (string) (int) $new_revision,
260 $this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove'));
261 $this->drupalPostForm(NULL, $edit, t('Save'));
265 * Asserts that a file exists physically on disk.
267 public function assertFileExists($file, $message = NULL) {
268 $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]);
269 $this->assertTrue(is_file($file->getFileUri()), $message);
273 * Asserts that a file exists in the database.
275 public function assertFileEntryExists($file, $message = NULL) {
276 $this->container->get('entity.manager')->getStorage('file')->resetCache();
277 $db_file = File::load($file->id());
278 $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', ['%file' => $file->getFileUri()]);
279 $this->assertEqual($db_file->getFileUri(), $file->getFileUri(), $message);
283 * Asserts that a file does not exist on disk.
285 public function assertFileNotExists($file, $message = NULL) {
286 $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]);
287 $this->assertFalse(is_file($file->getFileUri()), $message);
291 * Asserts that a file does not exist in the database.
293 public function assertFileEntryNotExists($file, $message) {
294 $this->container->get('entity.manager')->getStorage('file')->resetCache();
295 $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', ['%file' => $file->getFileUri()]);
296 $this->assertFalse(File::load($file->id()), $message);
300 * Asserts that a file's status is set to permanent in the database.
302 public function assertFileIsPermanent(FileInterface $file, $message = NULL) {
303 $message = isset($message) ? $message : format_string('File %file is permanent.', ['%file' => $file->getFileUri()]);
304 $this->assertTrue($file->isPermanent(), $message);