View source
<?php
namespace Drupal\Tests\Core\File;
use Drupal\Core\File\Event\FileUploadSanitizeNameEvent;
use Drupal\Tests\UnitTestCase;
class FileUploadSanitizeNameEventTest extends UnitTestCase {
public function testSetFilename() {
$event = new FileUploadSanitizeNameEvent('foo.txt', '');
$this
->assertSame('foo.txt', $event
->getFilename());
$event
->setFilename('foo.html');
$this
->assertSame('foo.html', $event
->getFilename());
}
public function testSetFilenameException() {
$event = new FileUploadSanitizeNameEvent('foo.txt', '');
$this
->assertSame('foo.txt', $event
->getFilename());
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('$filename must be a filename with no path information, "bar/foo.html" provided');
$event
->setFilename('bar/foo.html');
}
public function testConstructorException() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('$filename must be a filename with no path information, "bar/foo.txt" provided');
new FileUploadSanitizeNameEvent('bar/foo.txt', '');
}
public function testAllowedExtensions() {
$event = new FileUploadSanitizeNameEvent('foo.txt', '');
$this
->assertSame([], $event
->getAllowedExtensions());
$event = new FileUploadSanitizeNameEvent('foo.txt', 'gif png');
$this
->assertSame([
'gif',
'png',
], $event
->getAllowedExtensions());
}
public function testEventFilenameFunctions(string $filename) {
$event = new FileUploadSanitizeNameEvent($filename, '');
$this
->assertSame($filename, $event
->getFilename());
}
public function provideFilenames() {
return [
'ASCII filename with extension' => [
'example.txt',
],
'ASCII filename with complex extension' => [
'example.html.twig',
],
'ASCII filename with lots of dots' => [
'dotty.....txt',
],
'Unicode filename with extension' => [
'Ä Ö Ü Å Ø äöüåøhello.txt',
],
'Unicode filename without extension' => [
'Ä Ö Ü Å Ø äöüåøhello',
],
];
}
public function testStopPropagation() {
$this
->expectException(\RuntimeException::class);
$event = new FileUploadSanitizeNameEvent('test.txt', '');
$event
->stopPropagation();
}
}