Skip to content

Commit db70870

Browse files
authored
ENGCOM-5635: It is not possible to add MS tile image meta via default_head_blocks.xml #21798
2 parents 7680391 + e04dd03 commit db70870

File tree

3 files changed

+130
-16
lines changed

3 files changed

+130
-16
lines changed
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: 15 additions & 1 deletion
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,6 +189,10 @@ protected function processMetadataContent($name, $content)
179189
if (method_exists($this->pageConfig, $method)) {
180190
$content = $this->pageConfig->$method();
181191
}
192+
if ($content && $name === $this->msApplicationTileImage::META_NAME) {
193+
$content = $this->msApplicationTileImage->getUrl($content);
194+
}
195+
182196
return $content;
183197
}
184198

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

Lines changed: 63 additions & 15 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,6 +184,37 @@ 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+
207+
$this->pageConfigMock
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));
217+
174218
$this->assertEquals($expected, $this->renderer->renderMetadata());
175219
}
176220

@@ -277,12 +321,14 @@ public function testRenderAssets($groupOne, $groupTwo, $expectedResult)
277321
->willReturn($groupAssetsOne);
278322
$groupMockOne->expects($this->any())
279323
->method('getProperty')
280-
->willReturnMap([
281-
[GroupedCollection::PROPERTY_CAN_MERGE, true],
282-
[GroupedCollection::PROPERTY_CONTENT_TYPE, $groupOne['type']],
283-
['attributes', $groupOne['attributes']],
284-
['ie_condition', $groupOne['condition']],
285-
]);
324+
->willReturnMap(
325+
[
326+
[GroupedCollection::PROPERTY_CAN_MERGE, true],
327+
[GroupedCollection::PROPERTY_CONTENT_TYPE, $groupOne['type']],
328+
['attributes', $groupOne['attributes']],
329+
['ie_condition', $groupOne['condition']],
330+
]
331+
);
286332

287333
$assetMockTwo = $this->createMock(\Magento\Framework\View\Asset\AssetInterface::class);
288334
$assetMockTwo->expects($this->once())
@@ -300,12 +346,14 @@ public function testRenderAssets($groupOne, $groupTwo, $expectedResult)
300346
->willReturn($groupAssetsTwo);
301347
$groupMockTwo->expects($this->any())
302348
->method('getProperty')
303-
->willReturnMap([
304-
[GroupedCollection::PROPERTY_CAN_MERGE, true],
305-
[GroupedCollection::PROPERTY_CONTENT_TYPE, $groupTwo['type']],
306-
['attributes', $groupTwo['attributes']],
307-
['ie_condition', $groupTwo['condition']],
308-
]);
349+
->willReturnMap(
350+
[
351+
[GroupedCollection::PROPERTY_CAN_MERGE, true],
352+
[GroupedCollection::PROPERTY_CONTENT_TYPE, $groupTwo['type']],
353+
['attributes', $groupTwo['attributes']],
354+
['ie_condition', $groupTwo['condition']],
355+
]
356+
);
309357

310358
$this->pageConfigMock->expects($this->once())
311359
->method('getAssetCollection')

0 commit comments

Comments
 (0)