You are here

class FileValidatorTest in Mini site 8

Class FileValidatorTest.

Tests file validator.

@group minisite

@package Drupal\testmode\Tests

Hierarchy

Expanded class hierarchy of FileValidatorTest

File

tests/src/Unit/FileValidatorTest.php, line 19

Namespace

Drupal\Tests\minisite\Unit
View 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

Namesort descending Modifiers Type Description Overrides
FileValidatorTest::dataProviderFilesToTree public function Data provider for testFilesToTree().
FileValidatorTest::dataProviderNormaliseExtensions public function Data provider for testNormaliseExtensions().
FileValidatorTest::dataProviderValidateFileExtension public function Data provider for testValidateFileExtension().
FileValidatorTest::testFilesToTree public function Test filesToTree() method.
FileValidatorTest::testNormaliseExtensions public function Test normaliseExtensions() method.
FileValidatorTest::testValidateFileExtension public function Test validateFileExtension() method.
MockHelperTrait::callProtectedMethod protected static function Call protected methods on the class.
MockHelperTrait::getProtectedValue protected static function Get protected value from the object.
MockHelperTrait::isDebug protected function Check if testing framework was ran with --debug option.
MockHelperTrait::prepareMock protected function Helper to prepare class mock.
MockHelperTrait::setProtectedValue protected static function Set protected property value.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340