From 584cfd33f3ab7a0bab86e2f303a28c1c95c477da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Mart=C3=ADnez?= Date: Wed, 25 Oct 2017 13:13:53 +0200 Subject: [PATCH] Override page.main.title block in category and product pages with new RawTitle block, to avoid category and product names being translated Unit tests for new RawTitle block --- .../frontend/layout/catalog_category_view.xml | 16 +-- .../frontend/layout/catalog_product_view.xml | 14 ++- .../Magento/Theme/Block/Html/RawTitle.php | 63 ++++++++++ .../Test/Unit/Block/Html/RawTitleTest.php | 113 ++++++++++++++++++ .../view/frontend/templates/html/title.phtml | 4 +- 5 files changed, 195 insertions(+), 15 deletions(-) create mode 100644 app/code/Magento/Theme/Block/Html/RawTitle.php create mode 100644 app/code/Magento/Theme/Test/Unit/Block/Html/RawTitleTest.php diff --git a/app/code/Magento/Catalog/view/frontend/layout/catalog_category_view.xml b/app/code/Magento/Catalog/view/frontend/layout/catalog_category_view.xml index 5fee1d8447e5a..73a129a59cf1c 100644 --- a/app/code/Magento/Catalog/view/frontend/layout/catalog_category_view.xml +++ b/app/code/Magento/Catalog/view/frontend/layout/catalog_category_view.xml @@ -42,12 +42,14 @@ - - - page-title-heading - page-title-heading toolbar-amount - - - + + + + page-title-heading + page-title-heading toolbar-amount + + + + diff --git a/app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml b/app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml index c3d0406740dcc..179992c9dd9a0 100644 --- a/app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml +++ b/app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml @@ -17,12 +17,14 @@ - - - product - itemprop="name" - - + + + + product + itemprop="name" + + + itemscope itemtype="http://schema.org/Product" diff --git a/app/code/Magento/Theme/Block/Html/RawTitle.php b/app/code/Magento/Theme/Block/Html/RawTitle.php new file mode 100644 index 0000000000000..97e59518a1064 --- /dev/null +++ b/app/code/Magento/Theme/Block/Html/RawTitle.php @@ -0,0 +1,63 @@ +pageTitle)) { + return $this->pageTitle; + } + return $this->pageConfig->getTitle()->getShort(); + } + + /** + * Provide own page content heading + * + * @return string + */ + public function getPageHeading() + { + if (!empty($this->pageTitle)) { + return $this->pageTitle; + } + return $this->pageConfig->getTitle()->getShortHeading(); + } + + /** + * Set own page title + * + * @param string $pageTitle + * @return void + */ + public function setPageTitle($pageTitle) + { + $this->pageTitle = $pageTitle; + } +} diff --git a/app/code/Magento/Theme/Test/Unit/Block/Html/RawTitleTest.php b/app/code/Magento/Theme/Test/Unit/Block/Html/RawTitleTest.php new file mode 100644 index 0000000000000..7195e72f61069 --- /dev/null +++ b/app/code/Magento/Theme/Test/Unit/Block/Html/RawTitleTest.php @@ -0,0 +1,113 @@ +objectManagerHelper = new ObjectManagerHelper($this); + $this->pageConfigMock = $this->createMock(\Magento\Framework\View\Page\Config::class); + $this->pageTitleMock = $this->createMock(\Magento\Framework\View\Page\Title::class); + + $context = $this->objectManagerHelper->getObject( + \Magento\Framework\View\Element\Template\Context::class, + ['pageConfig' => $this->pageConfigMock] + ); + + $this->rawHtmlTitle = $this->objectManagerHelper->getObject( + \Magento\Theme\Block\Html\RawTitle::class, + ['context' => $context] + ); + } + + /** + * @return void + */ + public function testGetPageTitleWithSetPageTitle() + { + $title = 'some title'; + + $this->rawHtmlTitle->setPageTitle($title); + $this->pageConfigMock->expects($this->never()) + ->method('getTitle'); + + $this->assertEquals($title, $this->rawHtmlTitle->getPageTitle()); + } + + /** + * @return void + */ + public function testGetPageTitle() + { + $title = 'some title'; + + $this->pageTitleMock->expects($this->once()) + ->method('getShort') + ->willReturn($title); + $this->pageConfigMock->expects($this->once()) + ->method('getTitle') + ->willReturn($this->pageTitleMock); + + $this->assertEquals($title, $this->rawHtmlTitle->getPageTitle()); + } + + /** + * @return void + */ + public function testGetPageHeadingWithSetPageTitle() + { + $title = 'some title'; + + $this->rawHtmlTitle->setPageTitle($title); + $this->pageConfigMock->expects($this->never()) + ->method('getTitle'); + + $this->assertEquals($title, $this->rawHtmlTitle->getPageHeading()); + } + + /** + * @return void + */ + public function testGetPageHeading() + { + $title = 'some title'; + + $this->pageTitleMock->expects($this->once()) + ->method('getShortHeading') + ->willReturn($title); + $this->pageConfigMock->expects($this->once()) + ->method('getTitle') + ->willReturn($this->pageTitleMock); + + $this->assertEquals($title, $this->rawHtmlTitle->getPageHeading()); + } +} diff --git a/app/code/Magento/Theme/view/frontend/templates/html/title.phtml b/app/code/Magento/Theme/view/frontend/templates/html/title.phtml index 69ff4c8909f8c..28d0b39c2a85e 100644 --- a/app/code/Magento/Theme/view/frontend/templates/html/title.phtml +++ b/app/code/Magento/Theme/view/frontend/templates/html/title.phtml @@ -7,12 +7,12 @@ // @codingStandardsIgnoreFile /** - * @var $block \Magento\Theme\Block\Html\Title + * @var $block \Magento\Theme\Block\Html\Title|\Magento\Theme\Block\Html\RawTitle */ $cssClass = $block->getCssClass() ? ' ' . $block->getCssClass() : ''; $title = ''; if (trim($block->getPageHeading())) { - $title = 'getAddBaseAttribute() . '>' + $title = 'getAddBaseAttribute() . '>' . $block->escapeHtml($block->getPageHeading()) . ''; } ?>