Skip to content

Commit 8892b0b

Browse files
author
Vasilii Burlacu
committed
magento/magento:5023 - It is not possible to add MS tile image meta via default_head_blocks.xml
# Introduced a new class that provides URL for MS tile image during page config metadata generation
1 parent b488bd4 commit 8892b0b

File tree

5 files changed

+110
-59
lines changed

5 files changed

+110
-59
lines changed

lib/internal/Magento/Framework/View/Page/Config.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,6 @@ class Config
137137
'robots' => null,
138138
];
139139

140-
/**
141-
* @var array
142-
*/
143-
private $additionalMetaAssets = ['msapplication-TileImage'];
144-
145140
/**
146141
* @var \Magento\Framework\App\State
147142
*/
@@ -556,31 +551,6 @@ public function addBodyClass($className)
556551
return $this;
557552
}
558553

559-
/**
560-
* Retrieve the additional meta assets
561-
*
562-
* @return array
563-
*/
564-
public function getAdditionalMetaAssets()
565-
{
566-
return $this->additionalMetaAssets;
567-
}
568-
569-
/**
570-
* Adjust metadata content url
571-
*
572-
* @param string $content
573-
* @return string $content
574-
*/
575-
public function getMetaAssetUrl($content)
576-
{
577-
if (!parse_url($content, PHP_URL_SCHEME)) {
578-
return $this->assetRepo->getUrl($content);
579-
}
580-
581-
return $content;
582-
}
583-
584554
/**
585555
* Set additional element attribute
586556
*
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\View\Page\Config\Metadata;
9+
10+
use Magento\Framework\View\Asset\Repository as AssetRepository;
11+
12+
/**
13+
* Class MsApplicationTileImage
14+
*
15+
* Returns the URL for page `msapplication-TileImage` meta
16+
*/
17+
class MsApplicationTileImage
18+
{
19+
/**#@+
20+
* Constant of asset name
21+
*/
22+
const META_NAME = 'msapplication-TileImage';
23+
24+
/**
25+
* @var AssetRepository
26+
*/
27+
private $assetRepo;
28+
29+
/**
30+
* @param AssetRepository $assetRepo
31+
*/
32+
public function __construct(AssetRepository $assetRepo)
33+
{
34+
$this->assetRepo = $assetRepo;
35+
}
36+
37+
/**
38+
* Get asset URL from given metadata content
39+
*
40+
* @param string $content
41+
*
42+
* @return string
43+
*/
44+
public function getUrl(string $content): string
45+
{
46+
if (!parse_url($content, PHP_URL_SCHEME)) {
47+
return $this->assetRepo->getUrl($content);
48+
}
49+
50+
return $content;
51+
}
52+
}

lib/internal/Magento/Framework/View/Page/Config/Renderer.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Exception\LocalizedException;
1010
use Magento\Framework\View\Asset\GroupedCollection;
1111
use Magento\Framework\View\Page\Config;
12+
use Magento\Framework\View\Page\Config\Metadata\MsApplicationTileImage;
1213

1314
/**
1415
* Page config Renderer model
@@ -74,28 +75,37 @@ class Renderer implements RendererInterface
7475
*/
7576
protected $urlBuilder;
7677

78+
/**
79+
* @var MsApplicationTileImage
80+
*/
81+
private $msApplicationTileImage;
82+
7783
/**
7884
* @param Config $pageConfig
7985
* @param \Magento\Framework\View\Asset\MergeService $assetMergeService
8086
* @param \Magento\Framework\UrlInterface $urlBuilder
8187
* @param \Magento\Framework\Escaper $escaper
8288
* @param \Magento\Framework\Stdlib\StringUtils $string
8389
* @param \Psr\Log\LoggerInterface $logger
90+
* @param MsApplicationTileImage|null $msApplicationTileImage
8491
*/
8592
public function __construct(
8693
Config $pageConfig,
8794
\Magento\Framework\View\Asset\MergeService $assetMergeService,
8895
\Magento\Framework\UrlInterface $urlBuilder,
8996
\Magento\Framework\Escaper $escaper,
9097
\Magento\Framework\Stdlib\StringUtils $string,
91-
\Psr\Log\LoggerInterface $logger
98+
\Psr\Log\LoggerInterface $logger,
99+
MsApplicationTileImage $msApplicationTileImage = null
92100
) {
93101
$this->pageConfig = $pageConfig;
94102
$this->assetMergeService = $assetMergeService;
95103
$this->urlBuilder = $urlBuilder;
96104
$this->escaper = $escaper;
97105
$this->string = $string;
98106
$this->logger = $logger;
107+
$this->msApplicationTileImage = $msApplicationTileImage ?:
108+
\Magento\Framework\App\ObjectManager::getInstance()->get(MsApplicationTileImage::class);
99109
}
100110

101111
/**
@@ -179,8 +189,8 @@ protected function processMetadataContent($name, $content)
179189
if (method_exists($this->pageConfig, $method)) {
180190
$content = $this->pageConfig->$method();
181191
}
182-
if ($content && in_array($name, $this->pageConfig->getAdditionalMetaAssets())) {
183-
$content = $this->pageConfig->getMetaAssetUrl($content);
192+
if ($content && $name === $this->msApplicationTileImage::META_NAME) {
193+
$content = $this->msApplicationTileImage->getUrl($content);
184194
}
185195

186196
return $content;

lib/internal/Magento/Framework/View/Test/Unit/Page/Config/RendererTest.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1010
use Magento\Framework\View\Asset\GroupedCollection;
11+
use Magento\Framework\View\Page\Config\Metadata\MsApplicationTileImage;
1112
use Magento\Framework\View\Page\Config\Renderer;
1213
use Magento\Framework\View\Page\Config\Generator;
1314

@@ -58,6 +59,11 @@ class RendererTest extends \PHPUnit\Framework\TestCase
5859
*/
5960
protected $loggerMock;
6061

62+
/**
63+
* @var MsApplicationTileImage|\PHPUnit_Framework_MockObject_MockObject
64+
*/
65+
protected $msApplicationTileImageMock;
66+
6167
/**
6268
* @var \Magento\Framework\View\Asset\GroupedCollection|\PHPUnit_Framework_MockObject_MockObject
6369
*/
@@ -99,6 +105,10 @@ protected function setUp()
99105
$this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
100106
->getMock();
101107

108+
$this->msApplicationTileImageMock = $this->getMockBuilder(MsApplicationTileImage::class)
109+
->disableOriginalConstructor()
110+
->getMock();
111+
102112
$this->assetsCollection = $this->getMockBuilder(\Magento\Framework\View\Asset\GroupedCollection::class)
103113
->setMethods(['getGroups'])
104114
->disableOriginalConstructor()
@@ -120,7 +130,8 @@ protected function setUp()
120130
'urlBuilder' => $this->urlBuilderMock,
121131
'escaper' => $this->escaperMock,
122132
'string' => $this->stringMock,
123-
'logger' => $this->loggerMock
133+
'logger' => $this->loggerMock,
134+
'msApplicationTileImage' => $this->msApplicationTileImageMock
124135
]
125136
);
126137
}
@@ -147,15 +158,17 @@ public function testRenderMetadata()
147158
'content_type' => 'content_type_value',
148159
'x_ua_compatible' => 'x_ua_compatible_value',
149160
'media_type' => 'media_type_value',
150-
'og:video:secure_url' => 'secure_url'
161+
'og:video:secure_url' => 'secure_url',
162+
'msapplication-TileImage' => 'https://site.domain/ms-tile.jpg'
151163
];
152164
$metadataValueCharset = 'newCharsetValue';
153165

154166
$expected = '<meta charset="newCharsetValue"/>' . "\n"
155167
. '<meta name="metadataName" content="metadataValue"/>' . "\n"
156168
. '<meta http-equiv="Content-Type" content="content_type_value"/>' . "\n"
157169
. '<meta http-equiv="X-UA-Compatible" content="x_ua_compatible_value"/>' . "\n"
158-
. '<meta property="og:video:secure_url" content="secure_url"/>' . "\n";
170+
. '<meta property="og:video:secure_url" content="secure_url"/>' . "\n"
171+
. '<meta name="msapplication-TileImage" content="https://site.domain/ms-tile.jpg"/>' . "\n";
159172

160173
$this->stringMock->expects($this->at(0))
161174
->method('upperCaseWords')
@@ -171,10 +184,36 @@ public function testRenderMetadata()
171184
->method('getMetadata')
172185
->will($this->returnValue($metadata));
173186

187+
$this->msApplicationTileImageMock
188+
->expects($this->once())
189+
->method('getUrl')
190+
->with('https://site.domain/ms-tile.jpg')
191+
->will($this->returnValue('https://site.domain/ms-tile.jpg'));
192+
193+
$this->assertEquals($expected, $this->renderer->renderMetadata());
194+
}
195+
196+
/**
197+
* Test renderMetadata when it has 'msapplication-TileImage' meta passed
198+
*/
199+
public function testRenderMetadataWithMsApplicationTileImageAsset()
200+
{
201+
$metadata = [
202+
'msapplication-TileImage' => 'images/ms-tile.jpg'
203+
];
204+
$expectedMetaUrl = 'https://site.domain/images/ms-tile.jpg';
205+
$expected = '<meta name="msapplication-TileImage" content="' . $expectedMetaUrl . '"/>' . "\n";
206+
174207
$this->pageConfigMock
175-
->expects($this->any())
176-
->method('getAdditionalMetaAssets')
177-
->will($this->returnValue(['msapplication-TileImage']));
208+
->expects($this->once())
209+
->method('getMetadata')
210+
->will($this->returnValue($metadata));
211+
212+
$this->msApplicationTileImageMock
213+
->expects($this->once())
214+
->method('getUrl')
215+
->with('images/ms-tile.jpg')
216+
->will($this->returnValue($expectedMetaUrl));
178217

179218
$this->assertEquals($expected, $this->renderer->renderMetadata());
180219
}

lib/internal/Magento/Framework/View/Test/Unit/Page/ConfigTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -377,26 +377,6 @@ public function testAddRss()
377377
$this->assertInstanceOf(\Magento\Framework\View\Page\Config::class, $this->model->addRss($title, $href));
378378
}
379379

380-
/**
381-
* Testing case for getting meta asset URL with URL in content
382-
*/
383-
public function testGetMetaAssetUrlWithUrlInContent()
384-
{
385-
$this->assetRepo->expects($this->never())->method('getUrl');
386-
$this->assertEquals('http://test.com/image.png', $this->model->getMetaAssetUrl('http://test.com/image.png'));
387-
}
388-
389-
/**
390-
* Testing case for getting meta asset URL without URL in content
391-
*/
392-
public function testGetMetaAssetUrlWithoutUrlInContent()
393-
{
394-
$this->assetRepo->expects($this->once())->method('getUrl')->with('image.png')->will(
395-
$this->returnValue('http://test.com/image.png')
396-
);
397-
$this->assertEquals('http://test.com/image.png', $this->model->getMetaAssetUrl('image.png'));
398-
}
399-
400380
public function testAddBodyClass()
401381
{
402382
$className = 'test class';

0 commit comments

Comments
 (0)