FileEntityFileTypeClassificationTest.php in File Entity (fieldable files) 8.2
File
tests/src/Functional/FileEntityFileTypeClassificationTest.php
View source
<?php
namespace Drupal\Tests\file_entity\Functional;
use Drupal\file\Entity\File;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\views\Views;
class FileEntityFileTypeClassificationTest extends BrowserTestBase {
use CronRunTrait;
use TestFileCreationTrait;
public static $modules = array(
'file',
);
protected $defaultTheme = 'stark';
function getFileType($file) {
$type = \Drupal::database()
->select('file_managed', 'fm')
->fields('fm', array(
'type',
))
->condition('fid', $file
->id(), '=')
->execute()
->fetchAssoc();
return $type;
}
function testFileTypeClassification() {
$file = current($this
->getTestFiles('text'));
$text_file = File::create((array) $file);
$text_file
->save();
$file = current($this
->getTestFiles('image'));
$image_file = File::create((array) $file);
$image_file
->save();
\Drupal::service('module_installer')
->install(array(
'file_entity',
));
$change_summary = \Drupal::entityDefinitionUpdateManager()
->getChangeSummary();
$this
->assertTrue(empty($change_summary), 'No entity definition changes pending');
$file_type = $this
->getFileType($text_file);
$this
->assertEqual($file_type['type'], 'undefined', t('The text file has an undefined file type.'));
$file_type = $this
->getFileType($image_file);
$this
->assertEqual($file_type['type'], 'undefined', t('The image file has an undefined file type.'));
$account = $this
->drupalCreateUser([
'bypass file access',
]);
$this
->drupalLogin($account);
$this
->assertNotEqual($image_file
->bundle(), 'image', 'The image file does not have correct bundle before loading it.');
$this
->drupalGet('file/' . $image_file
->id() . '/edit');
$this
->drupalPostForm(NULL, [], t('Save'));
$image_file = File::load($image_file
->id());
$this
->assertEqual($image_file
->bundle(), 'image', 'The image file has correct bundle after load.');
$this
->cronRun();
$file_type = $this
->getFileType($text_file);
$this
->assertEqual($file_type['type'], 'document', t('The text file was properly assigned the Document file type.'));
$file_type = $this
->getFileType($image_file);
$this
->assertEqual($file_type['type'], 'image', t('The image file was properly assigned the Image file type.'));
\Drupal::service('module_installer')
->uninstall([
'file_entity',
]);
$this
->assertEqual([], \Drupal::entityDefinitionUpdateManager()
->getChangeList());
$image_file = File::load($image_file
->id());
$this
->assertEqual(get_class($image_file), File::class);
$this
->cronRun();
Views::viewsData()
->getAll();
}
}