Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #25856 / Group Ordered Products report by SKU #25858

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AdminGenerateProductsOrderedReportActionGroup">
<annotations>
<description>Navigate to Reports > Products > Ordered page. Enters the provided Order From/To Dates. Clicks on 'Refresh'.</description>
</annotations>
<arguments>
<argument name="orderFromDate" type="string"/>
<argument name="orderToDate" type="string"/>
</arguments>

<amOnPage url="{{SoldReportPage.url}}" stepKey="navigateToOrderedProductPage"/>
<fillField selector="{{SoldReportFilterSection.dateFrom}}" userInput="{{orderFromDate}}" stepKey="fillFromDate"/>
<fillField selector="{{SoldReportFilterSection.dateTo}}" userInput="{{orderToDate}}" stepKey="fillToDate"/>
<click selector="{{SoldReportFilterSection.buttonRefresh}}" stepKey="clickRefresh"/>
</actionGroup>
</actionGroups>
12 changes: 12 additions & 0 deletions app/code/Magento/Reports/Test/Mftf/Page/SoldReportPage.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/PageObject.xsd">
<page name="SoldReportPage" url="reports/report_product/sold/" area="admin" module="Reports">
<section name="SoldReportMainSection"/>
</page>
</pages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
<section name="SoldReportFilterSection">
<element name="dateFrom" type="input" selector="#gridProductsSold_period_date_from"/>
<element name="dateTo" type="input" selector="#gridProductsSold_period_date_to"/>
<element name="buttonRefresh" type="button" selector="//button/span[contains(text(),'Refresh')]" />
<element name="gridProduct" type="text" selector="#gridProductsSold_table"/>
</section>
</sections>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="AdminReportsOrderedGroupedBySkuTest">
<annotations>
<title value="Verify grouped by SKU on report"/>
<description value="Verify the list of configurable product grouped by SKU, on report page 'Reports > Products > Ordered'"/>
<group value="reports"/>
</annotations>
<before>
<createData entity="_defaultCategory" stepKey="createCategory"/>
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
<actionGroup ref="CreateConfigurableProductActionGroup" stepKey="createConfigurableProduct">
<argument name="product" value="_defaultProduct"/>
<argument name="category" value="$$createCategory$$"/>
</actionGroup>
<createData entity="Simple_US_Customer" stepKey="createCustomer"/>
</before>
<after>
<deleteData createDataKey="createCustomer" stepKey="deleteCustomer"/>
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
<actionGroup ref="DeleteProductBySkuActionGroup" stepKey="deleteConfigurableProduct">
<argument name="sku" value="{{_defaultProduct.sku}}"/>
</actionGroup>
<actionGroup ref="DeleteProductAttributeByLabelActionGroup" stepKey="deleteAttributeSet">
<argument name="ProductAttribute" value="colorProductAttribute"/>
</actionGroup>
<actionGroup ref="logout" stepKey="logout"/>
</after>

<!--Add first configurable product to order-->
<actionGroup ref="NavigateToNewOrderPageExistingCustomerActionGroup" stepKey="navigateToFirstOrderWithExistingCustomer">
<argument name="customer" value="$$createCustomer$$"/>
</actionGroup>
<actionGroup ref="AddConfigurableProductToOrderActionGroup" stepKey="addFirstConfigurableProductToOrder">
<argument name="product" value="_defaultProduct"/>
<argument name="attribute" value="colorProductAttribute"/>
<argument name="option" value="colorProductAttribute1"/>
</actionGroup>
<click selector="{{AdminOrderFormActionSection.SubmitOrder}}" stepKey="submitFirstOrder"/>

<!--Add second configurable product to order-->
<actionGroup ref="NavigateToNewOrderPageExistingCustomerActionGroup" stepKey="navigateToSecondOrderWithExistingCustomer">
<argument name="customer" value="$$createCustomer$$"/>
</actionGroup>
<actionGroup ref="AddConfigurableProductToOrderActionGroup" stepKey="addSecondConfigurableProductToOrder">
<argument name="product" value="_defaultProduct"/>
<argument name="attribute" value="colorProductAttribute"/>
<argument name="option" value="colorProductAttribute2"/>
</actionGroup>
<click selector="{{AdminOrderFormActionSection.SubmitOrder}}" stepKey="submitSecondOrder"/>

<!-- Get date -->
<generateDate stepKey="generateStartDate" date="-1 minute" format="m/d/Y"/>
<generateDate stepKey="generateEndDate" date="+1 minute" format="m/d/Y"/>
<actionGroup ref="AdminGenerateProductsOrderedReportActionGroup" stepKey="generateReport">
<argument name="orderFromDate" value="$generateStartDate"/>
<argument name="orderToDate" value="$generateEndDate" />
</actionGroup>

<!-- Verify data -->
<grabTextFrom selector="{{SoldReportFilterSection.gridProduct}}" stepKey="grabData"/>
<assertContains stepKey="assertFirst">
<actualResult type="string">{$grabData}</actualResult>
<expectedResult type="string">{{_defaultProduct.sku}}-{{colorProductAttribute1.name}}</expectedResult>
</assertContains>
<assertContains stepKey="assertSecond">
<actualResult type="string">{$grabData}</actualResult>
<expectedResult type="string">{{_defaultProduct.sku}}-{{colorProductAttribute2.name}}</expectedResult>
</assertContains>
</test>
</tests>
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Reports/etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@
"REPORT_VIEWED_PRD_AGGRED_YEARLY_PRD_ID_SEQUENCE_PRD_SEQUENCE_VAL": true
}
}
}
}