View source
<?php
namespace Drupal\Tests\file\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
abstract class FileManagedUnitTestBase extends KernelTestBase {
protected static $modules = [
'file_test',
'file',
'system',
'field',
'user',
];
protected function setUp() {
parent::setUp();
file_test_reset();
$this
->installConfig([
'system',
]);
$this
->installEntitySchema('file');
$this
->installEntitySchema('user');
$this
->installSchema('file', [
'file_usage',
]);
$user = User::create([
'uid' => 1,
'name' => $this
->randomMachineName(),
]);
$user
->enforceIsNew();
$user
->save();
\Drupal::currentUser()
->setAccount($user);
}
public function assertFileHooksCalled($expected) {
\Drupal::state()
->resetCache();
$actual = array_keys(array_filter(file_test_get_all_calls()));
$uncalled = array_diff($expected, $actual);
if (count($uncalled)) {
$this
->assertTrue(FALSE, new FormattableMarkup('Expected hooks %expected to be called but %uncalled was not called.', [
'%expected' => implode(', ', $expected),
'%uncalled' => implode(', ', $uncalled),
]));
}
else {
$this
->assertTrue(TRUE, new FormattableMarkup('All the expected hooks were called: %expected', [
'%expected' => empty($expected) ? '(none)' : implode(', ', $expected),
]));
}
$unexpected = array_diff($actual, $expected);
if (count($unexpected)) {
$this
->assertTrue(FALSE, new FormattableMarkup('Unexpected hooks were called: %unexpected.', [
'%unexpected' => empty($unexpected) ? '(none)' : implode(', ', $unexpected),
]));
}
else {
$this
->assertTrue(TRUE, 'No unexpected hooks were called.');
}
}
public function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) {
$actual_count = count(file_test_get_calls($hook));
if (!isset($message)) {
if ($actual_count == $expected_count) {
$message = new FormattableMarkup('hook_file_@name was called correctly.', [
'@name' => $hook,
]);
}
elseif ($expected_count == 0) {
$message = \Drupal::translation()
->formatPlural($actual_count, 'hook_file_@name was not expected to be called but was actually called once.', 'hook_file_@name was not expected to be called but was actually called @count times.', [
'@name' => $hook,
'@count' => $actual_count,
]);
}
else {
$message = new FormattableMarkup('hook_file_@name was expected to be called %expected times but was called %actual times.', [
'@name' => $hook,
'%expected' => $expected_count,
'%actual' => $actual_count,
]);
}
}
$this
->assertEquals($expected_count, $actual_count, $message);
}
public function assertFileUnchanged(FileInterface $before, FileInterface $after) {
$this
->assertEquals($before
->id(), $after
->id(), 'File id is the same');
$this
->assertEquals($before
->getOwner()
->id(), $after
->getOwner()
->id(), 'File owner is the same');
$this
->assertEquals($before
->getFilename(), $after
->getFilename(), 'File name is the same');
$this
->assertEquals($before
->getFileUri(), $after
->getFileUri(), 'File path is the same');
$this
->assertEquals($before
->getMimeType(), $after
->getMimeType(), 'File MIME type is the same');
$this
->assertEquals($before
->getSize(), $after
->getSize(), 'File size is the same');
$this
->assertEquals($before
->isPermanent(), $after
->isPermanent(), 'File status is the same');
}
public function assertDifferentFile(FileInterface $file1, FileInterface $file2) {
$this
->assertNotEquals($file1
->id(), $file2
->id(), 'Files have different ids');
$this
->assertNotEquals($file1
->getFileUri(), $file2
->getFileUri(), 'Files have different paths');
}
public function assertSameFile(FileInterface $file1, FileInterface $file2) {
$this
->assertEquals($file1
->id(), $file2
->id(), 'Files have the same ids');
$this
->assertEquals($file1
->getFileUri(), $file2
->getFileUri(), 'Files have the same path');
}
public function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) {
\Drupal::state()
->set('file_test.count_hook_invocations', FALSE);
$file = File::create([
'uri' => $this
->createUri($filepath, $contents, $scheme),
'uid' => 1,
]);
$file
->save();
$this
->assertGreaterThan(0, $file
->id());
\Drupal::state()
->set('file_test.count_hook_invocations', TRUE);
return $file;
}
public function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) {
if (!isset($filepath)) {
$filepath = 'Файл для тестирования ' . $this
->randomMachineName();
}
if (!isset($scheme)) {
$scheme = 'public';
}
$filepath = $scheme . '://' . $filepath;
if (!isset($contents)) {
$contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
}
file_put_contents($filepath, $contents);
$this
->assertFileExists($filepath);
return $filepath;
}
}