Skip to content

Commit bbac499

Browse files
committed
Merge remote-tracking branch 'origin/AC-15345' into spartans_pr_03092025
2 parents 5e4af73 + 3d63b11 commit bbac499

File tree

3 files changed

+222
-7
lines changed

3 files changed

+222
-7
lines changed

app/code/Magento/Review/Block/Form.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2013 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Review\Block;
77

@@ -214,6 +214,6 @@ public function getRegisterUrl()
214214
*/
215215
protected function getProductId()
216216
{
217-
return $this->getRequest()->getParam('id', false);
217+
return (int) $this->getRequest()->getParam('id', false);
218218
}
219219
}

app/code/Magento/Review/Test/Unit/Block/FormTest.php

Lines changed: 193 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Review\Test\Unit\Block;
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Customer\Model\Url;
1213
use Magento\Framework\App\RequestInterface;
1314
use Magento\Framework\DataObject;
1415
use Magento\Framework\Serialize\Serializer\Json;
1516
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
17+
use Magento\Framework\Url\EncoderInterface;
1618
use Magento\Framework\UrlInterface;
1719
use Magento\Framework\View\Element\Template\Context;
1820
use Magento\Review\Block\Form;
1921
use Magento\Review\Helper\Data;
2022
use Magento\Store\Model\StoreManagerInterface;
2123
use PHPUnit\Framework\MockObject\MockObject;
2224
use PHPUnit\Framework\TestCase;
25+
use Magento\Review\Model\RatingFactory;
26+
use Magento\Review\Model\Rating;
27+
use Magento\Review\Model\ResourceModel\Rating\Collection as RatingCollection;
2328

29+
/**
30+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
31+
*/
2432
class FormTest extends TestCase
2533
{
2634
/** @var Form */
@@ -129,6 +137,34 @@ public function testGetProductInfo()
129137
$this->assertSame($productMock, $this->object->getProductInfo());
130138
}
131139

140+
public function testGetProductInfoNonIntParam()
141+
{
142+
$productId = 3;
143+
$productIdNonInt = "3abc";
144+
$storeId = 1;
145+
146+
$this->storeManager->expects(
147+
$this->any()
148+
)->method(
149+
'getStore'
150+
)->willReturn(
151+
new DataObject(['id' => $storeId])
152+
);
153+
154+
$this->requestMock->expects($this->once())
155+
->method('getParam')
156+
->with('id', false)
157+
->willReturn($productIdNonInt);
158+
159+
$productMock = $this->getMockForAbstractClass(ProductInterface::class);
160+
$this->productRepository->expects($this->once())
161+
->method('getById')
162+
->with($productId, false, $storeId)
163+
->willReturn($productMock);
164+
165+
$this->assertSame($productMock, $this->object->getProductInfo());
166+
}
167+
132168
/**
133169
* @param bool $isSecure
134170
* @param string $actionUrl
@@ -173,4 +209,159 @@ public function testGetJsLayout()
173209
->willReturn(json_encode($jsLayout));
174210
$this->assertEquals('{"some-layout":"layout information"}', $this->object->getJsLayout());
175211
}
212+
213+
public function testGetRatingsReturnsPreparedCollection(): void
214+
{
215+
$storeId = 10;
216+
217+
$store = new DataObject(['id' => $storeId]);
218+
219+
$storeManager = $this->createMock(StoreManagerInterface::class);
220+
$storeManager->method('getStore')->willReturn($store);
221+
222+
$ratingCollection = $this->createMock(RatingCollection::class);
223+
$ratingCollection->expects($this->once())
224+
->method('addEntityFilter')
225+
->with('product')
226+
->willReturnSelf();
227+
$ratingCollection->expects($this->once())
228+
->method('setPositionOrder')
229+
->willReturnSelf();
230+
$ratingCollection->expects($this->once())
231+
->method('addRatingPerStoreName')
232+
->with($storeId)
233+
->willReturnSelf();
234+
$ratingCollection->expects($this->once())
235+
->method('setStoreFilter')
236+
->with($storeId)
237+
->willReturnSelf();
238+
$ratingCollection->expects($this->once())
239+
->method('setActiveFilter')
240+
->with(true)
241+
->willReturnSelf();
242+
$ratingCollection->expects($this->once())
243+
->method('load')
244+
->willReturnSelf();
245+
$ratingCollection->expects($this->once())
246+
->method('addOptionToItems')
247+
->willReturnSelf();
248+
249+
$rating = $this->createMock(Rating::class);
250+
$rating->method('getResourceCollection')->willReturn($ratingCollection);
251+
252+
$ratingFactory = $this->createMock(RatingFactory::class);
253+
$ratingFactory->method('create')->willReturn($rating);
254+
255+
$formBlock = $this->getFormBlockWithInjectedDependencies($storeManager, $ratingFactory);
256+
257+
$result = $formBlock->getRatings();
258+
259+
$this->assertSame($ratingCollection, $result);
260+
}
261+
262+
public function testGetRegisterUrl(): void
263+
{
264+
$expectedUrl = 'https://example.com/customer/account/create/';
265+
266+
$customerUrl = $this->getMockBuilder(Url::class)
267+
->disableOriginalConstructor()
268+
->onlyMethods(['getRegisterUrl'])
269+
->getMock();
270+
271+
$customerUrl->expects($this->once())
272+
->method('getRegisterUrl')
273+
->willReturn($expectedUrl);
274+
275+
$formBlock = $this->getFormBlockWithInjectedDependencies(
276+
$this->storeManager,
277+
$this->createMock(RatingFactory::class)
278+
);
279+
$this->setProtectedProperty($formBlock, 'customerUrl', $customerUrl);
280+
281+
$this->assertSame($expectedUrl, $formBlock->getRegisterUrl());
282+
}
283+
284+
public function testConstructSetsLoginLinkWhenGuestCannotWrite(): void
285+
{
286+
$currentUrl = 'https://example.com/current';
287+
$encoded = 'ENCODED_REFERER';
288+
$loginUrlBase = 'https://example.com/customer/account/login/';
289+
290+
$urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
291+
$urlBuilder->method('getUrl')
292+
->willReturnCallback(function ($route, $params = []) use ($currentUrl, $encoded, $loginUrlBase) {
293+
if ($route === '*/*/*') {
294+
$this->assertSame(['_current' => true], $params);
295+
return $currentUrl;
296+
}
297+
if ($route === 'customer/account/login/') {
298+
$this->assertArrayHasKey(Url::REFERER_QUERY_PARAM_NAME, $params);
299+
$this->assertSame($encoded, $params[Url::REFERER_QUERY_PARAM_NAME]);
300+
return $loginUrlBase . '?' . Url::REFERER_QUERY_PARAM_NAME . '=' . $encoded;
301+
}
302+
return '';
303+
});
304+
305+
$urlEncoder = $this->createMock(EncoderInterface::class);
306+
$urlEncoder->expects($this->once())
307+
->method('encode')
308+
->with($currentUrl . '#review-form')
309+
->willReturn($encoded);
310+
311+
$httpContext = $this->createMock(\Magento\Framework\App\Http\Context::class);
312+
$httpContext->method('getValue')
313+
->with(\Magento\Customer\Model\Context::CONTEXT_AUTH)
314+
->willReturn(false);
315+
316+
$reviewData = $this->createMock(\Magento\Review\Helper\Data::class);
317+
$reviewData->method('getIsGuestAllowToWrite')->willReturn(false);
318+
319+
$formBlock = $this->getMockBuilder(Form::class)
320+
->disableOriginalConstructor()
321+
->onlyMethods([])
322+
->getMock();
323+
324+
$this->setProtectedProperty($formBlock, '_urlBuilder', $urlBuilder);
325+
$this->setProtectedProperty($formBlock, 'urlEncoder', $urlEncoder);
326+
$this->setProtectedProperty($formBlock, 'httpContext', $httpContext);
327+
$this->setProtectedProperty($formBlock, '_reviewData', $reviewData);
328+
329+
$ref = new \ReflectionObject($formBlock);
330+
$method = $ref->getMethod('_construct');
331+
$method->setAccessible(true);
332+
$method->invoke($formBlock);
333+
334+
$expectedLoginUrl = $loginUrlBase . '?' . Url::REFERER_QUERY_PARAM_NAME . '=' . $encoded;
335+
$this->assertSame($expectedLoginUrl, $formBlock->getLoginLink());
336+
}
337+
338+
private function getFormBlockWithInjectedDependencies(
339+
StoreManagerInterface $storeManager,
340+
RatingFactory $ratingFactory
341+
): Form {
342+
$formBlock = $this->getMockBuilder(Form::class)
343+
->disableOriginalConstructor()
344+
->onlyMethods([])
345+
->getMock();
346+
347+
// Inject protected properties via reflection to avoid full framework context construction
348+
$this->setProtectedProperty($formBlock, '_storeManager', $storeManager);
349+
$this->setProtectedProperty($formBlock, '_ratingFactory', $ratingFactory);
350+
351+
return $formBlock;
352+
}
353+
354+
private function setProtectedProperty(object $object, string $property, mixed $value): void
355+
{
356+
$reflection = new \ReflectionObject($object);
357+
while ($reflection && !$reflection->hasProperty($property)) {
358+
$reflection = $reflection->getParentClass();
359+
if ($reflection === false) {
360+
$this->fail('Property ' . $property . ' not found in class hierarchy');
361+
}
362+
}
363+
$prop = $reflection->getProperty($property);
364+
$prop->setAccessible(true);
365+
$prop->setValue($object, $value);
366+
}
176367
}

dev/tests/integration/testsuite/Magento/Review/Block/FormTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2017 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Review\Block;
88

9+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
910
use Magento\Framework\App\Area;
1011
use Magento\Framework\App\Config\Value;
1112
use Magento\Framework\App\ReinitableConfig;
13+
use Magento\Framework\App\Request\Http;
1214
use Magento\Framework\App\State;
1315
use Magento\Framework\View\Element\ButtonLockManager;
16+
use Magento\TestFramework\Fixture\AppArea;
17+
use Magento\TestFramework\Fixture\DataFixture;
18+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
1419
use Magento\TestFramework\ObjectManager;
1520

1621
class FormTest extends \PHPUnit\Framework\TestCase
@@ -83,6 +88,25 @@ public static function getCorrectFlagDataProvider()
8388
];
8489
}
8590

91+
#[
92+
AppArea('frontend'),
93+
DataFixture(ProductFixture::class, as: 'product'),
94+
]
95+
public function testGetProductInfo()
96+
{
97+
$fixtures = DataFixtureStorageManager::getStorage();
98+
$product = $fixtures->get('product');
99+
100+
$form = $this->objectManager->create(Form::class);
101+
$form->getRequest()
102+
->setMethod(Http::METHOD_GET)
103+
->setParams(['id' => $product->getId() . "abc"]);
104+
105+
$productInfo = $form->getProductInfo();
106+
$this->assertEquals($product->getId(), $productInfo->getId());
107+
$this->assertEquals($product->getSku(), $productInfo->getSku());
108+
}
109+
86110
private function getObjectManager()
87111
{
88112
return \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

0 commit comments

Comments
 (0)