3 namespace Drupal\Tests\file\Functional;
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\file\FileInterface;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\file\Entity\File;
10 use Drupal\Tests\TestFileCreationTrait;
13 * Provides methods specifically for testing File module's field handling.
15 abstract class FileFieldTestBase extends BrowserTestBase {
17 use FileFieldCreationTrait;
18 use TestFileCreationTrait {
19 getTestFiles as drupalGetTestFiles;
27 public static $modules = ['node', 'file', 'file_module_test', 'field_ui'];
30 * An user with administration permissions.
32 * @var \Drupal\user\UserInterface
36 protected function setUp() {
38 $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']);
39 $this->drupalLogin($this->adminUser);
40 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
44 * Retrieves a sample file of the specified type.
46 * @return \Drupal\file\FileInterface
48 public function getTestFile($type_name, $size = NULL) {
49 // Get a file to upload.
50 $file = current($this->drupalGetTestFiles($type_name, $size));
52 // Add a filesize property to files as would be read by
53 // \Drupal\file\Entity\File::load().
54 $file->filesize = filesize($file->uri);
56 return File::create((array) $file);
60 * Retrieves the fid of the last inserted file.
62 public function getLastFileId() {
63 return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
67 * Updates an existing file field with new settings.
69 public function updateFileField($name, $type_name, $field_settings = [], $widget_settings = []) {
70 $field = FieldConfig::loadByName('node', $type_name, $name);
71 $field->setSettings(array_merge($field->getSettings(), $field_settings));
74 entity_get_form_display('node', $type_name, 'default')
75 ->setComponent($name, [
76 'settings' => $widget_settings,
82 * Uploads a file to a node.
84 * @param \Drupal\file\FileInterface $file
85 * The File to be uploaded.
86 * @param string $field_name
87 * The name of the field on which the files should be saved.
89 * A numeric node id to upload files to an existing node, or a string
90 * indicating the desired bundle for a new node.
91 * @param bool $new_revision
92 * The revision number.
93 * @param array $extras
94 * Additional values when a new node is created.
99 public function uploadNodeFile(FileInterface $file, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = []) {
100 return $this->uploadNodeFiles([$file], $field_name, $nid_or_type, $new_revision, $extras);
104 * Uploads multiple files to a node.
106 * @param \Drupal\file\FileInterface[] $files
107 * The files to be uploaded.
108 * @param string $field_name
109 * The name of the field on which the files should be saved.
110 * @param $nid_or_type
111 * A numeric node id to upload files to an existing node, or a string
112 * indicating the desired bundle for a new node.
113 * @param bool $new_revision
114 * The revision number.
115 * @param array $extras
116 * Additional values when a new node is created.
121 public function uploadNodeFiles(array $files, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = []) {
123 'title[0][value]' => $this->randomMachineName(),
124 'revision' => (string) (int) $new_revision,
127 $node_storage = $this->container->get('entity.manager')->getStorage('node');
128 if (is_numeric($nid_or_type)) {
130 $node_storage->resetCache([$nid]);
131 $node = $node_storage->load($nid);
135 $extras['type'] = $nid_or_type;
136 $node = $this->drupalCreateNode($extras);
138 // Save at least one revision to better simulate a real site.
139 $node->setNewRevision();
141 $node_storage->resetCache([$nid]);
142 $node = $node_storage->load($nid);
143 $this->assertNotEqual($nid, $node->getRevisionId(), 'Node revision exists.');
146 // Attach files to the node.
147 $field_storage = FieldStorageConfig::loadByName('node', $field_name);
148 // File input name depends on number of files already uploaded.
149 $field_num = count($node->{$field_name});
150 $name = 'files[' . $field_name . "_$field_num]";
151 if ($field_storage->getCardinality() != 1) {
154 foreach ($files as $file) {
155 $file_path = $this->container->get('file_system')->realpath($file->getFileUri());
156 if (count($files) == 1) {
157 $edit[$name] = $file_path;
160 $edit[$name][] = $file_path;
163 $this->drupalPostForm("node/$nid/edit", $edit, t('Save'));
169 * Removes a file from a node.
171 * Note that if replacing a file, it must first be removed then added again.
173 public function removeNodeFile($nid, $new_revision = TRUE) {
175 'revision' => (string) (int) $new_revision,
178 $this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove'));
179 $this->drupalPostForm(NULL, $edit, t('Save'));
183 * Replaces a file within a node.
185 public function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) {
187 'files[' . $field_name . '_0]' => \Drupal::service('file_system')->realpath($file->getFileUri()),
188 'revision' => (string) (int) $new_revision,
191 $this->drupalPostForm('node/' . $nid . '/edit', [], t('Remove'));
192 $this->drupalPostForm(NULL, $edit, t('Save'));
196 * Asserts that a file exists physically on disk.
198 * Overrides PHPUnit\Framework\Assert::assertFileExists() to also work with
201 * @param \Drupal\File\FileInterface|string $file
202 * Either the file entity or the file URI.
203 * @param string $message
204 * (optional) A message to display with the assertion.
206 public static function assertFileExists($file, $message = NULL) {
207 $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]);
208 $filename = $file instanceof FileInterface ? $file->getFileUri() : $file;
209 parent::assertFileExists($filename, $message);
213 * Asserts that a file exists in the database.
215 public function assertFileEntryExists($file, $message = NULL) {
216 $this->container->get('entity.manager')->getStorage('file')->resetCache();
217 $db_file = File::load($file->id());
218 $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', ['%file' => $file->getFileUri()]);
219 $this->assertEqual($db_file->getFileUri(), $file->getFileUri(), $message);
223 * Asserts that a file does not exist on disk.
225 * Overrides PHPUnit\Framework\Assert::assertFileExists() to also work with
228 * @param \Drupal\File\FileInterface|string $file
229 * Either the file entity or the file URI.
230 * @param string $message
231 * (optional) A message to display with the assertion.
233 public static function assertFileNotExists($file, $message = NULL) {
234 $message = isset($message) ? $message : format_string('File %file exists on the disk.', ['%file' => $file->getFileUri()]);
235 $filename = $file instanceof FileInterface ? $file->getFileUri() : $file;
236 parent::assertFileNotExists($filename, $message);
240 * Asserts that a file does not exist in the database.
242 public function assertFileEntryNotExists($file, $message) {
243 $this->container->get('entity.manager')->getStorage('file')->resetCache();
244 $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', ['%file' => $file->getFileUri()]);
245 $this->assertFalse(File::load($file->id()), $message);
249 * Asserts that a file's status is set to permanent in the database.
251 public function assertFileIsPermanent(FileInterface $file, $message = NULL) {
252 $message = isset($message) ? $message : format_string('File %file is permanent.', ['%file' => $file->getFileUri()]);
253 $this->assertTrue($file->isPermanent(), $message);