From 4ae12bbc019d34054c89dd5da833a31a9a4ee47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Thu, 23 Nov 2017 13:14:47 +0100 Subject: [PATCH] MAG2-42 - Added upgrade script for customers updating their shop from 2.0/2.1 to 2.2 so that the serialized config entries are converted to json --- Model/PayoneConfig.php | 2 +- Setup/UpgradeData.php | 42 ++++++++++++++++++++++++++++- Test/Unit/Setup/UpgradeDataTest.php | 20 ++++++++++++-- etc/module.xml | 2 +- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/Model/PayoneConfig.php b/Model/PayoneConfig.php index c5af54ba..61beb883 100644 --- a/Model/PayoneConfig.php +++ b/Model/PayoneConfig.php @@ -32,7 +32,7 @@ abstract class PayoneConfig { /* Module version */ - const MODULE_VERSION = '2.0.1'; + const MODULE_VERSION = '2.0.2'; /* Authorization request types */ const REQUEST_TYPE_PREAUTHORIZATION = 'preauthorization'; diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php index 85049f16..294f9876 100644 --- a/Setup/UpgradeData.php +++ b/Setup/UpgradeData.php @@ -30,6 +30,7 @@ use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Sales\Setup\SalesSetupFactory; +use Payone\Core\Helper\Shop; /** * Class UpgradeData @@ -43,14 +44,23 @@ class UpgradeData implements UpgradeDataInterface */ protected $salesSetupFactory; + /** + * PAYONE shop helper object + * + * @var Shop + */ + protected $shopHelper; + /** * Constructor * * @param SalesSetupFactory $salesSetupFactory + * @param Shop $shopHelper */ - public function __construct(SalesSetupFactory $salesSetupFactory) + public function __construct(SalesSetupFactory $salesSetupFactory, Shop $shopHelper) { $this->salesSetupFactory = $salesSetupFactory; + $this->shopHelper = $shopHelper; } /** @@ -167,7 +177,37 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface ['type' => 'varchar', 'length' => 64, 'default' => ''] ); } + if (version_compare($this->shopHelper->getMagentoVersion(), '2.2.0', '>=') && + version_compare($context->getVersion(), '2.0.2', '<')) { + $this->convertSerializedDataToJson($setup); + } $setup->endSetup(); } + /** + * Convert serialized data to json-encoded data in the core_config_data table + * + * @param ModuleDataSetupInterface $setup + * @return void + */ + private function convertSerializedDataToJson(ModuleDataSetupInterface $setup) + { + $sTable = $setup->getTable('core_config_data'); + + $select = $setup->getConnection() + ->select() + ->from($sTable, ['config_id', 'value']) + ->where('path LIKE "%payone%"') + ->where('value LIKE "a:%"'); + + $serializedRows = $setup->getConnection()->fetchAssoc($select); + foreach ($serializedRows as $id => $serializedRow) { + $aValue = unserialize($serializedRow['value']); + $sNewValue = json_encode($aValue); + + $data = ['value' => $sNewValue]; + $where = ['config_id = ?' => $id]; + $setup->getConnection()->update($sTable, $data, $where); + } + } } diff --git a/Test/Unit/Setup/UpgradeDataTest.php b/Test/Unit/Setup/UpgradeDataTest.php index 003ce307..d83ccc09 100644 --- a/Test/Unit/Setup/UpgradeDataTest.php +++ b/Test/Unit/Setup/UpgradeDataTest.php @@ -35,6 +35,7 @@ use Magento\Framework\DB\Adapter\Pdo\Mysql; use Payone\Core\Test\Unit\BaseTestCase; use Payone\Core\Model\Test\PayoneObjectManager; +use Payone\Core\Helper\Shop; class UpgradeDataTest extends BaseTestCase { @@ -59,21 +60,36 @@ protected function setUp() ->getMock(); $salesSetupFactory->method('create')->willReturn($salesSetup); + $shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock(); + $shopHelper->method('getMagentoVersion')->willReturn('2.2.0'); + $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ - 'salesSetupFactory' => $salesSetupFactory + 'salesSetupFactory' => $salesSetupFactory, + 'shopHelper' => $shopHelper ]); } public function testInstall() { - $connection = $this->getMockBuilder(Mysql::class)->disableOriginalConstructor()->getMock(); + $fetchResult = [['value' => serialize(['a' => 'b'])]]; + + $connection = $this->getMockBuilder(Mysql::class) + ->setMethods(['tableColumnExists', 'select', 'from', 'where', 'fetchAssoc', 'update']) + ->disableOriginalConstructor() + ->getMock(); $connection->method('tableColumnExists')->willReturn(false); + $connection->method('select')->willReturn($connection); + $connection->method('from')->willReturn($connection); + $connection->method('where')->willReturn($connection); + $connection->method('update')->willReturn(1); + $connection->method('fetchAssoc')->willReturn($fetchResult); $setup = $this->getMockBuilder(ModuleDataSetupInterface::class)->disableOriginalConstructor()->getMock(); $setup->method('getTable')->willReturn('table'); $setup->method('getConnection')->willReturn($connection); $context = $this->getMockBuilder(ModuleContextInterface::class)->disableOriginalConstructor()->getMock(); + $context->method('getVersion')->willReturn('2.0.1'); $result = $this->classToTest->upgrade($setup, $context); $this->assertNull($result); diff --git a/etc/module.xml b/etc/module.xml index f45e4f4f..589adf8c 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -25,7 +25,7 @@ */ --> - +