View source
<?php
namespace Drupal\Tests\file\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\Entity\File;
class MoveTest extends FileManagedUnitTestBase {
public function testNormal() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile(NULL, $contents);
$desired_filepath = 'public://' . $this
->randomMachineName();
$result = file_move(clone $source, $desired_filepath, FileSystemInterface::EXISTS_ERROR);
$this
->assertNotFalse($result, 'File moved successfully.');
$this
->assertFileDoesNotExist($source
->getFileUri());
$this
->assertEquals($contents, file_get_contents($result
->getFileUri()), 'Contents of file correctly written.');
$this
->assertFileHooksCalled([
'move',
'load',
'update',
]);
$this
->assertEquals($source
->id(), $result
->id(), new FormattableMarkup("Source file id's' %fid is unchanged after move.", [
'%fid' => $source
->id(),
]));
$loaded_file = File::load($result
->id());
$this
->assertNotEmpty($loaded_file, 'File can be loaded from the database.');
$this
->assertFileUnchanged($result, $loaded_file);
}
public function testExistingRename() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile(NULL, $contents);
$target = $this
->createFile();
$this
->assertDifferentFile($source, $target);
$result = file_move(clone $source, $target
->getFileUri(), FileSystemInterface::EXISTS_RENAME);
$this
->assertNotFalse($result, 'File moved successfully.');
$this
->assertFileDoesNotExist($source
->getFileUri());
$this
->assertEquals($contents, file_get_contents($result
->getFileUri()), 'Contents of file correctly written.');
$this
->assertFileHooksCalled([
'move',
'load',
'update',
]);
$this
->assertFileUnchanged($result, File::load($result
->id()));
$this
->assertFileUnchanged($target, File::load($target
->id()));
$this
->assertDifferentFile($target, $result);
$loaded_source = File::load($source
->id());
$this
->assertEquals($result
->id(), $loaded_source
->id(), "Returned file's id matches the source.");
$this
->assertNotEquals($source
->getFileUri(), $loaded_source
->getFileUri(), 'Returned file path has changed from the original.');
}
public function testExistingReplace() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile(NULL, $contents);
$target = $this
->createFile();
$this
->assertDifferentFile($source, $target);
$result = file_move(clone $source, $target
->getFileUri(), FileSystemInterface::EXISTS_REPLACE);
$this
->assertEquals($contents, file_get_contents($result
->getFileUri()), 'Contents of file were overwritten.');
$this
->assertFileDoesNotExist($source
->getFileUri());
$this
->assertNotEmpty($result, 'File moved successfully.');
$this
->assertFileHooksCalled([
'move',
'update',
'delete',
'load',
]);
$loaded_result = File::load($result
->id());
$this
->assertFileUnchanged($result, $loaded_result);
$this
->assertSameFile($target, $loaded_result);
$this
->assertDifferentFile($source, $loaded_result);
}
public function testExistingReplaceSelf() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile(NULL, $contents);
$result = file_move(clone $source, $source
->getFileUri(), FileSystemInterface::EXISTS_REPLACE);
$this
->assertFalse($result, 'File move failed.');
$this
->assertEquals($contents, file_get_contents($source
->getFileUri()), 'Contents of file were not altered.');
$this
->assertFileHooksCalled([]);
$this
->assertFileUnchanged($source, File::load($source
->id()));
}
public function testExistingError() {
$contents = $this
->randomMachineName(10);
$source = $this
->createFile();
$target = $this
->createFile(NULL, $contents);
$this
->assertDifferentFile($source, $target);
$result = file_move(clone $source, $target
->getFileUri(), FileSystemInterface::EXISTS_ERROR);
$this
->assertFalse($result, 'File move failed.');
$this
->assertFileExists($source
->getFileUri());
$this
->assertEquals($contents, file_get_contents($target
->getFileUri()), 'Contents of file were not altered.');
$this
->assertFileHooksCalled([]);
$this
->assertFileUnchanged($source, File::load($source
->id()));
$this
->assertFileUnchanged($target, File::load($target
->id()));
}
}