[ticket/17414] Extend unit test coverage for base class

PHPBB-17414
This commit is contained in:
Marc Alexander 2024-10-17 18:40:53 +02:00
parent 38ce4985f4
commit 1e80400d09
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
6 changed files with 130 additions and 14 deletions

View file

@ -65,7 +65,7 @@ class factory
function garbage_collect($name) function garbage_collect($name)
{ {
$captcha = $this->get_instance($name); $captcha = $this->get_instance($name);
$captcha->garbage_collect(0); $captcha->garbage_collect();
} }
/** /**

View file

@ -151,6 +151,7 @@ abstract class base implements plugin_interface
{ {
$this->code = gen_rand_string_friendly(CAPTCHA_MAX_CHARS); $this->code = gen_rand_string_friendly(CAPTCHA_MAX_CHARS);
$this->confirm_id = md5(unique_id()); $this->confirm_id = md5(unique_id());
$this->attempts = 0;
$sql = 'INSERT INTO ' . CONFIRM_TABLE . ' ' . $this->db->sql_build_array('INSERT', array( $sql = 'INSERT INTO ' . CONFIRM_TABLE . ' ' . $this->db->sql_build_array('INSERT', array(
'confirm_id' => $this->confirm_id, 'confirm_id' => $this->confirm_id,
@ -207,13 +208,13 @@ abstract class base implements plugin_interface
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function garbage_collect(int $confirm_type = 0): void public function garbage_collect(confirm_type $confirm_type = confirm_type::UNDEFINED): void
{ {
$sql = 'SELECT DISTINCT c.session_id $sql = 'SELECT DISTINCT c.session_id
FROM ' . CONFIRM_TABLE . ' c FROM ' . CONFIRM_TABLE . ' c
LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id) LEFT JOIN ' . SESSIONS_TABLE . ' s ON (c.session_id = s.session_id)
WHERE s.session_id IS NULL' . WHERE s.session_id IS NULL' .
((empty($type)) ? '' : ' AND c.confirm_type = ' . (int) $type); ((empty($confirm_type)) ? '' : ' AND c.confirm_type = ' . $confirm_type->value);
$result = $this->db->sql_query($sql); $result = $this->db->sql_query($sql);
if ($row = $this->db->sql_fetchrow($result)) if ($row = $this->db->sql_fetchrow($result))

View file

@ -198,11 +198,11 @@ class legacy_wrapper implements plugin_interface
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function garbage_collect(int $confirm_type = 0): void public function garbage_collect(confirm_type $confirm_type = confirm_type::UNDEFINED): void
{ {
if (method_exists($this->legacy_captcha, 'garbage_collect')) if (method_exists($this->legacy_captcha, 'garbage_collect'))
{ {
$this->legacy_captcha->garbage_collect($confirm_type); $this->legacy_captcha->garbage_collect($confirm_type->value);
} }
} }

View file

@ -110,10 +110,10 @@ interface plugin_interface
/** /**
* Garbage collect captcha plugin * Garbage collect captcha plugin
* *
* @param int $confirm_type Confirm type to garbage collect, defaults to all (0) * @param confirm_type $confirm_type Confirm type to garbage collect, defaults to all (0)
* @return void * @return void
*/ */
public function garbage_collect(int $confirm_type = 0): void; public function garbage_collect(confirm_type $confirm_type = confirm_type::UNDEFINED): void;
/** /**
* Display acp page * Display acp page

View file

@ -41,10 +41,6 @@ class phpbb_captcha_qa_test extends \phpbb_database_test_case
public function test_is_installed() public function test_is_installed()
{ {
$this->assertFalse($this->qa->is_installed());
$this->qa->install();
$this->assertTrue($this->qa->is_installed()); $this->assertTrue($this->qa->is_installed());
} }

View file

@ -62,7 +62,7 @@ class phpbb_captcha_turnstile_test extends \phpbb_database_test_case
{ {
// Mock the dependencies // Mock the dependencies
$this->config = $this->createMock(config::class); $this->config = $this->createMock(config::class);
$this->db = $this->createMock(driver_interface::class); $this->db = $this->new_dbal();
$this->language = $this->createMock(language::class); $this->language = $this->createMock(language::class);
$this->log = $this->createMock(log_interface::class); $this->log = $this->createMock(log_interface::class);
$this->request = $this->createMock(request::class); $this->request = $this->createMock(request::class);
@ -83,7 +83,7 @@ class phpbb_captcha_turnstile_test extends \phpbb_database_test_case
); );
} }
public function testIsAvailable(): void public function test_is_available(): void
{ {
// Test when both sitekey and secret are present // Test when both sitekey and secret are present
$this->config->method('offsetGet')->willReturnMap([ $this->config->method('offsetGet')->willReturnMap([
@ -97,9 +97,128 @@ class phpbb_captcha_turnstile_test extends \phpbb_database_test_case
]); ]);
$this->assertTrue($this->turnstile->is_available()); $this->assertTrue($this->turnstile->is_available());
$this->assertEquals(0, $this->turnstile->get_attempt_count());
} }
public function test_not_vailable(): void public function test_attempt_count_increase(): void
{
// Test when both sitekey and secret are present
$this->config->method('offsetGet')->willReturnMap([
['captcha_turnstile_sitekey', 'sitekey_value'],
['captcha_turnstile_secret', 'secret_value'],
]);
$this->request->method('variable')->willReturnMap([
['confirm_id', '', false, request_interface::REQUEST, 'confirm_id'],
['confirm_code', '', false, request_interface::REQUEST, 'confirm_code']
]);
$this->turnstile->init(confirm_type::REGISTRATION);
$this->assertFalse($this->turnstile->validate());
$confirm_id_reflection = new \ReflectionProperty($this->turnstile, 'confirm_id');
$confirm_id = $confirm_id_reflection->getValue($this->turnstile);
$this->request = $this->createMock(request::class);
$this->request->method('variable')->willReturnMap([
['confirm_id', '', false, request_interface::REQUEST, $confirm_id],
['confirm_code', '', false, request_interface::REQUEST, 'confirm_code']
]);
$this->turnstile = new turnstile(
$this->config,
$this->db,
$this->language,
$this->log,
$this->request,
$this->template,
$this->user
);
$this->turnstile->init(confirm_type::REGISTRATION);
$this->assertEquals(1, $this->turnstile->get_attempt_count());
// Run some garbage collection
$this->turnstile->garbage_collect(confirm_type::REGISTRATION);
// Start again at 0 after garbage collection
$this->turnstile->init(confirm_type::REGISTRATION);
$this->assertEquals(0, $this->turnstile->get_attempt_count());
}
public function test_reset(): void
{
// Test when both sitekey and secret are present
$this->config->method('offsetGet')->willReturnMap([
['captcha_turnstile_sitekey', 'sitekey_value'],
['captcha_turnstile_secret', 'secret_value'],
]);
$this->request->method('variable')->willReturnMap([
['confirm_id', '', false, request_interface::REQUEST, 'confirm_id'],
['confirm_code', '', false, request_interface::REQUEST, 'confirm_code']
]);
$this->turnstile->init(confirm_type::REGISTRATION);
$this->assertFalse($this->turnstile->validate());
$this->turnstile->reset();
$confirm_id_reflection = new \ReflectionProperty($this->turnstile, 'confirm_id');
$confirm_id = $confirm_id_reflection->getValue($this->turnstile);
$this->request = $this->createMock(request::class);
$this->request->method('variable')->willReturnMap([
['confirm_id', '', false, request_interface::REQUEST, $confirm_id],
['confirm_code', '', false, request_interface::REQUEST, 'confirm_code']
]);
$this->turnstile = new turnstile(
$this->config,
$this->db,
$this->language,
$this->log,
$this->request,
$this->template,
$this->user
);
$this->turnstile->init(confirm_type::REGISTRATION);
// Should be zero attempts since we reset the captcha
$this->assertEquals(0, $this->turnstile->get_attempt_count());
}
public function test_get_hidden_fields(): void
{
// Test when both sitekey and secret are present
$this->config->method('offsetGet')->willReturnMap([
['captcha_turnstile_sitekey', 'sitekey_value'],
['captcha_turnstile_secret', 'secret_value'],
]);
$this->request->method('variable')->willReturnMap([
['confirm_id', '', false, request_interface::REQUEST, 'confirm_id'],
['confirm_code', '', false, request_interface::REQUEST, 'confirm_code']
]);
$this->turnstile->init(confirm_type::REGISTRATION);
$this->assertFalse($this->turnstile->validate());
$this->turnstile->reset();
$confirm_id_reflection = new \ReflectionProperty($this->turnstile, 'confirm_id');
$confirm_id = $confirm_id_reflection->getValue($this->turnstile);
$this->assertEquals(
[
'confirm_id' => $confirm_id,
'confirm_code' => '',
],
$this->turnstile->get_hidden_fields(),
);
$this->assertEquals('CONFIRM_CODE_WRONG', $this->turnstile->get_error());
}
public function test_not_available(): void
{ {
$this->request->method('variable')->willReturnMap([ $this->request->method('variable')->willReturnMap([
['confirm_id', '', false, request_interface::REQUEST, 'confirm_id'], ['confirm_id', '', false, request_interface::REQUEST, 'confirm_id'],