Skip to content

Commit

Permalink
CRM-8048: Do not skip Magento customers if some data is not matched t…
Browse files Browse the repository at this point in the history
…o Oro (#9208)

* CRM-8048: Do not skip Magento customers if some data is not matched to Oro - added country_text to Order, Cart and Customer addresses - country text is set in data converter by country id received from magento - if country is found, than country_text is set to null - if there is no country country_text is rendered instead in address book

* CRM-8048: Do not skip Magento customers if some data is not matched to Oro - added unit and functional test for changed functionality - fixed installer version

* CRM-8048: Do not skip Magento customers if some data is not matched to Oro - fixed failing tests

* CRM-8048: Do not skip Magento customers if some data is not matched to Oro - fixed test name

* CRM-8048: Do not skip Magento customers if some data is not matched to Oro - reverted incorrect test namespaces to get CI green
  • Loading branch information
x86demon authored and bemzaslava committed Apr 3, 2017
1 parent 4259e47 commit d15cdd0
Show file tree
Hide file tree
Showing 29 changed files with 671 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Oro/Bundle/MagentoBundle/Entity/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Address extends ExtendAddress implements OriginAwareInterface, Integration
const SYNC_TO_MAGENTO = 1;
const MAGENTO_REMOVED = 2;

use IntegrationEntityTrait, OriginTrait;
use IntegrationEntityTrait, OriginTrait, CountryTextTrait;

/*
* FIELDS are duplicated to enable dataaudit only for customer address fields
Expand Down
2 changes: 1 addition & 1 deletion src/Oro/Bundle/MagentoBundle/Entity/CartAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
class CartAddress extends ExtendCartAddress implements OriginAwareInterface
{
use IntegrationEntityTrait, OriginTrait;
use IntegrationEntityTrait, OriginTrait, CountryTextTrait;

/**
* @var string
Expand Down
64 changes: 64 additions & 0 deletions src/Oro/Bundle/MagentoBundle/Entity/CountryTextTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Oro\Bundle\MagentoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

trait CountryTextTrait
{
/**
* @var string
*
* @ORM\Column(name="country_text", type="string", length=255, nullable=true)
*/
protected $countryText;

/**
* @return string
*/
public function getCountryText()
{
return $this->countryText;
}

/**
* @param string $countryText
* @return $this
*/
public function setCountryText($countryText)
{
$this->countryText = $countryText;

return $this;
}

/**
* Get name of country
*
* @return string
*/
public function getCountryName()
{
return $this->getCountry() ? parent::getCountryName() : $this->getCountryText();
}

/**
* Get country ISO3 code
*
* @return string
*/
public function getCountryIso3()
{
return $this->getCountry() ? parent::getCountryIso3() : $this->getCountryText();
}

/**
* Get country ISO2 code
*
* @return string
*/
public function getCountryIso2()
{
return $this->getCountry() ? parent::getCountryIso2() : $this->getCountryText();
}
}
2 changes: 1 addition & 1 deletion src/Oro/Bundle/MagentoBundle/Entity/OrderAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
class OrderAddress extends ExtendOrderAddress implements IntegrationAwareInterface, OriginAwareInterface
{
use IntegrationEntityTrait, OriginTrait;
use IntegrationEntityTrait, OriginTrait, CountryTextTrait;

/**
* @var ArrayCollection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => true
]
);
$builder->add('countryText', 'text', ['required' => false]);

$builder->add(
'region',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CartAddressApiType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('phone', 'text', ['required' => false]);
$builder->add('countryText', 'text', ['required' => false]);
$builder->remove('organization');

$builder->addEventSubscriber(new PatchSubscriber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => true,
]
);
$builder->add('countryText', 'text', ['required' => false]);

$builder->add(
'region',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ protected function getHeaderConversionRules()
*/
public function convertToImportFormat(array $importedRecord, $skipNullValues = true)
{
if (!empty($importedRecord['country_id'])) {
$importedRecord['countryText'] = $importedRecord['country_id'];
}
$importedRecord = parent::convertToImportFormat($importedRecord, $skipNullValues);

if (!empty($importedRecord['street'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ protected function updateAddresses(Cart $entity)
$mageRegionId = $address->getRegion() ? $address->getRegion()->getCode() : null;
$this->addressHelper->updateAddressCountryRegion($address, $mageRegionId);
if ($address->getCountry()) {
$address->setCountryText(null);
$this->getPropertyAccessor()->setValue($entity, $addressName, $address);
} else {
$this->getPropertyAccessor()->setValue($entity, $addressName, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ protected function processAddresses(Customer $entity)
}
}
}
if ($address->getCountry()) {
$address->setCountryText(null);
}
$address->setOwner($entity);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ protected function processAddresses(Order $order)
{
/** @var OrderAddress $address */
foreach ($order->getAddresses() as $address) {
if ($address->getCountry()) {
$address->setCountryText(null);
}
$address->setOwner($order);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function setExtendExtension(ExtendExtension $extendExtension)
*/
public function getMigrationVersion()
{
return 'v1_46';
return 'v1_46_1';
}

/**
Expand Down Expand Up @@ -187,6 +187,7 @@ protected function createOrocrmMagentoCartAddressTable(Schema $schema)
$table->addColumn('city', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('postal_code', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('organization', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('country_text', 'string', ['notnull' => false, 'length' => 255]);
$table->addColumn('region_text', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('name_prefix', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('first_name', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
Expand Down Expand Up @@ -460,6 +461,7 @@ protected function createOrocrmMagentoCustomerAddrTable(Schema $schema)
$table->addColumn('street2', 'string', ['notnull' => false, 'length' => 500, 'precision' => 0]);
$table->addColumn('city', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('postal_code', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('country_text', 'string', ['notnull' => false, 'length' => 255]);
$table->addColumn('region_text', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('name_prefix', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('first_name', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
Expand Down Expand Up @@ -515,6 +517,7 @@ protected function createOrocrmMagentoOrderAddressTable(Schema $schema)
$table->addColumn('city', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('postal_code', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('organization', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('country_text', 'string', ['notnull' => false, 'length' => 255]);
$table->addColumn('region_text', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('first_name', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
$table->addColumn('last_name', 'string', ['notnull' => false, 'length' => 255, 'precision' => 0]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Oro\Bundle\MagentoBundle\Migrations\Schema\v1_46_1;

use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;

class AddCountryTextToAddresses implements Migration
{
/**
* {@inheritdoc}
*/
public function up(Schema $schema, QueryBag $queries)
{
$this->addCountryTextField($schema, 'orocrm_magento_customer_addr');
$this->addCountryTextField($schema, 'orocrm_magento_order_address');
$this->addCountryTextField($schema, 'orocrm_magento_cart_address');
}

/**
* @param Schema $schema
* @param string $tableName
*/
private function addCountryTextField(Schema $schema, $tableName)
{
$table = $schema->getTable($tableName);
$table->addColumn('country_text', 'string', ['notnull' => false, 'length' => 255]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ oro:
country:
label: Country
description: The country specified in the Magento customer address.
country_text.label: Country (Text)
region:
label: State
description: The region specified in the Magento customer address.
Expand Down Expand Up @@ -831,6 +832,7 @@ oro:
country:
label: Country
description: The country specified in the Magento cart address.
country_text.label: Country (Text)
region:
label: State
description: The region specified in the Magento cart address.
Expand Down Expand Up @@ -861,6 +863,7 @@ oro:
country:
label: Country
description: The country specified in the Magento order address.
country_text.label: Country (Text)
region:
label: State
description: The region specified in the Magento order address.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Oro\Bundle\MagentoBundle\Tests\Functional\ImportExport\Strategy;

use Akeneo\Bundle\BatchBundle\Entity\JobExecution;
use Akeneo\Bundle\BatchBundle\Entity\JobInstance;
use Akeneo\Bundle\BatchBundle\Entity\StepExecution;
use Oro\Bundle\AddressBundle\Entity\Country;
use Oro\Bundle\ImportExportBundle\Context\StepExecutionProxyContext;
use Oro\Bundle\MagentoBundle\Entity\Order;
use Oro\Bundle\MagentoBundle\Entity\OrderAddress;
use Oro\Bundle\MagentoBundle\ImportExport\Strategy\OrderStrategy;
use Oro\Bundle\MagentoBundle\Tests\Functional\Fixture\LoadMagentoChannel;
use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;

/**
* @dbIsolation
*/
class OrderStrategyTest extends WebTestCase
{
/**
* @var OrderStrategy
*/
private $strategy;

/**
* @var StepExecutionProxyContext
*/
protected $context;

/**
* @var StepExecution
*/
protected $stepExecution;

protected function setUp()
{
$this->initClient();

$this->loadFixtures([
LoadMagentoChannel::class
]);

$this->strategy = $this->getContainer()->get('oro_magento.import.strategy.order.add_or_update');
$this->strategy->setEntityName(Order::class);

$jobInstance = new JobInstance();
$jobInstance->setRawConfiguration(['channel' => 3]);
$jobExecution = new JobExecution();
$jobExecution->setJobInstance($jobInstance);
$this->stepExecution = new StepExecution('step', $jobExecution);
$this->context = new StepExecutionProxyContext($this->stepExecution);
$this->strategy->setImportExportContext($this->context);
$this->strategy->setStepExecution($this->stepExecution);
}

public function testProcessAddressCountryTextSetTuNullWhenCountryIsSet()
{
$em = $this->getContainer()->get('doctrine')->getManagerForClass(Order::class);
$order = $em->getRepository(Order::class)->findOneBy([]);

$street = 'Test_Street_001';
$country = new Country('US');
$address = new OrderAddress();
$address->setCountry($country);
$address->setCountryText('Test');
$address->setStreet($street);

$order->resetAddresses([$address]);

/** @var Order $processedOrder */
$processedOrder = $this->strategy->process($order);
/** @var OrderAddress $processedAddress */
$processedAddress = $processedOrder->getAddresses()->first();
$this->assertInstanceOf(OrderAddress::class, $processedAddress);
$this->assertSame($street, $processedAddress->getStreet());
$this->assertInstanceOf(Country::class, $processedAddress->getCountry());
$this->assertNull($processedAddress->getCountryText());
}

public function testProcessAddressCountryTextSetTuNullWhenCountryIsEmpty()
{
$em = $this->getContainer()->get('doctrine')->getManagerForClass(Order::class);
$order = $em->getRepository(Order::class)->findOneBy([]);

$street = 'Test_Street_002';
$address = new OrderAddress();
$address->setCountryText('Test');
$address->setStreet($street);

$order->resetAddresses([$address]);

/** @var Order $processedOrder */
$processedOrder = $this->strategy->process($order);
/** @var OrderAddress $processedAddress */
$processedAddress = $processedOrder->getAddresses()->first();
$this->assertInstanceOf(OrderAddress::class, $processedAddress);
$this->assertSame($street, $processedAddress->getStreet());
$this->assertNull($processedAddress->getCountry());
$this->assertSame('Test', $processedAddress->getCountryText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Oro\Bundle\MagentoBundle\Tests\Unit\Entity;

use Oro\Bundle\AddressBundle\Entity\Country;

class AddressTest extends AbstractEntityTestCase
{
/**
Expand All @@ -19,10 +21,13 @@ public function getSetDataProvider()
{
$owner = $this->createMock('Oro\Bundle\MagentoBundle\Entity\Customer');
$originId = 123;
$country = new Country('US');

return [
'owner' => ['owner', $owner, $owner],
'origin_id' => ['originId', $originId, $originId],
'country' => ['country', $country, $country],
'country_text' => ['countryText', 'USA', 'USA']
];
}
}
30 changes: 30 additions & 0 deletions src/Oro/Bundle/MagentoBundle/Tests/Unit/Entity/CartAddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Oro\Bundle\MagentoBundle\Tests\Unit\Entity;

use Oro\Bundle\AddressBundle\Entity\Country;
use Oro\Bundle\MagentoBundle\Entity\CartAddress;

class CartAddressTest extends AbstractEntityTestCase
{
/**
* {@inheritDoc}
*/
public function getEntityFQCN()
{
return CartAddress::class;
}

/**
* {@inheritDoc}
*/
public function getSetDataProvider()
{
$country = new Country('US');

return [
'country' => ['country', $country, $country],
'country_text' => ['countryText', 'USA', 'USA']
];
}
}
Loading

0 comments on commit d15cdd0

Please sign in to comment.