[ticket/17535] Fix tests PHP deprecations and notices

PHPBB-17535
This commit is contained in:
rxu 2025-07-18 11:00:58 +07:00
parent 8a99024087
commit 902c99bc0c
No known key found for this signature in database
GPG key ID: 8117904FEDEFDD17
36 changed files with 193 additions and 111 deletions

View file

@ -556,6 +556,7 @@ class compress_tar extends compress
{
var $isgz = false;
var $isbz = false;
var $file = '';
var $filename = '';
var $mode = '';
var $type = '';

View file

@ -70,11 +70,11 @@ class deactivated_super_global implements \ArrayAccess, \Countable, \IteratorAgg
/**
* Redirects isset to the correct request class call.
*
* @param string $offset The key of the super global being accessed.
* @param mixed $offset The key of the super global being accessed.
*
* @return bool Whether the key on the super global exists.
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
return $this->request->is_set($offset, $this->super_global);
}
@ -82,17 +82,17 @@ class deactivated_super_global implements \ArrayAccess, \Countable, \IteratorAgg
/**#@+
* Part of the \ArrayAccess implementation, will always result in a FATAL error.
*/
public function offsetGet($offset): void
#[\ReturnTypeWillChange] public function offsetGet($offset): void
{
$this->error();
}
public function offsetSet($offset, $value): void
#[\ReturnTypeWillChange] public function offsetSet($offset, $value): void
{
$this->error();
}
public function offsetUnset($offset): void
#[\ReturnTypeWillChange] public function offsetUnset($offset): void
{
$this->error();
}

View file

@ -27,7 +27,7 @@ class user extends \phpbb\session
protected $language;
var $style = array();
var $date_format;
var $date_format = '';
/**
* DateTimeZone object holding the timezone of the user
@ -627,12 +627,12 @@ class user extends \phpbb\session
* Format user date
*
* @param int $gmepoch unix timestamp
* @param string|false $format date format in date() notation. | used to indicate relative dates, for example |d m Y|, h:i is translated to Today, h:i.
* @param string $format date format in date() notation. | used to indicate relative dates, for example |d m Y|, h:i is translated to Today, h:i.
* @param bool $forcedate force non-relative date format.
*
* @return mixed translated date
*/
function format_date($gmepoch, $format = false, $forcedate = false)
function format_date($gmepoch, $format = '', $forcedate = false)
{
global $phpbb_dispatcher;
static $utc;

View file

@ -21,9 +21,10 @@ class phpbb_console_command_cache_purge_test extends phpbb_test_case
{
protected $cache_dir;
protected $cache;
protected $config;
protected $db;
protected $db_tools;
protected $config;
protected $language;
protected $user;
protected function setUp(): void

View file

@ -23,6 +23,7 @@ class phpbb_console_command_thumbnail_test extends phpbb_database_test_case
protected $db;
protected $config;
protected $cache;
protected $language;
protected $user;
protected $storage;
protected $temp;

View file

@ -15,12 +15,14 @@ abstract class phpbb_console_user_base extends phpbb_database_test_case
{
protected $db;
protected $config;
protected $email;
protected $user;
protected $language;
protected $log;
protected $passwords_manager;
/** @var Symfony\Component\Console\Helper\QuestionHelper */
protected $question;
protected $command_name;
protected $user_loader;
protected $phpbb_root_path;
protected $php_ext;

View file

@ -15,21 +15,59 @@ require_once __DIR__ . '/../mock/lang.php';
class phpbb_datetime_from_format_test extends phpbb_test_case
{
/** @var \phpbb\language\language */
protected $lang;
/** @var \phpbb\user */
protected $user;
protected function setUp(): void
{
global $phpbb_root_path, $phpEx;
$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
$this->lang = new \phpbb\language\language($lang_loader);
// Set up language data for testing
$reflection_class = new ReflectionClass('\phpbb\language\language');
// Set default language files loaded flag to true
$common_language_files_loaded_flag = $reflection_class->getProperty('common_language_files_loaded');
$common_language_files_loaded_flag->setAccessible(true);
$common_language_files_loaded_flag->setValue($this->lang, true);
// Set up test language data
$lang_array = $reflection_class->getProperty('lang');
$lang_array->setAccessible(true);
$lang_array->setValue($this->lang, [
'datetime' => [
'TODAY' => 'Today',
'TOMORROW' => 'Tomorrow',
'YESTERDAY' => 'Yesterday',
'AGO' => [
0 => 'less than a minute ago',
1 => '%d minute ago',
2 => '%d minutes ago',
],
],
]);
$this->user = new \phpbb\user($this->lang, '\phpbb\datetime');
}
public static function from_format_data()
{
return array(
array(
return [
[
'UTC',
'Y-m-d',
'2012-06-08',
),
],
array(
[
'Europe/Berlin',
'Y-m-d H:i:s',
'2012-06-08 14:01:02',
),
);
],
];
}
/**
@ -37,25 +75,9 @@ class phpbb_datetime_from_format_test extends phpbb_test_case
*/
public function test_from_format($timezone, $format, $expected)
{
global $phpbb_root_path, $phpEx;
$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
$lang = new \phpbb\language\language($lang_loader);
$user = new \phpbb\user($lang, '\phpbb\datetime');
$user->timezone = new DateTimeZone($timezone);
$user->lang['datetime'] = array(
'TODAY' => 'Today',
'TOMORROW' => 'Tomorrow',
'YESTERDAY' => 'Yesterday',
'AGO' => array(
0 => 'less than a minute ago',
1 => '%d minute ago',
2 => '%d minutes ago',
),
);
$timestamp = $user->get_timestamp_from_format($format, $expected, new DateTimeZone($timezone));
$this->assertEquals($expected, $user->format_date($timestamp, $format, true));
$this->user->timezone = new DateTimeZone($timezone);
$timestamp = $this->user->get_timestamp_from_format($format, $expected, new DateTimeZone($timezone));
$this->assertEquals($expected, $this->user->format_date($timestamp, $format, true));
}
public static function relative_format_date_data()
@ -65,58 +87,54 @@ class phpbb_datetime_from_format_test extends phpbb_test_case
// So we use 18:01 in the morning and 06:01 in the afternoon.
$testing_time = gmdate('H') <= 12 ? '18:01' : '06:01';
return array(
array(
return [
[
gmdate('Y-m-d', time() + 2 * 86400) . ' ' . $testing_time, false,
gmdate('Y-m-d', time() + 2 * 86400) . ' ' . $testing_time,
),
array(
],
[
gmdate('Y-m-d', time() + 86400) . ' ' . $testing_time, false,
'Tomorrow ' . $testing_time,
),
array(
],
[
gmdate('Y-m-d', time() + 86400) . ' ' . $testing_time, true,
gmdate('Y-m-d', time() + 86400) . ' ' . $testing_time,
),
array(
],
[
gmdate('Y-m-d') . ' ' . $testing_time, false,
'Today ' . $testing_time,
),
array(
],
[
gmdate('Y-m-d') . ' ' . $testing_time, true,
gmdate('Y-m-d') . ' ' . $testing_time,
),
array(
],
[
gmdate('Y-m-d', time() - 86400) . ' ' . $testing_time, false,
'Yesterday ' . $testing_time,
),
array(
],
[
gmdate('Y-m-d', time() - 86400) . ' ' . $testing_time, true,
gmdate('Y-m-d', time() - 86400) . ' ' . $testing_time,
),
array(
],
[
gmdate('Y-m-d', time() - 2 * 86400) . ' ' . $testing_time, false,
gmdate('Y-m-d', time() - 2 * 86400) . ' ' . $testing_time,
),
],
// Test edge cases: Yesterday 00:00, Today 00:00, Tomorrow 00:00
array(
[
gmdate('Y-m-d', strtotime('yesterday')) . ' 00:00', false,
'Yesterday 00:00',
),
array(
],
[
gmdate('Y-m-d', strtotime('today')) . ' 00:00', false,
'Today 00:00',
),
array(
],
[
gmdate('Y-m-d', strtotime('tomorrow')) . ' 00:00', false,
'Tomorrow 00:00',
),
);
],
];
}
/**
@ -124,24 +142,8 @@ class phpbb_datetime_from_format_test extends phpbb_test_case
*/
public function test_relative_format_date($timestamp, $forcedate, $expected)
{
global $phpbb_root_path, $phpEx;
$lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx);
$lang = new \phpbb\language\language($lang_loader);
$user = new \phpbb\user($lang, '\phpbb\datetime');
$user->timezone = new DateTimeZone('UTC');
$user->lang['datetime'] = array(
'TODAY' => 'Today',
'TOMORROW' => 'Tomorrow',
'YESTERDAY' => 'Yesterday',
'AGO' => array(
0 => 'less than a minute ago',
1 => '%d minute ago',
2 => '%d minutes ago',
),
);
$timestamp = $user->get_timestamp_from_format('Y-m-d H:i', $timestamp, new DateTimeZone('UTC'));
$this->user->timezone = new DateTimeZone('UTC');
$timestamp = $this->user->get_timestamp_from_format('Y-m-d H:i', $timestamp, new DateTimeZone('UTC'));
/* This code is equal to the one from \phpbb\datetime function format()
* If the delta is less than or equal to 1 hour
@ -156,9 +158,9 @@ class phpbb_datetime_from_format_test extends phpbb_test_case
($delta >= -5 || (($now_ts/ 60) % 60) == (($timestamp / 60) % 60))
)
{
$expected = $user->lang(['datetime', 'AGO'], max(0, (int) floor($delta / 60)));
$expected = $this->lang->lang(['datetime', 'AGO'], max(0, (int) floor($delta / 60)));
}
$this->assertEquals($expected, $user->format_date($timestamp, '|Y-m-d| H:i', $forcedate));
$this->assertEquals($expected, $this->user->format_date($timestamp, '|Y-m-d| H:i', $forcedate));
}
}

View file

@ -13,6 +13,10 @@
class phpbb_dbal_migrator_tool_config_text_test extends phpbb_database_test_case
{
protected $db;
protected $config_text;
protected $tool;
public function getDataSet()
{
return $this->createXMLDataSet(__DIR__.'/fixtures/migrator_config_text.xml');

View file

@ -16,6 +16,11 @@ require_once __DIR__ . '/ext/foo/bar/ucp/ucp_test_info.php';
class phpbb_dbal_migrator_tool_module_test extends phpbb_database_test_case
{
protected $db;
protected $cache;
protected $user;
protected $tool;
public function getDataSet()
{
return $this->createXMLDataSet(__DIR__.'/fixtures/migrator_module.xml');

View file

@ -22,6 +22,12 @@ class phpbb_dbal_migrator_tool_permission_role_test extends phpbb_database_test_
/** @var \phpbb\db\migration\tool\permission */
protected $tool;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\cache\service */
protected $cache;
public $group_ids = [
'REGISTERED' => 2,
'GLOBAL_MODERATORS' => 4,

View file

@ -16,6 +16,12 @@ class phpbb_dbal_migrator_tool_permission_test extends phpbb_database_test_case
/** @var \phpbb\auth\auth */
protected $auth;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\cache\service */
protected $cache;
/** @var \phpbb\db\migration\tool\permission */
protected $tool;

View file

@ -19,6 +19,9 @@ class phpbb_email_parsing_test extends phpbb_test_case
/** @var \ReflectionProperty */
protected $reflection_template_property;
/** @var \phpbb\messenger\method\email */
protected $email;
protected function setUp(): void
{
global $phpbb_container, $config, $phpbb_root_path, $phpEx, $request, $user;

View file

@ -24,7 +24,7 @@ class phpbb_error_collector_test extends phpbb_test_case
public function test_collection()
{
$collector = new \phpbb\error_collector(E_ALL | E_STRICT); // php set_error_handler() default
$collector = new \phpbb\error_collector(E_ALL | E_NOTICE); // php set_error_handler() default
$collector->install();
// Cause a warning

View file

@ -16,6 +16,8 @@
*/
class phpbb_functional_jumpbox_test extends phpbb_functional_test_case
{
protected $crawler;
public function test_jumpbox()
{
$this->login();

View file

@ -37,6 +37,8 @@ class phpbb_messenger_method_base_test extends \phpbb_test_case
protected $twig_extensions_collection;
protected $twig_lexer;
protected $user;
protected $filesystem;
protected $symfony_request;
public function setUp(): void
{

View file

@ -37,6 +37,8 @@ class phpbb_messenger_method_email_test extends \phpbb_test_case
protected $twig_extensions_collection;
protected $twig_lexer;
protected $user;
protected $filesystem;
protected $symfony_request;
public function setUp(): void
{

View file

@ -18,6 +18,7 @@ use phpbb\messenger\queue;
class phpbb_messenger_queue_test extends phpbb_test_case
{
protected $config;
protected $cache_file;
protected $dispatcher;
protected $service_collection;

View file

@ -43,6 +43,9 @@ class migrations_check_config_added_test extends phpbb_test_case
/** @var string */
protected $php_ext;
/** @var string */
protected $schema_data;
protected function setUp(): void
{
global $phpbb_root_path;

View file

@ -15,6 +15,7 @@ class phpbb_migrator_convert_timezones_test extends phpbb_database_test_case
{
protected $db;
protected $db_doctrine;
protected $migration;
public function getDataSet()
{

View file

@ -13,6 +13,9 @@
class get_callable_from_step_test extends phpbb_database_test_case
{
protected $migrator;
protected $module_added;
protected function setUp(): void
{
global $phpbb_root_path, $php_ext, $table_prefix, $phpbb_log, $user;

View file

@ -13,6 +13,8 @@
class get_schema_steps_test extends phpbb_test_case
{
protected $helper;
protected function setUp(): void
{
parent::setUp();

View file

@ -21,9 +21,30 @@ require_once __DIR__ . '/../dbal/migration/dummy_order_5.php';
class schema_generator_test extends phpbb_test_case
{
/** @var \phpbb\config\config */
protected $config;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\db\tools\doctrine */
protected $db_tools;
/** @var \Doctrine\DBAL\Connection */
protected $doctrine_db;
/** @var \phpbb\db\migration\schema_generator */
protected $generator;
/** @var string */
protected $table_prefix;
/** @var string */
protected $phpbb_root_path;
/** @var string */
protected $php_ext;
protected function setUp(): void
{
global $phpbb_root_path, $phpEx;

View file

@ -27,6 +27,12 @@ class guesser_test extends \phpbb_test_case
protected $fileinfo_supported = false;
protected $guesser;
protected $guesser_no_fileinfo;
protected $path;
protected $jpg_file;
protected $phpbb_root_path;
protected function setUp(): void
{
global $phpbb_root_path;

View file

@ -25,6 +25,8 @@ class phpbb_mock_user
public $data = [];
public $lang = [];
public $ip = '';
public $module;
public $session_id;
private $options = array();
public function optionget($item, $data = false)

View file

@ -21,7 +21,7 @@ abstract class phpbb_tests_notification_base extends phpbb_database_test_case
{
/** @var phpbb_notification_manager_helper */
protected $notifications;
protected $db, $container, $user, $config, $auth, $cache;
protected $db, $container, $user, $config, $auth, $cache, $user_loader, $phpbb_dispatcher;
protected function get_notification_types()
{

View file

@ -14,7 +14,9 @@ require_once __DIR__ . '/../mock/sql_insert_buffer.php';
class phpbb_notification_convert_test extends phpbb_database_test_case
{
protected $notifications, $db, $doctrine_db, $container, $user, $config, $auth, $cache;
protected $db;
protected $doctrine_db;
protected $migration;
public function getDataSet()
{

View file

@ -19,8 +19,7 @@ require_once __DIR__ . '/../../phpBB/includes/functions_posting.php';
abstract class phpbb_notification_submit_post_base extends phpbb_database_test_case
{
// protected $notifications, $db, $container, $user, $config, $auth, $cache;
protected $db;
protected $item_type = '';
protected $poll_data = array();

View file

@ -15,6 +15,10 @@ require_once __DIR__ . '/../template/template_test_case.php';
class phpbb_pagination_pagination_test extends phpbb_template_template_test_case
{
protected $config;
protected $helper;
protected $routing_helper;
protected $pagination;
protected $test_path = 'tests/pagination';
public function return_callback_implode()

View file

@ -13,6 +13,9 @@
class phpbb_passwords_drivers_test extends \phpbb_test_case
{
/** @var \phpbb\passwords\driver\helper */
protected $driver_helper = [];
/** @var array */
protected $passwords_drivers = [];

View file

@ -34,10 +34,11 @@ class phpbb_profilefield_type_date_test extends phpbb_test_case
->will($this->returnCallback(array($this, 'create_datetime_callback')));
$this->user->timezone = new DateTimeZone('UTC');
$this->user->lang = array(
'datetime' => array(),
'DATE_FORMAT' => 'm/d/Y',
);
$this->user->expects($this->any())
->method('__get')->with('lang')->willReturn([
'datetime' => [],
'DATE_FORMAT' => 'm/d/Y',
]);
$request = $this->createMock('\phpbb\request\request');
$template = $this->createMock('\phpbb\template\template');

View file

@ -17,6 +17,7 @@ class phpbb_search_native_test extends phpbb_search_test_case
{
protected $db;
protected $db_tools;
protected $search;
public function getDataSet()
{

View file

@ -31,6 +31,7 @@ class phpbb_session_testable_factory
protected $config;
protected $cache;
protected $request;
protected $server_data;
/**
* Initialises the factory with a set of default config and cache values.

View file

@ -15,6 +15,9 @@ require_once __DIR__ . '/template_test_case.php';
class phpbb_template_allfolder_test extends phpbb_template_template_test_case
{
protected $extension_manager;
protected $ext_template_path;
public function test_allfolder()
{
$this->setup_engine_for_allfolder();

View file

@ -15,6 +15,8 @@ require_once __DIR__ . '/template_test_case.php';
class phpbb_template_template_events_test extends phpbb_template_template_test_case
{
protected $extension_manager;
public static function template_data()
{
return array(

View file

@ -60,14 +60,15 @@ class phpbb_text_processing_message_parser_test extends phpbb_test_case
);
$user->style = array('style_id' => 1);
$user->lang = array(
'NO_POLL_TITLE' => 'You have to enter a poll title.',
'POLL_TITLE_TOO_LONG' => 'The poll title must contain fewer than 100 characters.',
'POLL_TITLE_COMP_TOO_LONG' => 'The parsed size of your poll title is too large, consider removing BBCodes or smilies.',
'TOO_FEW_POLL_OPTIONS' => 'You must enter at least two poll options.',
'TOO_MANY_POLL_OPTIONS' => 'You have tried to enter too many poll options.',
'TOO_MANY_USER_OPTIONS' => 'You cannot specify more options per user than existing poll options.',
);
$user->expects($this->any())
-> method('__get')->with('lang')->willReturn([
'NO_POLL_TITLE' => 'You have to enter a poll title.',
'POLL_TITLE_TOO_LONG' => 'The poll title must contain fewer than 100 characters.',
'POLL_TITLE_COMP_TOO_LONG' => 'The parsed size of your poll title is too large, consider removing BBCodes or smilies.',
'TOO_FEW_POLL_OPTIONS' => 'You must enter at least two poll options.',
'TOO_MANY_POLL_OPTIONS' => 'You have tried to enter too many poll options.',
'TOO_MANY_USER_OPTIONS' => 'You cannot specify more options per user than existing poll options.',
]);
$phpbb_container = new phpbb_mock_container_builder;
$phpbb_container->set('user', $user);

View file

@ -70,6 +70,7 @@ class test_ucp_controller_webpush_test extends phpbb_database_test_case
$this->request = $this->createMock(\phpbb\request\request_interface::class);
$this->template = $this->createMock(\Twig\Environment::class);
$this->user = new \phpbb\user($this->language, '\phpbb\datetime');
$user = $this->user;
$this->user_loader = new \phpbb\user_loader($this->avatar_helper, $this->db, $this->phpbb_root_path, $this->php_ext, 'phpbb_users');
$this->path_helper = new \phpbb\path_helper($symfony_request, $this->request, $phpbb_root_path, $phpEx);
@ -216,9 +217,6 @@ class test_ucp_controller_webpush_test extends phpbb_database_test_case
'user_id' => 2,
'user_options' => 230271,
];
$this->user->lang = [
'GUEST' => 'Guest',
];
$json_response = $this->controller->notification();
@ -274,9 +272,6 @@ class test_ucp_controller_webpush_test extends phpbb_database_test_case
'user_id' => ANONYMOUS,
'user_options' => 230271,
];
$this->user->lang = [
'GUEST' => 'Guest',
];
$json_response = $this->controller->notification();
@ -332,9 +327,6 @@ class test_ucp_controller_webpush_test extends phpbb_database_test_case
'user_id' => ANONYMOUS,
'user_options' => 230271,
];
$this->user->lang = [
'GUEST' => 'Guest',
];
$this->expectException(http_exception::class);
$this->expectExceptionMessage('NO_AUTH_OPERATION');
@ -382,9 +374,6 @@ class test_ucp_controller_webpush_test extends phpbb_database_test_case
'user_id' => 2,
'user_options' => 230271,
];
$this->user->lang = [
'GUEST' => 'Guest',
];
$json_response = $this->controller->notification();