Skip to content

Commit 9125aa6

Browse files
Merge pull request #5053 from magento-qwerty/MAGETWO-55858
[CIA] Output escaping methods shouldn't be part of AbstractBlock
2 parents 57a2aad + e5f5e3c commit 9125aa6

File tree

8 files changed

+215
-44
lines changed

8 files changed

+215
-44
lines changed

app/code/Magento/Catalog/view/frontend/templates/product/list.phtml

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ use Magento\Framework\App\Action\Action;
1313
* Product list template
1414
*
1515
* @var $block \Magento\Catalog\Block\Product\ListProduct
16+
* @var \Magento\Framework\Escaper $escaper
1617
*/
1718
?>
1819
<?php
1920
$_productCollection = $block->getLoadedProductCollection();
21+
/** @var \Magento\Catalog\Helper\Output $_helper */
2022
$_helper = $this->helper(Magento\Catalog\Helper\Output::class);
2123
?>
2224
<?php if (!$_productCollection->count()) :?>
23-
<div class="message info empty"><div><?= $block->escapeHtml(__('We can\'t find products matching the selection.')) ?></div></div>
25+
<div class="message info empty"><div><?= $escaper->escapeHtml(__('We can\'t find products matching the selection.')) ?></div></div>
2426
<?php else :?>
2527
<?= $block->getToolbarHtml() ?>
2628
<?= $block->getAdditionalHtml() ?>
@@ -55,7 +57,7 @@ $_helper = $this->helper(Magento\Catalog\Helper\Output::class);
5557
}
5658
?>
5759
<?php // Product Image ?>
58-
<a href="<?= $block->escapeUrl($_product->getProductUrl()) ?>"
60+
<a href="<?= $escaper->escapeUrl($_product->getProductUrl()) ?>"
5961
class="product photo product-item-photo"
6062
tabindex="-1">
6163
<?= $productImage->toHtml() ?>
@@ -66,7 +68,7 @@ $_helper = $this->helper(Magento\Catalog\Helper\Output::class);
6668
?>
6769
<strong class="product name product-item-name">
6870
<a class="product-item-link"
69-
href="<?= $block->escapeUrl($_product->getProductUrl()) ?>">
71+
href="<?= $escaper->escapeUrl($_product->getProductUrl()) ?>">
7072
<?= /* @noEscape */ $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
7173
</a>
7274
</strong>
@@ -77,13 +79,13 @@ $_helper = $this->helper(Magento\Catalog\Helper\Output::class);
7779
<?php endif; ?>
7880

7981
<div class="product-item-inner">
80-
<div class="product actions product-item-actions"<?= strpos($pos, $viewMode . '-actions') ? $block->escapeHtmlAttr($position) : '' ?>>
81-
<div class="actions-primary"<?= strpos($pos, $viewMode . '-primary') ? $block->escapeHtmlAttr($position) : '' ?>>
82+
<div class="product actions product-item-actions"<?= strpos($pos, $viewMode . '-actions') ? $escaper->escapeHtmlAttr($position) : '' ?>>
83+
<div class="actions-primary"<?= strpos($pos, $viewMode . '-primary') ? $escaper->escapeHtmlAttr($position) : '' ?>>
8284
<?php if ($_product->isSaleable()) :?>
8385
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
8486
<form data-role="tocart-form"
85-
data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>"
86-
action="<?= $block->escapeUrl($postParams['action']) ?>"
87+
data-product-sku="<?= $escaper->escapeHtml($_product->getSku()) ?>"
88+
action="<?= $escaper->escapeUrl($postParams['action']) ?>"
8789
method="post">
8890
<input type="hidden"
8991
name="product"
@@ -92,20 +94,20 @@ $_helper = $this->helper(Magento\Catalog\Helper\Output::class);
9294
value="<?= /* @noEscape */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
9395
<?= $block->getBlockHtml('formkey') ?>
9496
<button type="submit"
95-
title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
97+
title="<?= $escaper->escapeHtmlAttr(__('Add to Cart')) ?>"
9698
class="action tocart primary">
97-
<span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
99+
<span><?= $escaper->escapeHtml(__('Add to Cart')) ?></span>
98100
</button>
99101
</form>
100102
<?php else :?>
101103
<?php if ($_product->isAvailable()) :?>
102-
<div class="stock available"><span><?= $block->escapeHtml(__('In stock')) ?></span></div>
104+
<div class="stock available"><span><?= $escaper->escapeHtml(__('In stock')) ?></span></div>
103105
<?php else :?>
104-
<div class="stock unavailable"><span><?= $block->escapeHtml(__('Out of stock')) ?></span></div>
106+
<div class="stock unavailable"><span><?= $escaper->escapeHtml(__('Out of stock')) ?></span></div>
105107
<?php endif; ?>
106108
<?php endif; ?>
107109
</div>
108-
<div data-role="add-to-links" class="actions-secondary"<?= strpos($pos, $viewMode . '-secondary') ? $block->escapeHtmlAttr($position) : '' ?>>
110+
<div data-role="add-to-links" class="actions-secondary"<?= strpos($pos, $viewMode . '-secondary') ? $escaper->escapeHtmlAttr($position) : '' ?>>
109111
<?php if ($addToBlock = $block->getChildBlock('addto')) :?>
110112
<?= $addToBlock->setProduct($_product)->getChildHtml() ?>
111113
<?php endif; ?>
@@ -114,9 +116,9 @@ $_helper = $this->helper(Magento\Catalog\Helper\Output::class);
114116
<?php if ($showDescription) :?>
115117
<div class="product description product-item-description">
116118
<?= /* @noEscape */ $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
117-
<a href="<?= $block->escapeUrl($_product->getProductUrl()) ?>"
119+
<a href="<?= $escaper->escapeUrl($_product->getProductUrl()) ?>"
118120
title="<?= /* @noEscape */ $_productNameStripped ?>"
119-
class="action more"><?= $block->escapeHtml(__('Learn More')) ?></a>
121+
class="action more"><?= $escaper->escapeHtml(__('Learn More')) ?></a>
120122
</div>
121123
<?php endif; ?>
122124
</div>
@@ -132,7 +134,7 @@ $_helper = $this->helper(Magento\Catalog\Helper\Output::class);
132134
{
133135
"[data-role=tocart-form], .form.map.checkout": {
134136
"catalogAddToCart": {
135-
"product_sku": "<?= $block->escapeJs($_product->getSku()) ?>"
137+
"product_sku": "<?= $escaper->escapeJs($_product->getSku()) ?>"
136138
}
137139
}
138140
}

app/code/Magento/Customer/Test/Unit/Block/Widget/DobTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,15 @@ public function testGetDateFormat(string $locale, string $expectedFormat)
354354
public function getDateFormatDataProvider(): array
355355
{
356356
return [
357-
['ar_SA', 'd/M/y'],
357+
[
358+
'ar_SA',
359+
preg_replace(
360+
'/[^MmDdYy\/\.\-]/',
361+
'',
362+
(new \IntlDateFormatter('ar_SA', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE))
363+
->getPattern()
364+
)
365+
],
358366
[Resolver::DEFAULT_LOCALE, self::DATE_FORMAT],
359367
];
360368
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Framework\View\TemplateEngine;
10+
11+
use Magento\Framework\View\Element\BlockInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Testing .phtml templating.
17+
*/
18+
class PhpTest extends TestCase
19+
{
20+
/**
21+
* @var Php
22+
*/
23+
private $templateEngine;
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
protected function setUp()
29+
{
30+
$objectManager = Bootstrap::getObjectManager();
31+
$this->templateEngine = $objectManager->get(Php::class);
32+
}
33+
34+
/**
35+
* See that templates get access to certain variables.
36+
*
37+
* @return void
38+
*/
39+
public function testVariablesAvailable(): void
40+
{
41+
$block = new class implements BlockInterface {
42+
/**
43+
* @inheritDoc
44+
*/
45+
public function toHtml()
46+
{
47+
return '<b>BLOCK</b>';
48+
}
49+
};
50+
51+
$rendered = $this->templateEngine->render($block, __DIR__ .'/../_files/test_template.phtml');
52+
$this->assertEquals(
53+
'<p>This template has access to &lt;b&gt;$escaper&lt;/b&gt; and $block &quot;<b>BLOCK</b>&quot;</p>'
54+
.PHP_EOL,
55+
$rendered
56+
);
57+
}
58+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
?>
7+
<?php
8+
// phpcs:disable
9+
?>
10+
<?php
11+
/**
12+
* Template meant for testing.
13+
*
14+
* @var \Magento\Framework\View\Element\BlockInterface $block
15+
* @var \Magento\Framework\Escaper $escaper
16+
*/
17+
?>
18+
<p>This template has access to <?= $escaper->escapeHtml('<b>$escaper</b>') ?> and $block &quot;<?= $block->toHtml() ?>&quot;</p>

lib/internal/Magento/Framework/Encryption/Test/Unit/EncryptorTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ public function testGetHashRandomSaltSpecifiedLength(): void
135135
*
136136
* @dataProvider validateHashDataProvider
137137
*/
138-
public function testValidateHash($password, $hash, $expected): void
138+
public function testValidateHash($password, $hash, $expected, int $requiresVersion): void
139139
{
140+
if ($requiresVersion > $this->encryptor->getLatestHashVersion()) {
141+
$this->markTestSkipped('On current installation encryptor does not support algo #' .$requiresVersion);
142+
}
140143
$actual = $this->encryptor->validateHash($password, $hash);
141144
$this->assertEquals($expected, $actual);
142145
}
@@ -149,10 +152,14 @@ public function testValidateHash($password, $hash, $expected): void
149152
public function validateHashDataProvider(): array
150153
{
151154
return [
152-
['password', 'hash:salt:1', false],
153-
['password', '67a1e09bb1f83f5007dc119c14d663aa:salt:0', true],
154-
['password', '13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1', true],
155-
['password', 'c6aad9e058f6c4b06187c06d2b69bf506a786af030f81fb6d83778422a68205e:salt:1:2', true],
155+
['password', 'hash:salt:1', false, 1],
156+
['password', '67a1e09bb1f83f5007dc119c14d663aa:salt:0', true, 0],
157+
['password', '13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1', true, 1],
158+
//Hashes after customer:hash:upgrade command issued
159+
//Upgraded from version #1 to #2
160+
['password', 'c6aad9e058f6c4b06187c06d2b69bf506a786af030f81fb6d83778422a68205e:salt:1:2', true, 2],
161+
//From #0 to #1
162+
['password', '3b68ca4706cbae291455e4340478076c1e1618e742b6144cfcc3e50f648903e4:salt:0:1', true, 1]
156163
];
157164
}
158165

lib/internal/Magento/Framework/Locale/Test/Unit/TranslatedListsTest.php

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,36 @@ class TranslatedListsTest extends TestCase
4444
* @var array
4545
*/
4646
private $expectedLocales = [
47-
'en_US' => 'English (United States)',
48-
'en_GB' => 'English (United Kingdom)',
49-
'uk_UA' => 'Ukrainian (Ukraine)',
50-
'de_DE' => 'German (Germany)',
51-
'sr_Cyrl_RS' => 'Serbian (Cyrillic, Serbia)',
52-
'sr_Latn_RS' => 'Serbian (Latin, Serbia)'
47+
'en_US',
48+
'en_GB',
49+
'uk_UA',
50+
'de_DE',
51+
'sr_Cyrl_RS',
52+
'sr_Latn_RS'
5353
];
5454

5555
/**
56-
* @var array
56+
* @var string[]
57+
*/
58+
private $languages = [
59+
'en_US' => 'English',
60+
'en_GB' => 'English',
61+
'uk_UA' => 'Ukrainian',
62+
'de_DE' => 'German',
63+
'sr_Cyrl_RS' => 'Serbian',
64+
'sr_Latn_RS' => 'Serbian'
65+
];
66+
67+
/**
68+
* @var string[]
5769
*/
58-
private $expectedTranslatedLocales = [
59-
'en_US' => 'English (United States) / English (United States)',
60-
'en_GB' => 'English (United Kingdom) / English (United Kingdom)',
61-
'uk_UA' => 'українська (Україна) / Ukrainian (Ukraine)',
62-
'de_DE' => 'Deutsch (Deutschland) / German (Germany)',
63-
'sr_Cyrl_RS' => 'српски (ћирилица, Србија) / Serbian (Cyrillic, Serbia)',
64-
'sr_Latn_RS' => 'Srpski (latinica, Srbija) / Serbian (Latin, Serbia)'
70+
private $countries = [
71+
'en_US' => 'United States',
72+
'en_GB' => 'United Kingdom',
73+
'uk_UA' => 'Ukraine',
74+
'de_DE' => 'Germany',
75+
'sr_Cyrl_RS' => 'Serbia',
76+
'sr_Latn_RS' => 'Serbia'
6577
];
6678

6779
protected function setUp()
@@ -168,20 +180,22 @@ public function testGetOptionTimezones()
168180

169181
public function testGetOptionLocales()
170182
{
183+
$expected = $this->getExpectedLocales();
171184
$locales = array_intersect(
172-
$this->expectedLocales,
185+
$expected,
173186
$this->convertOptionLocales($this->listsModel->getOptionLocales())
174187
);
175-
$this->assertEquals($this->expectedLocales, $locales);
188+
$this->assertEquals($expected, $locales);
176189
}
177190

178191
public function testGetTranslatedOptionLocales()
179192
{
193+
$expected = $this->getExpectedTranslatedLocales();
180194
$locales = array_intersect(
181-
$this->expectedTranslatedLocales,
195+
$expected,
182196
$this->convertOptionLocales($this->listsModel->getTranslatedOptionLocales())
183197
);
184-
$this->assertEquals($this->expectedTranslatedLocales, $locales);
198+
$this->assertEquals($expected, $locales);
185199
}
186200

187201
/**
@@ -198,4 +212,42 @@ private function convertOptionLocales($optionLocales): array
198212

199213
return $result;
200214
}
215+
216+
/**
217+
* Expected translated locales list.
218+
*
219+
* @return string[]
220+
*/
221+
private function getExpectedTranslatedLocales(): array
222+
{
223+
$expected = [];
224+
foreach ($this->expectedLocales as $locale) {
225+
$script = \Locale::getDisplayScript($locale);
226+
$scriptTranslated = $script ? \Locale::getDisplayScript($locale, $locale) .', ' : '';
227+
$expected[$locale] = ucwords(\Locale::getDisplayLanguage($locale, $locale))
228+
. ' (' . $scriptTranslated
229+
. \Locale::getDisplayRegion($locale, $locale) . ') / '
230+
. $this->languages[$locale]
231+
. ' (' . ($script ? $script .', ' : '') . $this->countries[$locale] . ')';
232+
}
233+
234+
return $expected;
235+
}
236+
237+
/**
238+
* Expected locales list.
239+
*
240+
* @return string[]
241+
*/
242+
private function getExpectedLocales(): array
243+
{
244+
$expected = [];
245+
foreach ($this->expectedLocales as $locale) {
246+
$script = \Locale::getScript($locale);
247+
$scriptDisplayed = $script ? \Locale::getDisplayScript($locale) . ', ' : '';
248+
$expected[$locale] = $this->languages[$locale] .' (' .$scriptDisplayed .$this->countries[$locale] .')';
249+
}
250+
251+
return $expected;
252+
}
201253
}

0 commit comments

Comments
 (0)