|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * Copyright 2023 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * NOTICE: All information contained herein is, and remains |
| 7 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 8 | + * and technical concepts contained herein are proprietary to Adobe |
| 9 | + * and its suppliers and are protected by all applicable intellectual |
| 10 | + * property laws, including trade secret and copyright laws. |
| 11 | + * Dissemination of this information or reproduction of this material |
| 12 | + * is strictly forbidden unless prior written permission is obtained |
| 13 | + * from Adobe. |
| 14 | + * ************************************************************************ |
| 15 | + */ |
| 16 | +declare(strict_types=1); |
| 17 | + |
| 18 | +namespace Magento\Review\Test\Unit\Controller\Adminhtml\Product; |
| 19 | + |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use Magento\Review\Controller\Adminhtml\Product\MassUpdateStatus; |
| 23 | +use Magento\Backend\App\Action\Context; |
| 24 | +use Magento\Framework\Registry; |
| 25 | +use Magento\Framework\Controller\ResultFactory; |
| 26 | +use Magento\Review\Model\RatingFactory; |
| 27 | +use Magento\Review\Model\Review; |
| 28 | +use Magento\Review\Model\ResourceModel\Review\Collection as ReviewCollection; |
| 29 | +use Magento\Review\Model\ResourceModel\Review\CollectionFactory; |
| 30 | +use Magento\Review\Model\ReviewFactory; |
| 31 | +use Magento\Framework\App\RequestInterface; |
| 32 | +use Magento\Framework\Message\ManagerInterface; |
| 33 | +use Magento\Backend\Model\View\Result\Redirect; |
| 34 | +use Magento\Review\Model\ResourceModel\Review as ReviewResourceModel; |
| 35 | + |
| 36 | +/** |
| 37 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 38 | + */ |
| 39 | +class MassUpdateStatusTest extends TestCase |
| 40 | +{ |
| 41 | + /** |
| 42 | + * @var MassUpdateStatus |
| 43 | + */ |
| 44 | + private $massUpdateStatus; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Collection|MockObject |
| 48 | + */ |
| 49 | + private $collectionMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var CollectionFactory|MockObject |
| 53 | + */ |
| 54 | + private $collectionFactoryMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var RequestInterface|MockObject |
| 58 | + */ |
| 59 | + private $requestMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var ManagerInterface|MockObject |
| 63 | + */ |
| 64 | + private $messageManagerMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var ResultFactory|MockObject |
| 68 | + */ |
| 69 | + private $resultRedirectFactory; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var Redirect|MockObject |
| 73 | + */ |
| 74 | + private $redirectMock; |
| 75 | + |
| 76 | + /** |
| 77 | + * @inheritdoc |
| 78 | + */ |
| 79 | + protected function setUp(): void |
| 80 | + { |
| 81 | + $this->collectionMock = $this->createMock(ReviewCollection::class); |
| 82 | + $resource = $this->createMock(ReviewResourceModel::class); |
| 83 | + $resource->method('getIdFieldName') |
| 84 | + ->willReturn('id'); |
| 85 | + $this->collectionMock->expects($this->once()) |
| 86 | + ->method('getResource') |
| 87 | + ->willReturn($resource); |
| 88 | + $this->collectionFactoryMock = $this->createMock(CollectionFactory::class); |
| 89 | + $contextMock = $this->createMock(Context::class); |
| 90 | + $this->requestMock = $this->createMock(RequestInterface::class); |
| 91 | + $contextMock->method('getRequest') |
| 92 | + ->willReturn($this->requestMock); |
| 93 | + $this->messageManagerMock = $this->createMock(ManagerInterface::class); |
| 94 | + $contextMock->method('getMessageManager') |
| 95 | + ->willReturn($this->messageManagerMock); |
| 96 | + $this->resultRedirectFactory = $this->createMock(ResultFactory::class); |
| 97 | + $this->redirectMock = $this->createMock(Redirect::class); |
| 98 | + $this->resultRedirectFactory->method('create')->willReturn($this->redirectMock); |
| 99 | + $contextMock->method('getResultFactory') |
| 100 | + ->willReturn($this->resultRedirectFactory); |
| 101 | + $this->massUpdateStatus = new MassUpdateStatus( |
| 102 | + $contextMock, |
| 103 | + $this->createMock(Registry::class), |
| 104 | + $this->createMock(ReviewFactory::class), |
| 105 | + $this->createMock(RatingFactory::class), |
| 106 | + $this->collectionFactoryMock |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @return void |
| 112 | + */ |
| 113 | + public function testExecute(): void |
| 114 | + { |
| 115 | + $this->requestMock->expects(self::atLeastOnce()) |
| 116 | + ->method('getParam') |
| 117 | + ->willReturnMap( |
| 118 | + [ |
| 119 | + ['reviews', null, [1, 2]], |
| 120 | + ['status', null, Review::STATUS_APPROVED], |
| 121 | + ['ret', null, 'index'], |
| 122 | + |
| 123 | + ] |
| 124 | + ); |
| 125 | + $this->collectionFactoryMock->expects($this->once()) |
| 126 | + ->method('create') |
| 127 | + ->willReturn($this->collectionMock); |
| 128 | + $this->collectionMock->expects($this->once()) |
| 129 | + ->method('addStoreData') |
| 130 | + ->willReturnSelf(); |
| 131 | + $this->collectionMock->expects($this->once()) |
| 132 | + ->method('addFieldToFilter') |
| 133 | + ->with('main_table.id', [1, 2]) |
| 134 | + ->willReturnSelf(); |
| 135 | + $modelMock = $this->getMockBuilder(Review::class) |
| 136 | + ->disableOriginalConstructor() |
| 137 | + ->addMethods(['setStatusId']) |
| 138 | + ->onlyMethods(['_getResource']) |
| 139 | + ->getMock(); |
| 140 | + $modelMock->expects($this->once()) |
| 141 | + ->method('setStatusId') |
| 142 | + ->with(Review::STATUS_APPROVED) |
| 143 | + ->willReturnSelf(); |
| 144 | + $modelMock->method('_getResource') |
| 145 | + ->willReturn($this->createMock(ReviewResourceModel::class)); |
| 146 | + $this->collectionMock->expects($this->once())->method('getIterator') |
| 147 | + ->willReturn(new \ArrayIterator([$modelMock])); |
| 148 | + $this->messageManagerMock->expects($this->once()) |
| 149 | + ->method('addSuccessMessage') |
| 150 | + ->with(__('A total of %1 record(s) have been updated.', 2)); |
| 151 | + $this->massUpdateStatus->execute(); |
| 152 | + } |
| 153 | +} |
0 commit comments