diff --git a/system/Validation/FileRules.php b/system/Validation/FileRules.php index dd8c4290e11d..c8e73496a27f 100644 --- a/system/Validation/FileRules.php +++ b/system/Validation/FileRules.php @@ -206,7 +206,7 @@ public function ext_in(?string $blank, string $params): bool return true; } - if (! in_array($file->guessExtension(), $params, true)) { + if (! in_array($file->getClientExtension(), $params, true)) { return false; } } diff --git a/tests/system/Validation/FileRulesTest.php b/tests/system/Validation/FileRulesTest.php index 6d59af537a06..90b03428915a 100644 --- a/tests/system/Validation/FileRulesTest.php +++ b/tests/system/Validation/FileRulesTest.php @@ -16,6 +16,7 @@ use CodeIgniter\Test\CIUnitTestCase; use Config\Services; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use Tests\Support\Validation\TestRules; @@ -62,6 +63,20 @@ protected function setUp(): void 'width' => 640, 'height' => 400, ], + 'excel_xlsx' => [ + 'tmp_name' => TESTPATH . '_support/Validation/uploads/abc77tz', + 'name' => 'whata.xlsx', + 'size' => 12345, + 'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'error' => UPLOAD_ERR_OK, + ], + 'excel_xls' => [ + 'tmp_name' => TESTPATH . '_support/Validation/uploads/abc77tz', + 'name' => 'whatb.xls', + 'size' => 12345, + 'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'error' => UPLOAD_ERR_OK, + ], 'bigfile' => [ 'tmp_name' => TESTPATH . '_support/Validation/uploads/phpUxc0ty', 'name' => 'my-big-file.png', @@ -280,21 +295,41 @@ public function testMimeTypeImpossible(): void $this->assertFalse($this->validation->run([])); } - public function testExtensionOk(): void - { - $this->validation->setRules(['avatar' => 'ext_in[avatar,jpg,jpeg,gif,png]']); - $this->assertTrue($this->validation->run([])); - } - - public function testExtensionNotOk(): void + public static function provideExtensionMore(): iterable { - $this->validation->setRules(['avatar' => 'ext_in[avatar,xls,doc,ppt]']); - $this->assertFalse($this->validation->run([])); + yield from [ + [ + 'avatar', + 'jpg,jpeg,gif,png', + true, + ], + [ + 'avatar', + 'xls,doc,ppt', + false, + ], + [ + 'excel_xlsx', + 'xls,xlsx', + true, + ], + [ + 'excel_xls', + 'xls,xlsx', + true, + ], + [ + 'excel_xls', + 'pdf', + false, + ], + ]; } - public function testExtensionImpossible(): void + #[DataProvider('provideExtensionMore')] + public function testExtensionMoreOk(string $field, string $rules, bool $expect): void { - $this->validation->setRules(['avatar' => 'ext_in[unknown,xls,doc,ppt]']); - $this->assertFalse($this->validation->run([])); + $this->validation->setRules([$field => "ext_in[{$field},{$rules}]"]); + $this->assertSame($expect, $this->validation->run([])); } }