From 29431e958855ef19cc5289f8c86f39e2bc30f0a5 Mon Sep 17 00:00:00 2001 From: Alessandro Pagnin Date: Sun, 18 Feb 2018 16:08:58 +0100 Subject: [PATCH 01/17] Category collection now use getStoreId and not directly the store manager --- .../ResourceModel/Category/Collection.php | 2 +- .../Category/Collection/UrlRewriteTest.php | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php index 573914a13f6e5..46bb74513b59c 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php @@ -371,7 +371,7 @@ public function joinUrlRewrite() ['request_path'], sprintf( '{{table}}.is_autogenerated = 1 AND {{table}}.store_id = %d AND {{table}}.entity_type = \'%s\'', - $this->_storeManager->getStore()->getId(), + $this->getStoreId(), CategoryUrlRewriteGenerator::ENTITY_TYPE ), 'left' diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php new file mode 100644 index 0000000000000..5949b2951de03 --- /dev/null +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php @@ -0,0 +1,40 @@ +_model = $this->getMockBuilder( + \Magento\Catalog\Model\ResourceModel\Category\Collection::class + )->disableOriginalConstructor() + ->setMethodsExcept(['joinUrlRewrite', 'setStoreId', 'getStoreId']) + ->getMock(); + } + + + public function testStoreIdUsedByUrlRewrite() + { + $this->_model->expects($this->once()) + ->method('joinTable') + ->with( + $this->anything(), + $this->anything(), + $this->anything(), + $this->equalTo('{{table}}.is_autogenerated = 1 AND {{table}}.store_id = 100 AND {{table}}.entity_type = \'category\''), + $this->anything() + ); + $this->_model->setStoreId(100); + $this->_model->joinUrlRewrite(); + } +} From 47c8d54a84770286e9912b83cf11805d29bf40ea Mon Sep 17 00:00:00 2001 From: Alessandro Pagnin Date: Sun, 18 Feb 2018 17:06:42 +0100 Subject: [PATCH 02/17] code style fixes --- .../ResourceModel/Category/Collection/UrlRewriteTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php index 5949b2951de03..ea8cb51bf5e5e 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php @@ -22,16 +22,16 @@ protected function setUp() ->getMock(); } - public function testStoreIdUsedByUrlRewrite() { + $cond = '{{table}}.is_autogenerated = 1 AND {{table}}.store_id = 100 AND {{table}}.entity_type = \'category\''; $this->_model->expects($this->once()) ->method('joinTable') ->with( $this->anything(), $this->anything(), $this->anything(), - $this->equalTo('{{table}}.is_autogenerated = 1 AND {{table}}.store_id = 100 AND {{table}}.entity_type = \'category\''), + $this->equalTo($cond), $this->anything() ); $this->_model->setStoreId(100); From 2a014892b5d19c04f5462d2f00c3c7f5b557b0c2 Mon Sep 17 00:00:00 2001 From: Alessandro Pagnin Date: Sun, 18 Feb 2018 20:05:40 +0100 Subject: [PATCH 03/17] fixes to code style --- .../Category/Collection/UrlRewriteTest.php | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php index ea8cb51bf5e5e..7dec04fceaef6 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php @@ -15,9 +15,8 @@ class UrlRewriteTest extends \PHPUnit\Framework\TestCase protected function setUp() { - $this->_model = $this->getMockBuilder( - \Magento\Catalog\Model\ResourceModel\Category\Collection::class - )->disableOriginalConstructor() + $this->_model = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category\Collection::class) + ->disableOriginalConstructor() ->setMethodsExcept(['joinUrlRewrite', 'setStoreId', 'getStoreId']) ->getMock(); } @@ -26,14 +25,14 @@ public function testStoreIdUsedByUrlRewrite() { $cond = '{{table}}.is_autogenerated = 1 AND {{table}}.store_id = 100 AND {{table}}.entity_type = \'category\''; $this->_model->expects($this->once()) - ->method('joinTable') - ->with( - $this->anything(), - $this->anything(), - $this->anything(), - $this->equalTo($cond), - $this->anything() - ); + ->method('joinTable') + ->with( + $this->anything(), + $this->anything(), + $this->anything(), + $this->equalTo($cond), + $this->anything() + ); $this->_model->setStoreId(100); $this->_model->joinUrlRewrite(); } From 2d12dac1672aa4125b1012e454a8b58bb9ee9b73 Mon Sep 17 00:00:00 2001 From: Alessandro Pagnin Date: Sun, 18 Mar 2018 23:26:09 +0100 Subject: [PATCH 04/17] add integration test --- .../ResourceModel/Category/CollectionTest.php | 62 +++++++++++++++++++ .../_files/category_multiple_stores.php | 61 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php new file mode 100644 index 0000000000000..7d2d5d8e443a7 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php @@ -0,0 +1,62 @@ +collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( + \Magento\Catalog\Model\ResourceModel\Category\Collection::class + ); + } + + protected function setDown() { + /* Refresh stores memory cache after store deletion */ + \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( + \Magento\Store\Model\StoreManagerInterface::class + )->reinitStores(); + } + + /** + * @magentoAppIsolation enabled + * @magentoDbIsolation enabled + * @magentoDataFixture Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php + */ + public function testJoinUrlRewriteOnDefault() + { + $categories = $this->collection->joinUrlRewrite()->addPathFilter('1/2/3'); + $this->assertCount(1, $categories); + /** @var $category \Magento\Catalog\Model\Category */ + $category = $categories->getFirstItem(); + $this->assertStringEndsWith('category.html', $category->getUrl()); + } + + /** + * @magentoAppIsolation enabled + * @magentoDbIsolation enabled + * @magentoDataFixture Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php + */ + public function testJoinUrlRewriteNotOnDefaultStore() + { + $store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class); + $storeId = $store->load('second_category_store', 'code')->getId(); + $categories = $this->collection->setStoreId($storeId)->joinUrlRewrite()->addPathFilter('1/2/3'); + $this->assertCount(1, $categories); + /** @var $category \Magento\Catalog\Model\Category */ + $category = $categories->getFirstItem(); + $this->assertStringEndsWith('category-3-on-2.html', $category->getUrl()); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php new file mode 100644 index 0000000000000..c6590bb6a0f9f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php @@ -0,0 +1,61 @@ +create( + \Magento\Catalog\Model\CategoryFactory::class +); +/** @var \Magento\Catalog\Model\CategoryRepository $repository */ +$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( + \Magento\Catalog\Model\CategoryRepository::class +); +/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */ +$storeManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( + \Magento\Store\Model\StoreManagerInterface::class +); +/** @var \Magento\Store\Model\Store $store */ +$store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class); +if (!$store->load('second_category_store', 'code')->getId()) { + $websiteId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( + \Magento\Store\Model\StoreManagerInterface::class + )->getWebsite()->getId(); + $groupId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( + \Magento\Store\Model\StoreManagerInterface::class + )->getWebsite()->getDefaultGroupId(); + $store->setCode( + 'second_category_store' + )->setWebsiteId( + $websiteId + )->setGroupId( + $groupId + )->setName( + 'Fixture Store' + )->setSortOrder( + 10 + )->setIsActive( + 1 + ); + $store->save(); +} + +/* Refresh stores memory cache */ +\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( + \Magento\Store\Model\StoreManagerInterface::class +)->reinitStores(); + +/** @var \Magento\Catalog\Model\Category $newCategory */ +$newCategory = $factory->create(); +$newCategory + ->setName('Category') + ->setParentId(2) + ->setLevel(2) + ->setPath('1/2/3') + ->setAvailableSortBy('name') + ->setDefaultSortBy('name') + ->setIsActive(true) + ->setPosition(1); +$repository->save($newCategory); +$currentStoreId = $storeManager->getStore()->getId(); +$storeManager->setCurrentStore($storeManager->getStore($store->getId())); +$newCategory + ->setUrlKey('category-3-on-2'); +$repository->save($newCategory); +$storeManager->setCurrentStore($storeManager->getStore($currentStoreId)); \ No newline at end of file From 8a097fc1a83acd63b702661e12bcb10a00b36630 Mon Sep 17 00:00:00 2001 From: Alessandro Pagnin Date: Sun, 18 Mar 2018 23:47:39 +0100 Subject: [PATCH 05/17] fix to code style --- .../Catalog/Model/ResourceModel/Category/CollectionTest.php | 6 ++++-- .../Model/ResourceModel/_files/category_multiple_stores.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php index 7d2d5d8e443a7..dd1a6eefbe532 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php @@ -23,7 +23,8 @@ protected function setUp() ); } - protected function setDown() { + protected function setDown() + { /* Refresh stores memory cache after store deletion */ \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( \Magento\Store\Model\StoreManagerInterface::class @@ -51,7 +52,8 @@ public function testJoinUrlRewriteOnDefault() */ public function testJoinUrlRewriteNotOnDefaultStore() { - $store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class); + $store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() + ->create(\Magento\Store\Model\Store::class); $storeId = $store->load('second_category_store', 'code')->getId(); $categories = $this->collection->setStoreId($storeId)->joinUrlRewrite()->addPathFilter('1/2/3'); $this->assertCount(1, $categories); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php index c6590bb6a0f9f..330a682106d89 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php @@ -58,4 +58,4 @@ $newCategory ->setUrlKey('category-3-on-2'); $repository->save($newCategory); -$storeManager->setCurrentStore($storeManager->getStore($currentStoreId)); \ No newline at end of file +$storeManager->setCurrentStore($storeManager->getStore($currentStoreId)); From af3804ac4e846d42593ab73df3535a24d4ff9f8b Mon Sep 17 00:00:00 2001 From: Stanislav Idolov Date: Mon, 26 Mar 2018 16:23:03 +0300 Subject: [PATCH 06/17] magento/magento2#13716 --- .../Category/Collection/UrlRewriteTest.php | 10 +++++----- .../Model/ResourceModel/Category/CollectionTest.php | 2 +- .../ResourceModel/_files/category_multiple_stores.php | 9 +++++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php index 7dec04fceaef6..b1f5f17f64a59 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php @@ -11,11 +11,11 @@ class UrlRewriteTest extends \PHPUnit\Framework\TestCase /** * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $_model; + private $model; protected function setUp() { - $this->_model = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category\Collection::class) + $this->model = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category\Collection::class) ->disableOriginalConstructor() ->setMethodsExcept(['joinUrlRewrite', 'setStoreId', 'getStoreId']) ->getMock(); @@ -24,7 +24,7 @@ protected function setUp() public function testStoreIdUsedByUrlRewrite() { $cond = '{{table}}.is_autogenerated = 1 AND {{table}}.store_id = 100 AND {{table}}.entity_type = \'category\''; - $this->_model->expects($this->once()) + $this->model->expects($this->once()) ->method('joinTable') ->with( $this->anything(), @@ -33,7 +33,7 @@ public function testStoreIdUsedByUrlRewrite() $this->equalTo($cond), $this->anything() ); - $this->_model->setStoreId(100); - $this->_model->joinUrlRewrite(); + $this->model->setStoreId(100); + $this->model->joinUrlRewrite(); } } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php index dd1a6eefbe532..7e26cdb921f39 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php @@ -10,7 +10,7 @@ class CollectionTest extends \PHPUnit\Framework\TestCase /** * @var \Magento\Catalog\Model\ResourceModel\Category\Collection */ - protected $collection; + private $collection; /** * Sets up the fixture, for example, opens a network connection. diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php index 330a682106d89..90bf630000e72 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php @@ -1,4 +1,9 @@ create( \Magento\Catalog\Model\CategoryFactory::class @@ -20,6 +25,7 @@ $groupId = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( \Magento\Store\Model\StoreManagerInterface::class )->getWebsite()->getDefaultGroupId(); + $store->setCode( 'second_category_store' )->setWebsiteId( @@ -55,7 +61,6 @@ $repository->save($newCategory); $currentStoreId = $storeManager->getStore()->getId(); $storeManager->setCurrentStore($storeManager->getStore($store->getId())); -$newCategory - ->setUrlKey('category-3-on-2'); +$newCategory->setUrlKey('category-3-on-2'); $repository->save($newCategory); $storeManager->setCurrentStore($storeManager->getStore($currentStoreId)); From 8220e3ff214d8911ba7dde971ed6856272228ca1 Mon Sep 17 00:00:00 2001 From: Vlad Veselov Date: Tue, 27 Mar 2018 11:35:13 +0300 Subject: [PATCH 07/17] Remove improper unit test it shall not pass code review as object under test is mocked and thus it does not test anything --- .../Category/Collection/UrlRewriteTest.php | 39 ------------------- 1 file changed, 39 deletions(-) delete mode 100644 app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php deleted file mode 100644 index b1f5f17f64a59..0000000000000 --- a/app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Category/Collection/UrlRewriteTest.php +++ /dev/null @@ -1,39 +0,0 @@ -model = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category\Collection::class) - ->disableOriginalConstructor() - ->setMethodsExcept(['joinUrlRewrite', 'setStoreId', 'getStoreId']) - ->getMock(); - } - - public function testStoreIdUsedByUrlRewrite() - { - $cond = '{{table}}.is_autogenerated = 1 AND {{table}}.store_id = 100 AND {{table}}.entity_type = \'category\''; - $this->model->expects($this->once()) - ->method('joinTable') - ->with( - $this->anything(), - $this->anything(), - $this->anything(), - $this->equalTo($cond), - $this->anything() - ); - $this->model->setStoreId(100); - $this->model->joinUrlRewrite(); - } -} From f4fc5b9899879e42beb14d891fa79626ced110bd Mon Sep 17 00:00:00 2001 From: "mastuhin.olexnadr" Date: Fri, 6 Apr 2018 12:07:22 +0300 Subject: [PATCH 08/17] ENGCOM-1103: [Forwardport] Category\Collection::joinUrlRewrite should use the store set on the collection #14381 --- .../Model/Category/ProductIndexerTest.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Bundle/Model/Category/ProductIndexerTest.php b/dev/tests/integration/testsuite/Magento/Bundle/Model/Category/ProductIndexerTest.php index c877ae1f388f8..5a7c70ebe8064 100644 --- a/dev/tests/integration/testsuite/Magento/Bundle/Model/Category/ProductIndexerTest.php +++ b/dev/tests/integration/testsuite/Magento/Bundle/Model/Category/ProductIndexerTest.php @@ -208,18 +208,28 @@ public function testCategoryCreate() } /** + * Finds 4 categories + * * @return Category[] */ private function getCategories() { - /** @var Category $category */ - $category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( - \Magento\Catalog\Model\Category::class + $collectionFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( + \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory::class ); - $result = $category->getCollection()->addAttributeToSelect('name')->getItems(); - $result = array_slice($result, 2); + /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */ + $collection = $collectionFactory->create(); + + $collection + ->addAttributeToSelect('name') + ->addAttributeToFilter('name', ['in' => [ + 'Category 1', + 'Category 2', + 'Category 3', + 'Category 4', + ]]); - return array_slice($result, 0, 4); + return array_values($collection->getItems()); } } From b9732d7d1055c03fb353254b32a30b154234be67 Mon Sep 17 00:00:00 2001 From: Eric COURTIAL Date: Mon, 9 Apr 2018 19:46:39 -0400 Subject: [PATCH 09/17] Added possibility to select PHP input file mode --- lib/internal/Magento/Framework/File/Csv.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/internal/Magento/Framework/File/Csv.php b/lib/internal/Magento/Framework/File/Csv.php index 3d177f89e2968..67177445a670d 100644 --- a/lib/internal/Magento/Framework/File/Csv.php +++ b/lib/internal/Magento/Framework/File/Csv.php @@ -126,13 +126,17 @@ public function getDataPairs($file, $keyIndex = 0, $valueIndex = 1) /** * Saving data row array into file * - * @param string $file - * @param array $data - * @return $this + * @param string $file + * @param array $data + * @param string $mode + * + * @return $this + * + * @throws \Magento\Framework\Exception\FileSystemException */ - public function saveData($file, $data) + public function saveData($file, $data, $mode = 'w') { - $fh = fopen($file, 'w'); + $fh = fopen($file, $mode); foreach ($data as $dataRow) { $this->file->filePutCsv($fh, $dataRow, $this->_delimiter, $this->_enclosure); } From 68e087843fbb17a2c3de1ed4ecd2c380feab8640 Mon Sep 17 00:00:00 2001 From: Eric COURTIAL Date: Wed, 11 Apr 2018 19:11:17 -0400 Subject: [PATCH 10/17] Added a new method to replace the saveData one --- lib/internal/Magento/Framework/File/Csv.php | 34 +++++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/File/Csv.php b/lib/internal/Magento/Framework/File/Csv.php index 67177445a670d..10ee9b193d543 100644 --- a/lib/internal/Magento/Framework/File/Csv.php +++ b/lib/internal/Magento/Framework/File/Csv.php @@ -126,21 +126,43 @@ public function getDataPairs($file, $keyIndex = 0, $valueIndex = 1) /** * Saving data row array into file * - * @param string $file - * @param array $data + * @param string $file + * @param array $data + * @return $this + * @throws \Magento\Framework\Exception\FileSystemException + * @deprecated + * @see appendData + */ + public function saveData($file, $data) + { + $fh = fopen($file, 'w'); + foreach ($data as $dataRow) { + $this->file->filePutCsv($fh, $dataRow, $this->_delimiter, $this->_enclosure); + } + fclose($fh); + return $this; + } + + /** + * Replace the saveData method by + * allowing to select the input mode + * + * @param $file + * @param $data * @param string $mode * * @return $this * * @throws \Magento\Framework\Exception\FileSystemException */ - public function saveData($file, $data, $mode = 'w') + public function appendData($file, $data, $mode = 'w') { - $fh = fopen($file, $mode); + $fileHandler = fopen($file, $mode); foreach ($data as $dataRow) { - $this->file->filePutCsv($fh, $dataRow, $this->_delimiter, $this->_enclosure); + $this->file->filePutCsv($fileHandler, $dataRow, $this->_delimiter, $this->_enclosure); } - fclose($fh); + fclose($fileHandler); + return $this; } } From 359a64706cca2e16966fd0cf861c12e48e68d8ac Mon Sep 17 00:00:00 2001 From: Alexander Shkurko Date: Thu, 18 Jan 2018 11:11:09 +0100 Subject: [PATCH 11/17] Recent orders were not filtered per store --- app/code/Magento/Sales/Block/Order/Recent.php | 40 ++++++++++-- .../Test/Unit/Block/Order/RecentTest.php | 64 +++++++++++++------ .../frontend/templates/order/recent.phtml | 9 ++- 3 files changed, 87 insertions(+), 26 deletions(-) diff --git a/app/code/Magento/Sales/Block/Order/Recent.php b/app/code/Magento/Sales/Block/Order/Recent.php index e57aa1fe420a0..b8d19235731dc 100644 --- a/app/code/Magento/Sales/Block/Order/Recent.php +++ b/app/code/Magento/Sales/Block/Order/Recent.php @@ -5,6 +5,12 @@ */ namespace Magento\Sales\Block\Order; +use Magento\Framework\View\Element\Template\Context; +use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; +use Magento\Customer\Model\Session; +use Magento\Sales\Model\Order\Config; +use Magento\Store\Model\StoreManagerInterface; + /** * Sales order history block * @@ -13,6 +19,11 @@ */ class Recent extends \Magento\Framework\View\Element\Template { + /** + * Limit of orders + */ + const ORDER_LIMIT = 5; + /** * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory */ @@ -28,23 +39,31 @@ class Recent extends \Magento\Framework\View\Element\Template */ protected $_orderConfig; + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + private $storeManager; + /** * @param \Magento\Framework\View\Element\Template\Context $context * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Sales\Model\Order\Config $orderConfig + * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param array $data */ public function __construct( - \Magento\Framework\View\Element\Template\Context $context, - \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, - \Magento\Customer\Model\Session $customerSession, - \Magento\Sales\Model\Order\Config $orderConfig, + Context $context, + CollectionFactory $orderCollectionFactory, + Session $customerSession, + Config $orderConfig, + StoreManagerInterface $storeManager, array $data = [] ) { $this->_orderCollectionFactory = $orderCollectionFactory; $this->_customerSession = $customerSession; $this->_orderConfig = $orderConfig; + $this->storeManager = $storeManager; parent::__construct($context, $data); $this->_isScopePrivate = true; } @@ -55,11 +74,22 @@ public function __construct( protected function _construct() { parent::_construct(); + $this->getRecentOrders(); + } + + /** + * Get recently placed orders. By default they will be limited by 5. + */ + protected function getRecentOrders() + { $orders = $this->_orderCollectionFactory->create()->addAttributeToSelect( '*' )->addAttributeToFilter( 'customer_id', $this->_customerSession->getCustomerId() + )->addAttributeToFilter( + 'store_id', + $this->storeManager->getStore()->getId() )->addAttributeToFilter( 'status', ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()] @@ -67,7 +97,7 @@ protected function _construct() 'created_at', 'desc' )->setPageSize( - '5' + self::ORDER_LIMIT )->load(); $this->setOrders($orders); } diff --git a/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php b/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php index 99528983a13c9..630263da9a1ed 100644 --- a/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php +++ b/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php @@ -5,6 +5,15 @@ */ namespace Magento\Sales\Test\Unit\Block\Order; +use Magento\Framework\View\Element\Template\Context; +use Magento\Sales\Model\ResourceModel\Order\CollectionFactory; +use Magento\Customer\Model\Session; +use Magento\Sales\Model\Order\Config; +use Magento\Store\Model\StoreManagerInterface; +use Magento\Framework\View\Layout; +use Magento\Store\Api\Data\StoreInterface; +use Magento\Sales\Model\ResourceModel\Order\Collection; + class RecentTest extends \PHPUnit\Framework\TestCase { /** @@ -32,26 +41,34 @@ class RecentTest extends \PHPUnit\Framework\TestCase */ protected $orderConfig; + /** + * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $storeManagerMock; + protected function setUp() { - $this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class); + $this->context = $this->createMock(Context::class); $this->orderCollectionFactory = $this->createPartialMock( - \Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class, + CollectionFactory::class, ['create'] ); - $this->customerSession = $this->createPartialMock(\Magento\Customer\Model\Session::class, ['getCustomerId']); + $this->customerSession = $this->createPartialMock(Session::class, ['getCustomerId']); $this->orderConfig = $this->createPartialMock( - \Magento\Sales\Model\Order\Config::class, + Config::class, ['getVisibleOnFrontStatuses'] ); + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) + ->getMockForAbstractClass(); } public function testConstructMethod() { $data = []; - $attribute = ['customer_id', 'status']; + $attribute = ['customer_id', 'store_id', 'status']; $customerId = 25; - $layout = $this->createPartialMock(\Magento\Framework\View\Layout::class, ['getBlock']); + $storeId = 4; + $layout = $this->createPartialMock(Layout::class, ['getBlock']); $this->context->expects($this->once()) ->method('getLayout') ->will($this->returnValue($layout)); @@ -64,14 +81,20 @@ public function testConstructMethod() ->method('getVisibleOnFrontStatuses') ->will($this->returnValue($statuses)); - $orderCollection = $this->createPartialMock(\Magento\Sales\Model\ResourceModel\Order\Collection::class, [ - 'addAttributeToSelect', - 'addFieldToFilter', - 'addAttributeToFilter', - 'addAttributeToSort', - 'setPageSize', - 'load' - ]); + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) + ->getMockForAbstractClass(); + $storeMock = $this->getMockBuilder(StoreInterface::class)->getMockForAbstractClass(); + $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock); + $storeMock->expects($this->any())->method('getId')->willReturn($storeId); + + $orderCollection = $this->createPartialMock(Collection::class, [ + 'addAttributeToSelect', + 'addFieldToFilter', + 'addAttributeToFilter', + 'addAttributeToSort', + 'setPageSize', + 'load' + ]); $this->orderCollectionFactory->expects($this->once()) ->method('create') ->will($this->returnValue($orderCollection)); @@ -85,17 +108,21 @@ public function testConstructMethod() ->willReturnSelf(); $orderCollection->expects($this->at(2)) ->method('addAttributeToFilter') - ->with($attribute[1], $this->equalTo(['in' => $statuses])) - ->will($this->returnSelf()); + ->with($attribute[1], $this->equalTo($storeId)) + ->willReturnSelf(); $orderCollection->expects($this->at(3)) + ->method('addAttributeToFilter') + ->with($attribute[2], $this->equalTo(['in' => $statuses])) + ->will($this->returnSelf()); + $orderCollection->expects($this->at(4)) ->method('addAttributeToSort') ->with('created_at', 'desc') ->will($this->returnSelf()); - $orderCollection->expects($this->at(4)) + $orderCollection->expects($this->at(5)) ->method('setPageSize') ->with('5') ->will($this->returnSelf()); - $orderCollection->expects($this->at(5)) + $orderCollection->expects($this->at(6)) ->method('load') ->will($this->returnSelf()); $this->block = new \Magento\Sales\Block\Order\Recent( @@ -103,6 +130,7 @@ public function testConstructMethod() $this->orderCollectionFactory, $this->customerSession, $this->orderConfig, + $this->storeManagerMock, $data ); $this->assertEquals($orderCollection, $this->block->getOrders()); diff --git a/app/code/Magento/Sales/view/frontend/templates/order/recent.phtml b/app/code/Magento/Sales/view/frontend/templates/order/recent.phtml index df7e6bf334d9a..bb354e920c529 100644 --- a/app/code/Magento/Sales/view/frontend/templates/order/recent.phtml +++ b/app/code/Magento/Sales/view/frontend/templates/order/recent.phtml @@ -8,10 +8,13 @@ ?>
-getOrders(); ?> +getOrders(); + $count = count($_orders); +?>
- getItems()) > 0): ?> + 0): ?> @@ -19,7 +22,7 @@
getChildHtml() ?> - getItems()) > 0): ?> + 0): ?>
From 8e3b57036c0adbb6512227ea3bff50e1c91fb900 Mon Sep 17 00:00:00 2001 From: Alexander Shkurko Date: Fri, 19 Jan 2018 11:20:13 +0100 Subject: [PATCH 12/17] Backward compatible: adding a constructor parameter --- app/code/Magento/Sales/Block/Order/Recent.php | 10 ++++++---- .../Magento/Sales/Test/Unit/Block/Order/RecentTest.php | 5 ++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Sales/Block/Order/Recent.php b/app/code/Magento/Sales/Block/Order/Recent.php index b8d19235731dc..faf18ae8085fc 100644 --- a/app/code/Magento/Sales/Block/Order/Recent.php +++ b/app/code/Magento/Sales/Block/Order/Recent.php @@ -10,6 +10,7 @@ use Magento\Customer\Model\Session; use Magento\Sales\Model\Order\Config; use Magento\Store\Model\StoreManagerInterface; +use Magento\Framework\App\ObjectManager; /** * Sales order history block @@ -57,15 +58,16 @@ public function __construct( CollectionFactory $orderCollectionFactory, Session $customerSession, Config $orderConfig, - StoreManagerInterface $storeManager, - array $data = [] + array $data = [], + StoreManagerInterface $storeManager = null ) { $this->_orderCollectionFactory = $orderCollectionFactory; $this->_customerSession = $customerSession; $this->_orderConfig = $orderConfig; - $this->storeManager = $storeManager; - parent::__construct($context, $data); $this->_isScopePrivate = true; + $this->storeManager = $storeManager ?: ObjectManager::getInstance() + ->get(StoreManagerInterface::class); + parent::__construct($context, $data); } /** diff --git a/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php b/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php index 630263da9a1ed..96162aca42e12 100644 --- a/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php +++ b/app/code/Magento/Sales/Test/Unit/Block/Order/RecentTest.php @@ -64,7 +64,6 @@ protected function setUp() public function testConstructMethod() { - $data = []; $attribute = ['customer_id', 'store_id', 'status']; $customerId = 25; $storeId = 4; @@ -130,8 +129,8 @@ public function testConstructMethod() $this->orderCollectionFactory, $this->customerSession, $this->orderConfig, - $this->storeManagerMock, - $data + [], + $this->storeManagerMock ); $this->assertEquals($orderCollection, $this->block->getOrders()); } From 732905f100ab7df46aa9ed3e59250e7598ce6a7b Mon Sep 17 00:00:00 2001 From: Alexander Shkurko Date: Tue, 23 Jan 2018 16:00:38 +0100 Subject: [PATCH 13/17] Fix possible error with setting class properties --- app/code/Magento/Sales/Block/Order/Recent.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Sales/Block/Order/Recent.php b/app/code/Magento/Sales/Block/Order/Recent.php index faf18ae8085fc..96e889037ad2b 100644 --- a/app/code/Magento/Sales/Block/Order/Recent.php +++ b/app/code/Magento/Sales/Block/Order/Recent.php @@ -50,8 +50,8 @@ class Recent extends \Magento\Framework\View\Element\Template * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Sales\Model\Order\Config $orderConfig - * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param array $data + * @param \Magento\Store\Model\StoreManagerInterface $storeManager */ public function __construct( Context $context, @@ -65,9 +65,9 @@ public function __construct( $this->_customerSession = $customerSession; $this->_orderConfig = $orderConfig; $this->_isScopePrivate = true; + parent::__construct($context, $data); $this->storeManager = $storeManager ?: ObjectManager::getInstance() ->get(StoreManagerInterface::class); - parent::__construct($context, $data); } /** From fe0759e5b67e02437379ff43f88b8be555d4f866 Mon Sep 17 00:00:00 2001 From: Alexander Shkurko Date: Tue, 23 Jan 2018 16:32:06 +0100 Subject: [PATCH 14/17] Revert changes to fix unit tests --- app/code/Magento/Sales/Block/Order/Recent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Block/Order/Recent.php b/app/code/Magento/Sales/Block/Order/Recent.php index 96e889037ad2b..17436c50bd0e4 100644 --- a/app/code/Magento/Sales/Block/Order/Recent.php +++ b/app/code/Magento/Sales/Block/Order/Recent.php @@ -65,9 +65,9 @@ public function __construct( $this->_customerSession = $customerSession; $this->_orderConfig = $orderConfig; $this->_isScopePrivate = true; - parent::__construct($context, $data); $this->storeManager = $storeManager ?: ObjectManager::getInstance() ->get(StoreManagerInterface::class); + parent::__construct($context, $data); } /** From 2bb9b695d7f610c0fa0b77aa17d024b29a0b0061 Mon Sep 17 00:00:00 2001 From: rostyslav-hymon Date: Mon, 30 Apr 2018 15:49:55 +0300 Subject: [PATCH 15/17] change function getRecentOrders() to private. --- app/code/Magento/Sales/Block/Order/Recent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Block/Order/Recent.php b/app/code/Magento/Sales/Block/Order/Recent.php index 17436c50bd0e4..7e5be0ebfbba2 100644 --- a/app/code/Magento/Sales/Block/Order/Recent.php +++ b/app/code/Magento/Sales/Block/Order/Recent.php @@ -82,7 +82,7 @@ protected function _construct() /** * Get recently placed orders. By default they will be limited by 5. */ - protected function getRecentOrders() + private function getRecentOrders() { $orders = $this->_orderCollectionFactory->create()->addAttributeToSelect( '*' From 9e01479c24c8658ed648a12f6bf69a1ccd0a7450 Mon Sep 17 00:00:00 2001 From: Stanislav Idolov Date: Thu, 3 May 2018 12:03:09 +0300 Subject: [PATCH 16/17] Refactored code for avoiding copy/paste --- lib/internal/Magento/Framework/File/Csv.php | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/internal/Magento/Framework/File/Csv.php b/lib/internal/Magento/Framework/File/Csv.php index 10ee9b193d543..571ad6b21efa7 100644 --- a/lib/internal/Magento/Framework/File/Csv.php +++ b/lib/internal/Magento/Framework/File/Csv.php @@ -126,29 +126,23 @@ public function getDataPairs($file, $keyIndex = 0, $valueIndex = 1) /** * Saving data row array into file * - * @param string $file - * @param array $data - * @return $this + * @param string $file + * @param array $data + * @return $this * @throws \Magento\Framework\Exception\FileSystemException * @deprecated * @see appendData */ public function saveData($file, $data) { - $fh = fopen($file, 'w'); - foreach ($data as $dataRow) { - $this->file->filePutCsv($fh, $dataRow, $this->_delimiter, $this->_enclosure); - } - fclose($fh); - return $this; + return $this->appendData($file, $data, 'w'); } /** - * Replace the saveData method by - * allowing to select the input mode + * Replace the saveData method by allowing to select the input mode * - * @param $file - * @param $data + * @param string $file + * @param array $data * @param string $mode * * @return $this From 0a4d090ca7d095ec61af469780bd440d144e25a4 Mon Sep 17 00:00:00 2001 From: Stanislav Idolov Date: Sun, 6 May 2018 07:56:36 +0300 Subject: [PATCH 17/17] ENGCOM-1103: [Forwardport] Category\Collection::joinUrlRewrite should use the store set on the collection #14381 --- .../Catalog/Model/ResourceModel/Category/CollectionTest.php | 2 ++ .../Model/ResourceModel/_files/category_multiple_stores.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php index 7e26cdb921f39..3b1c23e2ae10c 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/Category/CollectionTest.php @@ -3,6 +3,8 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Catalog\Model\ResourceModel\Category; class CollectionTest extends \PHPUnit\Framework\TestCase diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php index 90bf630000e72..84b57f8706620 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/ResourceModel/_files/category_multiple_stores.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + /** @var \Magento\Catalog\Model\CategoryFactory $factory */ $factory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( \Magento\Catalog\Model\CategoryFactory::class