Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/Validation/FileRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
59 changes: 47 additions & 12 deletions tests/system/Validation/FileRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,6 +63,20 @@
'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',
Expand Down Expand Up @@ -280,21 +295,41 @@
$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

Check failure on line 298 in tests/system/Validation/FileRulesTest.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\Validation\FileRulesTest::provideExtensionMore() return type has no value type specified in iterable type 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([]));
}
}
Loading