Skip to content
Merged
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
33 changes: 21 additions & 12 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class listener implements EventSubscriberInterface
/** @var \phpbb\routing\router */
protected $router;

/** @var array|null Page route paths mapped to page IDs */
protected $page_route_ids;

/** @var \phpbb\template\template */
protected $template;

Expand All @@ -44,6 +41,9 @@ class listener implements EventSubscriberInterface
/** @var string phpEx */
protected $php_ext;

/** @var array|null Page route paths mapped to viewonline data */
protected $viewonline_pages;

/**
* Constructor
*
Expand Down Expand Up @@ -157,33 +157,42 @@ public function viewonline_page($event)
// phpBB versions. Router paths do not include the front controller.
$session_path = preg_replace('#^.*\.' . preg_quote($this->php_ext, '#') . '(?=/)#', '', $session_path);

if ($this->page_route_ids === null)
if ($this->viewonline_pages === null)
{
$this->page_route_ids = array();
$this->viewonline_pages = array();
foreach ($this->router->getRouteCollection()->all() as $route_name => $route)
{
if (strpos($route_name, 'phpbb_pages_dynamic_route_') === 0)
{
$this->page_route_ids[$route->getPath()] = (int) substr($route_name, strlen('phpbb_pages_dynamic_route_'));
$defaults = $route->getDefaults();
if (array_key_exists('_pages_title', $defaults)
&& array_key_exists('_pages_display', $defaults)
&& array_key_exists('_pages_display_to_guests', $defaults))
{
$this->viewonline_pages[$route->getPath()] = array(
'route_name' => $route_name,
'title' => utf8_decode_ncr($defaults['_pages_title']),
'page_display' => $defaults['_pages_display'],
'page_display_to_guests' => $defaults['_pages_display_to_guests'],
);
}
}
}
}

if (!isset($this->page_route_ids[$session_path]))
if (!isset($this->viewonline_pages[$session_path]))
{
return;
}

$page_id = $this->page_route_ids[$session_path];
$page_routes = $this->page_operator->get_page_routes();
$page = $page_routes[$page_id] ?? null;
if (!$page || (!$page['page_display_to_guests'] && !$this->user->data['is_registered']))
$page = $this->viewonline_pages[$session_path];
if (!$page['page_display'] || (!$page['page_display_to_guests'] && !$this->user->data['is_registered']))
{
return;
}

$this->lang->add_lang('pages_common', 'phpbb/pages');
$event['location'] = $this->lang->lang('PAGES_VIEWONLINE', $page['title']);
$event['location_url'] = $this->helper->route('phpbb_pages_dynamic_route_' . $page_id);
$event['location_url'] = $this->helper->route($page['route_name']);
}
}
30 changes: 0 additions & 30 deletions operators/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,36 +139,6 @@ public function delete_page($page_id)
return (bool) $this->db->sql_affectedrows();
}

/**
* Get page routes (for use in viewonline)
*
* @return array Array of routes, page titles, and guest visibility settings for published pages
* @access public
*/
public function get_page_routes()
{
$routes = array();

// Load published page routes, titles, and guest visibility settings
$sql = 'SELECT page_id, page_route, page_title, page_display_to_guests
FROM ' . $this->pages_table . '
WHERE page_display = 1';
$result = $this->db->sql_query($sql);

while ($row = $this->db->sql_fetchrow($result))
{
$routes[$row['page_id']] = array(
'route' => $row['page_route'],
'title' => utf8_decode_ncr($row['page_title']),
'page_display_to_guests' => $row['page_display_to_guests'],
);
}
$this->db->sql_freeresult($result);

// Return an array of published page routes
return $routes;
}

/**
* Get all custom page link icons (pages_*.gif)
* Added by the user to the core style/theme/images directories
Expand Down
8 changes: 0 additions & 8 deletions operators/page_interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ public function add_page($entity);
*/
public function delete_page($page_id);

/**
* Get page routes (for use in viewonline)
*
* @return array Array of routes, page titles, and guest visibility settings for published pages
* @access public
*/
public function get_page_routes();

/**
* Get all custom page link icons (pages_*.gif)
* Added by the user to the core style/theme/images directories
Expand Down
5 changes: 4 additions & 1 deletion routing/page_loader_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ public function load_routes(): RouteCollection
{
$collection = new RouteCollection();

$sql = 'SELECT page_id, page_route
$sql = 'SELECT page_id, page_route, page_title, page_display, page_display_to_guests
FROM ' . $this->pages_table;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$route = new Route('/' . $row['page_route']);
$route->setDefault('_controller', 'phpbb.pages.controller:display');
$route->setDefault('route', $row['page_route']);
$route->setDefault('_pages_title', $row['page_title']);
$route->setDefault('_pages_display', (bool) $row['page_display']);
$route->setDefault('_pages_display_to_guests', (bool) $row['page_display_to_guests']);
$collection->add('phpbb_pages_dynamic_route_' . $row['page_id'], $route);
}
$this->db->sql_freeresult($result);
Expand Down
13 changes: 10 additions & 3 deletions tests/event/event_listener_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class event_listener_base extends \phpbb_test_case
/** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\routing\router */
protected $router;

/** @var \Symfony\Component\Routing\RouteCollection */
protected $route_collection;

/** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\template\template */
protected $template;

Expand Down Expand Up @@ -65,10 +68,14 @@ protected function setUp(): void
->disableOriginalConstructor()
->getMock();

$route_collection = new \Symfony\Component\Routing\RouteCollection();
$route_collection->add('phpbb_pages_dynamic_route_1', new \Symfony\Component\Routing\Route('/test'));
$this->route_collection = new \Symfony\Component\Routing\RouteCollection();
$route = new \Symfony\Component\Routing\Route('/test');
$route->setDefault('_pages_title', '$location');
$route->setDefault('_pages_display', true);
$route->setDefault('_pages_display_to_guests', true);
$this->route_collection->add('phpbb_pages_dynamic_route_1', $route);
$this->router->method('getRouteCollection')
->willReturn($route_collection);
->willReturn($this->route_collection);
}

/**
Expand Down
64 changes: 44 additions & 20 deletions tests/event/event_listener_viewonline_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,6 @@ public function viewonline_page_data()
*/
public function test_viewonline_page($on_page, $row, $location_url, $location, $expected_location_url, $expected_location)
{
$this->page_operator->expects(self::atMost(1))
->method('get_page_routes')
->willReturn(array(
1 => array(
'route' => 'test',
'title' => $location,
'page_display_to_guests' => 1,
)
));

$this->controller_helper->expects(self::atMost(1))
->method('route')
->willReturnCallback(function ($route, array $params = array()) {
Expand Down Expand Up @@ -185,14 +175,10 @@ public function test_viewonline_page_visibility($is_registered, $is_admin, $page
{
$this->user->data['is_registered'] = $is_registered;
$this->auth->method('acl_get')->with('a_')->willReturn($is_admin);
$page_routes = $page_display ? array(
1 => array(
'route' => 'test',
'title' => 'Secret page title',
'page_display_to_guests' => $page_display_to_guests,
),
) : array();
$this->page_operator->method('get_page_routes')->willReturn($page_routes);
$route = $this->route_collection->get('phpbb_pages_dynamic_route_1');
$route->setDefault('_pages_title', 'Secret page title');
$route->setDefault('_pages_display', (bool) $page_display);
$route->setDefault('_pages_display_to_guests', (bool) $page_display_to_guests);
$this->controller_helper->method('route')->willReturn('page-url');

$event = new \phpbb\event\data(array(
Expand Down Expand Up @@ -228,9 +214,9 @@ public function test_viewonline_page_ignores_invalid_session_url()
self::assertSame('unchanged', $event['location']);
}

public function test_viewonline_page_ignores_route_missing_from_database()
public function test_viewonline_page_ignores_route_missing_metadata()
{
$this->page_operator->method('get_page_routes')->willReturn(array());
$this->route_collection->get('phpbb_pages_dynamic_route_1')->setDefaults(array());
$event = new \phpbb\event\data(array(
'row' => array('session_page' => 'app.php/test'),
'location' => 'unchanged',
Expand All @@ -241,4 +227,42 @@ public function test_viewonline_page_ignores_route_missing_from_database()

self::assertSame('unchanged', $event['location']);
}

public function test_viewonline_page_decodes_title_entities()
{
$this->route_collection->get('phpbb_pages_dynamic_route_1')
->setDefault('_pages_title', 'Café');
$this->controller_helper->method('route')->willReturn('page-url');
$event = new \phpbb\event\data(array(
'row' => array('session_page' => 'app.php/test'),
'location' => 'Index page',
'location_url' => 'index.php',
));

$this->get_listener()->viewonline_page($event);

self::assertSame($this->lang->lang('PAGES_VIEWONLINE', 'Café'), $event['location']);
}

public function test_viewonline_page_uses_route_metadata_for_repeated_entries()
{
$route = $this->route_collection->get('phpbb_pages_dynamic_route_1');
$route->setDefault('_pages_title', 'Test page');
$this->controller_helper->method('route')->willReturn('page-url');

$listener = $this->get_listener();
foreach (array(1, 2) as $unused)
{
$event = new \phpbb\event\data(array(
'row' => array('session_page' => 'app.php/test'),
'location' => 'Index page',
'location_url' => 'index.php',
));

$listener->viewonline_page($event);

self::assertSame($this->lang->lang('PAGES_VIEWONLINE', 'Test page'), $event['location']);
self::assertSame('page-url', $event['location_url']);
}
}
}
62 changes: 0 additions & 62 deletions tests/operators/page_operator_get_page_routes_test.php

This file was deleted.

21 changes: 21 additions & 0 deletions tests/routing/page_loader_core_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ public function test_routes_have_correct_paths($id, $expected)
self::assertSame($expected, $route->getPath());
}

public static function route_metadata_data()
{
return array(
array(1, 'title_1', true, true),
array(3, 'title_3', true, false),
array(4, 'title_4', false, false),
);
}

/**
* @dataProvider route_metadata_data
*/
public function test_routes_have_viewonline_metadata($id, $title, $display, $display_to_guests)
{
$route = $this->loader->load_routes()->get('phpbb_pages_dynamic_route_' . $id);

self::assertSame($title, $route->getDefault('_pages_title'));
self::assertSame($display, $route->getDefault('_pages_display'));
self::assertSame($display_to_guests, $route->getDefault('_pages_display_to_guests'));
}

public function test_supports_type()
{
self::assertTrue($this->loader->supports_type('phpbb_pages_route'));
Expand Down