View source
<?php
namespace Drupal\Tests\file_entity\Functional;
use Drupal\file\Entity\File;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
class FileEntityUnitTest extends FileEntityTestBase {
function setUp() {
parent::setUp();
$this
->setUpFiles();
}
function testMimeTypeMappings() {
$tests = array(
'public://test.ogg' => 'audio/ogg',
'public://test.m4v' => 'video/x-m4v',
'public://test.mka' => 'audio/x-matroska',
'public://test.mkv' => 'video/x-matroska',
'public://test.webp' => 'image/webp',
);
$guesser = $this->container
->get('file.mime_type.guesser.extension');
foreach ($tests as $input => $expected) {
$this
->assertEqual($expected, $guesser
->guess($input));
}
}
function testFileEntity() {
$file = reset($this->files['text']);
$this
->assertEqual($file
->id(), $file->fid->value);
$this
->assertEqual($file
->getRevisionId(), NULL);
$this
->assertEqual($file
->bundle(), 'document');
}
function testImageDimensions() {
$files = array();
$text_fids = array();
foreach ($this->files['image'] as $file) {
$files[$file
->id()] = $file
->getAllMetadata();
$this
->assertTrue($file
->hasMetadata('height'), 'Image height retrieved on file save for an image file.');
$this
->assertTrue($file
->hasMetadata('width'), 'Image width retrieved on file save for an image file.');
}
foreach ($this->files['text'] as $file) {
$text_fids[] = $file
->id();
$this
->assertFalse($file
->hasMetadata('height'), 'No image height retrieved on file save for an text file.');
$this
->assertFalse($file
->hasMetadata('width'), 'No image width retrieved on file save for an text file.');
}
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
foreach (File::loadMultiple(array_keys($files)) as $file) {
$this
->assertTrue($file
->hasMetadata('height'), 'Image dimensions retrieved on file load for an image file.');
$this
->assertTrue($file
->hasMetadata('width'), 'Image dimensions retrieved on file load for an image file.');
$this
->assertEqual($file
->getMetadata('height'), $files[$file
->id()]['height'], 'Loaded image height is equal to saved image height.');
$this
->assertEqual($file
->getMetadata('width'), $files[$file
->id()]['width'], 'Loaded image width is equal to saved image width.');
}
foreach (File::loadMultiple($text_fids) as $file) {
$this
->assertFalse($file
->hasMetadata('height'), 'No image height retrieved on file load for an text file.');
$this
->assertFalse($file
->hasMetadata('width'), 'No image width retrieved on file load for an text file.');
}
$image_files = array_keys($files);
$file = File::load(reset($image_files));
$image = \Drupal::service('image.factory')
->get($file
->getFileUri());
$image
->resize($file
->getMetadata('width') / 2, $file
->getMetadata('height') / 2);
$image
->save();
$file
->save();
$this
->assertEqual($file
->getMetadata('height'), $files[$file
->id()]['height'] / 2, 'Image file height updated by file save.');
$this
->assertEqual($file
->getMetadata('width'), $files[$file
->id()]['width'] / 2, 'Image file width updated by file save.');
\Drupal::entityTypeManager()
->getStorage('file')
->resetCache();
$file = File::load($file
->id());
$this
->assertEqual($file
->getMetadata('height'), $files[$file
->id()]['height'] / 2, 'Updated image height retrieved by file load.');
$this
->assertEqual($file
->getMetadata('width'), $files[$file
->id()]['width'] / 2, 'Updated image width retrieved by file load.');
$file
->delete();
$this
->assertEquals(0, \Drupal::database()
->query('SELECT COUNT(*) FROM {file_metadata} WHERE fid = :fid', array(
':fid' => 'fid',
))
->fetchField(), 'Row deleted in {file_dimensions} when deleting the file.');
}
}