[ticket/701] Fix tests and move declarations to services file

B3P-701
This commit is contained in:
Marc Alexander
2021-09-04 17:15:12 +02:00
parent bca0afa86d
commit 9f374290ad
11 changed files with 54 additions and 55 deletions

View File

@@ -51,7 +51,9 @@ services:
- '@auth' - '@auth'
- '@config' - '@config'
- '@controller.helper' - '@controller.helper'
- '@dbal.conn'
- '@request' - '@request'
- '%tables.styles%'
board3.portal.columns: board3.portal.columns:
class: board3\portal\portal\columns class: board3\portal\portal\columns

View File

@@ -230,16 +230,9 @@ class helper
*/ */
public function assign_module_vars($row, $template_module) public function assign_module_vars($row, $template_module)
{ {
$use_fa = 0; $fa_styles = json_decode($this->config['board3_portal_fa_styles']);
$fa_styles = explode(';', $this->config['board3_portal_fa_styles']); $use_fa = !empty($fa_styles) && in_array($this->user->style['style_name'], $fa_styles);
foreach ($fa_styles as $fa_style)
{
if ($this->user->style['style_name'] == $fa_style)
{
$use_fa = 1;
break;
}
}
if (is_array($template_module)) if (is_array($template_module))
{ {
$this->template->assign_block_vars('modules_' . $this->portal_columns->number_to_string($row['module_column']), array( $this->template->assign_block_vars('modules_' . $this->portal_columns->number_to_string($row['module_column']), array(
@@ -250,8 +243,8 @@ class helper
'MODULE_ID' => $row['module_id'], 'MODULE_ID' => $row['module_id'],
'IMAGE_WIDTH' => $row['module_image_width'], 'IMAGE_WIDTH' => $row['module_image_width'],
'IMAGE_HEIGHT' => $row['module_image_height'], 'IMAGE_HEIGHT' => $row['module_image_height'],
'FA_ICON' => utf8_htmlspecialchars($row['module_fa_icon']), 'FA_ICON' => isset($row['module_fa_icon']) ? utf8_htmlspecialchars($row['module_fa_icon']) : '',
'FA_SIZE' => $row['module_fa_size'], 'FA_SIZE' => $row['module_fa_size'] ?? '',
'FA_ENABLED' => $use_fa, 'FA_ENABLED' => $use_fa,
)); ));
} }
@@ -264,8 +257,8 @@ class helper
'IMAGE_HEIGHT' => $row['module_image_height'], 'IMAGE_HEIGHT' => $row['module_image_height'],
'MODULE_ID' => $row['module_id'], 'MODULE_ID' => $row['module_id'],
'TITLE' => (isset($this->user->lang[$row['module_name']])) ? $this->user->lang[$row['module_name']] : utf8_normalize_nfc($row['module_name']), 'TITLE' => (isset($this->user->lang[$row['module_name']])) ? $this->user->lang[$row['module_name']] : utf8_normalize_nfc($row['module_name']),
'FA_ICON' => utf8_htmlspecialchars($row['module_fa_icon']), 'FA_ICON' => isset($row['module_fa_icon']) ? utf8_htmlspecialchars($row['module_fa_icon']) : '',
'FA_SIZE' => $row['module_fa_size'], 'FA_SIZE' => $row['module_fa_size'] ?? '',
'FA_ENABLED' => $use_fa, 'FA_ENABLED' => $use_fa,
)); ));
} }

View File

@@ -25,6 +25,9 @@ class modules_helper
*/ */
protected $config; protected $config;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\controller\helper Controller helper */ /** @var \phpbb\controller\helper Controller helper */
protected $controller_helper; protected $controller_helper;
@@ -34,6 +37,9 @@ class modules_helper
*/ */
protected $request; protected $request;
/** @var string Styles table */
protected $styles_table;
/** /**
* Constructor * Constructor
* NOTE: The parameters of this method must match in order and type with * NOTE: The parameters of this method must match in order and type with
@@ -41,14 +47,18 @@ class modules_helper
* @param \phpbb\auth\auth $auth Auth object * @param \phpbb\auth\auth $auth Auth object
* @param \phpbb\config\config $config phpBB config * @param \phpbb\config\config $config phpBB config
* @param \phpbb\controller\helper $controller_helper Controller helper * @param \phpbb\controller\helper $controller_helper Controller helper
* @param \phpbb\db\driver\driver_interface $db Dbal connection
* @param \phpbb\request\request $request phpBB request * @param \phpbb\request\request $request phpBB request
* @param string $styles_table Styles table
*/ */
public function __construct($auth, $config, $controller_helper, $request) public function __construct($auth, $config, $controller_helper, $db, $request, string $styles_table)
{ {
$this->auth = $auth; $this->auth = $auth;
$this->config = $config; $this->config = $config;
$this->controller_helper = $controller_helper; $this->controller_helper = $controller_helper;
$this->db = $db;
$this->request = $request; $this->request = $request;
$this->styles_table = $styles_table;
} }
/** /**
@@ -194,30 +204,27 @@ class modules_helper
* *
* @return string Select with available styles * @return string Select with available styles
*/ */
public function display_fa_styles() public function display_fa_styles(): string
{ {
global $phpbb_container; $portal_fa_styles = json_decode($this->config->offsetGet('board3_portal_fa_styles'));
$table_prefix = $phpbb_container->getParameter('core.table_prefix'); if (!$portal_fa_styles)
$db = $phpbb_container->get('dbal.conn'); {
$selected = explode(';', $this->config['board3_portal_fa_styles']); $portal_fa_styles = [];
}
$options = ''; $options = '';
$query = 'SELECT style_name $query = 'SELECT style_name
FROM ' . $table_prefix . 'styles'; FROM ' . $this->styles_table;
$result = $db->sql_query($query); $result = $this->db->sql_query($query);
while ($row = $db->sql_fetchrow($result)) while ($row = $this->db->sql_fetchrow($result))
{ {
$options .= '<option value="' . $row['style_name'] . '"'; $options .= '<option value="' . $row['style_name'] . '"';
foreach ($selected as $style) if (in_array($row['style_name'], $portal_fa_styles))
{
if ($style == $row['style_name'])
{ {
$options .= ' selected'; $options .= ' selected';
break;
}
} }
$options .= '>' . $row['style_name'] . '</option>'; $options .= '>' . $row['style_name'] . '</option>';
} }
$db->sql_freeresult($result); $this->db->sql_freeresult($result);
return '<select id="board3_fa_styles" name="board3_fa_styles[]" multiple>' . $options . '</select>'; return '<select id="board3_fa_styles" name="board3_fa_styles[]" multiple>' . $options . '</select>';
} }
@@ -226,15 +233,9 @@ class modules_helper
* *
* @param string $key Key of the parameter * @param string $key Key of the parameter
*/ */
public function store_fa_styles($key) public function store_fa_styles(string $key): void
{ {
$style_array = $this->request->variable($key, array('')); $style_array = $this->request->variable($key, ['']);
$styles = ''; $this->config->set('board3_portal_fa_styles', json_encode($style_array));
foreach ($style_array as $style)
{
$styles .= ($styles == '' ? '' : ';') . $style;
}
var_dump($styles);
$this->config->set('board3_portal_fa_styles', $styles);
} }
} }

View File

@@ -65,6 +65,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
$phpbb_container = new \phpbb_mock_container_builder(); $phpbb_container = new \phpbb_mock_container_builder();
// Mock module service collection // Mock module service collection
$config = new \phpbb\config\config([]); $config = new \phpbb\config\config([]);
$db = $this->db;
$auth = $this->getMockBuilder('\phpbb\auth\auth') $auth = $this->getMockBuilder('\phpbb\auth\auth')
->setMethods(['acl_get']) ->setMethods(['acl_get'])
->getMock(); ->getMock();
@@ -74,7 +75,7 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
->will($this->returnValue(true)); ->will($this->returnValue(true));
$controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx); $controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx);
$controller_helper->add_route('board3_portal_controller', 'portal'); $controller_helper->add_route('board3_portal_controller', 'portal');
$modules_helper = new \board3\portal\includes\modules_helper($auth, $config, $controller_helper, new \phpbb_mock_request); $modules_helper = new \board3\portal\includes\modules_helper($auth, $config, $controller_helper, $db, new \phpbb_mock_request, $table_prefix . 'styles');
$phpbb_container->set('board3.portal.module_collection', $phpbb_container->set('board3.portal.module_collection',
array( array(
new \board3\portal\modules\clock($config, $template), new \board3\portal\modules\clock($config, $template),
@@ -84,9 +85,10 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
)); ));
$this->portal_helper = new \board3\portal\includes\helper($phpbb_container->get('board3.portal.module_collection')); $this->portal_helper = new \board3\portal\includes\helper($phpbb_container->get('board3.portal.module_collection'));
$phpbb_container->set('board3.portal.helper', $this->portal_helper); $phpbb_container->set('board3.portal.helper', $this->portal_helper);
$phpbb_container->set('board3.portal.modules_helper', new \board3\portal\includes\modules_helper(new \phpbb\auth\auth(), $config, $controller_helper, $request)); $phpbb_container->set('board3.portal.modules_helper', $modules_helper);
$phpbb_container->setParameter('board3.portal.modules.table', $table_prefix . 'portal_modules'); $phpbb_container->setParameter('board3.portal.modules.table', $table_prefix . 'portal_modules');
$phpbb_container->setParameter('board3.portal.config.table', $table_prefix . 'portal_config'); $phpbb_container->setParameter('board3.portal.config.table', $table_prefix . 'portal_config');
$phpbb_container->setParameter('core.table_prefix', $table_prefix);
$this->portal_columns = new \board3\portal\portal\columns(); $this->portal_columns = new \board3\portal\portal\columns();
$phpbb_container->set('board3.portal.columns', $this->portal_columns); $phpbb_container->set('board3.portal.columns', $this->portal_columns);
$cache = $this->getMockBuilder('\phpbb\cache\service') $cache = $this->getMockBuilder('\phpbb\cache\service')
@@ -114,7 +116,6 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
->method('sql_save') ->method('sql_save')
->with($this->anything()) ->with($this->anything())
->will($this->returnArgument(2)); ->will($this->returnArgument(2));
$db = $this->db;
$this->language->set(array( $this->language->set(array(
'UNABLE_TO_MOVE' => 'UNABLE_TO_MOVE', 'UNABLE_TO_MOVE' => 'UNABLE_TO_MOVE',
'UNABLE_TO_MOVE_ROW' => 'UNABLE_TO_MOVE_ROW', 'UNABLE_TO_MOVE_ROW' => 'UNABLE_TO_MOVE_ROW',
@@ -208,6 +209,8 @@ class phpbb_acp_move_module_test extends \board3\portal\tests\testframework\data
'module_image_height' => '0', 'module_image_height' => '0',
'module_group_ids' => '', 'module_group_ids' => '',
'module_status' => '1', 'module_status' => '1',
'module_fa_icon' => '',
'module_fa_size' => '16',
), $module_data); ), $module_data);
} }

View File

@@ -43,6 +43,7 @@ class helper_test extends \board3\portal\tests\testframework\test_case
$this->language = new \board3\portal\tests\mock\language($this->language_file_loader); $this->language = new \board3\portal\tests\mock\language($this->language_file_loader);
$this->user = new \phpbb\user($this->language, '\phpbb\datetime'); $this->user = new \phpbb\user($this->language, '\phpbb\datetime');
$this->user->data['group_id'] = 2; $this->user->data['group_id'] = 2;
$this->user->style['style_name'] = 'prosilver';
$this->phpbb_root_path = dirname(__FILE__) . '/../../../../../../'; $this->phpbb_root_path = dirname(__FILE__) . '/../../../../../../';
$phpbb_extension_manager = new \phpbb_mock_extension_manager($this->phpbb_root_path, array('board3/portal')); $phpbb_extension_manager = new \phpbb_mock_extension_manager($this->phpbb_root_path, array('board3/portal'));
$this->language_file_loader->set_extension_manager($phpbb_extension_manager); $this->language_file_loader->set_extension_manager($phpbb_extension_manager);

View File

@@ -19,7 +19,7 @@ class phpbb_functions_fetch_news_test extends \board3\portal\tests\testframework
{ {
parent::setUp(); parent::setUp();
global $auth, $cache, $config, $phpbb_container, $phpbb_dispatcher, $template, $user, $phpbb_root_path, $phpEx; global $auth, $cache, $config, $phpbb_container, $phpbb_dispatcher, $template, $user, $phpbb_root_path, $phpEx, $table_prefix;
$config = new \phpbb\config\config([ $config = new \phpbb\config\config([
'allow_nocensors' => false, 'allow_nocensors' => false,
@@ -71,7 +71,7 @@ class phpbb_functions_fetch_news_test extends \board3\portal\tests\testframework
$phpbb_container = new \phpbb_mock_container_builder(); $phpbb_container = new \phpbb_mock_container_builder();
$controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx); $controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx);
$controller_helper->add_route('board3_portal_controller', 'portal'); $controller_helper->add_route('board3_portal_controller', 'portal');
$this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config, $controller_helper, $request); $this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config, $controller_helper, $this->db, $request, $table_prefix . 'styles');
$phpbb_container->set('board3.portal.modules_helper', $this->modules_helper); $phpbb_container->set('board3.portal.modules_helper', $this->modules_helper);
$phpbb_container->set('board3.portal.fetch_posts', new \board3\portal\portal\fetch_posts($auth, $cache, $this->config, $this->db, $this->modules_helper, $user)); $phpbb_container->set('board3.portal.fetch_posts', new \board3\portal\portal\fetch_posts($auth, $cache, $this->config, $this->db, $this->modules_helper, $user));
$template = $this->getMockBuilder('\phpbb\template') $template = $this->getMockBuilder('\phpbb\template')

View File

@@ -26,7 +26,7 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframe
public function setUp(): void public function setUp(): void
{ {
global $phpbb_root_path, $phpEx, $phpbb_dispatcher; global $phpbb_root_path, $phpEx, $phpbb_dispatcher, $table_prefix;
parent::setUp(); parent::setUp();
@@ -35,7 +35,6 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframe
$request = new \phpbb_mock_request(array('foo' => array('bar'))); $request = new \phpbb_mock_request(array('foo' => array('bar')));
$controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx); $controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx);
$controller_helper->add_route('board3_portal_controller', 'portal'); $controller_helper->add_route('board3_portal_controller', 'portal');
$phpbb_container = new \phpbb_mock_container_builder();
$phpbb_dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher') $phpbb_dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher')
->setMethods(['trigger_event']) ->setMethods(['trigger_event'])
->getMock(); ->getMock();
@@ -44,7 +43,7 @@ class board3_includes_modules_helper_test extends \board3\portal\tests\testframe
->with($this->anything()) ->with($this->anything())
->will($this->returnArgument(1)); ->will($this->returnArgument(1));
$this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config, $controller_helper, $request); $this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config, $controller_helper, $this->db, $request, $table_prefix . 'styles');
$phpbb_dispatcher = new \phpbb_mock_event_dispatcher(); $phpbb_dispatcher = new \phpbb_mock_event_dispatcher();
} }

View File

@@ -38,7 +38,7 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor
public function setUp(): void public function setUp(): void
{ {
parent::setUp(); parent::setUp();
global $cache, $config, $phpbb_root_path, $phpEx, $phpbb_dispatcher, $request, $user; global $cache, $config, $phpbb_root_path, $phpEx, $phpbb_dispatcher, $request, $user, $table_prefix;
$this->path_helper = new \phpbb\path_helper( $this->path_helper = new \phpbb\path_helper(
new \phpbb\symfony_request( new \phpbb\symfony_request(
@@ -65,7 +65,7 @@ class phpbb_unit_modules_calendar_test extends \board3\portal\tests\testframewor
$this->template = new \board3\portal\tests\mock\template($this); $this->template = new \board3\portal\tests\mock\template($this);
$controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx); $controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx);
$controller_helper->add_route('board3_portal_controller', 'portal'); $controller_helper->add_route('board3_portal_controller', 'portal');
$modules_helper = new \board3\portal\includes\modules_helper(new \phpbb\auth\auth(), new \phpbb\config\config(array()), $controller_helper, new \phpbb_mock_request()); $modules_helper = new \board3\portal\includes\modules_helper(new \phpbb\auth\auth(), new \phpbb\config\config(array()), $controller_helper, $this->db, new \phpbb_mock_request(), $table_prefix . 'styles');
$request = $this->request = new \phpbb_mock_request(); $request = $this->request = new \phpbb_mock_request();
$this->language_file_loader = new \phpbb\language\language_file_loader($phpbb_root_path, 'php'); $this->language_file_loader = new \phpbb\language\language_file_loader($phpbb_root_path, 'php');
$this->language = new \board3\portal\tests\mock\language($this->language_file_loader); $this->language = new \board3\portal\tests\mock\language($this->language_file_loader);

View File

@@ -22,7 +22,7 @@ class phpbb_portal_fetch_posts_test extends \board3\portal\tests\testframework\d
public function setUp(): void public function setUp(): void
{ {
global $auth, $cache, $config, $phpbb_dispatcher, $phpbb_root_path, $phpEx, $template, $user; global $auth, $cache, $config, $phpbb_dispatcher, $phpbb_root_path, $phpEx, $template, $user, $table_prefix;
parent::setUp(); parent::setUp();
@@ -76,7 +76,7 @@ class phpbb_portal_fetch_posts_test extends \board3\portal\tests\testframework\d
$this->auth = $auth; $this->auth = $auth;
$controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx); $controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx);
$controller_helper->add_route('board3_portal_controller', 'portal'); $controller_helper->add_route('board3_portal_controller', 'portal');
$this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config, $controller_helper, new phpbb_mock_request()); $this->modules_helper = new \board3\portal\includes\modules_helper($auth, $this->config, $controller_helper, $this->db, new phpbb_mock_request(), $table_prefix . 'styles');
$this->user = $user; $this->user = $user;
$template = $this->getMockBuilder('\phpbb\template') $template = $this->getMockBuilder('\phpbb\template')
->setMethods(['set_filenames', 'destroy_block_vars', 'assign_block_vars', 'assign_display']) ->setMethods(['set_filenames', 'destroy_block_vars', 'assign_block_vars', 'assign_display'])

View File

@@ -39,7 +39,7 @@ class modules_manager_confirm_box_test extends \board3\portal\tests\testframewor
public function setUp(): void public function setUp(): void
{ {
global $cache, $db, $portal_config, $phpbb_root_path, $phpEx; global $cache, $db, $portal_config, $phpbb_root_path, $phpEx, $table_prefix;
parent::setUp(); parent::setUp();
@@ -56,7 +56,7 @@ class modules_manager_confirm_box_test extends \board3\portal\tests\testframewor
$controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx); $controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx);
$controller_helper->add_route('board3_portal_controller', 'portal'); $controller_helper->add_route('board3_portal_controller', 'portal');
$modules_helper = new \board3\portal\includes\modules_helper($auth, $config, $controller_helper, $this->request); $modules_helper = new \board3\portal\includes\modules_helper($auth, $config, $controller_helper, $this->db, $this->request, $table_prefix . 'styles');
$this->portal_helper = new \board3\portal\includes\helper(array( $this->portal_helper = new \board3\portal\includes\helper(array(
new \board3\portal\modules\clock($config, null), new \board3\portal\modules\clock($config, null),

View File

@@ -33,7 +33,7 @@ class board3_portal_modules_manager_test extends \board3\portal\tests\testframew
public function setUp(): void public function setUp(): void
{ {
global $cache, $db, $phpbb_root_path, $phpEx; global $cache, $db, $phpbb_root_path, $phpEx, $table_prefix;
parent::setUp(); parent::setUp();
@@ -47,7 +47,7 @@ class board3_portal_modules_manager_test extends \board3\portal\tests\testframew
$controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx); $controller_helper = new \board3\portal\tests\mock\controller_helper($phpbb_root_path, $phpEx);
$controller_helper->add_route('board3_portal_controller', 'portal'); $controller_helper->add_route('board3_portal_controller', 'portal');
$modules_helper = new \board3\portal\includes\modules_helper($auth, $config, $controller_helper, $request); $modules_helper = new \board3\portal\includes\modules_helper($auth, $config, $controller_helper, $this->db, $request, $table_prefix . 'styles');
$portal_helper = new \board3\portal\includes\helper(array( $portal_helper = new \board3\portal\includes\helper(array(
new \board3\portal\modules\clock($config, null), new \board3\portal\modules\clock($config, null),