xref: /plugin/captcha/_test/HelperTest.php (revision 5697ecf8bf298450d6722c7b7298c8ff44eda91e)
14c488e71SAndreas Gohr<?php
24c488e71SAndreas Gohr
34c488e71SAndreas Gohrnamespace dokuwiki\plugin\captcha\test;
44c488e71SAndreas Gohr
5*5697ecf8SAndreas Gohruse dokuwiki\plugin\captcha\FileCookie;
64c488e71SAndreas Gohruse DokuWikiTest;
74c488e71SAndreas Gohr
84c488e71SAndreas Gohr/**
94c488e71SAndreas Gohr * @group plugin_captcha
104c488e71SAndreas Gohr * @group plugins
114c488e71SAndreas Gohr */
124c488e71SAndreas Gohrclass HelperTest extends DokuWikiTest
134c488e71SAndreas Gohr{
144c488e71SAndreas Gohr
154c488e71SAndreas Gohr    protected $pluginsEnabled = array('captcha');
164c488e71SAndreas Gohr
174c488e71SAndreas Gohr    public function testConfig()
184c488e71SAndreas Gohr    {
194c488e71SAndreas Gohr        global $conf;
204c488e71SAndreas Gohr        $conf['plugin']['captcha']['lettercount'] = 20;
214c488e71SAndreas Gohr
224c488e71SAndreas Gohr        $helper = new \helper_plugin_captcha();
234c488e71SAndreas Gohr
244c488e71SAndreas Gohr        // generateCAPTCHA generates a maximum of 16 chars
2509b1e97eSAndreas Gohr        $code = $helper->generateCaptchaCode("fixed", 0);
264c488e71SAndreas Gohr        $this->assertEquals(16, strlen($code));
274c488e71SAndreas Gohr    }
284c488e71SAndreas Gohr
294c488e71SAndreas Gohr    public function testDecrypt()
304c488e71SAndreas Gohr    {
314c488e71SAndreas Gohr        $helper = new \helper_plugin_captcha();
324c488e71SAndreas Gohr
334c488e71SAndreas Gohr        $rand = "12345";
344c488e71SAndreas Gohr        $secret = $helper->encrypt($rand);
354c488e71SAndreas Gohr        $this->assertNotSame(false, $secret);
364c488e71SAndreas Gohr        $this->assertSame($rand, $helper->decrypt($secret));
374c488e71SAndreas Gohr
384c488e71SAndreas Gohr        $this->assertFalse($helper->decrypt(''));
394c488e71SAndreas Gohr        $this->assertFalse($helper->decrypt('X'));
404c488e71SAndreas Gohr    }
414c488e71SAndreas Gohr
424c488e71SAndreas Gohr    public function testCheck()
434c488e71SAndreas Gohr    {
444c488e71SAndreas Gohr
454c488e71SAndreas Gohr        global $INPUT, $ID;
464c488e71SAndreas Gohr
474c488e71SAndreas Gohr        $helper = new \helper_plugin_captcha();
484c488e71SAndreas Gohr
494c488e71SAndreas Gohr        $INPUT->set($this->getInaccessibleProperty($helper, 'field_hp'), '');
504c488e71SAndreas Gohr        $INPUT->set($this->getInaccessibleProperty($helper, 'field_in'), 'X');
514c488e71SAndreas Gohr        $INPUT->set($this->getInaccessibleProperty($helper, 'field_sec'), '');
524c488e71SAndreas Gohr
534c488e71SAndreas Gohr        $this->assertFalse($helper->check(false));
544c488e71SAndreas Gohr        $INPUT->set($this->getInaccessibleProperty($helper, 'field_sec'), 'X');
554c488e71SAndreas Gohr        $this->assertFalse($helper->check(false));
564c488e71SAndreas Gohr
574c488e71SAndreas Gohr        // create the captcha and store the cookie
584c488e71SAndreas Gohr        $rand = 0;
5909b1e97eSAndreas Gohr        $code = $helper->generateCaptchaCode($helper->fixedIdent(), $rand);
604c488e71SAndreas Gohr
61*5697ecf8SAndreas Gohr        $cookie = new FileCookie($helper->fixedIdent(), $rand);
62*5697ecf8SAndreas Gohr        $cookie->set();
634c488e71SAndreas Gohr
644c488e71SAndreas Gohr        // check with missing secrect -> fail
654c488e71SAndreas Gohr        $INPUT->set($this->getInaccessibleProperty($helper, 'field_in'), $code);
664c488e71SAndreas Gohr        $this->assertFalse($helper->check(false));
674c488e71SAndreas Gohr
684c488e71SAndreas Gohr        // set secret -> success
694c488e71SAndreas Gohr        $INPUT->set($this->getInaccessibleProperty($helper, 'field_sec'), $helper->encrypt($rand));
704c488e71SAndreas Gohr        $this->assertTrue($helper->check(false));
714c488e71SAndreas Gohr
724c488e71SAndreas Gohr        // try again, cookie is gone -> fail
734c488e71SAndreas Gohr        $this->assertFalse($helper->check(true));
744c488e71SAndreas Gohr
754c488e71SAndreas Gohr        // set the cookie but change the ID -> fail
76*5697ecf8SAndreas Gohr        $cookie->set();
774c488e71SAndreas Gohr        $ID = 'test:fail';
784c488e71SAndreas Gohr        $this->assertFalse($helper->check(false));
794c488e71SAndreas Gohr    }
804c488e71SAndreas Gohr
814c488e71SAndreas Gohr    public function testGenerate()
824c488e71SAndreas Gohr    {
834c488e71SAndreas Gohr        $helper = new \helper_plugin_captcha();
844c488e71SAndreas Gohr
854c488e71SAndreas Gohr        $rand = 0;
8609b1e97eSAndreas Gohr        $code = $helper->generateCaptchaCode($helper->fixedIdent(), $rand);
8709b1e97eSAndreas Gohr        $newcode = $helper->generateCaptchaCode($helper->fixedIdent() . 'X', $rand);
884c488e71SAndreas Gohr        $this->assertNotEquals($newcode, $code);
8909b1e97eSAndreas Gohr        $newcode = $helper->generateCaptchaCode($helper->fixedIdent(), $rand + 0.1);
904c488e71SAndreas Gohr        $this->assertNotEquals($newcode, $code);
914c488e71SAndreas Gohr    }
924c488e71SAndreas Gohr
934c488e71SAndreas Gohr    public function testCleanup()
944c488e71SAndreas Gohr    {
954c488e71SAndreas Gohr        // we need a complete fresh environment:
964c488e71SAndreas Gohr        $this->setUpBeforeClass();
974c488e71SAndreas Gohr
984c488e71SAndreas Gohr        global $conf;
994c488e71SAndreas Gohr        $path = $conf['tmpdir'] . '/captcha/';
1004c488e71SAndreas Gohr        $today = "$path/" . date('Y-m-d');
1014c488e71SAndreas Gohr
1024c488e71SAndreas Gohr        $helper = new \helper_plugin_captcha();
1034c488e71SAndreas Gohr
1044c488e71SAndreas Gohr        // nothing at all
1054c488e71SAndreas Gohr        $dirs = glob("$path/*");
1064c488e71SAndreas Gohr        $this->assertEquals(array(), $dirs);
1074c488e71SAndreas Gohr
1084c488e71SAndreas Gohr        // store a cookie
109*5697ecf8SAndreas Gohr        $cookie = new FileCookie('test', 0);
110*5697ecf8SAndreas Gohr        $cookie->set();
1114c488e71SAndreas Gohr
1124c488e71SAndreas Gohr        // nothing but today's data
1134c488e71SAndreas Gohr        $dirs = glob("$path/*");
1144c488e71SAndreas Gohr        $this->assertEquals(array($today), $dirs);
1154c488e71SAndreas Gohr
1164c488e71SAndreas Gohr        // add some fake cookies
1174c488e71SAndreas Gohr        io_saveFile("$path/2017-01-01/foo.cookie", '');
1184c488e71SAndreas Gohr        io_saveFile("$path/2017-01-02/foo.cookie", '');
1194c488e71SAndreas Gohr        io_saveFile("$path/2017-01-03/foo.cookie", '');
1204c488e71SAndreas Gohr        io_saveFile("$path/2017-01-04/foo.cookie", '');
1214c488e71SAndreas Gohr
1224c488e71SAndreas Gohr        // all directories there
1234c488e71SAndreas Gohr        $dirs = glob("$path/*");
1244c488e71SAndreas Gohr        $this->assertEquals(
1254c488e71SAndreas Gohr            array(
1264c488e71SAndreas Gohr                "$path/2017-01-01",
1274c488e71SAndreas Gohr                "$path/2017-01-02",
1284c488e71SAndreas Gohr                "$path/2017-01-03",
1294c488e71SAndreas Gohr                "$path/2017-01-04",
1304c488e71SAndreas Gohr                $today,
1314c488e71SAndreas Gohr            ),
1324c488e71SAndreas Gohr            $dirs
1334c488e71SAndreas Gohr        );
1344c488e71SAndreas Gohr
1354c488e71SAndreas Gohr        // clean up
136*5697ecf8SAndreas Gohr        FileCookie::clean();
1374c488e71SAndreas Gohr
1384c488e71SAndreas Gohr        // nothing but today's data
1394c488e71SAndreas Gohr        $dirs = glob("$path/*");
1404c488e71SAndreas Gohr        $this->assertEquals(array($today), $dirs);
1414c488e71SAndreas Gohr    }
1424c488e71SAndreas Gohr}
143