Skip to content

Commit d2abc27

Browse files
author
Anna Bukatar
committed
ACP2E-546: Grid Custom view column position getting changed when switch between the view
1 parent 5d9fe40 commit d2abc27

File tree

3 files changed

+107
-7
lines changed

3 files changed

+107
-7
lines changed

app/code/Magento/Ui/Controller/Adminhtml/Bookmark/Save.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Magento\Ui\Controller\Adminhtml\AbstractAction;
1818

1919
/**
20-
* Class Save action
20+
* Class Bookmark Save action
2121
*
2222
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2323
*/
@@ -26,11 +26,11 @@ class Save extends AbstractAction implements HttpPostActionInterface
2626
/**
2727
* Identifier for current bookmark
2828
*/
29-
const CURRENT_IDENTIFIER = 'current';
29+
public const CURRENT_IDENTIFIER = 'current';
3030

31-
const ACTIVE_IDENTIFIER = 'activeIndex';
31+
public const ACTIVE_IDENTIFIER = 'activeIndex';
3232

33-
const VIEWS_IDENTIFIER = 'views';
33+
public const VIEWS_IDENTIFIER = 'views';
3434

3535
/**
3636
* @var BookmarkRepositoryInterface
@@ -180,7 +180,22 @@ protected function updateBookmark(BookmarkInterface $bookmark, $identifier, $tit
180180
protected function updateCurrentBookmark($identifier)
181181
{
182182
$bookmarks = $this->bookmarkManagement->loadByNamespace($this->_request->getParam('namespace'));
183+
$currentConfig = null;
183184
foreach ($bookmarks->getItems() as $bookmark) {
185+
if ($bookmark->getIdentifier() === self::CURRENT_IDENTIFIER) {
186+
$current = $bookmark->getConfig();
187+
$currentConfig = $current[self::CURRENT_IDENTIFIER];
188+
break;
189+
}
190+
}
191+
192+
foreach ($bookmarks->getItems() as $bookmark) {
193+
if ($bookmark->getCurrent() && $currentConfig !== null) {
194+
$bookmarkConfig = $bookmark->getConfig();
195+
$bookmarkConfig['views'][$bookmark->getIdentifier()]['data'] = $currentConfig;
196+
$bookmark->setConfig($this->serializer->serialize($bookmarkConfig));
197+
}
198+
184199
if ($bookmark->getIdentifier() == $identifier) {
185200
$bookmark->setCurrent(true);
186201
} else {

app/code/Magento/Ui/Test/Unit/Controller/Adminhtml/Bookmark/SaveTest.php

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
use Magento\Framework\View\Element\UiComponentFactory;
1313
use Magento\Ui\Api\BookmarkManagementInterface;
1414
use Magento\Ui\Api\BookmarkRepositoryInterface;
15+
use Magento\Ui\Api\Data\BookmarkInterface;
1516
use Magento\Ui\Api\Data\BookmarkInterfaceFactory;
1617
use Magento\Ui\Controller\Adminhtml\Bookmark\Save;
1718
use Magento\Backend\App\Action\Context;
1819
use PHPUnit\Framework\MockObject\MockObject;
1920
use PHPUnit\Framework\TestCase;
2021

2122
/**
22-
* Save controller test.
23+
* Bookmark Save controller test.
24+
*
25+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2326
*/
2427
class SaveTest extends TestCase
2528
{
@@ -111,4 +114,87 @@ public function testExecuteWontBeExecutedWhenNoUserIdInContext(): void
111114

112115
$this->model->execute();
113116
}
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+
}
114200
}

app/code/Magento/Ui/view/base/web/js/grid/controls/bookmarks/bookmarks.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ define([
447447
*/
448448
saveState: function () {
449449
this.store('current');
450-
451450
return this;
452451
},
453452

@@ -553,7 +552,7 @@ define([
553552
*/
554553
onActiveIndexChange: function () {
555554
this.activeView = this.getActiveView();
556-
555+
this.updateActiveView();
557556
this.store('activeIndex');
558557
},
559558

0 commit comments

Comments
 (0)