Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/epv-allowed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Error: Using addslashes in /controller/posting.php
Error: Using addslashes in /inc/functions_kb.php

# Released migration: converts the old serialized kb_extensions setting to
# JSON once. Released migrations must not be changed.
# JSON once, refusing objects (allowed_classes false). EPV flags any
# unserialize() call.
Error: Using unserialize in /migrations/version_2_0_1.php

# Released migrations without anything to revert: version_2_0_0 moves the
Expand Down
7 changes: 4 additions & 3 deletions controller/stylesheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ public function __construct(alternative_styles $alt_styles, request $request)
/**
* The stylesheet named by the "f" query parameter.
*
* @return Response The CSS, a 304 when the browser's copy is current, or a 404
* @return Response The CSS, a 304 when the browser's copy is current, or an empty 404
* (a stylesheet request is never shown, so it carries no text)
*/
public function serve(): Response
{
$path = $this->alt_styles->custom_file_path($this->request->variable('f', ''));
if ($path === null)
{
return new Response('Not found', 404, ['Content-Type' => 'text/plain; charset=UTF-8']);
return new Response('', 404, ['Content-Type' => 'text/plain; charset=UTF-8']);
}

$modified = (int) filemtime($path);
Expand All @@ -72,7 +73,7 @@ public function serve(): Response
$css = file_get_contents($path);

return $css === false
? new Response('Not found', 404, ['Content-Type' => 'text/plain; charset=UTF-8'])
? new Response('', 404, ['Content-Type' => 'text/plain; charset=UTF-8'])
: new Response($css, 200, $headers);
}
}
23 changes: 23 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -1666,3 +1666,26 @@ administrator in the first category, which fails with an SQL error on the old co
with two of the administrator's groups set to NEVER, the trace shows every row in words. Claude ran
the sweep (27 of 27) and `kb_form_access.js` (9 of 9) on the phpBB 3.3.19 and 4.0.0-a3-dev SQLite
boards.

## Link-box contrast, legacy migration steps and stand-alone tests - 11/12/2026

- The article link boxes used `#2e8b57` on white, 4.25:1; they now use `#237047`, about 6:1.
`tests/browser/kb_form_access.js` checks it (10 checks now); the check fails on the old colour.
- The stylesheet route answers a missing file with an empty 404 instead of English text.
- `version_1_0_0`'s `set_default_config()` listed every allowed file extension twice (once per
column of the query's row) and read undefined variables when no extension group was allowed. It
now lists each extension once. Its step for boards with an older settings table drops
`kb_config.is_dynamic` through phpBB's database tools, which also remove the column's default on
MSSQL first, instead of a plain `ALTER TABLE ... DROP COLUMN`.
- `version_2_0_1` unserializes the old `kb_extensions` setting with `allowed_classes => false`.
- `kb_alternative_style_test.php`, `kb_article_text_test.php`, `kb_board_search_test.php` and
`kb_recent_changes_test.php` used phpBB's `utf_tools.php` or `functions_kb.php` without loading
them, so they failed (or stopped silently, since `utf_tools.php` exits without `IN_PHPBB`) when
run alone. Every test file now passes on its own as well as in the suite.

`tests/kb_legacy_migration_test.php` runs the old-settings step on SQLite, MySQL and PostgreSQL and
checks that an object in the old setting is never created. Claude ran the PHPUnit suite on phpBB
3.3.x and master with the MySQL and PostgreSQL fixtures (586 tests, none skipped), every test file
alone, EPV on both branches and phpcs, and `kb_form_access.js` (10 of 10) and `kb_page_sweep.js`
(27 of 27) on phpBB 3.3.19 and 4.0.0-a3-dev SQLite boards.

32 changes: 9 additions & 23 deletions migrations/version_1_0_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,32 +347,14 @@ public function set_default_config()
WHERE e.group_id = g.group_id
AND g.allow_group = 1';
$result = $this->db->sql_query($sql);
// Each allowed extension once, under its group's name
$xt = [];
while ($row = $this->db->sql_fetchrow($result))
{
$extensions[] = $row;
$xt[$row['group_name']][] = $row['extension'];
}
$this->db->sql_freeresult($result);

foreach ($extensions as $extension)
{
$names[] = $extension['group_name'];
}
$names = array_values(array_unique($names));

foreach ($extensions as $extension)
{
foreach ($names as $name)
{
foreach ($extension as $ext)
{
if ($extension['group_name'] == $name)
{
$xt[$name][] = $extension['extension'];
}
}
}
}

$extensions = serialize($xt);

$sql = 'SELECT config_value
Expand Down Expand Up @@ -407,8 +389,12 @@ public function set_default_config()
}
else
{
$sql = 'ALTER TABLE ' . $this->table_prefix . 'kb_config DROP COLUMN is_dynamic';
$this->db->sql_query($sql);
// phpBB's database tools, which also drop the column's default
// constraint first on MSSQL, where a plain DROP COLUMN fails
if ($this->db_tools->sql_column_exists($this->table_prefix . 'kb_config', 'is_dynamic'))
{
$this->db_tools->sql_column_remove($this->table_prefix . 'kb_config', 'is_dynamic');
}
// MySQL-specific CHANGE/CHARACTER SET syntax - only meaningful (and
// only valid) on a MySQL-family driver; other DBMS keep whatever
// collation the column already had, which is harmless here.
Expand Down
3 changes: 2 additions & 1 deletion migrations/version_2_0_1.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function update_data()

if (!str_starts_with($extensions, '{'))
{
$extensions = json_encode(unserialize($extensions));
// The old setting holds only arrays and strings, so objects are refused
$extensions = json_encode(unserialize($extensions, ['allowed_classes' => false]));
}

return [
Expand Down
3 changes: 2 additions & 1 deletion styles/all/theme/phpbbmodders_knowledgebase.css
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ dd.action {

.kb_codebox code {
font: 105% Monaco, "Andale Mono", "Courier New", Courier, monospace;
color: #2e8b57;
/* A darker green than prosilver's #2e8b57 (4.25:1 on white), which passes WCAG AA at 6:1 */
color: #237047;
display: inline-block;
overflow: auto;
margin-bottom: -5px;
Expand Down
16 changes: 15 additions & 1 deletion tests/browser/kb_form_access.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
// - the custom-field filter and the "drafts only" box have a Go button when
// JavaScript is off (both normally submit on change);
// - no field on the add-article form, nor the ACP field type menu, has more than one label;
// - the inactive tabs' text passes WCAG AA contrast (4.5:1) on its background;
// - the inactive tabs' text and the article link boxes pass WCAG AA contrast
// (4.5:1) on their background;
// - article title links and the queue's buttons and menus are at least 24px
// high on a phone.
// Read-only: it submits nothing but the login forms.
Expand Down Expand Up @@ -109,6 +110,19 @@ function contrast(fore, back) {
const weak = tabs.filter(([, fore, back]) => !/rgba\(0, 0, 0, 0\)/.test(back) && contrast(fore, back) < 4.5);
record('inactive tabs pass 4.5:1 contrast', tabs.length > 0 && weak.length === 0, weak.map(([text]) => text).join(', '));

// The link boxes on an article: their text on the nearest painted background
await js.goto(`${base}/app.php/knowledgebase/article?k=1`, ready);
const boxes = await js.locator('.kb_codebox code').evaluateAll(codes => codes.map(code => {
let element = code, back = 'rgba(0, 0, 0, 0)';
while (element && /rgba\(0, 0, 0, 0\)|transparent/.test(back)) {
back = getComputedStyle(element).backgroundColor;
element = element.parentElement;
}
return [getComputedStyle(code).color, back];
}));
const faint = boxes.filter(([fore, back]) => contrast(fore, back) < 4.5);
record('article link boxes pass 4.5:1 contrast', boxes.length > 0 && faint.length === 0, faint.map(([fore]) => fore).join(', '));

// Touch targets on a phone: article title links, and the queue's buttons and menus
await js.setViewportSize({width: 375, height: 812});
const small = [];
Expand Down
3 changes: 3 additions & 0 deletions tests/kb_alternative_style_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace phpbbmodders\knowledgebase\controller;

require_once __DIR__ . '/kb_extensions_test.php';
if (!defined('IN_PHPBB')) define('IN_PHPBB', true);
require_once (getenv('PHPBB_ROOT_PATH') ?: dirname(__DIR__, 4)) . '/includes/utf/utf_tools.php';
require_once dirname(__DIR__) . '/inc/alternative_styles.php';
require_once dirname(__DIR__) . '/controller/stylesheet.php';
require_once dirname(__DIR__) . '/event/listener.php';
Expand Down Expand Up @@ -344,6 +346,7 @@ public function test_nothing_unsafe_is_ever_served(string $name)

$this->assertSame(404, $response->getStatusCode());
$this->assertStringNotContainsString('secret', $response->getContent());
$this->assertSame('', $response->getContent());
$this->assertSame('text/plain; charset=UTF-8', $response->headers->get('Content-Type'));
}

Expand Down
2 changes: 2 additions & 0 deletions tests/kb_article_text_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace phpbbmodders\knowledgebase\textformatter;

require_once __DIR__ . '/kb_extensions_test.php';
if (!defined('IN_PHPBB')) define('IN_PHPBB', true);
require_once (getenv('PHPBB_ROOT_PATH') ?: dirname(__DIR__, 4)) . '/includes/utf/utf_tools.php';
require_once dirname(__DIR__) . '/textformatter/article_text.php';

if (!defined('OPTION_FLAG_BBCODE')) define('OPTION_FLAG_BBCODE', 1);
Expand Down
2 changes: 2 additions & 0 deletions tests/kb_board_search_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace phpbbmodders\knowledgebase;

require_once __DIR__ . '/kb_extensions_test.php';
if (!defined('IN_PHPBB')) define('IN_PHPBB', true);
require_once (getenv('PHPBB_ROOT_PATH') ?: dirname(__DIR__, 4)) . '/includes/utf/utf_tools.php';
require_once dirname(__DIR__) . '/inc/functions_kb.php';
require_once dirname(__DIR__) . '/inc/board_search.php';
require_once dirname(__DIR__) . '/textformatter/article_text.php';
Expand Down
158 changes: 158 additions & 0 deletions tests/kb_legacy_migration_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
/**
*
* Knowledge Base extension for the phpBB Forum Software package
*
* @copyright (c) 2026, phpBB Modders, https://www.phpbbmodders.com/
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace phpbbmodders\knowledgebase\migrations;

require_once __DIR__ . '/kb_extensions_test.php';
require_once dirname(__DIR__) . '/migrations/version_1_0_0.php';
require_once dirname(__DIR__) . '/migrations/version_2_0_1.php';
if (!defined('CONFIG_TEXT_TABLE')) define('CONFIG_TEXT_TABLE', 'phpbb_config_text');

/**
* Two steps for boards that still hold an older Knowledge Base's settings.
*
* version_1_0_0 drops the old kb_config table's is_dynamic column through
* phpBB's database tools, which work on every database (a plain DROP COLUMN
* fails on MSSQL while the column has a default). version_2_0_1 converts the
* old serialized kb_extensions setting to JSON without creating any object.
*/
class kb_legacy_migration_test extends \PHPUnit\Framework\TestCase
{
private $db;
private $tools;
private $sqlite_fixture;

protected function tearDown(): void
{
if ($this->tools instanceof \phpbb\db\tools\doctrine) $this->tools->get_connection()->close();
if ($this->db) $this->db->sql_close();
if ($this->sqlite_fixture) @unlink($this->sqlite_fixture);
}

public function engines()
{
return [['sqlite'], ['mysql'], ['postgres']];
}

private function connect(string $engine): void
{
if ($engine === 'sqlite')
{
$this->db = new \phpbb\db\driver\sqlite3();
if (class_exists(\phpbb\db\tools\doctrine::class))
{
$this->sqlite_fixture = tempnam(sys_get_temp_dir(), 'kb-legacy-');
$this->assertTrue($this->db->sql_connect($this->sqlite_fixture, '', '', ''));
$this->tools = new \phpbb\db\tools\doctrine(\phpbb\db\doctrine\connection_factory::get_connection_from_params('sqlite3', $this->sqlite_fixture));
$this->tools->set_table_prefix('phpbb_');
}
else
{
$this->assertTrue($this->db->sql_connect(':memory:', '', '', ''));
$this->tools = new \phpbb\db\tools\sqlite3($this->db);
}
return;
}

$port = getenv('KB_TEST_' . strtoupper($engine) . '_PORT');
if (!$port) $this->markTestSkipped('Set the fixture database port to run integration tests');
$this->assertTrue(ctype_digit($port));
$this->db = $engine === 'mysql' ? new \phpbb\db\driver\mysqli() : new \phpbb\db\driver\postgres();
$this->assertIsObject($this->db->sql_connect('127.0.0.1', $engine === 'mysql' ? 'root' : 'postgres', '', 'kb_fixture', (int) $port));
if (class_exists(\phpbb\db\tools\doctrine::class))
{
$this->tools = new \phpbb\db\tools\doctrine(\phpbb\db\doctrine\connection_factory::get_connection_from_params(
$engine === 'mysql' ? 'mysqli' : 'postgres', '127.0.0.1', $engine === 'mysql' ? 'root' : 'postgres', '', 'kb_fixture', $port
));
$this->tools->set_table_prefix('phpbb_');
}
else
{
$this->tools = $engine === 'mysql' ? new \phpbb\db\tools\tools($this->db) : new \phpbb\db\tools\postgres($this->db);
}
}

/** @dataProvider engines */
public function test_the_old_settings_table_loses_is_dynamic_and_gets_the_new_settings($engine)
{
$this->connect($engine);
$tables = ['phpbb_kb_config', 'phpbb_extensions', 'phpbb_extension_groups', 'phpbb_config'];
foreach ($tables as $table)
{
if ($this->tools->sql_table_exists($table)) $this->tools->sql_table_drop($table);
}

// The tables set_default_config() reads, as an older Knowledge Base left them
$this->tools->sql_create_table('phpbb_extension_groups', ['COLUMNS' => ['group_id' => ['UINT', 0], 'group_name' => ['VCHAR', ''], 'allow_group' => ['BOOL', 0]]]);
$this->tools->sql_create_table('phpbb_extensions', ['COLUMNS' => ['group_id' => ['UINT', 0], 'extension' => ['VCHAR:100', '']]]);
$this->tools->sql_create_table('phpbb_config', ['COLUMNS' => ['config_name' => ['VCHAR', ''], 'config_value' => ['VCHAR_UNI', '']]]);
$this->tools->sql_create_table('phpbb_kb_config', ['COLUMNS' => [
'config_name' => ['VCHAR', ''],
'config_value' => ['TEXT_UNI', ''],
'is_dynamic' => ['BOOL', 0],
]]);
$this->db->sql_query("INSERT INTO phpbb_extension_groups (group_id, group_name, allow_group) VALUES (1, 'IMAGES', 1)");
$this->db->sql_query("INSERT INTO phpbb_extensions (group_id, extension) VALUES (1, 'png')");
$this->db->sql_query("INSERT INTO phpbb_config (config_name, config_value) VALUES ('max_filesize', '262144')");
$this->db->sql_query("INSERT INTO phpbb_kb_config (config_name, config_value, is_dynamic) VALUES ('anounce', '1', 0)");

$migration = new version_1_0_0(new \phpbb\config\config([]), $this->db, $this->tools, '', 'php', 'phpbb_', []);
$migration->set_default_config();

$this->assertFalse($this->tools->sql_column_exists('phpbb_kb_config', 'is_dynamic'));
$result = $this->db->sql_query('SELECT config_name, config_value FROM phpbb_kb_config');
$settings = [];
while ($row = $this->db->sql_fetchrow($result))
{
$settings[$row['config_name']] = $row['config_value'];
}
$this->db->sql_freeresult($result);
$this->assertSame('1', $settings['anounce']);
$this->assertSame('262144', $settings['max_filesize']);
$this->assertSame(['IMAGES' => ['png']], unserialize($settings['extensions']));

foreach ($tables as $table)
{
$this->tools->sql_table_drop($table);
}
}

public function test_the_serialized_extensions_setting_creates_no_object()
{
kb_legacy_wakeup_probe::$woken = false;
$stored = serialize(['IMAGES' => ['png', 'gif'], 'evil' => new kb_legacy_wakeup_probe()]);

$db = $this->createMock(\phpbb\db\driver\driver_interface::class);
$db->method('sql_query')->willReturn(true);
$db->method('sql_fetchrow')->willReturn(['config_value' => $stored]);
$migration = new version_2_0_1(new \phpbb\config\config([]), $db, $this->createMock(\phpbb\db\tools\tools_interface::class), '', 'php', 'phpbb_', []);

$steps = $migration->update_data();

$this->assertFalse(kb_legacy_wakeup_probe::$woken);
$this->assertSame('config_text.update', $steps[0][0]);
$converted = json_decode($steps[0][1][1], true);
$this->assertSame(['png', 'gif'], $converted['IMAGES']);
$this->assertArrayNotHasKey('woken', $converted['evil']);
}
}

/**
* A class whose __wakeup() records that unserialize() created it
*/
class kb_legacy_wakeup_probe
{
public static $woken = false;

public function __wakeup()
{
self::$woken = true;
}
}
1 change: 1 addition & 0 deletions tests/kb_recent_changes_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace phpbbmodders\knowledgebase;

require_once __DIR__ . '/kb_extensions_test.php';
require_once dirname(__DIR__) . '/inc/functions_kb.php';
require_once dirname(__DIR__) . '/inc/feed.php';
require_once dirname(__DIR__) . '/migrations/version_2_0_29.php';
require_once dirname(__DIR__) . '/controller/recent_changes.php';
Expand Down
Loading