Skip to content

Commit 28d11bc

Browse files
committed
MAGETWO-98617: Pager does not work on Newsletter Subscribers Admin page
1 parent 6d8e725 commit 28d11bc

File tree

4 files changed

+116
-123
lines changed

4 files changed

+116
-123
lines changed

app/code/Magento/Backend/Block/Widget/Grid/Massaction/AbstractMassaction.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -282,25 +282,23 @@ public function getGridIdsJson()
282282
if (!$this->getUseSelectAll()) {
283283
return '';
284284
}
285-
/** @var \Magento\Framework\Data\Collection $allIdsCollection */
286-
$allIdsCollection = clone $this->getParentBlock()->getCollection();
287285

288-
if ($this->getMassactionIdField()) {
289-
$massActionIdField = $this->getMassactionIdField();
286+
/** @var \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection $collection */
287+
$collection = clone $this->getParentBlock()->getCollection();
288+
289+
if ($collection instanceof AbstractDb) {
290+
$idsSelect = clone $collection->getSelect();
291+
$idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
292+
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
293+
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
294+
$idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
295+
$idsSelect->columns($this->getMassactionIdField(), 'main_table');
296+
$idList = $collection->getConnection()->fetchCol($idsSelect);
290297
} else {
291-
$massActionIdField = $this->getParentBlock()->getMassactionIdField();
298+
$idList = $collection->setPageSize(0)->getColumnValues($this->getMassactionIdField());
292299
}
293300

294-
if ($allIdsCollection instanceof AbstractDb) {
295-
$allIdsCollection->getSelect()->limit();
296-
$allIdsCollection->clear();
297-
}
298-
299-
$gridIds = $allIdsCollection->setPageSize(0)->getColumnValues($massActionIdField);
300-
if (!empty($gridIds)) {
301-
return join(",", $gridIds);
302-
}
303-
return '';
301+
return implode(',', $idList);
304302
}
305303

306304
/**

app/code/Magento/Backend/Test/Unit/Block/Widget/Grid/MassactionTest.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -269,62 +269,6 @@ public function testGetGridIdsJsonWithoutUseSelectAll()
269269
$this->assertEmpty($this->_block->getGridIdsJson());
270270
}
271271

272-
/**
273-
* @param array $items
274-
* @param string $result
275-
*
276-
* @dataProvider dataProviderGetGridIdsJsonWithUseSelectAll
277-
*/
278-
public function testGetGridIdsJsonWithUseSelectAll(array $items, $result)
279-
{
280-
$this->_block->setUseSelectAll(true);
281-
282-
if ($this->_block->getMassactionIdField()) {
283-
$massActionIdField = $this->_block->getMassactionIdField();
284-
} else {
285-
$massActionIdField = $this->_block->getParentBlock()->getMassactionIdField();
286-
}
287-
288-
$collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class)
289-
->disableOriginalConstructor()
290-
->getMock();
291-
292-
$this->_gridMock->expects($this->once())
293-
->method('getCollection')
294-
->willReturn($collectionMock);
295-
$collectionMock->expects($this->once())
296-
->method('setPageSize')
297-
->with(0)
298-
->willReturnSelf();
299-
$collectionMock->expects($this->once())
300-
->method('getColumnValues')
301-
->with($massActionIdField)
302-
->willReturn($items);
303-
304-
$this->assertEquals($result, $this->_block->getGridIdsJson());
305-
}
306-
307-
/**
308-
* @return array
309-
*/
310-
public function dataProviderGetGridIdsJsonWithUseSelectAll()
311-
{
312-
return [
313-
[
314-
[],
315-
'',
316-
],
317-
[
318-
[1],
319-
'1',
320-
],
321-
[
322-
[1, 2, 3],
323-
'1,2,3',
324-
],
325-
];
326-
}
327-
328272
/**
329273
* @param string $itemId
330274
* @param array|\Magento\Framework\DataObject $item

dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Grid/MassactionTest.php

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,6 @@ public function testMassactionDefaultValues()
8787
$this->assertFalse($blockEmpty->isAvailable());
8888
}
8989

90-
public function testGetJavaScript()
91-
{
92-
$this->loadLayout();
93-
94-
$javascript = $this->_block->getJavaScript();
95-
96-
$expectedItemFirst = '#"option_id1":{"label":"Option One",' .
97-
'"url":"http:\\\/\\\/localhost\\\/index\.php\\\/(?:key\\\/([\w\d]+)\\\/)?",' .
98-
'"complete":"Test","id":"option_id1"}#';
99-
$this->assertRegExp($expectedItemFirst, $javascript);
100-
101-
$expectedItemSecond = '#"option_id2":{"label":"Option Two",' .
102-
'"url":"http:\\\/\\\/localhost\\\/index\.php\\\/(?:key\\\/([\w\d]+)\\\/)?",' .
103-
'"confirm":"Are you sure\?","id":"option_id2"}#';
104-
$this->assertRegExp($expectedItemSecond, $javascript);
105-
}
106-
107-
public function testGetJavaScriptWithAddedItem()
108-
{
109-
$this->loadLayout();
110-
111-
$input = [
112-
'id' => 'option_id3',
113-
'label' => 'Option Three',
114-
'url' => '*/*/option3',
115-
'block_name' => 'admin.test.grid.massaction.option3',
116-
];
117-
$expected = '#"option_id3":{"id":"option_id3","label":"Option Three",' .
118-
'"url":"http:\\\/\\\/localhost\\\/index\.php\\\/(?:key\\\/([\w\d]+)\\\/)?",' .
119-
'"block_name":"admin.test.grid.massaction.option3"}#';
120-
121-
$this->_block->addItem($input['id'], $input);
122-
$this->assertRegExp($expected, $this->_block->getJavaScript());
123-
}
124-
12590
/**
12691
* @param string $mageMode
12792
* @param int $expectedCount
@@ -213,21 +178,4 @@ public function getItemsDataProvider()
213178
]
214179
];
215180
}
216-
217-
public function testGridContainsMassactionColumn()
218-
{
219-
$this->loadLayout();
220-
$this->_layout->getBlock('admin.test.grid')->toHtml();
221-
222-
$gridMassactionColumn = $this->_layout->getBlock('admin.test.grid')
223-
->getColumnSet()
224-
->getChildBlock('massaction');
225-
226-
$this->assertNotNull($gridMassactionColumn, 'Massaction column does not exist in the grid column set');
227-
$this->assertInstanceOf(
228-
\Magento\Backend\Block\Widget\Grid\Column::class,
229-
$gridMassactionColumn,
230-
'Massaction column is not an instance of \Magento\Backend\Block\Widget\Column'
231-
);
232-
}
233181
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Newsletter\Block\Adminhtml\Subscriber;
7+
8+
/**
9+
* @magentoAppArea adminhtml
10+
* @magentoDbIsolation enabled
11+
*
12+
* @see \Magento\Newsletter\Block\Adminhtml\Subscriber\Grid
13+
*/
14+
class GridTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/**
17+
* @var null|\Magento\Framework\ObjectManagerInterface
18+
*/
19+
private $objectManager = null;
20+
/**
21+
* @var null|\Magento\Framework\View\LayoutInterface
22+
*/
23+
private $layout = null;
24+
25+
/**
26+
* Set up layout.
27+
*/
28+
protected function setUp()
29+
{
30+
parent::setUp();
31+
32+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
33+
34+
$this->layout = $this->objectManager->create(\Magento\Framework\View\LayoutInterface::class);
35+
$this->layout->getUpdate()->load('newsletter_subscriber_grid');
36+
$this->layout->generateXml();
37+
$this->layout->generateElements();
38+
}
39+
40+
/**
41+
* Check if mass action block exists.
42+
*/
43+
public function testMassActionBlockExists()
44+
{
45+
$this->assertNotFalse(
46+
$this->getMassActionBlock(),
47+
'Mass action block does not exist in the grid, or it name was changed.'
48+
);
49+
}
50+
51+
/**
52+
* Check if mass action id field is correct.
53+
*/
54+
public function testMassActionFieldIdIsCorrect()
55+
{
56+
$this->assertEquals(
57+
'subscriber_id',
58+
$this->getMassActionBlock()->getMassactionIdField(),
59+
'Mass action id field is incorrect.'
60+
);
61+
}
62+
63+
/**
64+
* Check if function returns correct result.
65+
*
66+
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
67+
*/
68+
public function testMassActionBlockContainsCorrectIdList()
69+
{
70+
$this->assertEquals(
71+
implode(',', $this->getAllSubscriberIdList()),
72+
$this->getMassActionBlock()->getGridIdsJson(),
73+
'Function returns incorrect result.'
74+
);
75+
}
76+
77+
/**
78+
* Retrieve mass action block.
79+
*
80+
* @return bool|\Magento\Backend\Block\Widget\Grid\Massaction
81+
*/
82+
private function getMassActionBlock()
83+
{
84+
return $this->layout->getBlock('adminhtml.newslettrer.subscriber.grid.massaction');
85+
}
86+
87+
/**
88+
* Retrieve list of id of all subscribers.
89+
*
90+
* @return array
91+
*/
92+
private function getAllSubscriberIdList()
93+
{
94+
/** @var \Magento\Framework\App\ResourceConnection $resourceConnection */
95+
$resourceConnection = $this->objectManager->get(\Magento\Framework\App\ResourceConnection::class);
96+
$select = $resourceConnection->getConnection()
97+
->select()
98+
->from($resourceConnection->getTableName('newsletter_subscriber'))
99+
->columns(['subscriber_id' => 'subscriber_id']);
100+
101+
return $resourceConnection->getConnection()->fetchCol($select);
102+
}
103+
}

0 commit comments

Comments
 (0)