Skip to content

Commit

Permalink
Merge pull request #1401 from magento-tsg/2.2-develop-pr8
Browse files Browse the repository at this point in the history
Fixed issues:
- MAGETWO-70663 Impossible to assign products to category using Match products by rule with price attribute
- MAGETWO-67283 quote_address.free_shipping is different depending on whether Magento was upgraded vs installed
- MAGETWO-70642 Magento broken after changing transactional email
  • Loading branch information
Sergii Kovalenko authored Aug 10, 2017
2 parents 99686e2 + 3868c1e commit a716c96
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 13 deletions.
22 changes: 16 additions & 6 deletions app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,29 @@ public function generateProductUrlRewrites(\Magento\Catalog\Model\Category $cate
$storeId = $category->getStoreId();
if ($category->getAffectedProductIds()) {
$this->isSkippedProduct[$category->getEntityId()] = $category->getAffectedProductIds();
/* @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->productCollectionFactory->create()
->setStoreId($storeId)
->addIdFilter($category->getAffectedProductIds())
->addAttributeToSelect('visibility')
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url_path');
foreach ($collection as $product) {
$product->setStoreId($storeId);
$product->setData('save_rewrites_history', $saveRewriteHistory);
$mergeDataProvider->merge(
$this->productUrlRewriteGenerator->generate($product, $category->getEntityId())
);

$collection->setPageSize(1000);
$pageCount = $collection->getLastPageNumber();
$currentPage = 1;
while ($currentPage <= $pageCount) {
$collection->setCurPage($currentPage);
foreach ($collection as $product) {
$product->setStoreId($storeId);
$product->setData('save_rewrites_history', $saveRewriteHistory);
$mergeDataProvider->merge(
$this->productUrlRewriteGenerator->generate($product, $category->getEntityId())
);
}
$collection->clear();
$currentPage++;
}
} else {
$mergeDataProvider->merge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
*/
namespace Magento\CatalogUrlRewrite\Test\Unit\Observer;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogUrlRewrite\Observer\UrlRewriteHandler;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
use Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
Expand All @@ -16,6 +18,9 @@
use Magento\UrlRewrite\Model\MergeDataProvider;
use Magento\CatalogUrlRewrite\Model\CategoryProductUrlPathGenerator;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class UrlRewriteHandlerTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -58,6 +63,11 @@ class UrlRewriteHandlerTest extends \PHPUnit\Framework\TestCase
*/
private $mergeDataProviderFactoryMock;

/**
* @var MergeDataProvider|\PHPUnit_Framework_MockObject_MockObject
*/
private $mergeDataProviderMock;

/**
* @var Json|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -85,16 +95,15 @@ protected function setUp()
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();
$mergeDataProviderMock = $this->getMockBuilder(MergeDataProvider::class)
$this->mergeDataProviderMock = $this->getMockBuilder(MergeDataProvider::class)
->disableOriginalConstructor()
->getMock();
$this->categoryBasedProductRewriteGeneratorMock = $this->getMockBuilder(CategoryProductUrlPathGenerator::class)
->disableOriginalConstructor()
->getMock();
$this->mergeDataProviderFactoryMock->expects($this->any())
->method('create')
->willReturn($mergeDataProviderMock);

->willReturn($this->mergeDataProviderMock);
$this->serializerMock = $this->getMockBuilder(Json::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -111,6 +120,72 @@ protected function setUp()
);
}

/**
* @test
*/
public function testGenerateProductUrlRewrites()
{
/* @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject $category */
$category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
->setMethods(['getEntityId', 'getStoreId', 'getData', 'getAffectedProductIds'])
->disableOriginalConstructor()
->getMock();
$category->expects($this->any())
->method('getEntityId')
->willReturn(2);
$category->expects($this->any())
->method('getStoreId')
->willReturn(1);
$category->expects($this->any())
->method('getData')
->with('save_rewrites_history')
->willReturn(true);

/* @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject $childCategory1 */
$childCategory1 = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
->setMethods(['getEntityId'])
->disableOriginalConstructor()
->getMock();
$childCategory1->expects($this->any())
->method('getEntityId')
->willReturn(100);

/* @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject $childCategory1 */
$childCategory2 = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
->setMethods(['getEntityId'])
->disableOriginalConstructor()
->getMock();
$childCategory1->expects($this->any())
->method('getEntityId')
->willReturn(200);

$this->childrenCategoriesProviderMock->expects($this->once())
->method('getChildren')
->with($category, true)
->willReturn([$childCategory1, $childCategory2]);

/** @var Collection|\PHPUnit_Framework_MockObject_MockObject $productCollection */
$productCollection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
->disableOriginalConstructor()
->getMock();
$productCollection->expects($this->any())
->method('addCategoriesFilter')
->willReturnSelf();
$productCollection->expects($this->any())
->method('addIdFilter')
->willReturnSelf();
$productCollection->expects($this->any())->method('setStoreId')->willReturnSelf();
$productCollection->expects($this->any())->method('addAttributeToSelect')->willReturnSelf();
$iterator = new \ArrayIterator([]);
$productCollection->expects($this->any())->method('getIterator')->will($this->returnValue($iterator));

$this->collectionFactoryMock->expects($this->any())->method('create')->willReturn($productCollection);

$this->mergeDataProviderMock->expects($this->any())->method('getData')->willReturn([1, 2]);

$this->urlRewriteHandler->generateProductUrlRewrites($category);
}

public function testDeleteCategoryRewritesForChildren()
{
$category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
Expand Down
69 changes: 69 additions & 0 deletions app/code/Magento/OfflineShipping/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\OfflineShipping\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

/**
* Upgrade Data script.
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* @var string
*/
private $quoteConnectionName = 'checkout';

/**
* @var string
*/
private $salesConnectionName = 'sales';

/**
* {@inheritdoc}
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if ($context->getVersion() && version_compare($context->getVersion(), '2.0.1') < 0) {
$this->updateQuoteShippingAddresses($setup);
}
$setup->endSetup();
}

/**
* Replace Null with '0' for 'free_shipping' and 'simple_free_shipping' accordingly to upgraded schema.
*
* @param ModuleDataSetupInterface $setup
* @return void
*/
private function updateQuoteShippingAddresses(ModuleDataSetupInterface $setup)
{
$setup->getConnection()->update(
$setup->getTable('salesrule'),
['simple_free_shipping' => 0],
[new \Zend_Db_Expr('simple_free_shipping IS NULL')]
);
$setup->getConnection($this->salesConnectionName)->update(
$setup->getTable('sales_order_item'),
['free_shipping' => 0],
[new \Zend_Db_Expr('free_shipping IS NULL')]
);
$setup->getConnection($this->quoteConnectionName)->update(
$setup->getTable('quote_address'),
['free_shipping' => 0],
[new \Zend_Db_Expr('free_shipping IS NULL')]
);
$setup->getConnection($this->quoteConnectionName)->update(
$setup->getTable('quote_item'),
['free_shipping' => 0],
[new \Zend_Db_Expr('free_shipping IS NULL')]
);
}
}
104 changes: 104 additions & 0 deletions app/code/Magento/OfflineShipping/Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\OfflineShipping\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;

/**
* Upgrade schema DB for OfflineShipping module.
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* @var string
*/
private $quoteConnectionName = 'checkout';

/**
* @var string
*/
private $salesConnectionName = 'sales';

/**
* @inheritdoc
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if (version_compare($context->getVersion(), '2.0.1', '<')) {
$this->updateFreeShippingColumns($setup);
}

$setup->endSetup();
}

/**
* Modify 'free_shipping' and 'simple_free_shipping' columns added incorrectly in InstallSchema.
*
* @param SchemaSetupInterface $setup
* @return void
*/
private function updateFreeShippingColumns(SchemaSetupInterface $setup)
{
$setup->getConnection()->modifyColumn(
$setup->getTable('salesrule'),
'simple_free_shipping',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'Simple Free Shipping',
]
);
$setup->getConnection($this->salesConnectionName)->modifyColumn(
$setup->getTable('sales_order_item', $this->salesConnectionName),
'free_shipping',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'Free Shipping',
]
);
$setup->getConnection($this->quoteConnectionName)->modifyColumn(
$setup->getTable('quote_address', $this->quoteConnectionName),
'free_shipping',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'Free Shipping',
]
);
$setup->getConnection($this->quoteConnectionName)->modifyColumn(
$setup->getTable('quote_item', $this->quoteConnectionName),
'free_shipping',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
'unsigned' => true,
'nullable' => false,
'default' => '0',
'comment' => 'Free Shipping',
]
);
$setup->getConnection($this->quoteConnectionName)->modifyColumn(
$setup->getTable('quote_address_item', $this->quoteConnectionName),
'free_shipping',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
'unsigned' => true,
'comment' => 'Free Shipping',
]
);
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/OfflineShipping/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"magento/module-backend": "100.2.*",
"magento/module-shipping": "100.2.*",
"magento/module-catalog": "101.1.*",
"magento/module-sales": "100.2.*",
"magento/module-sales-rule": "100.2.*",
"magento/module-directory": "100.2.*",
"magento/module-quote": "100.2.*",
"magento/framework": "100.2.*"
},
"suggest": {
"magento/module-checkout": "100.2.*",
"magento/module-sales": "100.2.*",
"magento/module-offline-shipping-sample-data": "Sample Data version:100.2.*"
},
"type": "magento2-module",
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/OfflineShipping/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_OfflineShipping" setup_version="2.0.0">
<module name="Magento_OfflineShipping" setup_version="2.0.1">
<sequence>
<module name="Magento_Store"/>
<module name="Magento_Sales"/>
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Sales/Model/AdminOrder/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ public function applyCoupon($code)
$this->getQuote()->getShippingAddress()->setCollectShippingRates(true);

if (empty($code)) {
$this->getQuote()->getShippingAddress()->setFreeShipping(null);
$this->getQuote()->getShippingAddress()->setFreeShipping(0);
}
$this->getQuote()->setCouponCode($code);
$this->setRecollect(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function testApplyCoupon()
$quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode)->willReturnSelf();

$addressMock->expects($this->once())->method('setCollectShippingRates')->with(true)->willReturnSelf();
$addressMock->expects($this->once())->method('setFreeShipping')->with(null)->willReturnSelf();
$addressMock->expects($this->once())->method('setFreeShipping')->with(0)->willReturnSelf();

$object = $this->adminOrderCreate->applyCoupon($couponCode);
$this->assertEquals($this->adminOrderCreate, $object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ protected function getRequestData()
$data = array_filter($data, function ($param) {
return isset($param['error']) && $param['error'] > 0 ? false : true;
});

/**
* Set null to theme id in case it's empty string,
* in order to delete value from db config but not set empty string,
* which may cause an error in Magento/Theme/Model/ResourceModel/Theme/Collection::getThemeByFullPath().
*/
if (isset($data['theme_theme_id']) && $data['theme_theme_id'] === '') {
$data['theme_theme_id'] = null;
}
return $data;
}
}
Loading

0 comments on commit a716c96

Please sign in to comment.