Skip to content

Commit 5016336

Browse files
committed
System filter testing
1 parent e3bbc9f commit 5016336

6 files changed

Lines changed: 220 additions & 47 deletions

File tree

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+
}

tests/system/Honeypot/HoneypotTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
88
use CodeIgniter\Test\CIUnitTestCase;
99

10-
require_once __DIR__ . '/fixtures/HoneyTrap.php';
11-
1210
/**
1311
* @backupGlobals enabled
1412
*/
@@ -90,7 +88,7 @@ public function testConfigName()
9088
public function testHoneypotFilterBefore()
9189
{
9290
$config = [
93-
'aliases' => ['trap' => 'CodeIgniter\Honeypot\fixtures\HoneyTrap'],
91+
'aliases' => ['trap' => '\CodeIgniter\Filters\Honeypot'],
9492
'globals' => [
9593
'before' => ['trap'],
9694
'after' => [],
@@ -107,7 +105,7 @@ public function testHoneypotFilterBefore()
107105
public function testHoneypotFilterAfter()
108106
{
109107
$config = [
110-
'aliases' => ['trap' => 'CodeIgniter\Honeypot\fixtures\HoneyTrap'],
108+
'aliases' => ['trap' => '\CodeIgniter\Filters\Honeypot'],
111109
'globals' => [
112110
'before' => [],
113111
'after' => ['trap'],

tests/system/Honeypot/fixtures/HoneyTrap.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)