|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Review\Test\Unit\Controller\Adminhtml\Product; |
| 9 | + |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use PHPUnit\Framework\MockObject\MockObject; |
| 12 | +use Magento\Review\Controller\Adminhtml\Product\MassUpdateStatus; |
| 13 | +use Magento\Backend\App\Action\Context; |
| 14 | +use Magento\Framework\Registry; |
| 15 | +use Magento\Framework\Controller\ResultFactory; |
| 16 | +use Magento\Review\Model\RatingFactory; |
| 17 | +use Magento\Review\Model\Review; |
| 18 | +use Magento\Review\Model\ResourceModel\Review\Collection as ReviewCollection; |
| 19 | +use Magento\Review\Model\ResourceModel\Review\CollectionFactory; |
| 20 | +use Magento\Review\Model\ReviewFactory; |
| 21 | +use Magento\Framework\App\RequestInterface; |
| 22 | +use Magento\Framework\Message\ManagerInterface; |
| 23 | +use Magento\Backend\Model\View\Result\Redirect; |
| 24 | +use Magento\Review\Model\ResourceModel\Review as ReviewResourceModel; |
| 25 | + |
| 26 | +class MassUpdateStatusTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var MassUpdateStatus |
| 30 | + */ |
| 31 | + private $massUpdateStatus; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Collection|MockObject |
| 35 | + */ |
| 36 | + private $collectionMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var CollectionFactory|MockObject |
| 40 | + */ |
| 41 | + private $collectionFactoryMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var RequestInterface|MockObject |
| 45 | + */ |
| 46 | + private $requestMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var ManagerInterface|MockObject |
| 50 | + */ |
| 51 | + private $messageManagerMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var ResultFactory|MockObject |
| 55 | + */ |
| 56 | + private $resultRedirectFactory; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var Redirect|MockObject |
| 60 | + */ |
| 61 | + private $redirectMock; |
| 62 | + |
| 63 | + /** |
| 64 | + * @inheritdoc |
| 65 | + */ |
| 66 | + protected function setUp(): void |
| 67 | + { |
| 68 | + $this->collectionMock = $this->createMock(ReviewCollection::class); |
| 69 | + $resource = $this->createMock(ReviewResourceModel::class); |
| 70 | + $resource->method('getIdFieldName') |
| 71 | + ->willReturn('id'); |
| 72 | + $this->collectionMock->expects($this->once()) |
| 73 | + ->method('getResource') |
| 74 | + ->willReturn($resource); |
| 75 | + $this->collectionFactoryMock = $this->createMock(CollectionFactory::class); |
| 76 | + $contextMock = $this->createMock(Context::class); |
| 77 | + $this->requestMock = $this->createMock(RequestInterface::class); |
| 78 | + $contextMock->method('getRequest') |
| 79 | + ->willReturn($this->requestMock); |
| 80 | + $this->messageManagerMock = $this->createMock(ManagerInterface::class); |
| 81 | + $contextMock->method('getMessageManager') |
| 82 | + ->willReturn($this->messageManagerMock); |
| 83 | + $this->resultRedirectFactory = $this->createMock(ResultFactory::class); |
| 84 | + $this->redirectMock = $this->createMock(Redirect::class); |
| 85 | + $this->resultRedirectFactory->method('create')->willReturn($this->redirectMock); |
| 86 | + $contextMock->method('getResultFactory') |
| 87 | + ->willReturn($this->resultRedirectFactory); |
| 88 | + $this->massUpdateStatus = new MassUpdateStatus( |
| 89 | + $contextMock, |
| 90 | + $this->createMock(Registry::class), |
| 91 | + $this->createMock(ReviewFactory::class), |
| 92 | + $this->createMock(RatingFactory::class), |
| 93 | + $this->collectionFactoryMock |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return void |
| 99 | + */ |
| 100 | + public function testExecute(): void |
| 101 | + { |
| 102 | + $this->requestMock->expects(self::atLeastOnce()) |
| 103 | + ->method('getParam') |
| 104 | + ->willReturnMap( |
| 105 | + [ |
| 106 | + ['reviews', null, [1, 2]], |
| 107 | + ['status', null, Review::STATUS_APPROVED], |
| 108 | + ['ret', null, 'index'], |
| 109 | + |
| 110 | + ] |
| 111 | + ); |
| 112 | + $this->collectionFactoryMock->expects($this->once()) |
| 113 | + ->method('create') |
| 114 | + ->willReturn($this->collectionMock); |
| 115 | + $this->collectionMock->expects($this->once()) |
| 116 | + ->method('addStoreData') |
| 117 | + ->willReturnSelf(); |
| 118 | + $this->collectionMock->expects($this->once()) |
| 119 | + ->method('addFieldToFilter') |
| 120 | + ->with('main_table.id', [1, 2]) |
| 121 | + ->willReturnSelf(); |
| 122 | + $modelMock = $this->getMockBuilder(Review::class) |
| 123 | + ->disableOriginalConstructor() |
| 124 | + ->addMethods(['setStatusId']) |
| 125 | + ->onlyMethods(['_getResource']) |
| 126 | + ->getMock(); |
| 127 | + $modelMock->expects($this->once()) |
| 128 | + ->method('setStatusId') |
| 129 | + ->with(Review::STATUS_APPROVED) |
| 130 | + ->willReturnSelf(); |
| 131 | + $modelMock->method('_getResource') |
| 132 | + ->willReturn($this->createMock(ReviewResourceModel::class)); |
| 133 | + $this->collectionMock->expects($this->once())->method('getIterator') |
| 134 | + ->willReturn(new \ArrayObject([$modelMock])); |
| 135 | + $this->messageManagerMock->expects($this->once()) |
| 136 | + ->method('addSuccessMessage') |
| 137 | + ->with(__('A total of %1 record(s) have been updated.', 2)); |
| 138 | + $this->massUpdateStatus->execute(); |
| 139 | + } |
| 140 | +} |
0 commit comments