diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php index bf1572c..bb95185 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php @@ -313,8 +313,17 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin $random = new Random(); $settings = $field_definition->getSettings(); + /** + * Prepare upload destination. + * + * @see FileItem::getUploadLocation() + */ + $destination = trim($settings['file_directory'], '/'); + $destination = str_replace('[field_name]', $field_definition->getName(), $destination); + $destination = \Drupal::token()->replace($destination, []); + // Generate a file entity. - $destination = $settings['uri_scheme'] . '://' . $settings['file_directory'] . $random->name(10, TRUE) . '.txt'; + $destination = $settings['uri_scheme'] . '://' . $destination . '/' . $random->name(10, TRUE) . '.txt'; $data = $random->paragraphs(3); $file = file_save_data($data, $destination, FILE_EXISTS_ERROR); $values = array( diff --git a/core/modules/file/src/Tests/FileFieldPathTest.php b/core/modules/file/src/Tests/FileFieldPathTest.php index bf166ed..d42922b 100644 --- a/core/modules/file/src/Tests/FileFieldPathTest.php +++ b/core/modules/file/src/Tests/FileFieldPathTest.php @@ -17,20 +17,28 @@ class FileFieldPathTest extends FileFieldTestBase { * Tests the normal formatter display on node display. */ function testUploadPath() { + /** @var \Drupal\node\NodeStorageInterface $node_storage */ $node_storage = $this->container->get('entity.manager')->getStorage('node'); $field_name = strtolower($this->randomMachineName()); $type_name = 'article'; $this->createFileField($field_name, 'node', $type_name); + /** @var \Drupal\file\FileInterface $test_file */ $test_file = $this->getTestFile('text'); // Create a new node. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); - // Check that the file was uploaded to the file root. + // Check that the file was uploaded to the correct location. $node_storage->resetCache(array($nid)); $node = $node_storage->load($nid); $node_file = file_load($node->{$field_name}->target_id); - $this->assertPathMatch('public://' . $test_file->getFilename(), $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->getFileUri()))); + $expected_filename = + 'public://field/' . + $field_name . '/' . + $this->container->get('date.formatter')->format(REQUEST_TIME, 'custom', 'Y') . '/' . + $this->container->get('date.formatter')->format(REQUEST_TIME, 'custom', 'm') . '/' . + $test_file->getFilename(); + $this->assertPathMatch($expected_filename, $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->getFileUri()))); // Change the path to contain multiple subdirectories. $this->updateFileField($field_name, $type_name, array('file_directory' => 'foo/bar/baz')); diff --git a/core/modules/file/src/Tests/FileItemTest.php b/core/modules/file/src/Tests/FileItemTest.php index 2c5c11c..ac06f39 100644 --- a/core/modules/file/src/Tests/FileItemTest.php +++ b/core/modules/file/src/Tests/FileItemTest.php @@ -49,6 +49,9 @@ protected function setUp() { 'entity_type' => 'entity_test', 'field_name' => 'file_test', 'bundle' => 'entity_test', + 'settings' => array( + 'file_directory' => '', + ), ))->save(); file_put_contents('public://example.txt', $this->randomMachineName()); $this->file = entity_create('file', array(