View source
<?php
namespace Drupal\Tests\file\Kernel;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\Entity\File;
class SaveDataTest extends FileManagedUnitTestBase {
public function testWithoutFilename() {
$contents = $this
->randomMachineName(8);
$result = file_save_data($contents);
$this
->assertNotFalse($result, 'Unnamed file saved correctly.');
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
$this
->assertEqual(\Drupal::config('system.file')
->get('default_scheme'), $stream_wrapper_manager::getScheme($result
->getFileUri()), "File was placed in Drupal's files directory.");
$this
->assertEqual($result
->getFilename(), \Drupal::service('file_system')
->basename($result
->getFileUri()), "Filename was set to the file's basename.");
$this
->assertEqual($contents, file_get_contents($result
->getFileUri()), 'Contents of the file are correct.');
$this
->assertEqual($result
->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
$this
->assertTrue($result
->isPermanent(), "The file's status was set to permanent.");
$this
->assertFileHooksCalled([
'insert',
]);
$this
->assertFileUnchanged($result, File::load($result
->id()));
}
public function testWithFilename() {
$contents = $this
->randomMachineName(8);
$filename = 'Текстовый файл.txt';
$result = file_save_data($contents, 'public://' . $filename);
$this
->assertNotFalse($result, 'Unnamed file saved correctly.');
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
$this
->assertEqual('public', $stream_wrapper_manager::getScheme($result
->getFileUri()), "File was placed in Drupal's files directory.");
$this
->assertEqual($filename, \Drupal::service('file_system')
->basename($result
->getFileUri()), 'File was named correctly.');
$this
->assertEqual($contents, file_get_contents($result
->getFileUri()), 'Contents of the file are correct.');
$this
->assertEqual($result
->getMimeType(), 'text/plain', 'A MIME type was set.');
$this
->assertTrue($result
->isPermanent(), "The file's status was set to permanent.");
$this
->assertFileHooksCalled([
'insert',
]);
$this
->assertFileUnchanged($result, File::load($result
->id()));
}
public function testExistingRename() {
$existing = $this
->createFile();
$contents = $this
->randomMachineName(8);
$result = file_save_data($contents, $existing
->getFileUri(), FileSystemInterface::EXISTS_RENAME);
$this
->assertNotFalse($result, 'File saved successfully.');
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
$this
->assertEqual('public', $stream_wrapper_manager::getScheme($result
->getFileUri()), "File was placed in Drupal's files directory.");
$this
->assertEqual($result
->getFilename(), $existing
->getFilename(), 'Filename was set to the basename of the source, rather than that of the renamed file.');
$this
->assertEqual($contents, file_get_contents($result
->getFileUri()), 'Contents of the file are correct.');
$this
->assertEqual($result
->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
$this
->assertTrue($result
->isPermanent(), "The file's status was set to permanent.");
$this
->assertFileHooksCalled([
'insert',
]);
$this
->assertDifferentFile($existing, $result);
$this
->assertFileUnchanged($existing, File::load($existing
->id()));
$this
->assertFileUnchanged($result, File::load($result
->id()));
}
public function testExistingReplace() {
$existing = $this
->createFile();
$contents = $this
->randomMachineName(8);
$result = file_save_data($contents, $existing
->getFileUri(), FileSystemInterface::EXISTS_REPLACE);
$this
->assertNotFalse($result, 'File saved successfully.');
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
assert($stream_wrapper_manager instanceof StreamWrapperManagerInterface);
$this
->assertEqual('public', $stream_wrapper_manager::getScheme($result
->getFileUri()), "File was placed in Drupal's files directory.");
$this
->assertEqual($result
->getFilename(), $existing
->getFilename(), 'Filename was set to the basename of the existing file, rather than preserving the original name.');
$this
->assertEqual($contents, file_get_contents($result
->getFileUri()), 'Contents of the file are correct.');
$this
->assertEqual($result
->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
$this
->assertTrue($result
->isPermanent(), "The file's status was set to permanent.");
$this
->assertFileHooksCalled([
'load',
'update',
]);
$this
->assertSameFile($existing, $result);
$this
->assertFileUnchanged($result, File::load($result
->id()));
}
public function testExistingError() {
$contents = $this
->randomMachineName(8);
$existing = $this
->createFile(NULL, $contents);
$result = file_save_data('asdf', $existing
->getFileUri(), FileSystemInterface::EXISTS_ERROR);
$this
->assertFalse($result, 'Overwriting a file fails when FileSystemInterface::EXISTS_ERROR is specified.');
$this
->assertEqual($contents, file_get_contents($existing
->getFileUri()), 'Contents of existing file were unchanged.');
$this
->assertFileHooksCalled([]);
$this
->assertFileUnchanged($existing, File::load($existing
->id()));
}
}