Skip to content

Commit dda5ede

Browse files
committed
ACP2E-2522: Block Content not rendering in PDP meta description
1 parent 2f5faa7 commit dda5ede

File tree

2 files changed

+79
-9
lines changed
  • app/code/Magento/Catalog/Helper/Product
  • dev/tests/integration/testsuite/Magento/Catalog/Helper/Product

2 files changed

+79
-9
lines changed

app/code/Magento/Catalog/Helper/Product/View.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,8 @@ private function preparePageMetadata(ResultPage $resultPage, $product)
130130
$pageConfig->setKeywords($product->getName());
131131
}
132132

133-
$description = $product->getMetaDescription();
134-
if ($description) {
135-
$pageConfig->setDescription($description);
136-
} else {
137-
$productDescription = is_string($product->getDescription()) ?
138-
$this->string->substr(strip_tags($product->getDescription()), 0, 255) : '';
139-
$pageConfig->setDescription($productDescription);
140-
}
133+
$description = $product->getMetaDescription() ?: $product->getName();
134+
$pageConfig->setDescription($description);
141135

142136
if ($this->_catalogProduct->canUseCanonicalTag()) {
143137
$pageConfig->addRemotePageAsset(

dev/tests/integration/testsuite/Magento/Catalog/Helper/Product/ViewTest.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
*/
66
namespace Magento\Catalog\Helper\Product;
77

8+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
89
use Magento\Customer\Model\Context;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\TestFramework\Fixture\AppIsolation;
13+
use Magento\TestFramework\Fixture\DataFixture;
14+
use Magento\TestFramework\Fixture\DataFixtureStorage;
15+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
916

1017
/**
1118
* @magentoAppArea frontend
@@ -33,10 +40,15 @@ class ViewTest extends \PHPUnit\Framework\TestCase
3340
*/
3441
protected $objectManager;
3542

43+
/**
44+
* @var DataFixtureStorage
45+
*/
46+
private $fixtures;
47+
3648
protected function setUp(): void
3749
{
3850
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
39-
51+
$this->fixtures = DataFixtureStorageManager::getStorage();
4052
$this->objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('frontend');
4153
$this->objectManager->get(\Magento\Framework\App\Http\Context::class)
4254
->setValue(Context::CONTEXT_AUTH, false, false);
@@ -124,6 +136,70 @@ public function testPrepareAndRender()
124136
);
125137
}
126138

139+
/**
140+
* Product meta description should be rendered on the product HTML sources as is, without changes or substitutions
141+
*
142+
* @return void
143+
* @throws LocalizedException
144+
* @throws NoSuchEntityException
145+
*/
146+
#[
147+
AppIsolation(true),
148+
DataFixture(ProductFixture::class, ['meta_description' => 'Product Meta Description'], 'p1'),
149+
]
150+
public function testProductMetaDescriptionShouldBeRenderedAsIs()
151+
{
152+
$product = $this->fixtures->get('p1');
153+
$metaDescription = '<meta name="description" content="Product Meta Description"/>';
154+
155+
$this->objectManager->get(\Magento\Framework\App\RequestInterface::class)->setParam('id', $product->getId());
156+
$this->_helper->prepareAndRender($this->page, $product->getId(), $this->_controller);
157+
158+
/** @var \Magento\TestFramework\Response $response */
159+
$response = $this->objectManager->get(\Magento\TestFramework\Response::class);
160+
161+
$this->page->renderResult($response);
162+
163+
$this->assertNotEmpty($response->getBody());
164+
$this->assertStringContainsString(
165+
$metaDescription,
166+
$response->getBody(),
167+
'Empty meta description should be rendered as is'
168+
);
169+
}
170+
171+
/**
172+
* If the product meta description is empty, it should be substituted with the product name
173+
*
174+
* @return void
175+
* @throws LocalizedException
176+
* @throws NoSuchEntityException
177+
*/
178+
#[
179+
AppIsolation(true),
180+
DataFixture(ProductFixture::class, ['meta_description' => ''], 'p1'),
181+
]
182+
public function testEmptyProductMetaDescriptionShouldNotBeSubstitutedAndRendered()
183+
{
184+
$product = $this->fixtures->get('p1');
185+
$metaDescription = sprintf('<meta name="description" content="%s"/>', $product->getName());
186+
187+
$this->objectManager->get(\Magento\Framework\App\RequestInterface::class)->setParam('id', $product->getId());
188+
$this->_helper->prepareAndRender($this->page, $product->getId(), $this->_controller);
189+
190+
/** @var \Magento\TestFramework\Response $response */
191+
$response = $this->objectManager->get(\Magento\TestFramework\Response::class);
192+
193+
$this->page->renderResult($response);
194+
195+
$this->assertNotEmpty($response->getBody());
196+
$this->assertStringContainsStringIgnoringCase(
197+
$metaDescription,
198+
$response->getBody(),
199+
'Empty meta description should be substituted with the product name'
200+
);
201+
}
202+
127203
/**
128204
* @magentoAppIsolation enabled
129205
*/

0 commit comments

Comments
 (0)