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
5 changes: 5 additions & 0 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public function inject_frontend()
return;
}

if (!$this->consent_manager->has_optional_categories())
{
return;
}

$this->language->add_lang('common', 'phpbb/consentmanager');
$this->template->assign_vars($this->consent_manager->get_frontend_template_data(
$this->helper->route('phpbb_consentmanager_log_controller'),
Expand Down
17 changes: 14 additions & 3 deletions service/consent_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,15 @@ public function register($id, array $definition)
*/
public function get_frontend_template_data($log_url, $log_hash)
{
$payload = $this->build_frontend_payload($log_url, $log_hash);
$categories = $this->get_categories();
$has_optional_categories = $this->has_optional_categories();
$payload = $has_optional_categories ? $this->build_frontend_payload($log_url, $log_hash) : '';

return [
'S_CONSENTMANAGER_ENABLED' => true,
'S_CONSENTMANAGER_ENABLED' => $has_optional_categories,
'S_CONSENTMANAGER_ANALYTICS_ENABLED' => !empty($categories['analytics']['enabled']),
'S_CONSENTMANAGER_MARKETING_ENABLED' => !empty($categories['marketing']['enabled']),
'CONSENTMANAGER_PAYLOAD' => json_encode($payload, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT),
'CONSENTMANAGER_PAYLOAD' => $has_optional_categories ? json_encode($payload, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT) : '',
];
}

Expand Down Expand Up @@ -574,6 +575,16 @@ public function is_category_enabled($category)
return isset($categories[$category]) && $categories[$category]['enabled'];
}

/**
* Determine whether any optional consent categories are enabled.
*
* @return bool
*/
public function has_optional_categories()
{
return !empty($this->get_optional_category_ids($this->get_categories()));
}

/**
* Determine whether a category identifier is supported.
*
Expand Down
7 changes: 7 additions & 0 deletions service/consent_manager_interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,11 @@ public function get_version();
* @return bool
*/
public function is_category_enabled($category);

/**
* Determine whether any optional consent categories are enabled.
*
* @return bool
*/
public function has_optional_categories();
}
38 changes: 38 additions & 0 deletions tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function inject_frontend_assigns_template_payload_data()

/**
* @dataProvider inject_frontend_assigns_template_payload_data
* @runInSeparateProcess
*/
public function test_inject_frontend_assigns_template_payload($in_admin, $in_install)
{
Expand All @@ -74,6 +75,9 @@ public function test_inject_frontend_assigns_template_payload($in_admin, $in_ins
->with('common', 'phpbb/consentmanager');

$consent_manager = $this->createMock('\phpbb\consentmanager\service\consent_manager_interface');
$consent_manager->expects($invoke ? self::once() : self::never())
->method('has_optional_categories')
->willReturn(true);
$consent_manager->expects($invoke ? self::once() : self::never())
->method('get_frontend_template_data')
->with('/app.php/consent/log', generate_link_hash('phpbb.consentmanager.log'))
Expand Down Expand Up @@ -135,4 +139,38 @@ public function test_inject_frontend_assigns_template_payload($in_admin, $in_ins

$listener->inject_frontend();
}

public function test_inject_frontend_skips_category_blocks_when_frontend_disabled()
{
$helper = $this->createMock('\phpbb\controller\helper');
$helper->expects(self::never())
->method('route');

$this->language->expects(self::never())
->method('add_lang');

$consent_manager = $this->createMock('\phpbb\consentmanager\service\consent_manager_interface');
$consent_manager->expects(self::once())
->method('has_optional_categories')
->willReturn(false);
$consent_manager->expects(self::never())
->method('get_frontend_template_data');
$consent_manager->expects(self::never())
->method('get_frontend_category_data');

$template = $this->createMock('\phpbb\template\template');
$template->expects(self::never())
->method('assign_vars');
$template->expects(self::never())
->method('assign_block_vars');

$listener = new \phpbb\consentmanager\event\listener(
$helper,
$this->language,
$consent_manager,
$template
);

$listener->inject_frontend();
}
}
25 changes: 25 additions & 0 deletions tests/service/consent_manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function test_public_metadata_methods()
self::assertFalse($manager->is_supported_category('foobar'));
self::assertTrue($manager->is_category_enabled('analytics'));
self::assertFalse($manager->is_category_enabled('marketing'));
self::assertTrue($manager->has_optional_categories());

$categories = $manager->get_categories();
self::assertSame('necessary', $categories['necessary']['id']);
Expand Down Expand Up @@ -289,6 +290,30 @@ public function test_get_frontend_template_data_returns_json_payload()
self::assertArrayNotHasKey('services', $payload);
}

public function test_get_frontend_template_data_disables_frontend_without_optional_categories()
{
$manager = $this->get_manager(array(
'consentmanager_analytics_enabled' => 0,
'consentmanager_marketing_enabled' => 0,
));
$data = $manager->get_frontend_template_data('/app.php/consent/log', 'abc123');

self::assertFalse($data['S_CONSENTMANAGER_ENABLED']);
self::assertFalse($data['S_CONSENTMANAGER_ANALYTICS_ENABLED']);
self::assertFalse($data['S_CONSENTMANAGER_MARKETING_ENABLED']);
self::assertSame('', $data['CONSENTMANAGER_PAYLOAD']);
}

public function test_has_frontend_ui_returns_false_without_optional_categories()
{
$manager = $this->get_manager(array(
'consentmanager_analytics_enabled' => 0,
'consentmanager_marketing_enabled' => 0,
));

self::assertFalse($manager->has_optional_categories());
}

public function test_get_frontend_category_data_groups_services_by_enabled_category()
{
$dispatcher = $this->get_collect_registrations_dispatcher(function ($data) {
Expand Down
Loading