Skip to content

Commit bd3db8c

Browse files
authored
Merge pull request #1686 from jim-parry/refactor/filters
Refactor/filters
2 parents d268436 + 5016336 commit bd3db8c

20 files changed

Lines changed: 294 additions & 130 deletions

File tree

app/Config/Filters.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class Filters extends BaseConfig
77
// Makes reading things below nicer,
88
// and simpler to change out script that's used.
99
public $aliases = [
10-
'csrf' => \App\Filters\CSRF::class,
11-
'toolbar' => \App\Filters\DebugToolbar::class,
12-
'honeypot' => \App\Filters\Honeypot::class,
10+
'csrf' => \CodeIgniter\Filters\CSRF::class,
11+
'toolbar' => \CodeIgniter\Filters\DebugToolbar::class,
12+
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
1313
];
1414

1515
// Always applied before every request

app/Filters/Throttle.php

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace App\Filters;
1+
<?php namespace CodeIgniter\Filters;
22

33
use CodeIgniter\Filters\FilterInterface;
44
use CodeIgniter\HTTP\RequestInterface;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace App\Filters;
1+
<?php namespace CodeIgniter\Filters;
22

33
use CodeIgniter\Filters\FilterInterface;
44
use CodeIgniter\HTTP\RequestInterface;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Filters;
3+
namespace CodeIgniter\Filters;
44

55
use CodeIgniter\Filters\FilterInterface;
66
use CodeIgniter\HTTP\RequestInterface;

tests/system/Autoloader/FileLocatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ public function testListFilesFromMultipleDir()
209209
{
210210
$files = $this->locator->listFiles('Filters/');
211211

212-
$expectedWin = APPPATH . 'Filters\DebugToolbar.php';
213-
$expectedLin = APPPATH . 'Filters/DebugToolbar.php';
212+
$expectedWin = SYSTEMPATH . 'Filters\DebugToolbar.php';
213+
$expectedLin = SYSTEMPATH . 'Filters/DebugToolbar.php';
214214
$this->assertTrue(in_array($expectedWin, $files) || in_array($expectedLin, $files));
215215

216216
$expectedWin = SYSTEMPATH . 'Filters\Filters.php';

tests/system/Filters/CSRFTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace CodeIgniter\Filters;
3+
4+
use Config\Filters as FilterConfig;
5+
use CodeIgniter\Config\Services;
6+
use CodeIgniter\Filters\Exceptions\FilterException;
7+
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
8+
use CodeIgniter\HTTP\ResponseInterface;
9+
10+
/**
11+
* @backupGlobals enabled
12+
*/
13+
class CSRFTest extends \CIUnitTestCase
14+
{
15+
16+
protected $config;
17+
protected $request;
18+
protected $response;
19+
20+
protected function setUp()
21+
{
22+
parent::setUp();
23+
$this->config = new \Config\Filters();
24+
}
25+
26+
//--------------------------------------------------------------------
27+
public function testNormal()
28+
{
29+
$this->config->globals = [
30+
'before' => ['csrf'],
31+
'after' => [],
32+
];
33+
34+
$this->request = Services::request(null, false);
35+
$this->response = Services::response();
36+
37+
$filters = new Filters($this->config, $this->request, $this->response);
38+
$uri = 'admin/foo/bar';
39+
40+
// we expect CSRF requests to be ignored in CLI
41+
$expected = $this->request;
42+
$request = $filters->run($uri, 'before');
43+
$this->assertEquals($expected, $request);
44+
}
45+
46+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace CodeIgniter\Filters;
3+
4+
use Config\Filters as FilterConfig;
5+
use CodeIgniter\Config\Services;
6+
use CodeIgniter\Filters\Exceptions\FilterException;
7+
use CodeIgniter\HTTP\ResponseInterface;
8+
9+
/**
10+
* @backupGlobals enabled
11+
*/
12+
class DebugToolbarTest extends \CIUnitTestCase
13+
{
14+
15+
protected $request;
16+
protected $response;
17+
18+
protected function setUp()
19+
{
20+
parent::setUp();
21+
22+
$this->request = Services::request();
23+
$this->response = Services::response();
24+
}
25+
26+
//--------------------------------------------------------------------
27+
28+
public function testDebugToolbarFilter()
29+
{
30+
$_SERVER['REQUEST_METHOD'] = 'GET';
31+
32+
$config = new FilterConfig();
33+
$config->globals = [
34+
'before' => ['toolbar'], // not normal; exercising its before()
35+
'after' => ['toolbar'],
36+
];
37+
38+
$filter = new DebugToolbar();
39+
40+
$expectedBefore = $this->request;
41+
$expectedAfter = $this->response;
42+
43+
// nothing should change here, since we have no before logic
44+
$filter->before($this->request);
45+
$this->assertEquals($expectedBefore, $this->request);
46+
47+
// nothing should change here, since we are running in the CLI
48+
$filter->after($this->request, $this->response);
49+
$this->assertEquals($expectedAfter, $this->response);
50+
}
51+
52+
}

tests/system/Filters/FiltersTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace CodeIgniter\Filters;
33

4+
use Config\Filters as FilterConfig;
45
use CodeIgniter\Config\Services;
56
use CodeIgniter\Filters\Exceptions\FilterException;
67
use CodeIgniter\HTTP\ResponseInterface;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
namespace CodeIgniter\Filters;
3+
4+
use Config\Filters as FilterConfig;
5+
use CodeIgniter\Config\Services;
6+
use CodeIgniter\Filters\Exceptions\FilterException;
7+
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
8+
use CodeIgniter\HTTP\ResponseInterface;
9+
10+
/**
11+
* @backupGlobals enabled
12+
*/
13+
class HoneypotTest extends \CIUnitTestCase
14+
{
15+
16+
protected $config;
17+
protected $honey;
18+
protected $request;
19+
protected $response;
20+
21+
protected function setUp()
22+
{
23+
parent::setUp();
24+
$this->config = new \Config\Filters();
25+
$this->honey = new \Config\Honeypot();
26+
27+
unset($_POST[$this->honey->name]);
28+
$_SERVER['REQUEST_METHOD'] = 'POST';
29+
$_POST[$this->honey->name] = 'hey';
30+
}
31+
32+
//--------------------------------------------------------------------
33+
public function testBeforeTriggered()
34+
{
35+
$this->config->globals = [
36+
'before' => ['honeypot'],
37+
'after' => [],
38+
];
39+
40+
$this->request = Services::request(null, false);
41+
$this->response = Services::response();
42+
43+
$filters = new Filters($this->config, $this->request, $this->response);
44+
$uri = 'admin/foo/bar';
45+
46+
$this->expectException(HoneypotException::class);
47+
$request = $filters->run($uri, 'before');
48+
}
49+
50+
//--------------------------------------------------------------------
51+
public function testBeforeClean()
52+
{
53+
$this->config->globals = [
54+
'before' => ['honeypot'],
55+
'after' => [],
56+
];
57+
58+
unset($_POST[$this->honey->name]);
59+
$this->request = Services::request(null, false);
60+
$this->response = Services::response();
61+
62+
$expected = $this->request;
63+
64+
$filters = new Filters($this->config, $this->request, $this->response);
65+
$uri = 'admin/foo/bar';
66+
67+
$request = $filters->run($uri, 'before');
68+
$this->assertEquals($expected, $request);
69+
}
70+
71+
//--------------------------------------------------------------------
72+
73+
/**
74+
* @runInSeparateProcess
75+
* @preserveGlobalState disabled
76+
*/
77+
public function testAfter()
78+
{
79+
$this->config->globals = [
80+
'before' => [],
81+
'after' => ['honeypot'],
82+
];
83+
84+
$this->request = Services::request(null, false);
85+
$this->response = Services::response();
86+
87+
$filters = new Filters($this->config, $this->request, $this->response);
88+
$uri = 'admin/foo/bar';
89+
90+
$this->response->setBody('<form></form>');
91+
$this->response = $filters->run($uri, 'after');
92+
$this->assertContains($this->honey->name, $this->response->getBody());
93+
}
94+
95+
//--------------------------------------------------------------------
96+
97+
/**
98+
* @runInSeparateProcess
99+
* @preserveGlobalState disabled
100+
*/
101+
public function testAfterNotApplicable()
102+
{
103+
$this->config->globals = [
104+
'before' => [],
105+
'after' => ['honeypot'],
106+
];
107+
108+
$this->request = Services::request(null, false);
109+
$this->response = Services::response();
110+
111+
$filters = new Filters($this->config, $this->request, $this->response);
112+
$uri = 'admin/foo/bar';
113+
114+
$this->response->setBody('<div></div>');
115+
$this->response = $filters->run($uri, 'after');
116+
$this->assertNotContains($this->honey->name, $this->response->getBody());
117+
}
118+
119+
}

0 commit comments

Comments
 (0)