Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7065 - page.main.title is translating title #26269

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
53 changes: 48 additions & 5 deletions app/code/Magento/Theme/Block/Html/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/
namespace Magento\Theme\Block\Html;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\Template;
use Magento\Store\Model\ScopeInterface;

/**
* Html page title block
Expand All @@ -19,13 +21,39 @@
*/
class Title extends Template
{
/**
* Config path to 'Translate Title' header settings
*/
private const XML_PATH_HEADER_TRANSLATE_TITLE = 'design/header/translate_title';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Own page title to display on the page
*
* @var string
*/
protected $pageTitle;

/**
* Constructor
*
* @param Template\Context $context
* @param ScopeConfigInterface $scopeConfig
* @param array $data
*/
public function __construct(
Template\Context $context,
ScopeConfigInterface $scopeConfig,
array $data = []
) {
parent::__construct($context, $data);
$this->scopeConfig = $scopeConfig;
}

/**
* Provide own page title or pick it from Head Block
*
Expand All @@ -36,7 +64,10 @@ public function getPageTitle()
if (!empty($this->pageTitle)) {
return $this->pageTitle;
}
return __($this->pageConfig->getTitle()->getShort());

$pageTitle = $this->pageConfig->getTitle()->getShort();

return $this->shouldTranslateTitle() ? __($pageTitle) : $pageTitle;
}

/**
Expand All @@ -46,10 +77,9 @@ public function getPageTitle()
*/
public function getPageHeading()
{
if (!empty($this->pageTitle)) {
return __($this->pageTitle);
}
return __($this->pageConfig->getTitle()->getShortHeading());
$pageTitle = !empty($this->pageTitle) ? $this->pageTitle : $this->pageConfig->getTitle()->getShortHeading();

return $this->shouldTranslateTitle() ? __($pageTitle) : $pageTitle;
}

/**
Expand All @@ -62,4 +92,17 @@ public function setPageTitle($pageTitle)
{
$this->pageTitle = $pageTitle;
}

/**
* Check if page title should be translated
*
* @return bool
*/
private function shouldTranslateTitle(): bool
{
return $this->scopeConfig->isSetFlag(
static::XML_PATH_HEADER_TRANSLATE_TITLE,
ScopeInterface::SCOPE_STORE
);
}
}
124 changes: 105 additions & 19 deletions app/code/Magento/Theme/Test/Unit/Block/Html/TitleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,74 @@
*/
namespace Magento\Theme\Test\Unit\Block\Html;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Phrase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Page\Config;
use Magento\Framework\View\Page\Title as PageTitle;
use Magento\Store\Model\ScopeInterface;
use Magento\Theme\Block\Html\Title;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class TitleTest extends \PHPUnit\Framework\TestCase
/**
* Test class for \Magento\Theme\Block\Html\Title
*/
class TitleTest extends TestCase
{
/**
* Config path to 'Translate Title' header settings
*/
private const XML_PATH_HEADER_TRANSLATE_TITLE = 'design/header/translate_title';

/**
* @var ObjectManagerHelper
*/
protected $objectManagerHelper;
private $objectManagerHelper;

/**
* @var Config|MockObject
*/
private $pageConfigMock;

/**
* @var \Magento\Framework\View\Page\Config|\PHPUnit_Framework_MockObject_MockObject
* @var PageTitle|MockObject
*/
protected $pageConfigMock;
private $pageTitleMock;

/**
* @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
* @var ScopeConfigInterface|MockObject
*/
protected $pageTitleMock;
private $scopeConfigMock;

/**
* @var \Magento\Theme\Block\Html\Title
* @var Title
*/
protected $htmlTitle;
private $htmlTitle;

/**
* @return void
*/
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->pageConfigMock = $this->createMock(\Magento\Framework\View\Page\Config::class);
$this->pageTitleMock = $this->createMock(\Magento\Framework\View\Page\Title::class);
$this->pageConfigMock = $this->createMock(Config::class);
$this->pageTitleMock = $this->createMock(PageTitle::class);

$context = $this->objectManagerHelper->getObject(
\Magento\Framework\View\Element\Template\Context::class,
Context::class,
['pageConfig' => $this->pageConfigMock]
);

$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);

$this->htmlTitle = $this->objectManagerHelper->getObject(
\Magento\Theme\Block\Html\Title::class,
['context' => $context]
Title::class,
[
'context' => $context,
'scopeConfig' => $this->scopeConfigMock
]
);
}

Expand All @@ -64,10 +91,16 @@ public function testGetPageTitleWithSetPageTitle()
}

/**
* @param bool $shouldTranslateTitle
*
* @return void
* @dataProvider dataProviderShouldTranslateTitle
*/
public function testGetPageTitle()
public function testGetPageTitle($shouldTranslateTitle)
{
$this->scopeConfigMock->method('isSetFlag')
->with(static::XML_PATH_HEADER_TRANSLATE_TITLE, ScopeInterface::SCOPE_STORE)
->willReturn($shouldTranslateTitle);
$title = 'some title';

$this->pageTitleMock->expects($this->once())
Expand All @@ -77,28 +110,58 @@ public function testGetPageTitle()
->method('getTitle')
->willReturn($this->pageTitleMock);

$this->assertEquals($title, $this->htmlTitle->getPageTitle());
$result = $this->htmlTitle->getPageTitle();

if ($shouldTranslateTitle) {
$this->assertInstanceOf(Phrase::class, $result);
} else {
$this->assertInternalType('string', $result);
}

$this->assertEquals($title, $result);
}

/**
* @param bool $shouldTranslateTitle
*
* @return void
* @dataProvider dataProviderShouldTranslateTitle
*/
public function testGetPageHeadingWithSetPageTitle()
public function testGetPageHeadingWithSetPageTitle($shouldTranslateTitle)
{
$this->scopeConfigMock->method('isSetFlag')
->with(static::XML_PATH_HEADER_TRANSLATE_TITLE, ScopeInterface::SCOPE_STORE)
->willReturn($shouldTranslateTitle);

$title = 'some title';

$this->htmlTitle->setPageTitle($title);
$this->pageConfigMock->expects($this->never())
->method('getTitle');

$this->assertEquals($title, $this->htmlTitle->getPageHeading());
$result = $this->htmlTitle->getPageHeading();

if ($shouldTranslateTitle) {
$this->assertInstanceOf(Phrase::class, $result);
} else {
$this->assertInternalType('string', $result);
}

$this->assertEquals($title, $result);
}

/**
* @param bool $shouldTranslateTitle
*
* @return void
* @dataProvider dataProviderShouldTranslateTitle
*/
public function testGetPageHeading()
public function testGetPageHeading($shouldTranslateTitle)
{
$this->scopeConfigMock->method('isSetFlag')
->with(static::XML_PATH_HEADER_TRANSLATE_TITLE, ScopeInterface::SCOPE_STORE)
->willReturn($shouldTranslateTitle);

$title = 'some title';

$this->pageTitleMock->expects($this->once())
Expand All @@ -108,6 +171,29 @@ public function testGetPageHeading()
->method('getTitle')
->willReturn($this->pageTitleMock);

$this->assertEquals($title, $this->htmlTitle->getPageHeading());
$result = $this->htmlTitle->getPageHeading();

if ($shouldTranslateTitle) {
$this->assertInstanceOf(Phrase::class, $result);
} else {
$this->assertInternalType('string', $result);
}

$this->assertEquals($title, $result);
}

/**
* @return array
*/
public function dataProviderShouldTranslateTitle(): array
{
return [
[
true
],
[
false
]
];
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Theme/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Disallow: /*SID=
</search_engine_robots>
<header translate="welcome">
<welcome>Default welcome msg!</welcome>
<translate_title>1</translate_title>
</header>
<footer translate="copyright">
<copyright>Copyright &#169; 2013-present Magento, Inc. All rights reserved.</copyright>
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/Theme/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@
<item name="path" xsi:type="string">design/header/welcome</item>
<item name="fieldset" xsi:type="string">other_settings/header</item>
</item>
<item name="header_translate_title" xsi:type="array">
<item name="path" xsi:type="string">design/header/translate_title</item>
<item name="fieldset" xsi:type="string">other_settings/header</item>
</item>
<item name="footer_copyright" xsi:type="array">
<item name="path" xsi:type="string">design/footer/copyright</item>
<item name="fieldset" xsi:type="string">other_settings/footer</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@
<dataScope>header_logo_alt</dataScope>
</settings>
</field>
<field name="header_translate_title" formElement="select">
<settings>
<dataType>text</dataType>
<label translate="true">Translate Title</label>
<dataScope>header_translate_title</dataScope>
</settings>
<formElements>
<select>
<settings>
<options class="Magento\Config\Model\Config\Source\Yesno"/>
</settings>
</select>
</formElements>
</field>
</fieldset>
<fieldset name="footer">
<settings>
Expand Down