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