Skip to content

Commit 8eaa032

Browse files
committed
refactor: uneeded check mime in ext_in validation
1 parent caf4d53 commit 8eaa032

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

system/Validation/FileRules.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public function ext_in(?string $blank, string $params): bool
206206
return true;
207207
}
208208

209-
if (! in_array($file->guessExtension(), $params, true)) {
209+
if (! in_array($file->getClientExtension(), $params, true)) {
210210
return false;
211211
}
212212
}

tests/system/Validation/FileRulesTest.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use Config\Services;
1818
use InvalidArgumentException;
19+
use PHPUnit\Framework\Attributes\DataProvider;
1920
use PHPUnit\Framework\Attributes\Group;
2021
use Tests\Support\Validation\TestRules;
2122

@@ -62,6 +63,20 @@ protected function setUp(): void
6263
'width' => 640,
6364
'height' => 400,
6465
],
66+
'excel_xlsx' => [
67+
'tmp_name' => TESTPATH . '_support/Validation/uploads/abc77tz',
68+
'name' => 'whata.xlsx',
69+
'size' => 12345,
70+
'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
71+
'error' => UPLOAD_ERR_OK,
72+
],
73+
'excel_xls' => [
74+
'tmp_name' => TESTPATH . '_support/Validation/uploads/abc77tz',
75+
'name' => 'whatb.xls',
76+
'size' => 12345,
77+
'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
78+
'error' => UPLOAD_ERR_OK,
79+
],
6580
'bigfile' => [
6681
'tmp_name' => TESTPATH . '_support/Validation/uploads/phpUxc0ty',
6782
'name' => 'my-big-file.png',
@@ -280,21 +295,41 @@ public function testMimeTypeImpossible(): void
280295
$this->assertFalse($this->validation->run([]));
281296
}
282297

283-
public function testExtensionOk(): void
284-
{
285-
$this->validation->setRules(['avatar' => 'ext_in[avatar,jpg,jpeg,gif,png]']);
286-
$this->assertTrue($this->validation->run([]));
287-
}
288-
289-
public function testExtensionNotOk(): void
298+
public static function provideExtensionMore(): iterable
290299
{
291-
$this->validation->setRules(['avatar' => 'ext_in[avatar,xls,doc,ppt]']);
292-
$this->assertFalse($this->validation->run([]));
300+
yield from [
301+
[
302+
'avatar',
303+
'jpg,jpeg,gif,png',
304+
true,
305+
],
306+
[
307+
'avatar',
308+
'xls,doc,ppt',
309+
false,
310+
],
311+
[
312+
'excel_xlsx',
313+
'xls,xlsx',
314+
true,
315+
],
316+
[
317+
'excel_xls',
318+
'xls,xlsx',
319+
true,
320+
],
321+
[
322+
'excel_xls',
323+
'pdf',
324+
false,
325+
],
326+
];
293327
}
294328

295-
public function testExtensionImpossible(): void
329+
#[DataProvider('provideExtensionMore')]
330+
public function testExtensionMoreOk(string $field, string $rules, bool $expect): void
296331
{
297-
$this->validation->setRules(['avatar' => 'ext_in[unknown,xls,doc,ppt]']);
298-
$this->assertFalse($this->validation->run([]));
332+
$this->validation->setRules([$field => "ext_in[{$field},{$rules}]"]);
333+
$this->assertSame($expect, $this->validation->run([]));
299334
}
300335
}

0 commit comments

Comments
 (0)