You are here

public function ScanDirectoryTest::testOptionKey in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/File/ScanDirectoryTest.php \Drupal\KernelTests\Core\File\ScanDirectoryTest::testOptionKey()

Check that key parameter sets the return value's key.

@covers ::scanDirectory

File

core/tests/Drupal/KernelTests/Core/File/ScanDirectoryTest.php, line 116

Class

ScanDirectoryTest
Tests \Drupal\Core\File\FileSystem::scanDirectory.

Namespace

Drupal\KernelTests\Core\File

Code

public function testOptionKey() {

  // "filename", for the path starting with $dir.
  $expected = [
    $this->path . '/javascript-1.txt',
    $this->path . '/javascript-2.script',
  ];
  $actual = array_keys($this->fileSystem
    ->scanDirectory($this->path, '/^javascript-/', [
    'key' => 'filepath',
  ]));
  sort($actual);
  $this
    ->assertEquals($expected, $actual, 'Returned the correct values for the filename key.');

  // "basename", for the basename of the file.
  $expected = [
    'javascript-1.txt',
    'javascript-2.script',
  ];
  $actual = array_keys($this->fileSystem
    ->scanDirectory($this->path, '/^javascript-/', [
    'key' => 'filename',
  ]));
  sort($actual);
  $this
    ->assertEquals($expected, $actual, 'Returned the correct values for the basename key.');

  // "name" for the name of the file without an extension.
  $expected = [
    'javascript-1',
    'javascript-2',
  ];
  $actual = array_keys($this->fileSystem
    ->scanDirectory($this->path, '/^javascript-/', [
    'key' => 'name',
  ]));
  sort($actual);
  $this
    ->assertEquals($expected, $actual, 'Returned the correct values for the name key.');

  // Invalid option that should default back to "filename".
  $expected = [
    $this->path . '/javascript-1.txt',
    $this->path . '/javascript-2.script',
  ];
  $actual = array_keys($this->fileSystem
    ->scanDirectory($this->path, '/^javascript-/', [
    'key' => 'INVALID',
  ]));
  sort($actual);
  $this
    ->assertEquals($expected, $actual, 'An invalid key defaulted back to the default.');
}