You are here

public function FileTestBase::assertDirectoryPermissions in Drupal 9

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

Helper function to test the permissions of a directory.

Parameters

$directory: String directory path.

$expected_mode: Octal integer like 0664 or 0777.

$message: Optional message.

2 calls to FileTestBase::assertDirectoryPermissions()
DirectoryTest::testFileCheckDirectoryHandling in core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php
Tests directory handling functions.
DirectoryTest::testFileCheckLocalDirectoryHandling in core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php
Tests local directory handling functions.

File

core/tests/Drupal/KernelTests/Core/File/FileTestBase.php, line 124

Class

FileTestBase
Base class for file tests that adds some additional file specific assertions and helper functions.

Namespace

Drupal\KernelTests\Core\File

Code

public function assertDirectoryPermissions($directory, $expected_mode, $message = NULL) {

  // Clear out PHP's file stat cache to be sure we see the current value.
  clearstatcache(TRUE, $directory);

  // Mask out all but the last three octets.
  $actual_mode = fileperms($directory) & 0777;
  $expected_mode = $expected_mode & 0777;

  // PHP on Windows has limited support for file permissions. Usually each of
  // "user", "group" and "other" use one octal digit (3 bits) to represent the
  // read/write/execute bits. On Windows, chmod() ignores the "group" and
  // "other" bits, and fileperms() returns the "user" bits in all three
  // positions. $expected_mode is updated to reflect this.
  if (substr(PHP_OS, 0, 3) == 'WIN') {

    // Reset the "group" and "other" bits.
    $expected_mode = $expected_mode & 0700;

    // Shift the "user" bits to the "group" and "other" positions also.
    $expected_mode = $expected_mode | $expected_mode >> 3 | $expected_mode >> 6;
  }
  if (!isset($message)) {
    $message = t('Expected directory permission to be %expected, actually were %actual.', [
      '%actual' => decoct($actual_mode),
      '%expected' => decoct($expected_mode),
    ]);
  }
  $this
    ->assertEquals($expected_mode, $actual_mode, $message);
}