[ticket/17414] Change incomplete CAPTCHA to extend new base class

PHPBB-17414
This commit is contained in:
Marc Alexander 2024-10-20 10:55:15 +02:00
parent fd994f4742
commit 3b27b65c76
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
7 changed files with 86 additions and 66 deletions

View file

@ -19,7 +19,11 @@ services:
shared: false shared: false
arguments: arguments:
- '@config' - '@config'
- '@dbal.conn'
- '@language'
- '@request'
- '@template' - '@template'
- '@user'
- '%core.root_path%' - '%core.root_path%'
- '%core.php_ext%' - '%core.php_ext%'
calls: calls:

View file

@ -246,4 +246,11 @@ abstract class base implements plugin_interface
} }
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);
} }
/**
* {@inheritDoc}
*/
public function acp_page(mixed $id, mixed $module): void
{
}
} }

View file

@ -14,25 +14,34 @@
namespace phpbb\captcha\plugins; namespace phpbb\captcha\plugins;
use phpbb\config\config; use phpbb\config\config;
use phpbb\exception\runtime_exception; use phpbb\db\driver\driver_interface;
use phpbb\language\language;
use phpbb\request\request_interface;
use phpbb\template\template; use phpbb\template\template;
use phpbb\user;
class incomplete extends captcha_abstract class incomplete extends base
{ {
/** /**
* Constructor for incomplete captcha * Constructor for incomplete captcha
* *
* @param config $config * @param config $config
* @param driver_interface $db
* @param language $language
* @param request_interface $request
* @param template $template * @param template $template
* @param user $user
* @param string $phpbb_root_path * @param string $phpbb_root_path
* @param string $phpEx * @param string $phpEx
*/ */
public function __construct(protected config $config, protected template $template, public function __construct(config $config, driver_interface $db, language $language, request_interface $request,
protected string $phpbb_root_path, protected string $phpEx) protected template $template, user $user, protected string $phpbb_root_path, protected string $phpEx)
{} {
parent::__construct($config, $db, $language, $request, $user);
}
/** /**
* @return bool True if captcha is available, false if not * {@inheritDoc}
*/ */
public function is_available(): bool public function is_available(): bool
{ {
@ -40,70 +49,45 @@ class incomplete extends captcha_abstract
} }
/** /**
* Dummy implementation, not supported by this captcha * {@inheritDoc}
*
* @throws runtime_exception
* @return void
*/ */
public function get_generator_class(): void public function has_config(): bool
{ {
throw new runtime_exception('NO_GENERATOR_CLASS'); return false;
} }
/** /**
* Get CAPTCHA name language variable * {@inheritDoc}
*
* @return string Language variable
*/ */
public static function get_name(): string public function get_name(): string
{ {
return 'CAPTCHA_INCOMPLETE'; return 'CAPTCHA_INCOMPLETE';
} }
/** /**
* Init CAPTCHA * {@inheritDoc}
*
* @param int $type CAPTCHA type
* @return void
*/ */
public function init($type) public function set_name(string $name): void
{ {
} }
/** /**
* Execute demo * {@inheritDoc}
*
* @return void
*/ */
public function execute_demo() public function init(confirm_type $type): void
{ {
} }
/** /**
* Execute CAPTCHA * {@inheritDoc}
*
* @return void
*/ */
public function execute() public function get_demo_template(): string
{
}
/**
* Get template data for demo
*
* @param int|string $id ACP module ID
*
* @return string Demo template file name
*/
public function get_demo_template($id): string
{ {
return ''; return '';
} }
/** /**
* Get template data for CAPTCHA * {@inheritDoc}
*
* @return string CAPTCHA template file name
*/ */
public function get_template(): string public function get_template(): string
{ {
@ -118,9 +102,7 @@ class incomplete extends captcha_abstract
} }
/** /**
* Validate CAPTCHA * {@inheritDoc}
*
* @return false Incomplete CAPTCHA will never validate
*/ */
public function validate(): bool public function validate(): bool
{ {
@ -128,12 +110,26 @@ class incomplete extends captcha_abstract
} }
/** /**
* Check whether CAPTCHA is solved * {@inheritDoc}
* */
* @return false Incomplete CAPTCHA will never be solved public function get_error(): string
{
return '';
}
/**
* {@inheritDoc}
*/ */
public function is_solved(): bool public function is_solved(): bool
{ {
return false; return false;
} }
/**
* {@inheritDoc}
*/
public function get_attempt_count(): int
{
return 0;
}
} }

View file

@ -211,7 +211,7 @@ class legacy_wrapper implements plugin_interface
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function acp_page($id, $module): void public function acp_page(mixed $id, mixed $module): void
{ {
if (method_exists($this->legacy_captcha, 'acp_page')) if (method_exists($this->legacy_captcha, 'acp_page'))
{ {

View file

@ -122,5 +122,5 @@ interface plugin_interface
* @param mixed $module ACP module name * @param mixed $module ACP module name
* @return void * @return void
*/ */
public function acp_page($id, $module): void; public function acp_page(mixed $id, mixed $module): void;
} }

View file

@ -229,7 +229,7 @@ class turnstile extends base
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function acp_page($id, $module): void public function acp_page(mixed $id, mixed $module): void
{ {
$captcha_vars = [ $captcha_vars = [
'captcha_turnstile_sitekey' => 'CAPTCHA_TURNSTILE_SITEKEY', 'captcha_turnstile_sitekey' => 'CAPTCHA_TURNSTILE_SITEKEY',

View file

@ -11,11 +11,15 @@
* *
*/ */
use phpbb\captcha\plugins\confirm_type;
use phpbb\captcha\plugins\incomplete; use phpbb\captcha\plugins\incomplete;
use phpbb\config\config; use phpbb\config\config;
use phpbb\language\language;
use phpbb\request\request;
use phpbb\template\template; use phpbb\template\template;
use phpbb\user;
class phpbb_captcha_incomplete_test extends phpbb_test_case class phpbb_captcha_incomplete_test extends phpbb_database_test_case
{ {
protected config $config; protected config $config;
@ -32,21 +36,34 @@ class phpbb_captcha_incomplete_test extends phpbb_test_case
$this->assigned_vars = array_merge($this->assigned_vars, $vars); $this->assigned_vars = array_merge($this->assigned_vars, $vars);
} }
public function getDataSet()
{
return $this->createXMLDataSet(__DIR__ . '/../fixtures/empty.xml');
}
protected function setUp(): void protected function setUp(): void
{ {
global $phpbb_root_path, $phpEx; global $phpbb_root_path, $phpEx;
$this->config = new config([]); $this->config = new config([]);
$this->template = $this->getMockBuilder('\phpbb\template\twig\twig') $this->template = $this->getMockBuilder('\phpbb\template\twig\twig')
->setMethods(['assign_vars']) ->onlyMethods(['assign_vars'])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->template->method('assign_vars') $this->template->method('assign_vars')
->willReturnCallback([$this, 'assign_vars']); ->willReturnCallback([$this, 'assign_vars']);
$db = $this->new_dbal();
$language = $this->createMock(language::class);
$request = $this->createMock(request::class);
$user = $this->createMock(user::class);
$this->incomplete_captcha = new incomplete( $this->incomplete_captcha = new incomplete(
$this->config, $this->config,
$db,
$language,
$request,
$this->template, $this->template,
$user,
$phpbb_root_path, $phpbb_root_path,
$phpEx $phpEx
); );
@ -57,29 +74,25 @@ class phpbb_captcha_incomplete_test extends phpbb_test_case
$this->assertTrue($this->incomplete_captcha->is_available()); $this->assertTrue($this->incomplete_captcha->is_available());
$this->assertFalse($this->incomplete_captcha->is_solved()); $this->assertFalse($this->incomplete_captcha->is_solved());
$this->assertFalse($this->incomplete_captcha->validate()); $this->assertFalse($this->incomplete_captcha->validate());
$this->assertSame('CAPTCHA_INCOMPLETE', incomplete::get_name()); $this->assertFalse($this->incomplete_captcha->has_config());
$this->incomplete_captcha->init(0); $this->incomplete_captcha->set_name('foo');
$this->incomplete_captcha->execute(); $this->assertSame('CAPTCHA_INCOMPLETE', $this->incomplete_captcha->get_name());
$this->incomplete_captcha->execute_demo(); $this->incomplete_captcha->init(confirm_type::UNDEFINED);
$this->assertEmpty($this->assigned_vars); $this->assertEmpty($this->assigned_vars);
$this->assertEmpty($this->incomplete_captcha->get_demo_template(0)); $this->assertEmpty($this->incomplete_captcha->get_demo_template(0));
} $this->assertEmpty($this->incomplete_captcha->get_error());
$this->assertSame(0, $this->incomplete_captcha->get_attempt_count());
public function test_get_generator_class(): void
{
$this->expectException(\phpbb\exception\runtime_exception::class);
$this->incomplete_captcha->get_generator_class();
} }
public function test_get_tempate(): void public function test_get_tempate(): void
{ {
$this->incomplete_captcha->init(CONFIRM_REG); $this->incomplete_captcha->init(confirm_type::REGISTRATION);
$this->assertSame('captcha_incomplete.html', $this->incomplete_captcha->get_template()); $this->assertSame('captcha_incomplete.html', $this->incomplete_captcha->get_template());
$this->assertEquals('CONFIRM_INCOMPLETE', $this->assigned_vars['CONFIRM_LANG']); $this->assertEquals('CONFIRM_INCOMPLETE', $this->assigned_vars['CONFIRM_LANG']);
$this->assigned_vars = []; $this->assigned_vars = [];
$this->incomplete_captcha->init(CONFIRM_POST); $this->incomplete_captcha->init(confirm_type::POST);
$this->assertSame('captcha_incomplete.html', $this->incomplete_captcha->get_template()); $this->assertSame('captcha_incomplete.html', $this->incomplete_captcha->get_template());
$this->assertEquals('CONFIRM_INCOMPLETE', $this->assigned_vars['CONFIRM_LANG']); $this->assertEquals('CONFIRM_INCOMPLETE', $this->assigned_vars['CONFIRM_LANG']);
} }