|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\User\Test\Unit\Controller\Adminhtml\User; |
| 9 | + |
| 10 | +use Magento\Backend\App\Action\Context; |
| 11 | +use Magento\Backend\Model\Session; |
| 12 | +use Magento\Framework\App\Request\Http; |
| 13 | +use Magento\Framework\App\Response\RedirectInterface; |
| 14 | +use Magento\Framework\App\ResponseInterface; |
| 15 | +use Magento\Framework\Message\ManagerInterface; |
| 16 | +use Magento\Framework\ObjectManagerInterface; |
| 17 | +use Magento\User\Controller\Adminhtml\User\Save; |
| 18 | +use Magento\User\Model\User; |
| 19 | +use Magento\User\Model\UserFactory; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class SaveTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var Save|MockObject |
| 27 | + */ |
| 28 | + private $controller; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Http|MockObject |
| 32 | + */ |
| 33 | + private $requestMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ManagerInterface|MockObject |
| 37 | + */ |
| 38 | + private $messageManagerMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var UserFactory|MockObject |
| 42 | + */ |
| 43 | + private $userFactoryMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var User|MockObject |
| 47 | + */ |
| 48 | + private $userModelMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ObjectManagerInterface|MockObject |
| 52 | + */ |
| 53 | + private $objectManagerMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var Session|MockObject |
| 57 | + */ |
| 58 | + private $sessionMock; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var Context|MockObject |
| 62 | + */ |
| 63 | + private $contextMock; |
| 64 | + |
| 65 | + protected function setUp(): void |
| 66 | + { |
| 67 | + $this->requestMock = $this->createMock(Http::class); |
| 68 | + $this->messageManagerMock = $this->createMock(ManagerInterface::class); |
| 69 | + $this->userFactoryMock = $this->createPartialMock(UserFactory::class, ['create']); |
| 70 | + $this->userModelMock = $this->getMockBuilder(User::class) |
| 71 | + ->disableOriginalConstructor() |
| 72 | + ->onlyMethods(['isObjectNew', 'load', 'setData', 'validate']) |
| 73 | + ->addMethods(['setRoleId']) |
| 74 | + ->getMock(); |
| 75 | + $this->objectManagerMock = $this->createMock(ObjectManagerInterface::class); |
| 76 | + $this->sessionMock = $this->getMockBuilder(Session::class) |
| 77 | + ->disableOriginalConstructor() |
| 78 | + ->addMethods(['setUserData']) |
| 79 | + ->getMock(); |
| 80 | + // // @phpcsSuppress Magento2.Security.GlobalState |
| 81 | + $registryMock = $this->createMock(\Magento\Framework\Registry::class); |
| 82 | + $this->contextMock = $this->createMock(Context::class); |
| 83 | + $this->userFactoryMock->expects($this->once()) |
| 84 | + ->method('create') |
| 85 | + ->willReturn($this->userModelMock); |
| 86 | + $responseMock = $this->createMock(ResponseInterface::class); |
| 87 | + $redirectMock = $this->createMock(RedirectInterface::class); |
| 88 | + $this->contextMock->expects($this->once()) |
| 89 | + ->method('getRequest') |
| 90 | + ->willReturn($this->requestMock); |
| 91 | + $this->contextMock->expects($this->once()) |
| 92 | + ->method('getMessageManager') |
| 93 | + ->willReturn($this->messageManagerMock); |
| 94 | + $this->contextMock->expects($this->once()) |
| 95 | + ->method('getResponse') |
| 96 | + ->willReturn($responseMock); |
| 97 | + $this->contextMock->expects($this->once()) |
| 98 | + ->method('getRedirect') |
| 99 | + ->willReturn($redirectMock); |
| 100 | + $this->contextMock->expects($this->once()) |
| 101 | + ->method('getSession') |
| 102 | + ->willReturn($this->sessionMock); |
| 103 | + $this->contextMock->expects($this->once()) |
| 104 | + ->method('getObjectManager') |
| 105 | + ->willReturn($this->objectManagerMock); |
| 106 | + $this->controller = $this->getMockBuilder(Save::class) |
| 107 | + ->setConstructorArgs([ |
| 108 | + 'context' => $this->contextMock, |
| 109 | + 'userFactory' => $this->userFactoryMock, |
| 110 | + 'coreRegistry' => $registryMock |
| 111 | + ]) |
| 112 | + ->onlyMethods(['redirectToEdit']) |
| 113 | + ->getMock(); |
| 114 | + } |
| 115 | + |
| 116 | + public function testExecuteValidationFailure() |
| 117 | + { |
| 118 | + $userId = 1; |
| 119 | + $postData = ['username' => 'testuser']; |
| 120 | + $this->requestMock->expects($this->once()) |
| 121 | + ->method('getParam') |
| 122 | + ->with('user_id') |
| 123 | + ->willReturn($userId); |
| 124 | + $this->requestMock->expects($this->once()) |
| 125 | + ->method('getPostValue') |
| 126 | + ->willReturn($postData); |
| 127 | + $this->userModelMock->expects($this->once()) |
| 128 | + ->method('load') |
| 129 | + ->with($userId) |
| 130 | + ->willReturnSelf(); |
| 131 | + $this->userModelMock->expects($this->once()) |
| 132 | + ->method('isObjectNew') |
| 133 | + ->willReturn(false); |
| 134 | + $this->userModelMock->expects($this->once()) |
| 135 | + ->method('setData') |
| 136 | + ->willReturnSelf(); |
| 137 | + $this->userModelMock->expects($this->once()) |
| 138 | + ->method('validate') |
| 139 | + ->willReturn(['Validation error message']); |
| 140 | + $this->messageManagerMock->expects($this->once()) |
| 141 | + ->method('addError') |
| 142 | + ->with('Validation error message'); |
| 143 | + $this->sessionMock->expects($this->once()) |
| 144 | + ->method('setUserData') |
| 145 | + ->with($postData); |
| 146 | + $this->controller->expects($this->once()) |
| 147 | + ->method('redirectToEdit') |
| 148 | + ->with($this->userModelMock, $postData) |
| 149 | + ->will($this->returnCallback(function () use ($postData) { |
| 150 | + $this->sessionMock->setUserData($postData); |
| 151 | + })); |
| 152 | + |
| 153 | + $this->controller->execute(); |
| 154 | + } |
| 155 | +} |
0 commit comments