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