Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: FilterTestTrait::getFilterCaller() does not support Filter classes as array #8058

Merged
merged 3 commits into from
Oct 21, 2023
Merged
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
5 changes: 3 additions & 2 deletions app/Config/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class Filters extends BaseConfig
* Configures aliases for Filter classes to
* make reading things nicer and simpler.
*
* @var array<string, string>
* @phpstan-var array<string, class-string>
* @var array<string, array<int, string>|string> [filter_name => classname]
* or [filter_name => [classname1, classname2, ...]]
* @phpstan-var array<string, class-string|list<class-string>>
*/
public array $aliases = [
'csrf' => CSRF::class,
Expand Down
56 changes: 49 additions & 7 deletions system/Test/FilterTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,68 @@ protected function getFilterCaller($filter, string $position): Closure
throw new RuntimeException("No filter found with alias '{$filter}'");
}

$filter = $this->filtersConfig->aliases[$filter];
$filterClasses = $this->filtersConfig->aliases[$filter];
}

// Get an instance
$filter = new $filter();
$filterClasses = (array) $filterClasses;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduced a scenario where $filterClasses may note be set (if a FQCN was passed as $filter causing it to error out.

Test code:

    public function testLoginPass()
    {
        session()->set('uid', '54321fedcba');

        $caller = $this->getFilterCaller(LoginFilter::class, 'before');
        $result = $caller();

        $this->assertNull($result);
    }

Result:

2) FiltersTest::testNotAuthenticated with data set #0 ('App\Filters\LoginFilter')
ErrorException: Undefined variable $filterClasses

/home/runner/work/web/web/vendor/codeigniter4/framework/system/Test/FilterTestTrait.php:138
/home/runner/work/web/web/tests/misc/FiltersTest.php:60

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry!
Why can't static analysis detect?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sent #8195

}

if (! $filter instanceof FilterInterface) {
throw FilterException::forIncorrectInterface(get_class($filter));
foreach ($filterClasses as $class) {
// Get an instance
$filter = new $class();

if (! $filter instanceof FilterInterface) {
throw FilterException::forIncorrectInterface(get_class($filter));
}
}

$request = clone $this->request;

if ($position === 'before') {
return static fn (?array $params = null) => $filter->before($request, $params);
return static function (?array $params = null) use ($filterClasses, $request) {
foreach ($filterClasses as $class) {
$filter = new $class();

$result = $filter->before($request, $params);

// @TODO The following logic is in Filters class.
// Should use Filters class.
if ($result instanceof RequestInterface) {
$request = $result;

continue;
}
if ($result instanceof ResponseInterface) {
return $result;
}
if (empty($result)) {
continue;
}
}

return $result;
};
}

$response = clone $this->response;

return static fn (?array $params = null) => $filter->after($request, $response, $params);
return static function (?array $params = null) use ($filterClasses, $request, $response) {
foreach ($filterClasses as $class) {
$filter = new $class();

$result = $filter->after($request, $response, $params);

// @TODO The following logic is in Filters class.
// Should use Filters class.
if ($result instanceof ResponseInterface) {
$response = $result;

continue;
}
}

return $result;
};
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/system/Test/FilterTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Test;

use CodeIgniter\HTTP\RequestInterface;
use Config\Services;
use Tests\Support\Filters\Customfilter;

/**
Expand Down Expand Up @@ -62,12 +63,23 @@ public function testGetCallerInvalidPosition(): void
$this->getFilterCaller('test-customfilter', 'banana');
}

public function testCallerSupportArray(): void
{
$this->filtersConfig->aliases['test-customfilter'] = [Customfilter::class];

$caller = $this->getFilterCaller('test-customfilter', 'before');
$result = $caller();

$this->assertSame('http://hellowworld.com', $result->getBody());
}

public function testCallerUsesClonedInstance(): void
{
$caller = $this->getFilterCaller('test-customfilter', 'before');
$result = $caller();

$this->assertSame('http://hellowworld.com', $result->getBody());
$this->assertNull(Services::response()->getBody());

$this->resetServices();
}
Expand Down
Loading