class FileValidatorTest in Mini site 8
Class FileValidatorTest.
Tests file validator.
@group minisite
@package Drupal\testmode\Tests
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\minisite\Unit\FileValidatorTest uses MockHelperTrait
Expanded class hierarchy of FileValidatorTest
File
- tests/
src/ Unit/ FileValidatorTest.php, line 19
Namespace
Drupal\Tests\minisite\UnitView source
class FileValidatorTest extends UnitTestCase {
use MockHelperTrait;
/**
* Test validateFileExtension() method.
*
* @dataProvider dataProviderValidateFileExtension
* @covers \Drupal\minisite\FileValidator::validateFileExtension
*/
public function testValidateFileExtension($filename, $extensions) {
$this
->expectException(InvalidExtensionValidatorException::class);
$this
->expectExceptionMessage(sprintf('File %s has invalid extension.', $filename));
FileValidator::validateFileExtension($filename, $extensions);
}
/**
* Data provider for testValidateFileExtension().
*/
public function dataProviderValidateFileExtension() {
return [
[
'file.txt',
[
'ext',
],
],
[
'file.txt',
[
'ext',
'ext2',
],
],
[
'file',
[
'ext',
'ext2',
],
],
[
'file.ext.txt',
[
'ext',
'ext2',
],
],
];
}
/**
* Test normaliseExtensions() method.
*
* @dataProvider dataProviderNormaliseExtensions
* @covers \Drupal\minisite\FileValidator::normaliseExtensions
*/
public function testNormaliseExtensions($extensions, $expected) {
$actual = FileValidator::normaliseExtensions($extensions);
$this
->assertEquals($expected, $actual);
}
/**
* Data provider for testNormaliseExtensions().
*/
public function dataProviderNormaliseExtensions() {
return [
[
[],
[],
],
[
'',
[],
],
[
' ',
[],
],
[
', ',
[],
],
[
' , ',
[],
],
[
' , ',
[],
],
[
' ,',
[],
],
[
'a b c',
[
'a',
'b',
'c',
],
],
[
'a,b,c',
[
'a',
'b',
'c',
],
],
[
'a, b, c',
[
'a',
'b',
'c',
],
],
[
'a, b , c',
[
'a',
'b',
'c',
],
],
[
'a, b c',
[
'a',
'b',
'c',
],
],
[
' a, b c',
[
'a',
'b',
'c',
],
],
];
}
/**
* Test filesToTree() method.
*
* @dataProvider dataProviderFilesToTree
* @covers \Drupal\minisite\FileValidator::filesToTree
*/
public function testFilesToTree($files, $expected, $expectException = FALSE) {
if ($expectException) {
$this
->expectException(\RuntimeException::class);
$this
->expectExceptionMessage('Invalid file list provided');
}
$actual = FileValidator::filesToTree($files);
$this
->assertEquals($expected, $actual);
}
/**
* Data provider for testFilesToTree().
*/
public function dataProviderFilesToTree() {
return [
// Root files.
[
[
'file.txt',
],
[
'file.txt' => 'file.txt',
],
],
[
[
'file1.txt',
'file2.txt',
],
[
'file1.txt' => 'file1.txt',
'file2.txt' => 'file2.txt',
],
],
// Simple dirs.
[
[
'dir1/',
],
[
'dir1' => [
'.' => 'dir1/',
],
],
],
[
[
'dir1/',
'dir2/',
],
[
'dir1' => [
'.' => 'dir1/',
],
'dir2' => [
'.' => 'dir2/',
],
],
],
// Dirs with files.
[
[
'dir1/file1.txt',
'dir2/file2.txt',
],
[
'dir1' => [
'.' => 'dir1/',
'file1.txt' => 'dir1/file1.txt',
],
'dir2' => [
'.' => 'dir2/',
'file2.txt' => 'dir2/file2.txt',
],
],
],
[
[
'dir1/dir11/dir111/',
],
[
'dir1' => [
'.' => 'dir1/',
'dir11' => [
'.' => 'dir1/dir11/',
'dir111' => [
'.' => 'dir1/dir11/dir111/',
],
],
],
],
],
[
[
'dir1/file11.txt',
'dir2/file21.txt',
'dir2/file22.txt',
'dir3/',
'dir4/dir41/',
],
[
'dir1' => [
'.' => 'dir1/',
'file11.txt' => 'dir1/file11.txt',
],
'dir2' => [
'.' => 'dir2/',
'file21.txt' => 'dir2/file21.txt',
'file22.txt' => 'dir2/file22.txt',
],
'dir3' => [
'.' => 'dir3/',
],
'dir4' => [
'.' => 'dir4/',
'dir41' => [
'.' => 'dir4/dir41/',
],
],
],
],
// Mixed dirs, files, order.
[
[
'dir1/',
'dir1/dir11/dir111/',
'dir1/dir11/',
'dir1/dir11/dir111/file111.txt',
'dir1/dir11/dir111/file112.txt',
'file1.txt',
'file2.txt',
'dir2/file21.txt',
'dir2/dir21/file211.txt',
'dir2/file22.txt',
'dir2/dir21/file212.txt',
'dir2/file23.txt',
],
[
'file1.txt' => 'file1.txt',
'file2.txt' => 'file2.txt',
'dir1' => [
'.' => 'dir1/',
'dir11' => [
'.' => 'dir1/dir11/',
'dir111' => [
'.' => 'dir1/dir11/dir111/',
'file111.txt' => 'dir1/dir11/dir111/file111.txt',
'file112.txt' => 'dir1/dir11/dir111/file112.txt',
],
],
],
'dir2' => [
'.' => 'dir2/',
'dir21' => [
'.' => 'dir2/dir21/',
'file211.txt' => 'dir2/dir21/file211.txt',
'file212.txt' => 'dir2/dir21/file212.txt',
],
'file21.txt' => 'dir2/file21.txt',
'file22.txt' => 'dir2/file22.txt',
'file23.txt' => 'dir2/file23.txt',
],
],
],
// Edge case - dirs provided as files and then as dirs.
[
[
'dir1',
'dir1/file1.txt',
],
[],
TRUE,
],
[
[
'dir1/file1.txt',
'dir1',
],
[],
TRUE,
],
[
[
'dir1/dir11/dir111/file111.txt',
'dir1/dir11/dir111',
],
[],
TRUE,
],
// Repeating files.
[
[
'file1.txt',
'file1.txt',
],
[
'file1.txt' => 'file1.txt',
],
],
[
[
'dir1/file1.txt',
'dir1/file1.txt',
],
[
'dir1' => [
'.' => 'dir1/',
'file1.txt' => 'dir1/file1.txt',
],
],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FileValidatorTest:: |
public | function | Data provider for testFilesToTree(). | |
FileValidatorTest:: |
public | function | Data provider for testNormaliseExtensions(). | |
FileValidatorTest:: |
public | function | Data provider for testValidateFileExtension(). | |
FileValidatorTest:: |
public | function | Test filesToTree() method. | |
FileValidatorTest:: |
public | function | Test normaliseExtensions() method. | |
FileValidatorTest:: |
public | function | Test validateFileExtension() method. | |
MockHelperTrait:: |
protected static | function | Call protected methods on the class. | |
MockHelperTrait:: |
protected static | function | Get protected value from the object. | |
MockHelperTrait:: |
protected | function | Check if testing framework was ran with --debug option. | |
MockHelperTrait:: |
protected | function | Helper to prepare class mock. | |
MockHelperTrait:: |
protected static | function | Set protected property value. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |