Make WordPress Core


Ignore:
Timestamp:
06/27/2023 04:06:16 PM (18 months ago)
Author:
mikeschroder
Message:

Filesystem API: Allow optional inclusion of hidden files in list_files().

Adds a new optional $include_hidden parameter to allow the inclusion of hidden (. prefixed) files.
Defaults to false for backward compatibility.

Props yani.iliev, sabernhardt, costdev, rutviksavsani, zunaid321, azaozz.
Fixes #53659.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions/listFiles.php

    r51331 r56069  
    2020        $this->assertNotContains( ABSPATH . 'wp-admin/index.php', $admin_files );
    2121    }
     22
     23    /**
     24     * Tests that list_files() optionally includes hidden files.
     25     *
     26     * @ticket 53659
     27     *
     28     * @dataProvider data_list_files_should_optionally_include_hidden_files
     29     *
     30     * @param string   $filename       The name of the hidden file.
     31     * @param bool     $include_hidden Whether to include hidden ("." prefixed) files.
     32     * @param string[] $exclusions     List of folders and files to skip.
     33     * @param bool     $expected       Whether the file should be included in the results.
     34     */
     35    public function test_list_files_should_optionally_include_hidden_files( $filename, $include_hidden, $exclusions, $expected ) {
     36        $test_dir    = get_temp_dir() . 'test-list-files/';
     37        $hidden_file = $test_dir . $filename;
     38
     39        mkdir( $test_dir );
     40        touch( $hidden_file );
     41
     42        $actual = list_files( $test_dir, 100, $exclusions, $include_hidden );
     43
     44        unlink( $hidden_file );
     45        rmdir( $test_dir );
     46
     47        if ( $expected ) {
     48            $this->assertContains( $hidden_file, $actual, 'The file was not included.' );
     49        } else {
     50            $this->assertNotContains( $hidden_file, $actual, 'The file was included.' );
     51        }
     52    }
     53
     54    /**
     55     * Data provider.
     56     *
     57     * @return array[]
     58     */
     59    public function data_list_files_should_optionally_include_hidden_files() {
     60        return array(
     61            '$include_hidden = false and no exclusions' => array(
     62                'filename'       => '.hidden_file',
     63                'include_hidden' => false,
     64                'exclusions'     => array(),
     65                'expected'       => false,
     66            ),
     67            '$include_hidden = true and no exclusions'  => array(
     68                'filename'       => '.hidden_file',
     69                'include_hidden' => true,
     70                'exclusions'     => array(),
     71                'expected'       => true,
     72            ),
     73            '$include_hidden = true and an excluded filename' => array(
     74                'filename'       => '.hidden_file',
     75                'include_hidden' => true,
     76                'exclusions'     => array( '.hidden_file' ),
     77                'expected'       => false,
     78            ),
     79        );
     80    }
    2281}
Note: See TracChangeset for help on using the changeset viewer.