|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the WP_Filesystem_Direct::touch() 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::touch |
| 16 | + */ |
| 17 | +class Tests_Filesystem_WpFilesystemDirect_Touch extends WP_Filesystem_Direct_UnitTestCase { |
| 18 | + |
| 19 | + /** |
| 20 | + * Tests that `WP_Filesystem_Direct::touch()` creates a file. |
| 21 | + * |
| 22 | + * @ticket 57774 |
| 23 | + * |
| 24 | + * @dataProvider data_should_create_file |
| 25 | + * |
| 26 | + * @param string $file The file path. |
| 27 | + * @param int $mtime The modified time to set. |
| 28 | + * @param int $atime The accessed time to set. |
| 29 | + */ |
| 30 | + public function test_should_create_file( $file, $mtime, $atime ) { |
| 31 | + $file = str_replace( 'TEST_DATA', self::$file_structure['test_dir']['path'], $file ); |
| 32 | + |
| 33 | + if ( is_string( $mtime ) ) { |
| 34 | + $mtime = (int) str_replace( |
| 35 | + array( 'time plus one minute', time() + MINUTE_IN_SECONDS ), |
| 36 | + array( 'time', time() ), |
| 37 | + $mtime |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + $expected_mtime = 0 === $mtime ? time() : $mtime; |
| 42 | + |
| 43 | + if ( is_string( $atime ) ) { |
| 44 | + $atime = (int) str_replace( |
| 45 | + array( 'time plus one minute', time() + MINUTE_IN_SECONDS ), |
| 46 | + array( 'time', time() ), |
| 47 | + $atime |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + $expected_atime = 0 === $atime ? time() : $atime; |
| 52 | + |
| 53 | + $result = self::$filesystem->touch( $file, $mtime, $atime ); |
| 54 | + |
| 55 | + $actual_atime = fileatime( $file ); |
| 56 | + $actual_exists = file_exists( $file ); |
| 57 | + $actual_mtime = filemtime( $file ); |
| 58 | + |
| 59 | + if ( $actual_exists ) { |
| 60 | + unlink( $file ); |
| 61 | + } |
| 62 | + |
| 63 | + $this->assertTrue( $result, 'WP_Filesystem_Direct::touch() did not return true.' ); |
| 64 | + $this->assertTrue( $actual_exists, 'The file does not exist.' ); |
| 65 | + $this->assertSame( $actual_atime, $expected_atime, 'The file does not have the expected atime.' ); |
| 66 | + $this->assertSame( $actual_mtime, $expected_mtime, 'The file does not have the expected mtime.' ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Data provider. |
| 71 | + * |
| 72 | + * @return array[] |
| 73 | + */ |
| 74 | + public function data_should_create_file() { |
| 75 | + return array( |
| 76 | + 'default mtime or atime' => array( |
| 77 | + 'file' => 'TEST_DATA/file-to-create.txt', |
| 78 | + 'mtime' => 0, |
| 79 | + 'atime' => 0, |
| 80 | + ), |
| 81 | + 'set mtime and default atime' => array( |
| 82 | + 'file' => 'TEST_DATA/file-to-create.txt', |
| 83 | + 'mtime' => 'time plus one minute', |
| 84 | + 'atime' => 'time', |
| 85 | + ), |
| 86 | + 'default mtime and set atime' => array( |
| 87 | + 'file' => 'TEST_DATA/file-to-create.txt', |
| 88 | + 'mtime' => 'time', |
| 89 | + 'atime' => 'time plus one minute', |
| 90 | + ), |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments