View source
<?php
namespace Drupal\file\Tests;
use Drupal\file\FileInterface;
use Drupal\simpletest\KernelTestBase;
abstract class FileManagedUnitTestBase extends KernelTestBase {
public static $modules = array(
'file_test',
'file',
'system',
'field',
'user',
);
protected function setUp() {
parent::setUp();
file_test_reset();
$this
->installConfig(array(
'system',
));
$this
->installEntitySchema('file');
$this
->installEntitySchema('user');
$this
->installSchema('file', array(
'file_usage',
));
$user = entity_create('user', array(
'uid' => 1,
'name' => $this
->randomMachineName(),
));
$user
->enforceIsNew();
$user
->save();
\Drupal::currentUser()
->setAccount($user);
}
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, format_string('Expected hooks %expected to be called but %uncalled was not called.', array(
'%expected' => implode(', ', $expected),
'%uncalled' => implode(', ', $uncalled),
)));
}
else {
$this
->assertTrue(TRUE, format_string('All the expected hooks were called: %expected', array(
'%expected' => empty($expected) ? '(none)' : implode(', ', $expected),
)));
}
$unexpected = array_diff($actual, $expected);
if (count($unexpected)) {
$this
->assertTrue(FALSE, format_string('Unexpected hooks were called: %unexpected.', array(
'%unexpected' => empty($unexpected) ? '(none)' : implode(', ', $unexpected),
)));
}
else {
$this
->assertTrue(TRUE, 'No unexpected hooks were called.');
}
}
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 = format_string('hook_file_@name was called correctly.', array(
'@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.', array(
'@name' => $hook,
'@count' => $actual_count,
));
}
else {
$message = format_string('hook_file_@name was expected to be called %expected times but was called %actual times.', array(
'@name' => $hook,
'%expected' => $expected_count,
'%actual' => $actual_count,
));
}
}
$this
->assertEqual($actual_count, $expected_count, $message);
}
function assertFileUnchanged(FileInterface $before, FileInterface $after) {
$this
->assertEqual($before
->id(), $after
->id(), t('File id is the same: %file1 == %file2.', array(
'%file1' => $before
->id(),
'%file2' => $after
->id(),
)), 'File unchanged');
$this
->assertEqual($before
->getOwner()
->id(), $after
->getOwner()
->id(), t('File owner is the same: %file1 == %file2.', array(
'%file1' => $before
->getOwner()
->id(),
'%file2' => $after
->getOwner()
->id(),
)), 'File unchanged');
$this
->assertEqual($before
->getFilename(), $after
->getFilename(), t('File name is the same: %file1 == %file2.', array(
'%file1' => $before
->getFilename(),
'%file2' => $after
->getFilename(),
)), 'File unchanged');
$this
->assertEqual($before
->getFileUri(), $after
->getFileUri(), t('File path is the same: %file1 == %file2.', array(
'%file1' => $before
->getFileUri(),
'%file2' => $after
->getFileUri(),
)), 'File unchanged');
$this
->assertEqual($before
->getMimeType(), $after
->getMimeType(), t('File MIME type is the same: %file1 == %file2.', array(
'%file1' => $before
->getMimeType(),
'%file2' => $after
->getMimeType(),
)), 'File unchanged');
$this
->assertEqual($before
->getSize(), $after
->getSize(), t('File size is the same: %file1 == %file2.', array(
'%file1' => $before
->getSize(),
'%file2' => $after
->getSize(),
)), 'File unchanged');
$this
->assertEqual($before
->isPermanent(), $after
->isPermanent(), t('File status is the same: %file1 == %file2.', array(
'%file1' => $before
->isPermanent(),
'%file2' => $after
->isPermanent(),
)), 'File unchanged');
}
function assertDifferentFile(FileInterface $file1, FileInterface $file2) {
$this
->assertNotEqual($file1
->id(), $file2
->id(), t('Files have different ids: %file1 != %file2.', array(
'%file1' => $file1
->id(),
'%file2' => $file2
->id(),
)), 'Different file');
$this
->assertNotEqual($file1
->getFileUri(), $file2
->getFileUri(), t('Files have different paths: %file1 != %file2.', array(
'%file1' => $file1
->getFileUri(),
'%file2' => $file2
->getFileUri(),
)), 'Different file');
}
function assertSameFile(FileInterface $file1, FileInterface $file2) {
$this
->assertEqual($file1
->id(), $file2
->id(), t('Files have the same ids: %file1 == %file2.', array(
'%file1' => $file1
->id(),
'%file2-fid' => $file2
->id(),
)), 'Same file');
$this
->assertEqual($file1
->getFileUri(), $file2
->getFileUri(), t('Files have the same path: %file1 == %file2.', array(
'%file1' => $file1
->getFileUri(),
'%file2' => $file2
->getFileUri(),
)), 'Same file');
}
function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) {
\Drupal::state()
->set('file_test.count_hook_invocations', FALSE);
$file = entity_create('file', array(
'uri' => $this
->createUri($filepath, $contents, $scheme),
'uid' => 1,
));
$file
->save();
$this
->assertTrue($file
->id() > 0, 'The file was added to the database.', 'Create test file');
\Drupal::state()
->set('file_test.count_hook_invocations', TRUE);
return $file;
}
function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) {
if (!isset($filepath)) {
$filepath = 'Файл для тестирования ' . $this
->randomMachineName();
}
if (!isset($scheme)) {
$scheme = file_default_scheme();
}
$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
->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');
return $filepath;
}
}