|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the WP_Filesystem_Direct::chdir() method. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + */ |
| 7 | + |
| 8 | +require_once __DIR__ . '/base.php'; |
| 9 | + |
| 10 | +/** |
| 11 | + * @group admin |
| 12 | + * @group filesystem |
| 13 | + * @group filesystem-direct |
| 14 | + * |
| 15 | + * @covers WP_Filesystem_Direct::chdir |
| 16 | + */ |
| 17 | +class Tests_Filesystem_WpFilesystemDirect_Chdir extends WP_Filesystem_Direct_UnitTestCase { |
| 18 | + |
| 19 | + /** |
| 20 | + * Tests that `WP_Filesystem_Direct::chdir()` |
| 21 | + * returns false for a path that does not exist. |
| 22 | + * |
| 23 | + * @ticket 57774 |
| 24 | + * |
| 25 | + * @dataProvider data_should_fail_to_change_directory |
| 26 | + * |
| 27 | + * @param string $path The path. |
| 28 | + */ |
| 29 | + public function test_should_fail_to_change_directory( $path ) { |
| 30 | + $original_cwd = self::$filesystem->cwd(); |
| 31 | + $path = wp_normalize_path( realpath( self::$file_structure['test_dir']['path'] ) ) . $path; |
| 32 | + $chdir_result = self::$filesystem->chdir( $path ); |
| 33 | + $cwd_result = self::$filesystem->cwd(); |
| 34 | + |
| 35 | + // Reset the current working directory. |
| 36 | + self::$filesystem->chdir( $original_cwd ); |
| 37 | + |
| 38 | + $this->assertFalse( |
| 39 | + $chdir_result, |
| 40 | + 'Changing working directory succeeded.' |
| 41 | + ); |
| 42 | + |
| 43 | + $this->assertSame( |
| 44 | + $original_cwd, |
| 45 | + $cwd_result, |
| 46 | + 'The current working directory was changed.' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Data provider. |
| 52 | + * |
| 53 | + * @return array[] |
| 54 | + */ |
| 55 | + public function data_should_fail_to_change_directory() { |
| 56 | + return array( |
| 57 | + 'a file that exists' => array( |
| 58 | + 'path' => 'a_file_that_exists.txt', |
| 59 | + ), |
| 60 | + 'a file that does not exist' => array( |
| 61 | + 'path' => 'a_file_that_does_not_exist.txt', |
| 62 | + ), |
| 63 | + 'a directory that does not exist' => array( |
| 64 | + 'path' => 'a_directory_that_does_not_exist', |
| 65 | + ), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Tests that `WP_Filesystem_Direct::chdir()` changes to |
| 71 | + * an existing directory. |
| 72 | + * |
| 73 | + * @ticket 57774 |
| 74 | + */ |
| 75 | + public function test_should_change_directory() { |
| 76 | + $original_cwd = self::$filesystem->cwd(); |
| 77 | + $path = wp_normalize_path( realpath( self::$file_structure['test_dir']['path'] ) ); |
| 78 | + $chdir_result = self::$filesystem->chdir( $path ); |
| 79 | + $cwd_result = self::$filesystem->cwd(); |
| 80 | + |
| 81 | + // Reset the current working directory. |
| 82 | + self::$filesystem->chdir( $original_cwd ); |
| 83 | + |
| 84 | + $this->assertTrue( |
| 85 | + $chdir_result, |
| 86 | + 'Changing working directory failed.' |
| 87 | + ); |
| 88 | + |
| 89 | + $this->assertSame( |
| 90 | + $path, |
| 91 | + $cwd_result, |
| 92 | + 'The current working directory was incorrect.' |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments