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