|
12 | 12 | use Magento\Framework\View\Element\UiComponentFactory;
|
13 | 13 | use Magento\Ui\Api\BookmarkManagementInterface;
|
14 | 14 | use Magento\Ui\Api\BookmarkRepositoryInterface;
|
| 15 | +use Magento\Ui\Api\Data\BookmarkInterface; |
15 | 16 | use Magento\Ui\Api\Data\BookmarkInterfaceFactory;
|
16 | 17 | use Magento\Ui\Controller\Adminhtml\Bookmark\Save;
|
17 | 18 | use Magento\Backend\App\Action\Context;
|
18 | 19 | use PHPUnit\Framework\MockObject\MockObject;
|
19 | 20 | use PHPUnit\Framework\TestCase;
|
20 | 21 |
|
21 | 22 | /**
|
22 |
| - * Save controller test. |
| 23 | + * Bookmark Save controller test. |
| 24 | + * |
| 25 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
23 | 26 | */
|
24 | 27 | class SaveTest extends TestCase
|
25 | 28 | {
|
@@ -111,4 +114,87 @@ public function testExecuteWontBeExecutedWhenNoUserIdInContext(): void
|
111 | 114 |
|
112 | 115 | $this->model->execute();
|
113 | 116 | }
|
| 117 | + |
| 118 | + /** |
| 119 | + * Tests that on bookmark switch the previous bookmark config gets updated with the current bookmark config |
| 120 | + * And that the selected bookmark is set as "current" |
| 121 | + * |
| 122 | + * @return void |
| 123 | + */ |
| 124 | + public function testExecuteForCurrentBookmarkUpdate() : void |
| 125 | + { |
| 126 | + $updatedConfig = '{"views":{"bookmark1":{"data":{"data":["config"]}}}}'; |
| 127 | + $selectedIdentifier = 'bookmark2'; |
| 128 | + |
| 129 | + $this->userContext->method('getUserId')->willReturn(1); |
| 130 | + $bookmark = $this->getMockForAbstractClass(BookmarkInterface::class); |
| 131 | + $this->bookmarkFactory->expects($this->once())->method('create')->willReturn($bookmark); |
| 132 | + |
| 133 | + $request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class); |
| 134 | + $request->expects($this->atLeast(2)) |
| 135 | + ->method('getParam') |
| 136 | + ->withConsecutive(['data'], ['namespace']) |
| 137 | + ->willReturnOnConsecutiveCalls( |
| 138 | + '{"' . Save::ACTIVE_IDENTIFIER. '":"' . $selectedIdentifier . '"}', |
| 139 | + 'product_listing' |
| 140 | + ); |
| 141 | + |
| 142 | + $reflectionProperty = new \ReflectionProperty($this->model, '_request'); |
| 143 | + $reflectionProperty->setAccessible(true); |
| 144 | + $reflectionProperty->setValue($this->model, $request); |
| 145 | + |
| 146 | + $current = $this->createBookmark(); |
| 147 | + $bookmark1 = $this->createBookmark('bookmark1', '1', 'bookmark1_config'); |
| 148 | + $bookmark2 = $this->createBookmark($selectedIdentifier, '0', $selectedIdentifier .'_config'); |
| 149 | + |
| 150 | + $searchResult = $this->createMock(\Magento\Ui\Api\Data\BookmarkSearchResultsInterface::class); |
| 151 | + $searchResult->expects($this->atLeastOnce()) |
| 152 | + ->method('getItems') |
| 153 | + ->willReturn([$current, $bookmark1, $bookmark2]); |
| 154 | + $this->bookmarkManagement->expects($this->once())->method('loadByNamespace')->willReturn($searchResult); |
| 155 | + $bookmark1->expects($this->once())->method('setConfig')->with($updatedConfig); |
| 156 | + $bookmark1->expects($this->once())->method('setCurrent')->with(false); |
| 157 | + $bookmark2->expects($this->once())->method('setCurrent')->with(true); |
| 158 | + $this->model->execute(); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Creates a bookmark mock object |
| 163 | + * |
| 164 | + * @param string $identifier |
| 165 | + * @param string $current |
| 166 | + * @param string $config |
| 167 | + * @return BookmarkInterface|MockObject |
| 168 | + */ |
| 169 | + private function createBookmark(string $identifier = 'current', string $current = '0', string $config = 'config') |
| 170 | + { |
| 171 | + $bookmark = $this->getMockBuilder(BookmarkInterface::class) |
| 172 | + ->disableOriginalConstructor() |
| 173 | + ->setMethods(['getCurrent', 'getIdentifier']) |
| 174 | + ->getMockForAbstractClass(); |
| 175 | + $bookmark->expects($this->any())->method('getCurrent')->willReturn($current); |
| 176 | + $bookmark->expects($this->any())->method('getIdentifier')->willReturn($identifier); |
| 177 | + $configData = [ |
| 178 | + 'views' => [ |
| 179 | + $identifier => [ |
| 180 | + 'data' => [ |
| 181 | + $config |
| 182 | + ] |
| 183 | + ] |
| 184 | + ] |
| 185 | + ]; |
| 186 | + |
| 187 | + if ($identifier === 'current') { |
| 188 | + $configData = [ |
| 189 | + $identifier => [ |
| 190 | + 'data' => [ |
| 191 | + $config |
| 192 | + ] |
| 193 | + ] |
| 194 | + ]; |
| 195 | + } |
| 196 | + |
| 197 | + $bookmark->expects($this->any())->method('getConfig')->willReturn($configData); |
| 198 | + return $bookmark; |
| 199 | + } |
114 | 200 | }
|
0 commit comments