Skip to content

Commit 1413d65

Browse files
committed
FIX failing tests and PageConfig refactor
1 parent 432cf00 commit 1413d65

File tree

3 files changed

+66
-73
lines changed

3 files changed

+66
-73
lines changed

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

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Magento\Framework\View\Page;
88

99
use Magento\Framework\App;
10-
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\App\Area;
1111
use Magento\Framework\View;
1212

1313
/**
@@ -42,6 +42,7 @@ class Config
4242
const META_TITLE = 'title';
4343
const META_KEYWORDS = 'keywords';
4444
const META_ROBOTS = 'robots';
45+
const META_X_UI_COMPATIBLE = 'x_ua_compatible';
4546

4647
/**
4748
* Constant body attribute class
@@ -253,7 +254,7 @@ public function getMetadata()
253254
*/
254255
public function setContentType($contentType)
255256
{
256-
$this->setMetadata('content_type', $contentType);
257+
$this->setMetadata(self::META_CONTENT_TYPE, $contentType);
257258
}
258259

259260
/**
@@ -264,10 +265,10 @@ public function setContentType($contentType)
264265
public function getContentType()
265266
{
266267
$this->build();
267-
if (strtolower($this->metadata['content_type']) === 'auto') {
268-
$this->metadata['content_type'] = $this->getMediaType() . '; charset=' . $this->getCharset();
268+
if (strtolower($this->metadata[self::META_CONTENT_TYPE]) === 'auto') {
269+
$this->metadata[self::META_CONTENT_TYPE] = $this->getMediaType() . '; charset=' . $this->getCharset();
269270
}
270-
return $this->metadata['content_type'];
271+
return $this->metadata[self::META_CONTENT_TYPE];
271272
}
272273

273274
/**
@@ -276,7 +277,7 @@ public function getContentType()
276277
*/
277278
public function setMediaType($mediaType)
278279
{
279-
$this->setMetadata('media_type', $mediaType);
280+
$this->setMetadata(self::META_MEDIA_TYPE, $mediaType);
280281
}
281282

282283
/**
@@ -287,13 +288,13 @@ public function setMediaType($mediaType)
287288
public function getMediaType()
288289
{
289290
$this->build();
290-
if (empty($this->metadata['media_type'])) {
291-
$this->metadata['media_type'] = $this->scopeConfig->getValue(
291+
if (empty($this->metadata[self::META_MEDIA_TYPE])) {
292+
$this->metadata[self::META_MEDIA_TYPE] = $this->scopeConfig->getValue(
292293
'design/head/default_media_type',
293294
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
294295
);
295296
}
296-
return $this->metadata['media_type'];
297+
return $this->metadata[self::META_MEDIA_TYPE];
297298
}
298299

299300
/**
@@ -302,7 +303,7 @@ public function getMediaType()
302303
*/
303304
public function setCharset($charset)
304305
{
305-
$this->setMetadata('charset', $charset);
306+
$this->setMetadata(self::META_CHARSET, $charset);
306307
}
307308

308309
/**
@@ -313,13 +314,13 @@ public function setCharset($charset)
313314
public function getCharset()
314315
{
315316
$this->build();
316-
if (empty($this->metadata['charset'])) {
317-
$this->metadata['charset'] = $this->scopeConfig->getValue(
317+
if (empty($this->metadata[self::META_CHARSET])) {
318+
$this->metadata[self::META_CHARSET] = $this->scopeConfig->getValue(
318319
'design/head/default_charset',
319320
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
320321
);
321322
}
322-
return $this->metadata['charset'];
323+
return $this->metadata[self::META_CHARSET];
323324
}
324325

325326
/**
@@ -328,7 +329,7 @@ public function getCharset()
328329
*/
329330
public function setDescription($description)
330331
{
331-
$this->setMetadata('description', $description);
332+
$this->setMetadata(self::META_DESCRIPTION, $description);
332333
}
333334

334335
/**
@@ -339,49 +340,21 @@ public function setDescription($description)
339340
public function getDescription()
340341
{
341342
$this->build();
342-
if (empty($this->metadata['description'])) {
343-
$this->metadata['description'] = $this->scopeConfig->getValue(
343+
if (empty($this->metadata[self::META_DESCRIPTION])) {
344+
$this->metadata[self::META_DESCRIPTION] = $this->scopeConfig->getValue(
344345
'design/head/default_description',
345346
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
346347
);
347348
}
348-
return $this->metadata['description'];
349-
}
350-
351-
/**
352-
* Get rendered metadata
353-
* @param string $fieldName
354-
* @return string
355-
* @throws LocalizedException
356-
*/
357-
public function getRenderedMetaTagValue(string $fieldName)
358-
{
359-
switch ($fieldName) {
360-
case self::META_DESCRIPTION:
361-
return $this->getDescription();
362-
case self::META_CONTENT_TYPE:
363-
return $this->getContentType();
364-
case self::META_MEDIA_TYPE:
365-
return $this->getMediaType();
366-
case self::META_CHARSET:
367-
return $this->getCharset();
368-
case self::META_KEYWORDS:
369-
return $this->getKeywords();
370-
case self::META_ROBOTS:
371-
return $this->getRobots();
372-
case self::META_TITLE:
373-
return $this->getMetaTitle();
374-
default:
375-
throw new LocalizedException(__('No rendered meta function for %1', $fieldName));
376-
}
349+
return $this->metadata[self::META_DESCRIPTION];
377350
}
378351

379352
/**
380353
* @param string $title
381354
*/
382355
public function setMetaTitle($title)
383356
{
384-
$this->setMetadata('title', $title);
357+
$this->setMetadata(self::META_TITLE, $title);
385358
}
386359

387360
/**
@@ -392,11 +365,11 @@ public function setMetaTitle($title)
392365
public function getMetaTitle()
393366
{
394367
$this->build();
395-
if (empty($this->metadata['title'])) {
368+
if (empty($this->metadata[self::META_TITLE])) {
396369
return '';
397370
}
398371

399-
return $this->metadata['title'];
372+
return $this->metadata[self::META_TITLE];
400373
}
401374

402375
/**
@@ -405,7 +378,7 @@ public function getMetaTitle()
405378
*/
406379
public function setKeywords($keywords)
407380
{
408-
$this->setMetadata('keywords', $keywords);
381+
$this->setMetadata(self::META_KEYWORDS, $keywords);
409382
}
410383

411384
/**
@@ -416,13 +389,13 @@ public function setKeywords($keywords)
416389
public function getKeywords()
417390
{
418391
$this->build();
419-
if (empty($this->metadata['keywords'])) {
420-
$this->metadata['keywords'] = $this->scopeConfig->getValue(
392+
if (empty($this->metadata[self::META_KEYWORDS])) {
393+
$this->metadata[self::META_KEYWORDS] = $this->scopeConfig->getValue(
421394
'design/head/default_keywords',
422395
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
423396
);
424397
}
425-
return $this->metadata['keywords'];
398+
return $this->metadata[self::META_KEYWORDS];
426399
}
427400

428401
/**
@@ -431,27 +404,28 @@ public function getKeywords()
431404
*/
432405
public function setRobots($robots)
433406
{
434-
$this->setMetadata('robots', $robots);
407+
$this->setMetadata(self::META_ROBOTS, $robots);
435408
}
436409

437410
/**
438411
* Retrieve URL to robots file
439412
*
440413
* @return string
414+
* @throws \Magento\Framework\Exception\LocalizedException
441415
*/
442416
public function getRobots()
443417
{
444-
if ($this->getAreaResolver()->getAreaCode() !== 'frontend') {
418+
if ($this->getAreaResolver()->getAreaCode() !== Area::AREA_FRONTEND) {
445419
return 'NOINDEX,NOFOLLOW';
446420
}
447421
$this->build();
448-
if (empty($this->metadata['robots'])) {
449-
$this->metadata['robots'] = $this->scopeConfig->getValue(
422+
if (empty($this->metadata[self::META_ROBOTS])) {
423+
$this->metadata[self::META_ROBOTS] = $this->scopeConfig->getValue(
450424
'design/search_engine_robots/default_robots',
451425
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
452426
);
453427
}
454-
return $this->metadata['robots'];
428+
return $this->metadata[self::META_ROBOTS];
455429
}
456430

457431
/**

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Renderer implements RendererInterface
2323
protected $assetTypeOrder = ['css', 'ico', 'js'];
2424

2525
/**
26-
* @var \Magento\Framework\View\Page\Config
26+
* @var Config
2727
*/
2828
protected $pageConfig;
2929

@@ -53,7 +53,7 @@ class Renderer implements RendererInterface
5353
protected $urlBuilder;
5454

5555
/**
56-
* @param \Magento\Framework\View\Page\Config $pageConfig
56+
* @param Config $pageConfig
5757
* @param \Magento\Framework\View\Asset\MergeService $assetMergeService
5858
* @param \Magento\Framework\UrlInterface $urlBuilder
5959
* @param \Magento\Framework\Escaper $escaper
@@ -137,10 +137,30 @@ public function renderMetadata()
137137
*/
138138
protected function processMetadataContent($name, $content)
139139
{
140-
try {
141-
return $this->pageConfig->getRenderedMetaTagValue((string) $name);
142-
} catch (LocalizedException $e) {
143-
return (string) $content;
140+
switch ($name) {
141+
case Config::META_DESCRIPTION:
142+
return $this->pageConfig->getDescription();
143+
144+
case Config::META_CONTENT_TYPE:
145+
return $this->pageConfig->getContentType();
146+
147+
case Config::META_MEDIA_TYPE:
148+
return $this->pageConfig->getMediaType();
149+
150+
case Config::META_CHARSET:
151+
return $this->pageConfig->getCharset();
152+
153+
case Config::META_KEYWORDS:
154+
return $this->pageConfig->getKeywords();
155+
156+
case Config::META_ROBOTS:
157+
return $this->pageConfig->getRobots();
158+
159+
case Config::META_TITLE:
160+
return $this->pageConfig->getMetaTitle();
161+
162+
default:
163+
return $content;
144164
}
145165
}
146166

@@ -155,19 +175,19 @@ protected function getMetadataTemplate($name)
155175
}
156176

157177
switch ($name) {
158-
case 'charset':
178+
case Config::META_CHARSET:
159179
$metadataTemplate = '<meta charset="%content"/>' . "\n";
160180
break;
161181

162-
case 'content_type':
182+
case Config::META_CONTENT_TYPE:
163183
$metadataTemplate = '<meta http-equiv="Content-Type" content="%content"/>' . "\n";
164184
break;
165185

166-
case 'x_ua_compatible':
186+
case Config::META_X_UI_COMPATIBLE:
167187
$metadataTemplate = '<meta http-equiv="X-UA-Compatible" content="%content"/>' . "\n";
168188
break;
169189

170-
case 'media_type':
190+
case Config::META_MEDIA_TYPE:
171191
$metadataTemplate = false;
172192
break;
173193

@@ -354,7 +374,7 @@ protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $
354374
);
355375
$result .= sprintf($template, $asset->getUrl());
356376
}
357-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
377+
} catch (LocalizedException $e) {
358378
$this->logger->critical($e);
359379
$result .= sprintf($template, $this->urlBuilder->getUrl('', ['_direct' => 'core/index/notFound']));
360380
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,14 @@ public function testRenderMetadata()
157157
. '<meta http-equiv="X-UA-Compatible" content="x_ua_compatible_value"/>' . "\n"
158158
. '<meta property="og:video:secure_url" content="secure_url"/>' . "\n";
159159

160-
$this->stringMock->expects($this->at(0))
161-
->method('upperCaseWords')
162-
->with('charset', '_', '')
163-
->willReturn('Charset');
164-
165160
$this->pageConfigMock->expects($this->once())
166161
->method('getCharset')
167162
->willReturn($metadataValueCharset);
168163

164+
$this->pageConfigMock->expects($this->once())
165+
->method('getContentType')
166+
->willReturn('content_type_value');
167+
169168
$this->pageConfigMock
170169
->expects($this->once())
171170
->method('getMetadata')

0 commit comments

Comments
 (0)