Skip to content

It is not possible to add MS tile image meta via default_head_blocks.xml #21798

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

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\View\Page\Config\Metadata;

use Magento\Framework\View\Asset\Repository as AssetRepository;

/**
* Class MsApplicationTileImage
*
* Returns the URL for page `msapplication-TileImage` meta
*/
class MsApplicationTileImage
{
/**#@+
* Constant of asset name
*/
const META_NAME = 'msapplication-TileImage';

/**
* @var AssetRepository
*/
private $assetRepo;

/**
* @param AssetRepository $assetRepo
*/
public function __construct(AssetRepository $assetRepo)
{
$this->assetRepo = $assetRepo;
}

/**
* Get asset URL from given metadata content
*
* @param string $content
*
* @return string
*/
public function getUrl(string $content): string
{
if (!parse_url($content, PHP_URL_SCHEME)) {
return $this->assetRepo->getUrl($content);
}

return $content;
}
}
16 changes: 15 additions & 1 deletion lib/internal/Magento/Framework/View/Page/Config/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Asset\GroupedCollection;
use Magento\Framework\View\Page\Config;
use Magento\Framework\View\Page\Config\Metadata\MsApplicationTileImage;

/**
* Page config Renderer model
Expand Down Expand Up @@ -74,28 +75,37 @@ class Renderer implements RendererInterface
*/
protected $urlBuilder;

/**
* @var MsApplicationTileImage
*/
private $msApplicationTileImage;

/**
* @param Config $pageConfig
* @param \Magento\Framework\View\Asset\MergeService $assetMergeService
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Psr\Log\LoggerInterface $logger
* @param MsApplicationTileImage|null $msApplicationTileImage
*/
public function __construct(
Config $pageConfig,
\Magento\Framework\View\Asset\MergeService $assetMergeService,
\Magento\Framework\UrlInterface $urlBuilder,
\Magento\Framework\Escaper $escaper,
\Magento\Framework\Stdlib\StringUtils $string,
\Psr\Log\LoggerInterface $logger
\Psr\Log\LoggerInterface $logger,
MsApplicationTileImage $msApplicationTileImage = null
) {
$this->pageConfig = $pageConfig;
$this->assetMergeService = $assetMergeService;
$this->urlBuilder = $urlBuilder;
$this->escaper = $escaper;
$this->string = $string;
$this->logger = $logger;
$this->msApplicationTileImage = $msApplicationTileImage ?:
\Magento\Framework\App\ObjectManager::getInstance()->get(MsApplicationTileImage::class);
}

/**
Expand Down Expand Up @@ -179,6 +189,10 @@ protected function processMetadataContent($name, $content)
if (method_exists($this->pageConfig, $method)) {
$content = $this->pageConfig->$method();
}
if ($content && $name === $this->msApplicationTileImage::META_NAME) {
$content = $this->msApplicationTileImage->getUrl($content);
}

return $content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\Asset\GroupedCollection;
use Magento\Framework\View\Page\Config\Metadata\MsApplicationTileImage;
use Magento\Framework\View\Page\Config\Renderer;
use Magento\Framework\View\Page\Config\Generator;

Expand Down Expand Up @@ -58,6 +59,11 @@ class RendererTest extends \PHPUnit\Framework\TestCase
*/
protected $loggerMock;

/**
* @var MsApplicationTileImage|\PHPUnit_Framework_MockObject_MockObject
*/
protected $msApplicationTileImageMock;

/**
* @var \Magento\Framework\View\Asset\GroupedCollection|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -99,6 +105,10 @@ protected function setUp()
$this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
->getMock();

$this->msApplicationTileImageMock = $this->getMockBuilder(MsApplicationTileImage::class)
->disableOriginalConstructor()
->getMock();

$this->assetsCollection = $this->getMockBuilder(\Magento\Framework\View\Asset\GroupedCollection::class)
->setMethods(['getGroups'])
->disableOriginalConstructor()
Expand All @@ -120,7 +130,8 @@ protected function setUp()
'urlBuilder' => $this->urlBuilderMock,
'escaper' => $this->escaperMock,
'string' => $this->stringMock,
'logger' => $this->loggerMock
'logger' => $this->loggerMock,
'msApplicationTileImage' => $this->msApplicationTileImageMock
]
);
}
Expand All @@ -147,15 +158,17 @@ public function testRenderMetadata()
'content_type' => 'content_type_value',
'x_ua_compatible' => 'x_ua_compatible_value',
'media_type' => 'media_type_value',
'og:video:secure_url' => 'secure_url'
'og:video:secure_url' => 'secure_url',
'msapplication-TileImage' => 'https://site.domain/ms-tile.jpg'
];
$metadataValueCharset = 'newCharsetValue';

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

$this->stringMock->expects($this->at(0))
->method('upperCaseWords')
Expand All @@ -171,6 +184,37 @@ public function testRenderMetadata()
->method('getMetadata')
->will($this->returnValue($metadata));

$this->msApplicationTileImageMock
->expects($this->once())
->method('getUrl')
->with('https://site.domain/ms-tile.jpg')
->will($this->returnValue('https://site.domain/ms-tile.jpg'));

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

/**
* Test renderMetadata when it has 'msapplication-TileImage' meta passed
*/
public function testRenderMetadataWithMsApplicationTileImageAsset()
{
$metadata = [
'msapplication-TileImage' => 'images/ms-tile.jpg'
];
$expectedMetaUrl = 'https://site.domain/images/ms-tile.jpg';
$expected = '<meta name="msapplication-TileImage" content="' . $expectedMetaUrl . '"/>' . "\n";

$this->pageConfigMock
->expects($this->once())
->method('getMetadata')
->will($this->returnValue($metadata));

$this->msApplicationTileImageMock
->expects($this->once())
->method('getUrl')
->with('images/ms-tile.jpg')
->will($this->returnValue($expectedMetaUrl));

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

Expand Down Expand Up @@ -277,12 +321,14 @@ public function testRenderAssets($groupOne, $groupTwo, $expectedResult)
->willReturn($groupAssetsOne);
$groupMockOne->expects($this->any())
->method('getProperty')
->willReturnMap([
[GroupedCollection::PROPERTY_CAN_MERGE, true],
[GroupedCollection::PROPERTY_CONTENT_TYPE, $groupOne['type']],
['attributes', $groupOne['attributes']],
['ie_condition', $groupOne['condition']],
]);
->willReturnMap(
[
[GroupedCollection::PROPERTY_CAN_MERGE, true],
[GroupedCollection::PROPERTY_CONTENT_TYPE, $groupOne['type']],
['attributes', $groupOne['attributes']],
['ie_condition', $groupOne['condition']],
]
);

$assetMockTwo = $this->createMock(\Magento\Framework\View\Asset\AssetInterface::class);
$assetMockTwo->expects($this->once())
Expand All @@ -300,12 +346,14 @@ public function testRenderAssets($groupOne, $groupTwo, $expectedResult)
->willReturn($groupAssetsTwo);
$groupMockTwo->expects($this->any())
->method('getProperty')
->willReturnMap([
[GroupedCollection::PROPERTY_CAN_MERGE, true],
[GroupedCollection::PROPERTY_CONTENT_TYPE, $groupTwo['type']],
['attributes', $groupTwo['attributes']],
['ie_condition', $groupTwo['condition']],
]);
->willReturnMap(
[
[GroupedCollection::PROPERTY_CAN_MERGE, true],
[GroupedCollection::PROPERTY_CONTENT_TYPE, $groupTwo['type']],
['attributes', $groupTwo['attributes']],
['ie_condition', $groupTwo['condition']],
]
);

$this->pageConfigMock->expects($this->once())
->method('getAssetCollection')
Expand Down