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