diff --git a/app/code/Magento/Reports/Model/ResourceModel/Product/Sold/Collection.php b/app/code/Magento/Reports/Model/ResourceModel/Product/Sold/Collection.php
index 35a14e09e314f..bca9b8662715a 100644
--- a/app/code/Magento/Reports/Model/ResourceModel/Product/Sold/Collection.php
+++ b/app/code/Magento/Reports/Model/ResourceModel/Product/Sold/Collection.php
@@ -84,7 +84,7 @@ public function addOrderedQty($from = '', $to = '')
)->columns(
'SUM(order_items.qty_ordered) as ordered_qty'
)->group(
- 'order_items.product_id'
+ 'order_items.sku'
);
return $this;
}
diff --git a/app/code/Magento/Reports/Test/Mftf/ActionGroup/AdminGenerateProductsOrderedReportActionGroup.xml b/app/code/Magento/Reports/Test/Mftf/ActionGroup/AdminGenerateProductsOrderedReportActionGroup.xml
new file mode 100644
index 0000000000000..f6f8e4d93d033
--- /dev/null
+++ b/app/code/Magento/Reports/Test/Mftf/ActionGroup/AdminGenerateProductsOrderedReportActionGroup.xml
@@ -0,0 +1,24 @@
+
+
+
+
+
+
+ Navigate to Reports > Products > Ordered page. Enters the provided Order From/To Dates. Clicks on 'Refresh'.
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/code/Magento/Reports/Test/Mftf/Page/SoldReportPage.xml b/app/code/Magento/Reports/Test/Mftf/Page/SoldReportPage.xml
new file mode 100644
index 0000000000000..6c883c8234b9a
--- /dev/null
+++ b/app/code/Magento/Reports/Test/Mftf/Page/SoldReportPage.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
diff --git a/app/code/Magento/Reports/Test/Mftf/Section/SoldReportMainSection.xml b/app/code/Magento/Reports/Test/Mftf/Section/SoldReportMainSection.xml
new file mode 100644
index 0000000000000..79f00ce217f6d
--- /dev/null
+++ b/app/code/Magento/Reports/Test/Mftf/Section/SoldReportMainSection.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
diff --git a/app/code/Magento/Reports/Test/Mftf/Test/AdminReportsOrderedGroupedBySkuTest.xml b/app/code/Magento/Reports/Test/Mftf/Test/AdminReportsOrderedGroupedBySkuTest.xml
new file mode 100644
index 0000000000000..d507d6b38f74b
--- /dev/null
+++ b/app/code/Magento/Reports/Test/Mftf/Test/AdminReportsOrderedGroupedBySkuTest.xml
@@ -0,0 +1,78 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {$grabData}
+ {{_defaultProduct.sku}}-{{colorProductAttribute1.name}}
+
+
+ {$grabData}
+ {{_defaultProduct.sku}}-{{colorProductAttribute2.name}}
+
+
+
diff --git a/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/Sold/Collection/CollectionTest.php b/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/Sold/Collection/CollectionTest.php
index c4be25843b128..6e2fd8d847063 100644
--- a/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/Sold/Collection/CollectionTest.php
+++ b/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/Sold/Collection/CollectionTest.php
@@ -3,52 +3,154 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
+declare(strict_types=1);
namespace Magento\Reports\Test\Unit\Model\ResourceModel\Product\Sold\Collection;
+use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
-use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Reports\Model\ResourceModel\Product\Sold\Collection;
+use Magento\Sales\Model\Order;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
/**
+ * Verify data collection class.
+ *
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
-class CollectionTest extends \PHPUnit\Framework\TestCase
+class CollectionTest extends TestCase
{
/**
- * @var ObjectManager
+ * @var AdapterInterface|MockObject
*/
- protected $objectManager;
+ protected $adapterMock;
/**
- * @var \PHPUnit_Framework_MockObject_MockObject
+ * @var Select|MockObject
*/
protected $selectMock;
+ /**
+ * @var Collection;
+ */
+ protected $collection;
+
+ /**
+ * @inheritDoc
+ */
protected function setUp()
{
- $this->objectManager = new ObjectManager($this);
$this->selectMock = $this->createMock(Select::class);
- }
-
- public function testGetSelectCountSql()
- {
- /** @var $collection \PHPUnit_Framework_MockObject_MockObject */
- $collection = $this->getMockBuilder(Collection::class)
- ->setMethods(['getSelect'])
+ $this->adapterMock = $this->createMock(AdapterInterface::class);
+ $this->collection = $this->getMockBuilder(Collection::class)
+ ->setMethods([
+ 'getSelect',
+ 'getConnection',
+ 'getTable'
+ ])
->disableOriginalConstructor()
->getMock();
+ }
- $collection->expects($this->atLeastOnce())->method('getSelect')->willReturn($this->selectMock);
-
- $this->selectMock->expects($this->atLeastOnce())->method('reset')->willReturnSelf();
- $this->selectMock->expects($this->exactly(2))->method('columns')->willReturnSelf();
+ /**
+ * Verify get select count sql.
+ *
+ * @return void
+ */
+ public function testGetSelectCountSql(): void
+ {
+ $this->collection->expects($this->atLeastOnce())
+ ->method('getSelect')
+ ->willReturn($this->selectMock);
+ $this->selectMock->expects($this->atLeastOnce())
+ ->method('reset')
+ ->willReturnSelf();
+ $this->selectMock->expects($this->exactly(2))
+ ->method('columns')
+ ->willReturnSelf();
+ $this->selectMock->expects($this->at(6))
+ ->method('columns')
+ ->with('COUNT(DISTINCT main_table.entity_id)');
+ $this->selectMock->expects($this->at(7))
+ ->method('reset')
+ ->with(Select::COLUMNS);
+ $this->selectMock->expects($this->at(8))
+ ->method('columns')
+ ->with('COUNT(DISTINCT order_items.item_id)');
- $this->selectMock->expects($this->at(6))->method('columns')->with('COUNT(DISTINCT main_table.entity_id)');
+ $this->assertEquals($this->selectMock, $this->collection->getSelectCountSql());
+ }
- $this->selectMock->expects($this->at(7))->method('reset')->with(Select::COLUMNS);
- $this->selectMock->expects($this->at(8))->method('columns')->with('COUNT(DISTINCT order_items.item_id)');
+ /**
+ * Verify add ordered qty.
+ *
+ * @return void
+ */
+ public function testAddOrderedQty(): void
+ {
+ $this->collection->expects($this->once())
+ ->method('getConnection')
+ ->willReturn($this->adapterMock);
+ $this->adapterMock->expects($this->once())
+ ->method('quoteIdentifier')
+ ->with('order')
+ ->willReturn('sales_order');
+ $this->adapterMock->expects($this->once())
+ ->method('quoteInto')
+ ->with('sales_order.state <> ?', Order::STATE_CANCELED)
+ ->willReturn('');
+ $this->collection->expects($this->atLeastOnce())
+ ->method('getSelect')
+ ->willReturn($this->selectMock);
+ $this->collection->expects($this->exactly(2))
+ ->method('getTable')
+ ->withConsecutive(
+ ['sales_order_item'],
+ ['sales_order']
+ )->willReturnOnConsecutiveCalls(
+ 'sales_order_item',
+ 'sales_order'
+ );
+ $this->selectMock->expects($this->atLeastOnce())
+ ->method('reset')
+ ->willReturnSelf();
+ $this->selectMock->expects($this->atLeastOnce())
+ ->method('from')
+ ->with(
+ [ 'order_items' => 'sales_order_item'],
+ [
+ 'ordered_qty' => 'order_items.qty_ordered',
+ 'order_items_name' => 'order_items.name',
+ 'order_items_sku' => 'order_items.sku'
+ ]
+ )
+ ->willReturnSelf();
+ $this->selectMock->expects($this->atLeastOnce())
+ ->method('joinInner')
+ ->with(
+ ['order' => 'sales_order'],
+ 'sales_order.entity_id = order_items.order_id AND ',
+ []
+ )
+ ->willReturnSelf();
+ $this->selectMock->expects($this->atLeastOnce())
+ ->method('where')
+ ->with('order_items.parent_item_id IS NULL')
+ ->willReturnSelf();
+ $this->selectMock->expects($this->atLeastOnce())
+ ->method('having')
+ ->with('order_items.qty_ordered > ?', 0)
+ ->willReturnSelf();
+ $this->selectMock->expects($this->atLeastOnce())
+ ->method('columns')
+ ->with('SUM(order_items.qty_ordered) as ordered_qty')
+ ->willReturnSelf();
+ $this->selectMock->expects($this->atLeastOnce())
+ ->method('group')
+ ->with('order_items.sku')
+ ->willReturnSelf();
- $this->assertEquals($this->selectMock, $collection->getSelectCountSql());
+ $this->assertEquals($this->collection, $this->collection->addOrderedQty());
}
}
diff --git a/app/code/Magento/Reports/etc/db_schema_whitelist.json b/app/code/Magento/Reports/etc/db_schema_whitelist.json
index 8f55ef4fdbee6..ea65c73ab789b 100644
--- a/app/code/Magento/Reports/etc/db_schema_whitelist.json
+++ b/app/code/Magento/Reports/etc/db_schema_whitelist.json
@@ -149,4 +149,4 @@
"REPORT_VIEWED_PRD_AGGRED_YEARLY_PRD_ID_SEQUENCE_PRD_SEQUENCE_VAL": true
}
}
-}
\ No newline at end of file
+}