|
9 | 9 | */ |
10 | 10 | class Tests_Functions_wpFilesize extends WP_UnitTestCase { |
11 | 11 |
|
| 12 | + const TEST_FILE = DIR_TESTDATA . '/images/test-image-upside-down.jpg'; |
| 13 | + |
12 | 14 | /** |
13 | 15 | * @ticket 49412 |
14 | 16 | */ |
15 | | - public function test_wp_filesize() { |
16 | | - $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg'; |
17 | | - |
18 | | - $this->assertSame( filesize( $file ), wp_filesize( $file ) ); |
| 17 | + public function test_wp_filesize(): void { |
| 18 | + $this->assertSame( filesize( self::TEST_FILE ), wp_filesize( self::TEST_FILE ) ); |
19 | 19 | } |
20 | 20 |
|
21 | 21 | /** |
22 | 22 | * @ticket 49412 |
| 23 | + * @ticket 65670 |
23 | 24 | */ |
24 | | - public function test_wp_filesize_filters() { |
25 | | - $file = DIR_TESTDATA . '/images/test-image-upside-down.jpg'; |
| 25 | + public function test_wp_filesize_filters(): void { |
| 26 | + add_filter( 'wp_filesize', static fn () => 999 ); |
| 27 | + $this->assertSame( 999, wp_filesize( self::TEST_FILE ) ); |
26 | 28 |
|
27 | | - add_filter( |
28 | | - 'wp_filesize', |
29 | | - static function () { |
30 | | - return 999; |
31 | | - } |
32 | | - ); |
| 29 | + add_filter( 'wp_filesize', static fn () => '9991', 100 ); |
| 30 | + $this->assertSame( 9991, wp_filesize( self::TEST_FILE ) ); |
33 | 31 |
|
34 | | - $this->assertSame( 999, wp_filesize( $file ) ); |
| 32 | + add_filter( 'pre_wp_filesize', static fn () => 111 ); |
| 33 | + $this->assertSame( 111, wp_filesize( self::TEST_FILE ) ); |
35 | 34 |
|
36 | | - add_filter( |
37 | | - 'pre_wp_filesize', |
38 | | - static function () { |
39 | | - return 111; |
40 | | - } |
41 | | - ); |
| 35 | + add_filter( 'pre_wp_filesize', static fn () => '2222', 100 ); |
| 36 | + $this->assertSame( 2222, wp_filesize( self::TEST_FILE ) ); |
42 | 37 |
|
43 | | - $this->assertSame( 111, wp_filesize( $file ) ); |
| 38 | + add_filter( 'pre_wp_filesize', static fn () => -100, 200 ); |
| 39 | + $this->assertSame( 9991, wp_filesize( self::TEST_FILE ) ); |
44 | 40 | } |
45 | 41 |
|
46 | 42 | /** |
47 | 43 | * @ticket 49412 |
48 | 44 | */ |
49 | | - public function test_wp_filesize_with_nonexistent_file() { |
50 | | - $file = 'nonexistent/file.jpg'; |
| 45 | + public function test_wp_filesize_with_nonexistent_file(): void { |
| 46 | + $this->assertSame( 0, wp_filesize( 'nonexistent/file.jpg' ) ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @ticket 65670 |
| 51 | + */ |
| 52 | + public function test_wp_filesize_pre_wp_filesize_filter_null(): void { |
| 53 | + add_filter( 'pre_wp_filesize', '__return_null' ); |
| 54 | + |
| 55 | + $this->assertSame( filesize( self::TEST_FILE ), wp_filesize( self::TEST_FILE ) ); |
| 56 | + } |
51 | 57 |
|
52 | | - $this->assertSame( 0, wp_filesize( $file ) ); |
| 58 | + /** |
| 59 | + * @ticket 65670 |
| 60 | + * |
| 61 | + * @dataProvider data_wp_filesize_pre_wp_filesize_filter_negative |
| 62 | + * |
| 63 | + * @param float|int|string $value Negative value returned by the filter. |
| 64 | + */ |
| 65 | + public function test_wp_filesize_pre_wp_filesize_filter_negative( $value ): void { |
| 66 | + add_filter( 'pre_wp_filesize', static fn () => $value ); |
| 67 | + |
| 68 | + $this->assertSame( filesize( self::TEST_FILE ), wp_filesize( self::TEST_FILE ) ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Data provider. |
| 73 | + * |
| 74 | + * @return array<string, array{ 0: float|int|string }> |
| 75 | + */ |
| 76 | + public function data_wp_filesize_pre_wp_filesize_filter_negative(): array { |
| 77 | + return array( |
| 78 | + 'negative int' => array( -1 ), |
| 79 | + 'negative numeric string' => array( '-1' ), |
| 80 | + 'negative float' => array( -1.5 ), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @ticket 65670 |
| 86 | + * |
| 87 | + * @dataProvider data_wp_filesize_filter_invalid_value |
| 88 | + * |
| 89 | + * @param mixed $value |
| 90 | + */ |
| 91 | + public function test_wp_filesize_wp_filesize_filter_invalid_value( $value ): void { |
| 92 | + add_filter( 'wp_filesize', static fn () => $value ); |
| 93 | + $this->assertSame( 0, wp_filesize( self::TEST_FILE ) ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Data provider. |
| 98 | + * |
| 99 | + * @return array<string, array{ 0: mixed }> |
| 100 | + */ |
| 101 | + public function data_wp_filesize_filter_invalid_value(): array { |
| 102 | + return array( |
| 103 | + 'negative' => array( -1 ), |
| 104 | + 'null' => array( null ), |
| 105 | + 'array' => array( array( 'bad', 'array' ) ), |
| 106 | + ); |
53 | 107 | } |
54 | 108 | } |
0 commit comments