Commit fd0149eb authored by Tino Goratsch's avatar Tino Goratsch

rework and fix the inner workings of the Title class

parent 9f179842
......@@ -237,21 +237,51 @@ class Title
new GetSiteAndPageTitleBeforeEvent($this)
);
$title = !empty($this->getMetaTitle()) ? $this->getMetaTitle() : $this->getPageTitle();
return $this->renderPageTitlePrefix()
. $this->renderPageTitle()
. $this->renderPageTitleSuffix()
. $this->renderSiteTitle()
. $this->renderSiteSubTitle();
}
protected function renderPageTitle(): string
{
return !empty($this->getMetaTitle()) ? $this->getMetaTitle() : $this->getPageTitle();
}
protected function renderPageTitlePrefix(): string
{
if (!empty($this->pageTitlePrefix)) {
$title = $this->pageTitlePrefix . $this->getPageTitleSeparator() . $title;
return $this->pageTitlePrefix . $this->getPageTitleSeparator();
}
return '';
}
protected function renderPageTitleSuffix(): string
{
if (!empty($this->getPageTitlePostfix())) {
$title .= $this->getPageTitleSeparator() . $this->getPageTitlePostfix();
return $this->getPageTitleSeparator() . $this->getPageTitlePostfix();
}
return '';
}
protected function renderSiteTitle(): string
{
if (!empty($this->getSiteTitle())) {
$title .= $this->getSiteTitleSeparator() . $this->getSiteTitle();
return $this->getSiteTitleSeparator() . $this->getSiteTitle();
}
return '';
}
protected function renderSiteSubTitle(): string
{
if (!empty($this->getSiteSubtitle())) {
$title .= $this->getPageTitleSeparator() . $this->getSiteSubtitle();
return $this->getPageTitleSeparator() . $this->getSiteSubtitle();
}
return $title;
return '';
}
}
......@@ -49,7 +49,7 @@ class TitleTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('Foo', $this->title->getSiteAndPageTitle());
}
protected function setUpStepsExpectations($callCount)
protected function setUpStepsExpectations(int $callCount)
{
$steps = [
[
......
......@@ -48,27 +48,6 @@ class Title extends \ACP3\Core\Breadcrumb\Title
$this->request = $request;
}
/**
* {@inheritdoc}
*/
public function getSiteTitle()
{
if (empty(parent::getSiteTitle())) {
$this->addSiteTitle();
}
return parent::getSiteTitle();
}
private function addSiteTitle()
{
$settings = $this->getSettings();
if (isset($settings['site_title'])) {
$this->setSiteTitle($settings['site_title']);
}
}
/**
* @return array
*/
......@@ -81,27 +60,6 @@ class Title extends \ACP3\Core\Breadcrumb\Title
return $this->systemSettings;
}
/**
* {@inheritdoc}
*/
public function getSiteSubtitle()
{
if (parent::getSiteSubtitle() === null) {
$this->addSiteSubtitle();
}
return parent::getSiteSubtitle();
}
private function addSiteSubtitle()
{
$settings = $this->getSettings();
if ($this->allowSystemSubtitle() && !empty($settings['site_subtitle'])) {
$this->setSiteSubtitle($settings['site_subtitle']);
}
}
public function getSiteAndPageTitle()
{
$settings = $this->getSettings();
......@@ -122,10 +80,17 @@ class Title extends \ACP3\Core\Breadcrumb\Title
return parent::getSiteAndPageTitle();
}
private function allowSystemSubtitle()
private function allowSystemSubtitle(): bool
{
$settings = $this->getSettings();
return $this->getSettings()['site_subtitle_mode'] != 3;
}
protected function renderSiteSubTitle(): string
{
if ($this->allowSystemSubtitle()) {
return parent::renderSiteSubTitle();
}
return $settings['site_subtitle_mode'] != 3;
return '';
}
}
<?php
/**
* Copyright (c) by the ACP3 Developers.
* See the LICENSE file at the top-level module directory for licensing details.
*/
namespace ACP3\Modules\ACP3\System\Core\Breadcrumb;
use ACP3\Core\Settings\SettingsInterface;
use ACP3\Modules\ACP3\System\Installer\Schema;
class TitleConfigurator
{
/**
* @var \ACP3\Core\Settings\SettingsInterface
*/
private $settings;
public function __construct(SettingsInterface $settings)
{
$this->settings = $settings;
}
public function configure(Title $title): void
{
$settings = $this->settings->getSettings(Schema::MODULE_NAME);
$title->setSiteTitle($settings['site_title']);
$title->setSiteSubtitle($settings['site_subtitle']);
}
}
......@@ -6,3 +6,9 @@ services:
- '@core.breadcrumb'
- '@core.event_dispatcher'
- '@core.config'
configurator: 'core.breadcrumb.title_configurator:configure'
core.breadcrumb.title_configurator:
class: ACP3\Modules\ACP3\System\Core\Breadcrumb\TitleConfigurator
arguments:
- '@core.config'
......@@ -10,6 +10,7 @@ namespace ACP3\Modules\ACP3\System\Test\Core\Breadcrumb;
use ACP3\Core\Http\Request;
use ACP3\Core\Settings\SettingsInterface;
use ACP3\Modules\ACP3\System\Core\Breadcrumb\Title;
use ACP3\Modules\ACP3\System\Core\Breadcrumb\TitleConfigurator;
class TitleTest extends \ACP3\Core\Test\Breadcrumb\TitleTest
{
......@@ -17,6 +18,10 @@ class TitleTest extends \ACP3\Core\Test\Breadcrumb\TitleTest
* @var Title
*/
protected $title;
/**
* @var TitleConfigurator
*/
private $titleConfigurator;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
......@@ -29,6 +34,7 @@ class TitleTest extends \ACP3\Core\Test\Breadcrumb\TitleTest
protected function setUp()
{
$this->initializeMockObjects();
$this->setUpTitleConfigurator();
$this->title = new Title(
$this->requestMock,
......@@ -46,11 +52,17 @@ class TitleTest extends \ACP3\Core\Test\Breadcrumb\TitleTest
$this->configMock = $this->createMock(SettingsInterface::class);
}
private function setUpTitleConfigurator()
{
$this->titleConfigurator = new TitleConfigurator($this->configMock);
}
public function testGetSiteAndPageTitleWithNoCustomSiteTitle()
{
$this->setUpStepsExpectations(1);
$this->setUpConfigMockExpectations('SEO Title', '', 1, 0);
$this->titleConfigurator->configure($this->title);
$this->assertEquals('Foo | SEO Title', $this->title->getSiteAndPageTitle());
}
......@@ -61,9 +73,13 @@ class TitleTest extends \ACP3\Core\Test\Breadcrumb\TitleTest
* @param int $subtitleMode
* @param int $subtitleHomepageMode
*/
private function setUpConfigMockExpectations($siteTitle, $siteSubtitle, $subtitleMode, $subtitleHomepageMode)
private function setUpConfigMockExpectations(
string $siteTitle,
string $siteSubtitle,
int $subtitleMode,
int $subtitleHomepageMode)
{
$this->configMock->expects($this->once())
$this->configMock->expects($this->atLeastOnce())
->method('getSettings')
->with('system')
->willReturn([
......@@ -79,6 +95,7 @@ class TitleTest extends \ACP3\Core\Test\Breadcrumb\TitleTest
$this->setUpStepsExpectations(1);
$this->setUpConfigMockExpectations('SEO Title', 'Subtitle', 1, 0);
$this->titleConfigurator->configure($this->title);
$this->assertEquals('Foo | SEO Title - Subtitle', $this->title->getSiteAndPageTitle());
}
......@@ -92,6 +109,7 @@ class TitleTest extends \ACP3\Core\Test\Breadcrumb\TitleTest
->willReturn(true);
$this->setUpConfigMockExpectations('SEO Title', 'Subtitle', 1, 1);
$this->titleConfigurator->configure($this->title);
$this->assertEquals('Subtitle | SEO Title', $this->title->getSiteAndPageTitle());
}
......@@ -105,6 +123,7 @@ class TitleTest extends \ACP3\Core\Test\Breadcrumb\TitleTest
->willReturn(false);
$this->setUpConfigMockExpectations('SEO Title', 'Subtitle', 2, 1);
$this->titleConfigurator->configure($this->title);
$this->assertEquals('Foo | SEO Title', $this->title->getSiteAndPageTitle());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment