Skip to content

Commit

Permalink
Merge pull request #125 from FatchipRobert/MAG2-42-Conversion-of-old-…
Browse files Browse the repository at this point in the history
…serialized-config-entries

MAG2-42 - Added upgrade script for customers updating their shop from…
  • Loading branch information
Florian Bender authored Nov 23, 2017
2 parents beb4103 + 4ae12bb commit dc9ff4e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Model/PayoneConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
42 changes: 41 additions & 1 deletion Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}
}
20 changes: 18 additions & 2 deletions Test/Unit/Setup/UpgradeDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Payone_Core" setup_version="2.0.1">
<module name="Payone_Core" setup_version="2.0.2">
<sequence>
<module name="Magento_Quote" />
<module name="Magento_Sales" />
Expand Down

0 comments on commit dc9ff4e

Please sign in to comment.