|
5 | 5 | */
|
6 | 6 | namespace Magento\Catalog\Helper\Product;
|
7 | 7 |
|
| 8 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
8 | 9 | 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; |
9 | 16 |
|
10 | 17 | /**
|
11 | 18 | * @magentoAppArea frontend
|
@@ -33,10 +40,15 @@ class ViewTest extends \PHPUnit\Framework\TestCase
|
33 | 40 | */
|
34 | 41 | protected $objectManager;
|
35 | 42 |
|
| 43 | + /** |
| 44 | + * @var DataFixtureStorage |
| 45 | + */ |
| 46 | + private $fixtures; |
| 47 | + |
36 | 48 | protected function setUp(): void
|
37 | 49 | {
|
38 | 50 | $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
39 |
| - |
| 51 | + $this->fixtures = DataFixtureStorageManager::getStorage(); |
40 | 52 | $this->objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('frontend');
|
41 | 53 | $this->objectManager->get(\Magento\Framework\App\Http\Context::class)
|
42 | 54 | ->setValue(Context::CONTEXT_AUTH, false, false);
|
@@ -124,6 +136,71 @@ public function testPrepareAndRender()
|
124 | 136 | );
|
125 | 137 | }
|
126 | 138 |
|
| 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 not be substituted with any other data and should not be |
| 173 | + * rendered on the product HTML sources |
| 174 | + * |
| 175 | + * @return void |
| 176 | + * @throws LocalizedException |
| 177 | + * @throws NoSuchEntityException |
| 178 | + */ |
| 179 | + #[ |
| 180 | + AppIsolation(true), |
| 181 | + DataFixture(ProductFixture::class, ['meta_description' => ''], 'p1'), |
| 182 | + ] |
| 183 | + public function testEmptyProductMetaDescriptionShouldNotBeSubstitutedAndRendered() |
| 184 | + { |
| 185 | + $product = $this->fixtures->get('p1'); |
| 186 | + $metaDescription = '<meta name="description" content="'; |
| 187 | + |
| 188 | + $this->objectManager->get(\Magento\Framework\App\RequestInterface::class)->setParam('id', $product->getId()); |
| 189 | + $this->_helper->prepareAndRender($this->page, $product->getId(), $this->_controller); |
| 190 | + |
| 191 | + /** @var \Magento\TestFramework\Response $response */ |
| 192 | + $response = $this->objectManager->get(\Magento\TestFramework\Response::class); |
| 193 | + |
| 194 | + $this->page->renderResult($response); |
| 195 | + |
| 196 | + $this->assertNotEmpty($response->getBody()); |
| 197 | + $this->assertStringNotContainsStringIgnoringCase( |
| 198 | + $metaDescription, |
| 199 | + $response->getBody(), |
| 200 | + 'Empty meta description should not be substituted or rendered' |
| 201 | + ); |
| 202 | + } |
| 203 | + |
127 | 204 | /**
|
128 | 205 | * @magentoAppIsolation enabled
|
129 | 206 | */
|
|
0 commit comments