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 #14958 - remove sales sequence data on store view delete #22296

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
8529467
Fix #14958 - remove sales sequence data on store view delete
Bartlomiejsz Apr 12, 2019
4c4ae53
Fix #14958 - add unit tests
Bartlomiejsz Apr 12, 2019
774d40f
FIx #14958 - add last unit test
Bartlomiejsz Apr 12, 2019
8dab9d7
FIx #14958 - fix integration tests
Bartlomiejsz Apr 12, 2019
4385c2f
Revert "FIx #14958 - fix integration tests"
Bartlomiejsz Apr 15, 2019
78bfbc8
Revert "FIx #14958 - add last unit test"
Bartlomiejsz Apr 15, 2019
3ff5c06
Fix #14958 - move sales sequence removal to external class
Bartlomiejsz Apr 15, 2019
85b0730
Fix #14958 - change fields visibility
Bartlomiejsz Apr 15, 2019
309a25f
Fix #14958 - declare dependency on Magento_Store
Bartlomiejsz Apr 16, 2019
c83a283
magento/magento2#22296: Static test fix.
p-bystritsky Apr 23, 2019
328bc51
Fix #14958 - apply requested changes
Bartlomiejsz Jun 4, 2019
7c078cf
Fix #14958 - add declare strict_types
Bartlomiejsz Jun 4, 2019
76b2085
Fix #14958 - move two methods from resource models to private methods…
Bartlomiejsz Jun 7, 2019
11f013c
Fix #14958 - remove requirement of magento/module-store
Bartlomiejsz Aug 13, 2019
68d1bed
fix #14958 - fix static check
Bartlomiejsz Aug 14, 2019
48f213d
Merge remote-tracking branch 'mainline/2.3-develop' into feature/fix_…
engcom-Foxtrot Oct 7, 2019
6458cc8
magento/magento2#22296: Integration test fix.
engcom-Foxtrot Oct 7, 2019
94b6a17
Merge remote-tracking branch 'mainline/2.4-develop' into feature/fix_…
engcom-Foxtrot Dec 16, 2019
f894321
refactor integration tests, change fixtures
engcom-Foxtrot Jan 3, 2020
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
44 changes: 43 additions & 1 deletion app/code/Magento/SalesSequence/Model/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
*/
namespace Magento\SalesSequence\Model;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection as AppResource;
use Magento\Framework\DB\Ddl\Sequence as DdlSequence;
use Magento\Framework\Webapi\Exception;
use Magento\SalesSequence\Model\ResourceModel\Meta as ResourceMetadata;
use Magento\SalesSequence\Model\ResourceModel\Profile as ResourceProfile;
use Psr\Log\LoggerInterface as Logger;

/**
Expand Down Expand Up @@ -83,28 +85,36 @@ class Builder
*/
protected $logger;

/**
* @var ResourceProfile
*/
private $resourceProfile;

/**
* @param ResourceMetadata $resourceMetadata
* @param MetaFactory $metaFactory
* @param ProfileFactory $profileFactory
* @param AppResource $appResource
* @param DdlSequence $ddlSequence
* @param Logger $logger
* @param ResourceProfile|null $resourceProfile
*/
public function __construct(
ResourceMetadata $resourceMetadata,
MetaFactory $metaFactory,
ProfileFactory $profileFactory,
AppResource $appResource,
DdlSequence $ddlSequence,
Logger $logger
Logger $logger,
ResourceProfile $resourceProfile = null
) {
$this->resourceMetadata = $resourceMetadata;
$this->metaFactory = $metaFactory;
$this->profileFactory = $profileFactory;
$this->appResource = $appResource;
$this->ddlSequence = $ddlSequence;
$this->logger = $logger;
$this->resourceProfile = $resourceProfile ?: ObjectManager::getInstance()->get(ResourceProfile::class);
$this->data = array_flip($this->pattern);
}

Expand Down Expand Up @@ -264,4 +274,36 @@ public function create()
}
$this->data = array_flip($this->pattern);
}

/**
* Deletes all sequence linked entites
*
* @param $storeId
*
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function deleteByStoreId($storeId)
Bartlomiejsz marked this conversation as resolved.
Show resolved Hide resolved
{
$metadataIds = $this->resourceMetadata->getIdsByStore($storeId);
$profileIds = $this->resourceProfile->getProfileIdsByMetadataIds($metadataIds);

$this->appResource->getConnection()->delete(
$this->appResource->getTableName('sales_sequence_profile'),
['profile_id IN (?)' => $profileIds]
);

foreach ($metadataIds as $metadataId) {
$metadata = $this->metaFactory->create();
$this->resourceMetadata->load($metadata, $metadataId);
if (!$metadata->getId()) {
continue;
}

$this->appResource->getConnection()->dropTable(
$metadata->getSequenceTable()
);
$this->resourceMetadata->delete($metadata);
}
}
}
21 changes: 21 additions & 0 deletions app/code/Magento/SalesSequence/Model/ResourceModel/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ public function loadByEntityTypeAndStore($entityType, $storeId)
return $meta;
}

/**
* Retrieves Metadata Ids by store id
*
* @param int $storeId
* @return int[]
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getIdsByStore($storeId)
Bartlomiejsz marked this conversation as resolved.
Show resolved Hide resolved
{
$connection = $this->getConnection();
$bind = ['store_id' => $storeId];
$select = $connection->select()->from(
$this->getMainTable(),
[$this->getIdFieldName()]
)->where(
'store_id = :store_id'
);

return $connection->fetchCol($select, $bind);
}

/**
* Using for load sequence profile and setting it into metadata
*
Expand Down
16 changes: 16 additions & 0 deletions app/code/Magento/SalesSequence/Model/ResourceModel/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,20 @@ public function loadActiveProfile($metadataId)
}
return $profile;
}

/**
* Get profile ids by metadata ids
* @param int[] $metadataIds
* @return int[]
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getProfileIdsByMetadataIds(array $metadataIds)
Bartlomiejsz marked this conversation as resolved.
Show resolved Hide resolved
{
$connection = $this->getConnection();
$select = $connection->select()
->from($this->getMainTable(), ['profile_id'])
->where('meta_id IN (?)', $metadataIds);

return $connection->fetchCol($select);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\SalesSequence\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\SalesSequence\Model\Builder;

/**
* Class SequenceRemovalObserver
*/
class SequenceRemovalObserver implements ObserverInterface
{
/**
* @var Builder
*/
private $sequenceBuilder;

/**
* @param Builder $sequenceBuilder
*/
public function __construct(
Builder $sequenceBuilder
) {
$this->sequenceBuilder = $sequenceBuilder;
}

/**
* @param EventObserver $observer
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(EventObserver $observer)
{
$storeId = $observer->getData('store')->getId();
$this->sequenceBuilder->deleteByStoreId($storeId);

return $this;
}
}
62 changes: 57 additions & 5 deletions app/code/Magento/SalesSequence/Test/Unit/Model/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\SalesSequence\Test\Unit\Model;

use Magento\SalesSequence\Model\ResourceModel\Profile as ResourceProfile;

/**
* Class BuilderTest
*/
Expand All @@ -20,6 +22,11 @@ class BuilderTest extends \PHPUnit\Framework\TestCase
*/
private $resourceSequenceMeta;

/**
* @var ResourceProfile | \PHPUnit_Framework_MockObject_MockObject
*/
private $resourceSequenceProfile;

/**
* @var \Magento\SalesSequence\Model\Meta | \PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -68,7 +75,11 @@ protected function setUp()
);
$this->resourceSequenceMeta = $this->createPartialMock(
\Magento\SalesSequence\Model\ResourceModel\Meta::class,
['loadByEntityTypeAndStore', 'save', 'createSequence']
['loadByEntityTypeAndStore', 'save', 'createSequence', 'getIdsByStore', 'load', 'delete']
);
$this->resourceSequenceProfile = $this->createPartialMock(
ResourceProfile::class,
['getProfileIdsByMetadataIds']
);
$this->meta = $this->createPartialMock(
\Magento\SalesSequence\Model\Meta::class,
Expand All @@ -87,9 +98,6 @@ protected function setUp()
['create']
);
$this->profileFactory->expects($this->any())->method('create')->willReturn($this->profile);
$this->resourceMock->expects($this->atLeastOnce())
->method('getTableName')
->willReturn('sequence_lalalka_1');

$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->sequenceBuilder = $helper->getObject(
Expand All @@ -99,7 +107,8 @@ protected function setUp()
'metaFactory' => $this->metaFactory,
'profileFactory' => $this->profileFactory,
'appResource' => $this->resourceMock,
'ddlSequence' => $this->sequence
'ddlSequence' => $this->sequence,
'resourceProfile' => $this->resourceSequenceProfile
]
);
}
Expand All @@ -108,6 +117,9 @@ public function testAddSequenceExistMeta()
{
$entityType = 'lalalka';
$storeId = 1;
$this->resourceMock->expects($this->atLeastOnce())
->method('getTableName')
->willReturn('sequence_lalalka_1');
$this->resourceSequenceMeta->expects($this->once())
->method('loadByEntityTypeAndStore')
->with($entityType, $storeId)
Expand Down Expand Up @@ -138,6 +150,9 @@ public function testAddSequence()
$step = 1;
$maxValue = 120000;
$warningValue = 110000;
$this->resourceMock->expects($this->atLeastOnce())
->method('getTableName')
->willReturn('sequence_lalalka_1');
$this->resourceSequenceMeta->expects($this->once())
->method('loadByEntityTypeAndStore')
->with($entityType, $storeId)
Expand Down Expand Up @@ -182,6 +197,43 @@ public function testAddSequence()
->create();
}

public function testDeleteByStoreId()
{
$storeId = 1;
$metadataIds = [1, 2];
$profileIds = [10, 11];
$tableName = 'sales_sequence_profile';
$this->resourceSequenceMeta->expects($this->once())
->method('getIdsByStore')
->with($storeId)
->willReturn($metadataIds);
$this->resourceSequenceProfile->expects($this->once())
->method('getProfileIdsByMetadataIds')
->with($metadataIds)
->willReturn($profileIds);
$this->resourceMock->expects($this->once())
->method('getTableName')
->with($tableName)
->willReturn($tableName);
$this->resourceMock->expects($this->any())
->method('getConnection')
->willReturn($this->connectionMock);
$this->connectionMock->expects($this->once())
->method('delete')
->with($tableName, ['profile_id IN (?)' => $profileIds])
->willReturn(2);
$this->resourceSequenceMeta->expects($this->any())
->method('load')
->willReturn($this->meta);
$this->connectionMock->expects($this->any())
->method('dropTable')
->willReturn(true);
$this->resourceSequenceMeta->expects($this->any())
->method('delete')
->willReturn($this->resourceSequenceMeta);
$this->sequenceBuilder->deleteByStoreId($storeId);
}

/**
* Step create sequence
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@ public function testLoadBy()
$this->assertEquals($this->meta, $this->resource->loadByEntityTypeAndStore($entityType, $storeId));
}

public function testGetIdsByStore()
{
$metaTableName = 'sequence_meta';
$metaIdFieldName = 'meta_id';
$storeId = 1;
$metaIds = [1, 2];
$this->resourceMock->expects($this->any())
->method('getConnection')
->willReturn($this->connectionMock);
$this->resourceMock->expects($this->once())
->method('getTableName')
->willReturn($metaTableName);
$this->connectionMock->expects($this->any())->method('select')->willReturn($this->select);
$this->select->expects($this->at(0))
->method('from')
->with($metaTableName, [$metaIdFieldName])
->willReturn($this->select);
$this->select->expects($this->at(1))
->method('where')
->with('store_id = :store_id')
->willReturn($this->select);
$this->connectionMock->expects($this->once())
->method('fetchCol')
->with($this->select, ['store_id' => $storeId])
->willReturn($metaIds);
$this->assertEquals($metaIds, $this->resource->getIdsByStore($storeId));
}

/**
* @param $metaData
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,32 @@ public function testLoadActiveProfile()
$this->profile->expects($this->at(1))->method('setData')->with($profileData);
$this->assertEquals($this->profile, $this->resource->loadActiveProfile($metaId));
}

public function testGetProfileIdsByMetadataIds()
{
$profileTableName = 'sequence_profile';
$profileIdFieldName = 'profile_id';
$metadataIds = [1, 2];
$profileIds = [10, 11];
$this->resourceMock->expects($this->any())
->method('getConnection')
->willReturn($this->connectionMock);
$this->resourceMock->expects($this->once())
->method('getTableName')
->willReturn($profileTableName);
$this->connectionMock->expects($this->any())->method('select')->willReturn($this->select);
$this->select->expects($this->at(0))
->method('from')
->with($profileTableName, [$profileIdFieldName])
->willReturn($this->select);
$this->select->expects($this->at(1))
->method('where')
->with('meta_id IN (?)', $metadataIds)
->willReturn($this->select);
$this->connectionMock->expects($this->once())
->method('fetchCol')
->with($this->select)
->willReturn($profileIds);
$this->assertEquals($profileIds, $this->resource->getProfileIdsByMetadataIds($metadataIds));
}
}
12 changes: 12 additions & 0 deletions app/code/Magento/SalesSequence/etc/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="store_delete">
<observer name="magento_sequence" instance="Magento\SalesSequence\Observer\SequenceRemovalObserver" />
</event>
</config>
Loading