Files
dokuwiki-plugin-captcha/_test/HelperTest.php
2023-12-06 12:59:44 +01:00

143 lines
4.1 KiB
PHP

<?php
namespace dokuwiki\plugin\captcha\test;
use dokuwiki\plugin\captcha\FileCookie;
use DokuWikiTest;
/**
* @group plugin_captcha
* @group plugins
*/
class HelperTest extends DokuWikiTest
{
protected $pluginsEnabled = array('captcha');
public function testConfig()
{
global $conf;
$conf['plugin']['captcha']['lettercount'] = 20;
$helper = new \helper_plugin_captcha();
// generateCAPTCHA generates a maximum of 16 chars
$code = $helper->generateCaptchaCode("fixed", 0);
$this->assertEquals(16, strlen($code));
}
public function testDecrypt()
{
$helper = new \helper_plugin_captcha();
$rand = "12345";
$secret = $helper->encrypt($rand);
$this->assertNotSame(false, $secret);
$this->assertSame($rand, $helper->decrypt($secret));
$this->assertFalse($helper->decrypt(''));
$this->assertFalse($helper->decrypt('X'));
}
public function testCheck()
{
global $INPUT, $ID;
$helper = new \helper_plugin_captcha();
$INPUT->set($this->getInaccessibleProperty($helper, 'field_hp'), '');
$INPUT->set($this->getInaccessibleProperty($helper, 'field_in'), 'X');
$INPUT->set($this->getInaccessibleProperty($helper, 'field_sec'), '');
$this->assertFalse($helper->check(false));
$INPUT->set($this->getInaccessibleProperty($helper, 'field_sec'), 'X');
$this->assertFalse($helper->check(false));
// create the captcha and store the cookie
$rand = 0;
$code = $helper->generateCaptchaCode($helper->fixedIdent(), $rand);
$cookie = new FileCookie($helper->fixedIdent(), $rand);
$cookie->set();
// check with missing secrect -> fail
$INPUT->set($this->getInaccessibleProperty($helper, 'field_in'), $code);
$this->assertFalse($helper->check(false));
// set secret -> success
$INPUT->set($this->getInaccessibleProperty($helper, 'field_sec'), $helper->encrypt($rand));
$this->assertTrue($helper->check(false));
// try again, cookie is gone -> fail
$this->assertFalse($helper->check(true));
// set the cookie but change the ID -> fail
$cookie->set();
$ID = 'test:fail';
$this->assertFalse($helper->check(false));
}
public function testGenerate()
{
$helper = new \helper_plugin_captcha();
$rand = 0;
$code = $helper->generateCaptchaCode($helper->fixedIdent(), $rand);
$newcode = $helper->generateCaptchaCode($helper->fixedIdent() . 'X', $rand);
$this->assertNotEquals($newcode, $code);
$newcode = $helper->generateCaptchaCode($helper->fixedIdent(), $rand + 0.1);
$this->assertNotEquals($newcode, $code);
}
public function testCleanup()
{
// we need a complete fresh environment:
$this->setUpBeforeClass();
global $conf;
$path = $conf['tmpdir'] . '/captcha/';
$today = "$path/" . date('Y-m-d');
$helper = new \helper_plugin_captcha();
// nothing at all
$dirs = glob("$path/*");
$this->assertEquals(array(), $dirs);
// store a cookie
$cookie = new FileCookie('test', 0);
$cookie->set();
// nothing but today's data
$dirs = glob("$path/*");
$this->assertEquals(array($today), $dirs);
// add some fake cookies
io_saveFile("$path/2017-01-01/foo.cookie", '');
io_saveFile("$path/2017-01-02/foo.cookie", '');
io_saveFile("$path/2017-01-03/foo.cookie", '');
io_saveFile("$path/2017-01-04/foo.cookie", '');
// all directories there
$dirs = glob("$path/*");
$this->assertEquals(
array(
"$path/2017-01-01",
"$path/2017-01-02",
"$path/2017-01-03",
"$path/2017-01-04",
$today,
),
$dirs
);
// clean up
FileCookie::clean();
// nothing but today's data
$dirs = glob("$path/*");
$this->assertEquals(array($today), $dirs);
}
}