|
1 | 1 | <?php
|
2 | 2 | /**
|
3 |
| - * Copyright © Magento, Inc. All rights reserved. |
4 |
| - * See COPYING.txt for license details. |
| 3 | + * Copyright 2015 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */
|
6 | 6 | declare(strict_types=1);
|
7 | 7 |
|
8 | 8 | namespace Magento\Review\Test\Unit\Block;
|
9 | 9 |
|
10 | 10 | use Magento\Catalog\Api\Data\ProductInterface;
|
11 | 11 | use Magento\Catalog\Api\ProductRepositoryInterface;
|
| 12 | +use Magento\Customer\Model\Url; |
12 | 13 | use Magento\Framework\App\RequestInterface;
|
13 | 14 | use Magento\Framework\DataObject;
|
14 | 15 | use Magento\Framework\Serialize\Serializer\Json;
|
15 | 16 | use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
|
| 17 | +use Magento\Framework\Url\EncoderInterface; |
16 | 18 | use Magento\Framework\UrlInterface;
|
17 | 19 | use Magento\Framework\View\Element\Template\Context;
|
18 | 20 | use Magento\Review\Block\Form;
|
19 | 21 | use Magento\Review\Helper\Data;
|
20 | 22 | use Magento\Store\Model\StoreManagerInterface;
|
21 | 23 | use PHPUnit\Framework\MockObject\MockObject;
|
22 | 24 | 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; |
23 | 28 |
|
| 29 | +/** |
| 30 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 31 | + */ |
24 | 32 | class FormTest extends TestCase
|
25 | 33 | {
|
26 | 34 | /** @var Form */
|
@@ -129,6 +137,34 @@ public function testGetProductInfo()
|
129 | 137 | $this->assertSame($productMock, $this->object->getProductInfo());
|
130 | 138 | }
|
131 | 139 |
|
| 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 | + |
132 | 168 | /**
|
133 | 169 | * @param bool $isSecure
|
134 | 170 | * @param string $actionUrl
|
@@ -173,4 +209,159 @@ public function testGetJsLayout()
|
173 | 209 | ->willReturn(json_encode($jsLayout));
|
174 | 210 | $this->assertEquals('{"some-layout":"layout information"}', $this->object->getJsLayout());
|
175 | 211 | }
|
| 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 | + } |
176 | 367 | }
|
0 commit comments