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

Refactor/filters #1686

Merged
merged 4 commits into from
Jan 24, 2019
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
6 changes: 3 additions & 3 deletions app/Config/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class Filters extends BaseConfig
// Makes reading things below nicer,
// and simpler to change out script that's used.
public $aliases = [
'csrf' => \App\Filters\CSRF::class,
'toolbar' => \App\Filters\DebugToolbar::class,
'honeypot' => \App\Filters\Honeypot::class,
'csrf' => \CodeIgniter\Filters\CSRF::class,
'toolbar' => \CodeIgniter\Filters\DebugToolbar::class,
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
];

// Always applied before every request
Expand Down
46 changes: 0 additions & 46 deletions app/Filters/Throttle.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Filters/CSRF.php → system/Filters/CSRF.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php namespace App\Filters;
<?php namespace CodeIgniter\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php namespace App\Filters;
<?php namespace CodeIgniter\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
Expand Down
2 changes: 1 addition & 1 deletion app/Filters/Honeypot.php → system/Filters/Honeypot.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Filters;
namespace CodeIgniter\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Autoloader/FileLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ public function testListFilesFromMultipleDir()
{
$files = $this->locator->listFiles('Filters/');

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

$expectedWin = SYSTEMPATH . 'Filters\Filters.php';
Expand Down
46 changes: 46 additions & 0 deletions tests/system/Filters/CSRFTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace CodeIgniter\Filters;

use Config\Filters as FilterConfig;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
use CodeIgniter\HTTP\ResponseInterface;

/**
* @backupGlobals enabled
*/
class CSRFTest extends \CIUnitTestCase
{

protected $config;
protected $request;
protected $response;

protected function setUp()
{
parent::setUp();
$this->config = new \Config\Filters();
}

//--------------------------------------------------------------------
public function testNormal()
{
$this->config->globals = [
'before' => ['csrf'],
'after' => [],
];

$this->request = Services::request(null, false);
$this->response = Services::response();

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

// we expect CSRF requests to be ignored in CLI
$expected = $this->request;
$request = $filters->run($uri, 'before');
$this->assertEquals($expected, $request);
}

}
52 changes: 52 additions & 0 deletions tests/system/Filters/DebugToolbarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace CodeIgniter\Filters;

use Config\Filters as FilterConfig;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\HTTP\ResponseInterface;

/**
* @backupGlobals enabled
*/
class DebugToolbarTest extends \CIUnitTestCase
{

protected $request;
protected $response;

protected function setUp()
{
parent::setUp();

$this->request = Services::request();
$this->response = Services::response();
}

//--------------------------------------------------------------------

public function testDebugToolbarFilter()
{
$_SERVER['REQUEST_METHOD'] = 'GET';

$config = new FilterConfig();
$config->globals = [
'before' => ['toolbar'], // not normal; exercising its before()
'after' => ['toolbar'],
];

$filter = new DebugToolbar();

$expectedBefore = $this->request;
$expectedAfter = $this->response;

// nothing should change here, since we have no before logic
$filter->before($this->request);
$this->assertEquals($expectedBefore, $this->request);

// nothing should change here, since we are running in the CLI
$filter->after($this->request, $this->response);
$this->assertEquals($expectedAfter, $this->response);
}

}
1 change: 1 addition & 0 deletions tests/system/Filters/FiltersTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace CodeIgniter\Filters;

use Config\Filters as FilterConfig;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\HTTP\ResponseInterface;
Expand Down
119 changes: 119 additions & 0 deletions tests/system/Filters/HoneypotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
namespace CodeIgniter\Filters;

use Config\Filters as FilterConfig;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
use CodeIgniter\HTTP\ResponseInterface;

/**
* @backupGlobals enabled
*/
class HoneypotTest extends \CIUnitTestCase
{

protected $config;
protected $honey;
protected $request;
protected $response;

protected function setUp()
{
parent::setUp();
$this->config = new \Config\Filters();
$this->honey = new \Config\Honeypot();

unset($_POST[$this->honey->name]);
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST[$this->honey->name] = 'hey';
}

//--------------------------------------------------------------------
public function testBeforeTriggered()
{
$this->config->globals = [
'before' => ['honeypot'],
'after' => [],
];

$this->request = Services::request(null, false);
$this->response = Services::response();

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$this->expectException(HoneypotException::class);
$request = $filters->run($uri, 'before');
}

//--------------------------------------------------------------------
public function testBeforeClean()
{
$this->config->globals = [
'before' => ['honeypot'],
'after' => [],
];

unset($_POST[$this->honey->name]);
$this->request = Services::request(null, false);
$this->response = Services::response();

$expected = $this->request;

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$request = $filters->run($uri, 'before');
$this->assertEquals($expected, $request);
}

//--------------------------------------------------------------------

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testAfter()
{
$this->config->globals = [
'before' => [],
'after' => ['honeypot'],
];

$this->request = Services::request(null, false);
$this->response = Services::response();

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$this->response->setBody('<form></form>');
$this->response = $filters->run($uri, 'after');
$this->assertContains($this->honey->name, $this->response->getBody());
}

//--------------------------------------------------------------------

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testAfterNotApplicable()
{
$this->config->globals = [
'before' => [],
'after' => ['honeypot'],
];

$this->request = Services::request(null, false);
$this->response = Services::response();

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$this->response->setBody('<div></div>');
$this->response = $filters->run($uri, 'after');
$this->assertNotContains($this->honey->name, $this->response->getBody());
}

}
6 changes: 2 additions & 4 deletions tests/system/Honeypot/HoneypotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
use CodeIgniter\Test\CIUnitTestCase;

require_once __DIR__ . '/fixtures/HoneyTrap.php';

/**
* @backupGlobals enabled
*/
Expand Down Expand Up @@ -90,7 +88,7 @@ public function testConfigName()
public function testHoneypotFilterBefore()
{
$config = [
'aliases' => ['trap' => 'CodeIgniter\Honeypot\fixtures\HoneyTrap'],
'aliases' => ['trap' => '\CodeIgniter\Filters\Honeypot'],
'globals' => [
'before' => ['trap'],
'after' => [],
Expand All @@ -107,7 +105,7 @@ public function testHoneypotFilterBefore()
public function testHoneypotFilterAfter()
{
$config = [
'aliases' => ['trap' => 'CodeIgniter\Honeypot\fixtures\HoneyTrap'],
'aliases' => ['trap' => '\CodeIgniter\Filters\Honeypot'],
'globals' => [
'before' => [],
'after' => ['trap'],
Expand Down
43 changes: 0 additions & 43 deletions tests/system/Honeypot/fixtures/HoneyTrap.php

This file was deleted.

Loading