function FileTestBase::assertFilePermissions

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/File/FileTestBase.php \Drupal\KernelTests\Core\File\FileTestBase::assertFilePermissions()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileTestBase.php \Drupal\KernelTests\Core\File\FileTestBase::assertFilePermissions()
  3. 10 core/tests/Drupal/KernelTests/Core/File/FileTestBase.php \Drupal\KernelTests\Core\File\FileTestBase::assertFilePermissions()

Helper function to test the permissions of a file.

Parameters

$filepath: String file path.

$expected_mode: Octal integer like 0664 or 0777.

$message: Optional message.

4 calls to FileTestBase::assertFilePermissions()
FileCopyTest::testNormal in core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php
Copy a normal file.
FileCopyTest::testOverwriteSelf in core/tests/Drupal/KernelTests/Core/File/FileCopyTest.php
Copy a file onto itself.
FileMoveTest::testNormal in core/tests/Drupal/KernelTests/Core/File/FileMoveTest.php
Move a normal file.
FileSaveDataTest::testFileSaveData in core/tests/Drupal/KernelTests/Core/File/FileSaveDataTest.php
Tests the file_unmanaged_save_data() function.

File

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

Class

FileTestBase
Provides file-specific assertions and helper functions.

Namespace

Drupal\KernelTests\Core\File

Code

public function assertFilePermissions($filepath, $expected_mode, $message = NULL) {
    // Clear out PHP's file stat cache to be sure we see the current value.
    clearstatcache(TRUE, $filepath);
    // Mask out all but the last three octets.
    $actual_mode = fileperms($filepath) & 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 (str_starts_with(PHP_OS, '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 = sprintf('Expected file permission to be %s, actually were %s.', decoct($actual_mode), decoct($expected_mode));
    }
    $this->assertEquals($expected_mode, $actual_mode, $message);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.