1<?php
2
3namespace dokuwiki\plugin\captcha\test;
4
5use DokuWikiTest;
6
7/**
8 * @group plugin_captcha
9 * @group plugins
10 */
11class HelperTest extends DokuWikiTest
12{
13
14    protected $pluginsEnabled = array('captcha');
15
16    public function testConfig()
17    {
18        global $conf;
19        $conf['plugin']['captcha']['lettercount'] = 20;
20
21        $helper = new \helper_plugin_captcha();
22
23        // generateCAPTCHA generates a maximum of 16 chars
24        $code = $helper->_generateCAPTCHA("fixed", 0);
25        $this->assertEquals(16, strlen($code));
26    }
27
28    public function testDecrypt()
29    {
30        $helper = new \helper_plugin_captcha();
31
32        $rand = "12345";
33        $secret = $helper->encrypt($rand);
34        $this->assertNotSame(false, $secret);
35        $this->assertSame($rand, $helper->decrypt($secret));
36
37        $this->assertFalse($helper->decrypt(''));
38        $this->assertFalse($helper->decrypt('X'));
39    }
40
41    public function testCheck()
42    {
43
44        global $INPUT, $ID;
45
46        $helper = new \helper_plugin_captcha();
47
48        $INPUT->set($this->getInaccessibleProperty($helper, 'field_hp'), '');
49        $INPUT->set($this->getInaccessibleProperty($helper, 'field_in'), 'X');
50        $INPUT->set($this->getInaccessibleProperty($helper, 'field_sec'), '');
51
52        $this->assertFalse($helper->check(false));
53        $INPUT->set($this->getInaccessibleProperty($helper, 'field_sec'), 'X');
54        $this->assertFalse($helper->check(false));
55
56        // create the captcha and store the cookie
57        $rand = 0;
58        $code = $helper->_generateCAPTCHA($helper->_fixedIdent(), $rand);
59
60        $this->callInaccessibleMethod($helper, 'storeCaptchaCookie', [$helper->_fixedIdent(), $rand]);
61
62        // check with missing secrect -> fail
63        $INPUT->set($this->getInaccessibleProperty($helper, 'field_in'), $code);
64        $this->assertFalse($helper->check(false));
65
66        // set secret -> success
67        $INPUT->set($this->getInaccessibleProperty($helper, 'field_sec'), $helper->encrypt($rand));
68        $this->assertTrue($helper->check(false));
69
70        // try again, cookie is gone -> fail
71        $this->assertFalse($helper->check(true));
72
73        // set the cookie but change the ID -> fail
74        $this->callInaccessibleMethod($helper, 'storeCaptchaCookie', [$helper->_fixedIdent(), $rand]);
75        $ID = 'test:fail';
76        $this->assertFalse($helper->check(false));
77    }
78
79    public function testGenerate()
80    {
81        $helper = new \helper_plugin_captcha();
82
83        $rand = 0;
84        $code = $helper->_generateCAPTCHA($helper->_fixedIdent(), $rand);
85        $newcode = $helper->_generateCAPTCHA($helper->_fixedIdent() . 'X', $rand);
86        $this->assertNotEquals($newcode, $code);
87        $newcode = $helper->_generateCAPTCHA($helper->_fixedIdent(), $rand + 0.1);
88        $this->assertNotEquals($newcode, $code);
89    }
90
91    public function testCleanup()
92    {
93        // we need a complete fresh environment:
94        $this->setUpBeforeClass();
95
96        global $conf;
97        $path = $conf['tmpdir'] . '/captcha/';
98        $today = "$path/" . date('Y-m-d');
99
100        $helper = new \helper_plugin_captcha();
101
102        // nothing at all
103        $dirs = glob("$path/*");
104        $this->assertEquals(array(), $dirs);
105
106        // store a cookie
107        $this->callInaccessibleMethod($helper, 'storeCaptchaCookie', ['test', 0]);
108
109        // nothing but today's data
110        $dirs = glob("$path/*");
111        $this->assertEquals(array($today), $dirs);
112
113        // add some fake cookies
114        io_saveFile("$path/2017-01-01/foo.cookie", '');
115        io_saveFile("$path/2017-01-02/foo.cookie", '');
116        io_saveFile("$path/2017-01-03/foo.cookie", '');
117        io_saveFile("$path/2017-01-04/foo.cookie", '');
118
119        // all directories there
120        $dirs = glob("$path/*");
121        $this->assertEquals(
122            array(
123                "$path/2017-01-01",
124                "$path/2017-01-02",
125                "$path/2017-01-03",
126                "$path/2017-01-04",
127                $today,
128            ),
129            $dirs
130        );
131
132        // clean up
133        $helper->_cleanCaptchaCookies();
134
135        // nothing but today's data
136        $dirs = glob("$path/*");
137        $this->assertEquals(array($today), $dirs);
138    }
139}
140