Update to Drupal 8.0.0 beta 14. For more information, see https://drupal.org/node/2544542
This commit is contained in:
parent
3b2511d96d
commit
81ccda77eb
2155 changed files with 54307 additions and 46870 deletions
|
@ -6,8 +6,10 @@
|
|||
*/
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Datetime\Entity\DateFormat;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Render\BubbleableMetadata;
|
||||
use Drupal\Core\Render\Element;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Url;
|
||||
|
@ -631,6 +633,7 @@ function file_file_download($uri) {
|
|||
*/
|
||||
function file_cron() {
|
||||
$age = \Drupal::config('system.file')->get('temporary_maximum_age');
|
||||
$file_storage = \Drupal::entityManager()->getStorage('file');
|
||||
|
||||
// Only delete temporary files if older than $age. Note that automatic cleanup
|
||||
// is disabled if $age set to 0.
|
||||
|
@ -640,7 +643,7 @@ function file_cron() {
|
|||
->condition('changed', REQUEST_TIME - $age, '<')
|
||||
->range(0, 100)
|
||||
->execute();
|
||||
$files = file_load_multiple($fids);
|
||||
$files = $file_storage->loadMultiple($fids);
|
||||
foreach ($files as $file) {
|
||||
$references = \Drupal::service('file.usage')->listUsage($file);
|
||||
if (empty($references)) {
|
||||
|
@ -937,7 +940,7 @@ function file_file_predelete(File $file) {
|
|||
/**
|
||||
* Implements hook_tokens().
|
||||
*/
|
||||
function file_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
||||
function file_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
|
||||
$token_service = \Drupal::token();
|
||||
|
||||
$url_options = array('absolute' => TRUE);
|
||||
|
@ -986,30 +989,36 @@ function file_tokens($type, $tokens, array $data = array(), array $options = arr
|
|||
|
||||
// These tokens are default variations on the chained tokens handled below.
|
||||
case 'created':
|
||||
$date_format = DateFormat::load('medium');
|
||||
$bubbleable_metadata->addCacheableDependency($date_format);
|
||||
$replacements[$original] = format_date($file->getCreatedTime(), 'medium', '', NULL, $langcode);
|
||||
break;
|
||||
|
||||
case 'changed':
|
||||
$date_format = DateFormat::load('medium');
|
||||
$bubbleable_metadata = $bubbleable_metadata->addCacheableDependency($date_format);
|
||||
$replacements[$original] = format_date($file->getChangedTime(), 'medium', '', NULL, $langcode);
|
||||
break;
|
||||
|
||||
case 'owner':
|
||||
$name = $file->getOwner()->label();
|
||||
$owner = $file->getOwner();
|
||||
$bubbleable_metadata->addCacheableDependency($owner);
|
||||
$name = $owner->label();
|
||||
$replacements[$original] = $sanitize ? SafeMarkup::checkPlain($name) : $name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($date_tokens = $token_service->findWithPrefix($tokens, 'created')) {
|
||||
$replacements += $token_service->generate('date', $date_tokens, array('date' => $file->getCreatedTime()), $options);
|
||||
$replacements += $token_service->generate('date', $date_tokens, array('date' => $file->getCreatedTime()), $options, $bubbleable_metadata);
|
||||
}
|
||||
|
||||
if ($date_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
|
||||
$replacements += $token_service->generate('date', $date_tokens, array('date' => $file->getChangedTime()), $options);
|
||||
$replacements += $token_service->generate('date', $date_tokens, array('date' => $file->getChangedTime()), $options, $bubbleable_metadata);
|
||||
}
|
||||
|
||||
if (($owner_tokens = $token_service->findWithPrefix($tokens, 'owner')) && $file->getOwner()) {
|
||||
$replacements += $token_service->generate('user', $owner_tokens, array('user' => $file->getOwner()), $options);
|
||||
$replacements += $token_service->generate('user', $owner_tokens, array('user' => $file->getOwner()), $options, $bubbleable_metadata);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1225,7 +1234,7 @@ function template_preprocess_file_link(&$variables) {
|
|||
$file = $variables['file'];
|
||||
$options = array();
|
||||
|
||||
$file_entity = ($file instanceof File) ? $file : file_load($file->fid);
|
||||
$file_entity = ($file instanceof File) ? $file : File::load($file->fid);
|
||||
$url = file_create_url($file_entity->getFileUri());
|
||||
|
||||
$mime_type = $file->getMimeType();
|
||||
|
|
|
@ -7,13 +7,12 @@
|
|||
|
||||
namespace Drupal\file\Controller;
|
||||
|
||||
use Drupal\system\Controller\FormAjaxController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
/**
|
||||
* Defines a controller to respond to file widget AJAX requests.
|
||||
*/
|
||||
class FileWidgetAjaxController extends FormAjaxController {
|
||||
class FileWidgetAjaxController {
|
||||
|
||||
/**
|
||||
* Returns the progress status for a file upload process.
|
||||
|
|
|
@ -23,59 +23,54 @@ class FileFieldItemList extends EntityReferenceFieldItemList {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function insert() {
|
||||
parent::insert();
|
||||
public function postSave($update) {
|
||||
$entity = $this->getEntity();
|
||||
|
||||
// Add a new usage for newly uploaded files.
|
||||
foreach ($this->referencedEntities() as $file) {
|
||||
\Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function update() {
|
||||
parent::update();
|
||||
$entity = $this->getEntity();
|
||||
|
||||
// Get current target file entities and file IDs.
|
||||
$files = $this->referencedEntities();
|
||||
$fids = array();
|
||||
|
||||
foreach ($files as $file) {
|
||||
$fids[] = $file->id();
|
||||
}
|
||||
|
||||
// On new revisions, all files are considered to be a new usage and no
|
||||
// deletion of previous file usages are necessary.
|
||||
if (!empty($entity->original) && $entity->getRevisionId() != $entity->original->getRevisionId()) {
|
||||
foreach ($files as $file) {
|
||||
if (!$update) {
|
||||
// Add a new usage for newly uploaded files.
|
||||
foreach ($this->referencedEntities() as $file) {
|
||||
\Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
|
||||
}
|
||||
return;
|
||||
}
|
||||
else {
|
||||
// Get current target file entities and file IDs.
|
||||
$files = $this->referencedEntities();
|
||||
$ids = array();
|
||||
|
||||
// Get the file IDs attached to the field before this update.
|
||||
$field_name = $this->getFieldDefinition()->getName();
|
||||
$original_fids = array();
|
||||
$original_items = $entity->original->getTranslation($this->getLangcode())->$field_name;
|
||||
foreach ($original_items as $item) {
|
||||
$original_fids[] = $item->target_id;
|
||||
}
|
||||
/** @var \Drupal\file\FileInterface $file */
|
||||
foreach ($files as $file) {
|
||||
$ids[] = $file->id();
|
||||
}
|
||||
|
||||
// Decrement file usage by 1 for files that were removed from the field.
|
||||
$removed_fids = array_filter(array_diff($original_fids, $fids));
|
||||
$removed_files = \Drupal::entityManager()->getStorage('file')->loadMultiple($removed_fids);
|
||||
foreach ($removed_files as $file) {
|
||||
\Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id());
|
||||
}
|
||||
// On new revisions, all files are considered to be a new usage and no
|
||||
// deletion of previous file usages are necessary.
|
||||
if (!empty($entity->original) && $entity->getRevisionId() != $entity->original->getRevisionId()) {
|
||||
foreach ($files as $file) {
|
||||
\Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Add new usage entries for newly added files.
|
||||
foreach ($files as $file) {
|
||||
if (!in_array($file->id(), $original_fids)) {
|
||||
\Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
|
||||
// Get the file IDs attached to the field before this update.
|
||||
$field_name = $this->getFieldDefinition()->getName();
|
||||
$original_ids = array();
|
||||
$original_items = $entity->original->getTranslation($this->getLangcode())->$field_name;
|
||||
foreach ($original_items as $item) {
|
||||
$original_ids[] = $item->target_id;
|
||||
}
|
||||
|
||||
// Decrement file usage by 1 for files that were removed from the field.
|
||||
$removed_ids = array_filter(array_diff($original_ids, $ids));
|
||||
$removed_files = \Drupal::entityManager()->getStorage('file')->loadMultiple($removed_ids);
|
||||
foreach ($removed_files as $file) {
|
||||
\Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id());
|
||||
}
|
||||
|
||||
// Add new usage entries for newly added files.
|
||||
foreach ($files as $file) {
|
||||
if (!in_array($file->id(), $original_ids)) {
|
||||
\Drupal::service('file.usage')->add($file, 'file', $entity->getEntityTypeId(), $entity->id());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ use Drupal\Core\Render\ElementInfoManagerInterface;
|
|||
use Drupal\Core\Url;
|
||||
use Drupal\file\Element\ManagedFile;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'file_generic' widget.
|
||||
|
@ -346,10 +347,10 @@ class FileWidget extends WidgetBase implements ContainerFactoryPluginInterface {
|
|||
$removed_files = array_slice($values['fids'], $keep);
|
||||
$removed_names = array();
|
||||
foreach ($removed_files as $fid) {
|
||||
$file = file_load($fid);
|
||||
$file = File::load($fid);
|
||||
$removed_names[] = $file->getFilename();
|
||||
}
|
||||
$args = array('%field' => $field_storage->getFieldName(), '@max' => $field_storage->getCardinality(), '@count' => $keep, '%list' => implode(', ', $removed_names));
|
||||
$args = array('%field' => $field_storage->getName(), '@max' => $field_storage->getCardinality(), '@count' => $uploaded, '%list' => implode(', ', $removed_names));
|
||||
$message = t('Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', $args);
|
||||
drupal_set_message($message, 'warning');
|
||||
$values['fids'] = array_slice($values['fids'], 0, $keep);
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests the file copy function.
|
||||
*
|
||||
|
@ -39,7 +41,7 @@ class CopyTest extends FileManagedUnitTestBase {
|
|||
|
||||
// Reload the file from the database and check that the changes were
|
||||
// actually saved.
|
||||
$this->assertFileUnchanged($result, file_load($result->id(), TRUE));
|
||||
$this->assertFileUnchanged($result, File::load($result->id()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,9 +68,9 @@ class CopyTest extends FileManagedUnitTestBase {
|
|||
|
||||
// Load all the affected files to check the changes that actually made it
|
||||
// to the database.
|
||||
$loaded_source = file_load($source->id(), TRUE);
|
||||
$loaded_target = file_load($target->id(), TRUE);
|
||||
$loaded_result = file_load($result->id(), TRUE);
|
||||
$loaded_source = File::load($source->id());
|
||||
$loaded_target = File::load($target->id());
|
||||
$loaded_result = File::load($result->id());
|
||||
|
||||
// Verify that the source file wasn't changed.
|
||||
$this->assertFileUnchanged($source, $loaded_source);
|
||||
|
@ -106,9 +108,9 @@ class CopyTest extends FileManagedUnitTestBase {
|
|||
|
||||
// Load all the affected files to check the changes that actually made it
|
||||
// to the database.
|
||||
$loaded_source = file_load($source->id(), TRUE);
|
||||
$loaded_target = file_load($target->id(), TRUE);
|
||||
$loaded_result = file_load($result->id(), TRUE);
|
||||
$loaded_source = File::load($source->id());
|
||||
$loaded_target = File::load($target->id());
|
||||
$loaded_result = File::load($result->id());
|
||||
|
||||
// Verify that the source file wasn't changed.
|
||||
$this->assertFileUnchanged($source, $loaded_source);
|
||||
|
@ -141,7 +143,7 @@ class CopyTest extends FileManagedUnitTestBase {
|
|||
// Check that the correct hooks were called.
|
||||
$this->assertFileHooksCalled(array());
|
||||
|
||||
$this->assertFileUnchanged($source, file_load($source->id(), TRUE));
|
||||
$this->assertFileUnchanged($target, file_load($target->id(), TRUE));
|
||||
$this->assertFileUnchanged($source, File::load($source->id()));
|
||||
$this->assertFileUnchanged($target, File::load($target->id()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests the file delete function.
|
||||
*
|
||||
|
@ -24,7 +26,7 @@ class DeleteTest extends FileManagedUnitTestBase {
|
|||
$file->delete();
|
||||
$this->assertFileHooksCalled(array('delete'));
|
||||
$this->assertFalse(file_exists($file->getFileUri()), 'Test file has actually been deleted.');
|
||||
$this->assertFalse(file_load($file->id()), 'File was removed from the database.');
|
||||
$this->assertFalse(File::load($file->id()), 'File was removed from the database.');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,7 +42,7 @@ class DeleteTest extends FileManagedUnitTestBase {
|
|||
$usage = $file_usage->listUsage($file);
|
||||
$this->assertEqual($usage['testing']['test'], array(1 => 1), 'Test file is still in use.');
|
||||
$this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
|
||||
$this->assertTrue(file_load($file->id()), 'File still exists in the database.');
|
||||
$this->assertTrue(File::load($file->id()), 'File still exists in the database.');
|
||||
|
||||
// Clear out the call to hook_file_load().
|
||||
file_test_reset();
|
||||
|
@ -50,7 +52,7 @@ class DeleteTest extends FileManagedUnitTestBase {
|
|||
$this->assertFileHooksCalled(array('load', 'update'));
|
||||
$this->assertTrue(empty($usage), 'File usage data was removed.');
|
||||
$this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.');
|
||||
$file = file_load($file->id());
|
||||
$file = File::load($file->id());
|
||||
$this->assertTrue($file, 'File still exists in the database.');
|
||||
$this->assertTrue($file->isTemporary(), 'File is temporary.');
|
||||
file_test_reset();
|
||||
|
@ -69,6 +71,6 @@ class DeleteTest extends FileManagedUnitTestBase {
|
|||
// file_cron() loads
|
||||
$this->assertFileHooksCalled(array('delete'));
|
||||
$this->assertFalse(file_exists($file->getFileUri()), 'File has been deleted after its last usage was removed.');
|
||||
$this->assertFalse(file_load($file->id()), 'File was removed from the database.');
|
||||
$this->assertFalse(File::load($file->id()), 'File was removed from the database.');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ class DownloadTest extends FileManagedTestBase {
|
|||
$url = file_create_url($file->getFileUri());
|
||||
// URLs can't contain characters outside the ASCII set so $filename has to be
|
||||
// encoded.
|
||||
$filename = $GLOBALS['base_url'] . '/' . file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath() . '/' . rawurlencode($file->getFilename());
|
||||
$filename = $GLOBALS['base_url'] . '/' . \Drupal::service('stream_wrapper_manager')->getViaScheme('public')->getDirectoryPath() . '/' . rawurlencode($file->getFilename());
|
||||
$this->assertEqual($filename, $url, 'Correctly generated a URL for a created file.');
|
||||
$this->drupalHead($url);
|
||||
$this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the created file.');
|
||||
|
@ -117,7 +117,7 @@ class DownloadTest extends FileManagedTestBase {
|
|||
$clean_urls = $clean_url_setting == 'clean';
|
||||
$request = $this->prepareRequestForGenerator($clean_urls);
|
||||
$base_path = $request->getSchemeAndHttpHost() . $request->getBasePath();
|
||||
$this->checkUrl('public', '', $basename, $base_path . '/' . file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath() . '/' . $basename_encoded);
|
||||
$this->checkUrl('public', '', $basename, $base_path . '/' . \Drupal::service('stream_wrapper_manager')->getViaScheme('public')->getDirectoryPath() . '/' . $basename_encoded);
|
||||
$this->checkUrl('private', '', $basename, $base_path . '/' . $script_path . 'system/files/' . $basename_encoded);
|
||||
}
|
||||
$this->assertEqual(file_create_url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='), 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==', t('Generated URL matches expected URL.'));
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests the display of file fields in node and views.
|
||||
|
@ -57,7 +58,7 @@ class FileFieldDisplayTest extends FileFieldTestBase {
|
|||
$node_storage = $this->container->get('entity.manager')->getStorage('node');
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$file_link = array(
|
||||
'#theme' => 'file_link',
|
||||
'#file' => $node_file,
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests that files are uploaded to proper locations.
|
||||
*
|
||||
|
@ -29,7 +31,7 @@ class FileFieldPathTest extends FileFieldTestBase {
|
|||
// Check that the file was uploaded to the file root.
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$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())));
|
||||
|
||||
// Change the path to contain multiple subdirectories.
|
||||
|
@ -41,7 +43,7 @@ class FileFieldPathTest extends FileFieldTestBase {
|
|||
// Check that the file was uploaded into the subdirectory.
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id, TRUE);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertPathMatch('public://foo/bar/baz/' . $test_file->getFilename(), $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->getFileUri())));
|
||||
|
||||
// Check the path when used with tokens.
|
||||
|
@ -54,7 +56,7 @@ class FileFieldPathTest extends FileFieldTestBase {
|
|||
// Check that the file was uploaded into the subdirectory.
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
// Do token replacement using the same user which uploaded the file, not
|
||||
// the user running the test case.
|
||||
$data = array('user' => $this->adminUser);
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Ensure that files added to nodes appear correctly in RSS feeds.
|
||||
|
@ -63,7 +64,7 @@ class FileFieldRSSContentTest extends FileFieldTestBase {
|
|||
// Get the uploaded file from the node.
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
|
||||
// Check that the RSS enclosure appears in the RSS feed.
|
||||
$this->drupalGet('rss.xml');
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests creating and deleting revisions with files attached.
|
||||
*
|
||||
|
@ -40,7 +42,7 @@ class FileFieldRevisionTest extends FileFieldTestBase {
|
|||
// Check that the file exists on disk and in the database.
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file_r1 = file_load($node->{$field_name}->target_id);
|
||||
$node_file_r1 = File::load($node->{$field_name}->target_id);
|
||||
$node_vid_r1 = $node->getRevisionId();
|
||||
$this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.');
|
||||
$this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
|
||||
|
@ -50,7 +52,7 @@ class FileFieldRevisionTest extends FileFieldTestBase {
|
|||
$this->replaceNodeFile($test_file, $field_name, $nid);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file_r2 = file_load($node->{$field_name}->target_id);
|
||||
$node_file_r2 = File::load($node->{$field_name}->target_id);
|
||||
$node_vid_r2 = $node->getRevisionId();
|
||||
$this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.');
|
||||
$this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.');
|
||||
|
@ -58,7 +60,7 @@ class FileFieldRevisionTest extends FileFieldTestBase {
|
|||
|
||||
// Check that the original file is still in place on the first revision.
|
||||
$node = node_revision_load($node_vid_r1);
|
||||
$current_file = file_load($node->{$field_name}->target_id);
|
||||
$current_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertEqual($node_file_r1->id(), $current_file->id(), 'Original file still in place after replacing file in new revision.');
|
||||
$this->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.');
|
||||
$this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision');
|
||||
|
@ -69,7 +71,7 @@ class FileFieldRevisionTest extends FileFieldTestBase {
|
|||
$this->drupalPostForm('node/' . $nid . '/edit', array('revision' => '1'), t('Save and keep published'));
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file_r3 = file_load($node->{$field_name}->target_id);
|
||||
$node_file_r3 = File::load($node->{$field_name}->target_id);
|
||||
$node_vid_r3 = $node->getRevisionId();
|
||||
$this->assertEqual($node_file_r2->id(), $node_file_r3->id(), 'Previous revision file still in place after creating a new revision without a new file.');
|
||||
$this->assertFileIsPermanent($node_file_r3, 'New revision file is permanent.');
|
||||
|
@ -78,7 +80,7 @@ class FileFieldRevisionTest extends FileFieldTestBase {
|
|||
$this->drupalPostForm('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', array(), t('Revert'));
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file_r4 = file_load($node->{$field_name}->target_id);
|
||||
$node_file_r4 = File::load($node->{$field_name}->target_id);
|
||||
$this->assertEqual($node_file_r1->id(), $node_file_r4->id(), 'Original revision file still in place after reverting to the original revision.');
|
||||
$this->assertFileIsPermanent($node_file_r4, 'Original revision file still permanent after reverting to the original revision.');
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ use Drupal\field\Entity\FieldStorageConfig;
|
|||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\file\FileInterface;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Provides methods specifically for testing File module's field handling.
|
||||
|
@ -45,7 +46,8 @@ abstract class FileFieldTestBase extends WebTestBase {
|
|||
// Get a file to upload.
|
||||
$file = current($this->drupalGetTestFiles($type_name, $size));
|
||||
|
||||
// Add a filesize property to files as would be read by file_load().
|
||||
// Add a filesize property to files as would be read by
|
||||
// \Drupal\file\Entity\File::load().
|
||||
$file->filesize = filesize($file->uri);
|
||||
|
||||
return entity_create('file', (array) $file);
|
||||
|
@ -145,18 +147,57 @@ abstract class FileFieldTestBase extends WebTestBase {
|
|||
|
||||
/**
|
||||
* Uploads a file to a node.
|
||||
*
|
||||
* @param \Drupal\file\FileInterface $file
|
||||
* The File to be uploaded.
|
||||
* @param string $field_name
|
||||
* The name of the field on which the files should be saved.
|
||||
* @param $nid_or_type
|
||||
* A numeric node id to upload files to an existing node, or a string
|
||||
* indicating the desired bundle for a new node.
|
||||
* @param bool $new_revision
|
||||
* The revision number.
|
||||
* @param array $extras
|
||||
* Additional values when a new node is created.
|
||||
*
|
||||
* @return int
|
||||
* The node id.
|
||||
*/
|
||||
function uploadNodeFile($file, $field_name, $nid_or_type, $new_revision = TRUE, $extras = array()) {
|
||||
function uploadNodeFile(FileInterface $file, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = array()) {
|
||||
return $this->uploadNodeFiles([$file], $field_name, $nid_or_type, $new_revision, $extras);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads multiple files to a node.
|
||||
*
|
||||
* @param \Drupal\file\FileInterface[] $files
|
||||
* The files to be uploaded.
|
||||
* @param string $field_name
|
||||
* The name of the field on which the files should be saved.
|
||||
* @param $nid_or_type
|
||||
* A numeric node id to upload files to an existing node, or a string
|
||||
* indicating the desired bundle for a new node.
|
||||
* @param bool $new_revision
|
||||
* The revision number.
|
||||
* @param array $extras
|
||||
* Additional values when a new node is created.
|
||||
*
|
||||
* @return int
|
||||
* The node id.
|
||||
*/
|
||||
function uploadNodeFiles(array $files, $field_name, $nid_or_type, $new_revision = TRUE, array $extras = array()) {
|
||||
$edit = array(
|
||||
'title[0][value]' => $this->randomMachineName(),
|
||||
'revision' => (string) (int) $new_revision,
|
||||
);
|
||||
|
||||
$node_storage = $this->container->get('entity.manager')->getStorage('node');
|
||||
if (is_numeric($nid_or_type)) {
|
||||
$nid = $nid_or_type;
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
}
|
||||
else {
|
||||
$node_storage = $this->container->get('entity.manager')->getStorage('node');
|
||||
// Add a new node.
|
||||
$extras['type'] = $nid_or_type;
|
||||
$node = $this->drupalCreateNode($extras);
|
||||
|
@ -169,13 +210,23 @@ abstract class FileFieldTestBase extends WebTestBase {
|
|||
$this->assertNotEqual($nid, $node->getRevisionId(), 'Node revision exists.');
|
||||
}
|
||||
|
||||
// Attach a file to the node.
|
||||
// Attach files to the node.
|
||||
$field_storage = FieldStorageConfig::loadByName('node', $field_name);
|
||||
$name = 'files[' . $field_name . '_0]';
|
||||
// File input name depends on number of files already uploaded.
|
||||
$field_num = count($node->{$field_name});
|
||||
$name = 'files[' . $field_name . "_$field_num]";
|
||||
if ($field_storage->getCardinality() != 1) {
|
||||
$name .= '[]';
|
||||
}
|
||||
$edit[$name] = drupal_realpath($file->getFileUri());
|
||||
foreach ($files as $file) {
|
||||
$file_path = $this->container->get('file_system')->realpath($file->getFileUri());
|
||||
if (count($files) == 1) {
|
||||
$edit[$name] = $file_path;
|
||||
}
|
||||
else {
|
||||
$edit[$name][] = $file_path;
|
||||
}
|
||||
}
|
||||
$this->drupalPostForm("node/$nid/edit", $edit, t('Save and keep published'));
|
||||
|
||||
return $nid;
|
||||
|
@ -221,7 +272,7 @@ abstract class FileFieldTestBase extends WebTestBase {
|
|||
*/
|
||||
function assertFileEntryExists($file, $message = NULL) {
|
||||
$this->container->get('entity.manager')->getStorage('file')->resetCache();
|
||||
$db_file = file_load($file->id());
|
||||
$db_file = File::load($file->id());
|
||||
$message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->getFileUri()));
|
||||
$this->assertEqual($db_file->getFileUri(), $file->getFileUri(), $message);
|
||||
}
|
||||
|
@ -240,7 +291,7 @@ abstract class FileFieldTestBase extends WebTestBase {
|
|||
function assertFileEntryNotExists($file, $message) {
|
||||
$this->container->get('entity.manager')->getStorage('file')->resetCache();
|
||||
$message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->getFileUri()));
|
||||
$this->assertFalse(file_load($file->id()), $message);
|
||||
$this->assertFalse(File::load($file->id()), $message);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,6 +9,7 @@ namespace Drupal\file\Tests;
|
|||
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests validation functions such as file type, max file size, max size per
|
||||
|
@ -44,7 +45,7 @@ class FileFieldValidateTest extends FileFieldTestBase {
|
|||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertFileExists($node_file, 'File exists after uploading to the required field.');
|
||||
$this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
|
||||
|
||||
|
@ -63,7 +64,7 @@ class FileFieldValidateTest extends FileFieldTestBase {
|
|||
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
|
||||
$this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
|
||||
}
|
||||
|
@ -95,7 +96,7 @@ class FileFieldValidateTest extends FileFieldTestBase {
|
|||
$nid = $this->uploadNodeFile($small_file, $field_name, $type_name);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize)));
|
||||
$this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize)));
|
||||
|
||||
|
@ -112,7 +113,7 @@ class FileFieldValidateTest extends FileFieldTestBase {
|
|||
$nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->getSize()))));
|
||||
$this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->getSize()))));
|
||||
}
|
||||
|
@ -136,7 +137,7 @@ class FileFieldValidateTest extends FileFieldTestBase {
|
|||
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
|
||||
$this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
|
||||
|
||||
|
@ -155,7 +156,7 @@ class FileFieldValidateTest extends FileFieldTestBase {
|
|||
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.');
|
||||
$this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ use Drupal\comment\Tests\CommentTestTrait;
|
|||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field_ui\Tests\FieldUiTestTrait;
|
||||
use Drupal\user\RoleInterface;
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests the file field widget, single and multi-valued, with and without AJAX,
|
||||
|
@ -58,7 +59,7 @@ class FileFieldWidgetTest extends FileFieldTestBase {
|
|||
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertFileExists($node_file, 'New file saved to disk on node creation.');
|
||||
|
||||
// Ensure the file can be downloaded.
|
||||
|
@ -111,8 +112,9 @@ class FileFieldWidgetTest extends FileFieldTestBase {
|
|||
// names).
|
||||
$field_name = 'test_file_field_1';
|
||||
$field_name2 = 'test_file_field_2';
|
||||
$this->createFileField($field_name, 'node', $type_name, array('cardinality' => 3));
|
||||
$this->createFileField($field_name2, 'node', $type_name, array('cardinality' => 3));
|
||||
$cardinality = 3;
|
||||
$this->createFileField($field_name, 'node', $type_name, array('cardinality' => $cardinality));
|
||||
$this->createFileField($field_name2, 'node', $type_name, array('cardinality' => $cardinality));
|
||||
|
||||
$test_file = $this->getTestFile('text');
|
||||
|
||||
|
@ -214,6 +216,49 @@ class FileFieldWidgetTest extends FileFieldTestBase {
|
|||
$node = $node_storage->load($nid);
|
||||
$this->assertTrue(empty($node->{$field_name}->target_id), 'Node was successfully saved without any files.');
|
||||
}
|
||||
|
||||
$upload_files = array($test_file, $test_file);
|
||||
// Try to upload multiple files, but fewer than the maximum.
|
||||
$nid = $this->uploadNodeFiles($upload_files, $field_name, $type_name);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$this->assertEqual(count($node->{$field_name}), count($upload_files), 'Node was successfully saved with mulitple files.');
|
||||
|
||||
// Try to upload more files than allowed on revision.
|
||||
$this->uploadNodeFiles($upload_files, $field_name, $nid, 1);
|
||||
$args = array(
|
||||
'%field' => $field_name,
|
||||
'@count' => $cardinality
|
||||
);
|
||||
$this->assertRaw(t('%field: this field cannot hold more than @count values.', $args));
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$this->assertEqual(count($node->{$field_name}), count($upload_files), 'More files than allowed could not be saved to node.');
|
||||
|
||||
// Try to upload exactly the allowed number of files on revision.
|
||||
$this->uploadNodeFile($test_file, $field_name, $nid, 1);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$this->assertEqual(count($node->{$field_name}), $cardinality, 'Node was successfully revised to maximum number of files.');
|
||||
|
||||
// Try to upload exactly the allowed number of files, new node.
|
||||
$upload_files[] = $test_file;
|
||||
$nid = $this->uploadNodeFiles($upload_files, $field_name, $type_name);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$this->assertEqual(count($node->{$field_name}), $cardinality, 'Node was successfully saved with maximum number of files.');
|
||||
|
||||
// Try to upload more files than allowed, new node.
|
||||
$upload_files[] = $test_file;
|
||||
$this->uploadNodeFiles($upload_files, $field_name, $type_name);
|
||||
|
||||
$args = [
|
||||
'%field' => $field_name,
|
||||
'@max' => $cardinality,
|
||||
'@count' => count($upload_files),
|
||||
'%list' => $test_file->getFileName(),
|
||||
];
|
||||
$this->assertRaw(t('Field %field can only hold @max values but there were @count uploaded. The following files have been omitted as a result: %list.', $args));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -238,7 +283,7 @@ class FileFieldWidgetTest extends FileFieldTestBase {
|
|||
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$node_file = file_load($node->{$field_name}->target_id);
|
||||
$node_file = File::load($node->{$field_name}->target_id);
|
||||
$this->assertFileExists($node_file, 'New file saved to disk on node creation.');
|
||||
|
||||
// Ensure the private file is available to the user who uploaded it.
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests file listing page functionality.
|
||||
|
@ -103,7 +104,7 @@ class FileListingTest extends FileFieldTestBase {
|
|||
$this->drupalGet('admin/content/files');
|
||||
|
||||
foreach ($nodes as $node) {
|
||||
$file = entity_load('file', $node->file->target_id);
|
||||
$file = File::load($node->file->target_id);
|
||||
$this->assertText($file->getFilename());
|
||||
$this->assertLinkByHref(file_create_url($file->getFileUri()));
|
||||
$this->assertLinkByHref('admin/content/files/usage/' . $file->id());
|
||||
|
@ -117,11 +118,11 @@ class FileListingTest extends FileFieldTestBase {
|
|||
$nodes[1]->save();
|
||||
|
||||
$this->drupalGet('admin/content/files');
|
||||
$file = entity_load('file', $orphaned_file);
|
||||
$file = File::load($orphaned_file);
|
||||
$usage = $this->sumUsages($file_usage->listUsage($file));
|
||||
$this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage);
|
||||
|
||||
$file = entity_load('file', $used_file);
|
||||
$file = File::load($used_file);
|
||||
$usage = $this->sumUsages($file_usage->listUsage($file));
|
||||
$this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage);
|
||||
|
||||
|
@ -130,7 +131,7 @@ class FileListingTest extends FileFieldTestBase {
|
|||
|
||||
// Test file usage page.
|
||||
foreach ($nodes as $node) {
|
||||
$file = entity_load('file', $node->file->target_id);
|
||||
$file = File::load($node->file->target_id);
|
||||
$usage = $file_usage->listUsage($file);
|
||||
$this->drupalGet('admin/content/files/usage/' . $file->id());
|
||||
$this->assertResponse(200);
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Render\BubbleableMetadata;
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Generates text using placeholders for dummy content to check file token
|
||||
|
@ -40,7 +42,7 @@ class FileTokenReplaceTest extends FileFieldTestBase {
|
|||
// Load the node and the file.
|
||||
$node_storage->resetCache(array($nid));
|
||||
$node = $node_storage->load($nid);
|
||||
$file = file_load($node->{$field_name}->target_id);
|
||||
$file = File::load($node->{$field_name}->target_id);
|
||||
|
||||
// Generate and test sanitized tokens.
|
||||
$tests = array();
|
||||
|
@ -57,12 +59,31 @@ class FileTokenReplaceTest extends FileFieldTestBase {
|
|||
$tests['[file:owner]'] = SafeMarkup::checkPlain(user_format_name($this->adminUser));
|
||||
$tests['[file:owner:uid]'] = $file->getOwnerId();
|
||||
|
||||
$base_bubbleable_metadata = BubbleableMetadata::createFromObject($file);
|
||||
$metadata_tests = [];
|
||||
$metadata_tests['[file:fid]'] = $base_bubbleable_metadata;
|
||||
$metadata_tests['[file:name]'] = $base_bubbleable_metadata;
|
||||
$metadata_tests['[file:path]'] = $base_bubbleable_metadata;
|
||||
$metadata_tests['[file:mime]'] = $base_bubbleable_metadata;
|
||||
$metadata_tests['[file:size]'] = $base_bubbleable_metadata;
|
||||
$metadata_tests['[file:url]'] = $base_bubbleable_metadata;
|
||||
$bubbleable_metadata = clone $base_bubbleable_metadata;
|
||||
$metadata_tests['[file:created]'] = $bubbleable_metadata->addCacheTags(['rendered']);
|
||||
$metadata_tests['[file:created:short]'] = $bubbleable_metadata;
|
||||
$metadata_tests['[file:changed]'] = $bubbleable_metadata;
|
||||
$metadata_tests['[file:changed:short]'] = $bubbleable_metadata;
|
||||
$bubbleable_metadata = clone $base_bubbleable_metadata;
|
||||
$metadata_tests['[file:owner]'] = $bubbleable_metadata->addCacheTags(['user:2']);
|
||||
$metadata_tests['[file:owner:uid]'] = $bubbleable_metadata;
|
||||
|
||||
// Test to make sure that we generated something for each token.
|
||||
$this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
|
||||
|
||||
foreach ($tests as $input => $expected) {
|
||||
$output = $token_service->replace($input, array('file' => $file), array('langcode' => $language_interface->getId()));
|
||||
$bubbleable_metadata = new BubbleableMetadata();
|
||||
$output = $token_service->replace($input, array('file' => $file), array('langcode' => $language_interface->getId()), $bubbleable_metadata);
|
||||
$this->assertEqual($output, $expected, format_string('Sanitized file token %token replaced.', array('%token' => $input)));
|
||||
$this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
|
||||
}
|
||||
|
||||
// Generate and test unsanitized tokens.
|
||||
|
|
|
@ -7,8 +7,10 @@
|
|||
|
||||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests the file_load() function.
|
||||
* Tests \Drupal\file\Entity\File::load().
|
||||
*
|
||||
* @group file
|
||||
*/
|
||||
|
@ -17,7 +19,7 @@ class LoadTest extends FileManagedUnitTestBase {
|
|||
* Try to load a non-existent file by fid.
|
||||
*/
|
||||
function testLoadMissingFid() {
|
||||
$this->assertFalse(file_load(-1), 'Try to load an invalid fid fails.');
|
||||
$this->assertFalse(File::load(-1), 'Try to load an invalid fid fails.');
|
||||
$this->assertFileHooksCalled(array());
|
||||
}
|
||||
|
||||
|
@ -45,10 +47,9 @@ class LoadTest extends FileManagedUnitTestBase {
|
|||
function testSingleValues() {
|
||||
// Create a new file entity from scratch so we know the values.
|
||||
$file = $this->createFile('druplicon.txt', NULL, 'public');
|
||||
|
||||
$by_fid_file = file_load($file->id());
|
||||
$by_fid_file = File::load($file->id());
|
||||
$this->assertFileHookCalled('load');
|
||||
$this->assertTrue(is_object($by_fid_file), 'file_load() returned an object.');
|
||||
$this->assertTrue(is_object($by_fid_file), '\Drupal\file\Entity\File::load() returned an object.');
|
||||
$this->assertEqual($by_fid_file->id(), $file->id(), 'Loading by fid got the same fid.', 'File');
|
||||
$this->assertEqual($by_fid_file->getFileUri(), $file->getFileUri(), 'Loading by fid got the correct filepath.', 'File');
|
||||
$this->assertEqual($by_fid_file->getFilename(), $file->getFilename(), 'Loading by fid got the correct filename.', 'File');
|
||||
|
@ -68,16 +69,16 @@ class LoadTest extends FileManagedUnitTestBase {
|
|||
file_test_reset();
|
||||
$by_path_files = entity_load_multiple_by_properties('file', array('uri' => $file->getFileUri()));
|
||||
$this->assertFileHookCalled('load');
|
||||
$this->assertEqual(1, count($by_path_files), 'file_load_multiple() returned an array of the correct size.');
|
||||
$this->assertEqual(1, count($by_path_files), 'entity_load_multiple_by_properties() returned an array of the correct size.');
|
||||
$by_path_file = reset($by_path_files);
|
||||
$this->assertTrue($by_path_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
|
||||
$this->assertEqual($by_path_file->id(), $file->id(), 'Loading by filepath got the correct fid.', 'File');
|
||||
|
||||
// Load by fid.
|
||||
file_test_reset();
|
||||
$by_fid_files = file_load_multiple(array($file->id()));
|
||||
$by_fid_files = File::loadMultiple(array($file->id()));
|
||||
$this->assertFileHooksCalled(array());
|
||||
$this->assertEqual(1, count($by_fid_files), 'file_load_multiple() returned an array of the correct size.');
|
||||
$this->assertEqual(1, count($by_fid_files), '\Drupal\file\Entity\File::loadMultiple() returned an array of the correct size.');
|
||||
$by_fid_file = reset($by_fid_files);
|
||||
$this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
|
||||
$this->assertEqual($by_fid_file->getFileUri(), $file->getFileUri(), 'Loading by fid got the correct filepath.', 'File');
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests the file move function.
|
||||
*
|
||||
|
@ -38,7 +40,7 @@ class MoveTest extends FileManagedUnitTestBase {
|
|||
|
||||
// Reload the file from the database and check that the changes were
|
||||
// actually saved.
|
||||
$loaded_file = file_load($result->id(), TRUE);
|
||||
$loaded_file = File::load($result->id());
|
||||
$this->assertTrue($loaded_file, 'File can be loaded from the database.');
|
||||
$this->assertFileUnchanged($result, $loaded_file);
|
||||
}
|
||||
|
@ -66,14 +68,14 @@ class MoveTest extends FileManagedUnitTestBase {
|
|||
$this->assertFileHooksCalled(array('move', 'load', 'update'));
|
||||
|
||||
// Compare the returned value to what made it into the database.
|
||||
$this->assertFileUnchanged($result, file_load($result->id(), TRUE));
|
||||
$this->assertFileUnchanged($result, File::load($result->id()));
|
||||
// The target file should not have been altered.
|
||||
$this->assertFileUnchanged($target, file_load($target->id(), TRUE));
|
||||
$this->assertFileUnchanged($target, File::load($target->id()));
|
||||
// Make sure we end up with two distinct files afterwards.
|
||||
$this->assertDifferentFile($target, $result);
|
||||
|
||||
// Compare the source and results.
|
||||
$loaded_source = file_load($source->id(), TRUE);
|
||||
$loaded_source = File::load($source->id());
|
||||
$this->assertEqual($loaded_source->id(), $result->id(), "Returned file's id matches the source.");
|
||||
$this->assertNotEqual($loaded_source->getFileUri(), $source->getFileUri(), 'Returned file path has changed from the original.');
|
||||
}
|
||||
|
@ -102,7 +104,7 @@ class MoveTest extends FileManagedUnitTestBase {
|
|||
|
||||
// Reload the file from the database and check that the changes were
|
||||
// actually saved.
|
||||
$loaded_result = file_load($result->id(), TRUE);
|
||||
$loaded_result = File::load($result->id());
|
||||
$this->assertFileUnchanged($result, $loaded_result);
|
||||
// Check that target was re-used.
|
||||
$this->assertSameFile($target, $loaded_result);
|
||||
|
@ -129,7 +131,7 @@ class MoveTest extends FileManagedUnitTestBase {
|
|||
|
||||
// Load the file from the database and make sure it is identical to what
|
||||
// was returned.
|
||||
$this->assertFileUnchanged($source, file_load($source->id(), TRUE));
|
||||
$this->assertFileUnchanged($source, File::load($source->id()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -156,7 +158,7 @@ class MoveTest extends FileManagedUnitTestBase {
|
|||
|
||||
// Load the file from the database and make sure it is identical to what
|
||||
// was returned.
|
||||
$this->assertFileUnchanged($source, file_load($source->id(), TRUE));
|
||||
$this->assertFileUnchanged($target, file_load($target->id(), TRUE));
|
||||
$this->assertFileUnchanged($source, File::load($source->id()));
|
||||
$this->assertFileUnchanged($target, File::load($target->id()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests the file_save_data() function.
|
||||
*
|
||||
|
@ -32,7 +34,7 @@ class SaveDataTest extends FileManagedUnitTestBase {
|
|||
$this->assertFileHooksCalled(array('insert'));
|
||||
|
||||
// Verify that what was returned is what's in the database.
|
||||
$this->assertFileUnchanged($result, file_load($result->id(), TRUE));
|
||||
$this->assertFileUnchanged($result, File::load($result->id()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -57,7 +59,7 @@ class SaveDataTest extends FileManagedUnitTestBase {
|
|||
$this->assertFileHooksCalled(array('insert'));
|
||||
|
||||
// Verify that what was returned is what's in the database.
|
||||
$this->assertFileUnchanged($result, file_load($result->id(), TRUE));
|
||||
$this->assertFileUnchanged($result, File::load($result->id()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,10 +84,10 @@ class SaveDataTest extends FileManagedUnitTestBase {
|
|||
|
||||
// Ensure that the existing file wasn't overwritten.
|
||||
$this->assertDifferentFile($existing, $result);
|
||||
$this->assertFileUnchanged($existing, file_load($existing->id(), TRUE));
|
||||
$this->assertFileUnchanged($existing, File::load($existing->id()));
|
||||
|
||||
// Verify that was returned is what's in the database.
|
||||
$this->assertFileUnchanged($result, file_load($result->id(), TRUE));
|
||||
$this->assertFileUnchanged($result, File::load($result->id()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,7 +114,7 @@ class SaveDataTest extends FileManagedUnitTestBase {
|
|||
$this->assertSameFile($existing, $result);
|
||||
|
||||
// Verify that what was returned is what's in the database.
|
||||
$this->assertFileUnchanged($result, file_load($result->id(), TRUE));
|
||||
$this->assertFileUnchanged($result, File::load($result->id()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -131,6 +133,6 @@ class SaveDataTest extends FileManagedUnitTestBase {
|
|||
$this->assertFileHooksCalled(array());
|
||||
|
||||
// Ensure that the existing file wasn't overwritten.
|
||||
$this->assertFileUnchanged($existing, file_load($existing->id(), TRUE));
|
||||
$this->assertFileUnchanged($existing, File::load($existing->id()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ class SaveTest extends FileManagedUnitTestBase {
|
|||
$this->assertFileHooksCalled(array('insert'));
|
||||
|
||||
$this->assertTrue($file->id() > 0, 'A new file ID is set when saving a new file to the database.', 'File');
|
||||
$loaded_file = file_load($file->id());
|
||||
$loaded_file = File::load($file->id());
|
||||
$this->assertNotNull($loaded_file, 'Record exists in the database.');
|
||||
$this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
|
||||
$this->assertEqual($file->getSize(), filesize($file->getFileUri()), 'File size was set correctly.', 'File');
|
||||
|
@ -53,7 +53,7 @@ class SaveTest extends FileManagedUnitTestBase {
|
|||
|
||||
$this->assertEqual($file->id(), $file->id(), 'The file ID of an existing file is not changed when updating the database.', 'File');
|
||||
$this->assertTrue($file->getChangedTime() >= $file->getChangedTime(), "Timestamp didn't go backwards.", 'File');
|
||||
$loaded_file = file_load($file->id());
|
||||
$loaded_file = File::load($file->id());
|
||||
$this->assertNotNull($loaded_file, 'Record still exists in the database.', 'File');
|
||||
$this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
|
||||
$this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was saved correctly.');
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
namespace Drupal\file\Tests;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests the file_save_upload() function.
|
||||
*
|
||||
|
@ -81,7 +83,7 @@ class SaveUploadTest extends FileManagedTestBase {
|
|||
function testNormal() {
|
||||
$max_fid_after = db_query('SELECT MAX(fid) AS fid FROM {file_managed}')->fetchField();
|
||||
$this->assertTrue($max_fid_after > $this->maxFidBefore, 'A new file was created.');
|
||||
$file1 = file_load($max_fid_after);
|
||||
$file1 = File::load($max_fid_after);
|
||||
$this->assertTrue($file1, 'Loaded the file.');
|
||||
// MIME type of the uploaded image may be either image/jpeg or image/png.
|
||||
$this->assertEqual(substr($file1->getMimeType(), 0, 5), 'image', 'A MIME type was set.');
|
||||
|
@ -100,13 +102,13 @@ class SaveUploadTest extends FileManagedTestBase {
|
|||
// Check that the correct hooks were called.
|
||||
$this->assertFileHooksCalled(array('validate', 'insert'));
|
||||
|
||||
$file2 = file_load($max_fid_after);
|
||||
$file2 = File::load($max_fid_after);
|
||||
$this->assertTrue($file2, 'Loaded the file');
|
||||
// MIME type of the uploaded image may be either image/jpeg or image/png.
|
||||
$this->assertEqual(substr($file2->getMimeType(), 0, 5), 'image', 'A MIME type was set.');
|
||||
|
||||
// Load both files using file_load_multiple().
|
||||
$files = file_load_multiple(array($file1->id(), $file2->id()));
|
||||
// Load both files using File::loadMultiple().
|
||||
$files = File::loadMultiple(array($file1->id(), $file2->id()));
|
||||
$this->assertTrue(isset($files[$file1->id()]), 'File was loaded successfully');
|
||||
$this->assertTrue(isset($files[$file2->id()]), 'File was loaded successfully');
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
namespace Drupal\file\Tests\Views;
|
||||
|
||||
use Drupal\Core\Render\RenderContext;
|
||||
use Drupal\file\Entity\File;
|
||||
use Drupal\views\Views;
|
||||
use Drupal\views\Tests\ViewUnitTestBase;
|
||||
|
@ -69,17 +70,22 @@ class ExtensionViewsFieldTest extends ViewUnitTestBase {
|
|||
* Tests file extension views field handler extension_detect_tar option.
|
||||
*/
|
||||
public function testFileExtensionTarOption() {
|
||||
/** @var \Drupal\Core\Render\RendererInterface $renderer */
|
||||
$renderer = \Drupal::service('renderer');
|
||||
|
||||
$view = Views::getView('file_extension_view');
|
||||
$view->setDisplay();
|
||||
$this->executeView($view);
|
||||
|
||||
// Test without the tar option.
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[0]), 'png');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[1]), 'tar');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[2]), 'gz');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[3]), '');
|
||||
// Test with the tar option.
|
||||
$renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[0]), 'png');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[1]), 'tar');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[2]), 'gz');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[3]), '');
|
||||
});
|
||||
|
||||
// Test with the tar option.
|
||||
$view = Views::getView('file_extension_view');
|
||||
$view->setDisplay();
|
||||
$view->initHandlers();
|
||||
|
@ -87,10 +93,12 @@ class ExtensionViewsFieldTest extends ViewUnitTestBase {
|
|||
$view->field['extension']->options['settings']['extension_detect_tar'] = TRUE;
|
||||
$this->executeView($view);
|
||||
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[0]), 'png');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[1]), 'tar');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[2]), 'tar.gz');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[3]), '');
|
||||
$renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[0]), 'png');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[1]), 'tar');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[2]), 'tar.gz');
|
||||
$this->assertEqual($view->field['extension']->advancedRender($view->result[3]), '');
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -222,7 +222,7 @@ function file_test_file_url_alter(&$uri) {
|
|||
}
|
||||
// Public created files.
|
||||
else {
|
||||
$wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
|
||||
$wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
|
||||
$path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
|
||||
}
|
||||
|
||||
|
@ -252,7 +252,7 @@ function file_test_file_url_alter(&$uri) {
|
|||
}
|
||||
// Public created files.
|
||||
else {
|
||||
$wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
|
||||
$wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
|
||||
$path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
|
||||
}
|
||||
|
||||
|
@ -275,7 +275,7 @@ function file_test_file_url_alter(&$uri) {
|
|||
}
|
||||
// Public created files.
|
||||
else {
|
||||
$wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
|
||||
$wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
|
||||
$path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue