Skip to content

Commit

Permalink
NEXT-36114 - import customer bound on salesChannel by name
Browse files Browse the repository at this point in the history
  • Loading branch information
ennasus4sun committed Jun 19, 2024
1 parent 4f1c273 commit 55e1c69
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,20 @@
#[Package('core')]
class CustomerSerializer extends EntitySerializer implements ResetInterface
{
/**
* @var array<string, string|null>
*/
private array $cacheCustomerGroups = [];

/**
* @var array<string, string|null>
*/
private array $cachePaymentMethods = [];

/**
* @var array<string, string|null>
*/
private array $cacheSalesChannels = [];

/**
* @internal
*
* @param array<string, string|null> $cacheCustomerGroups
* @param array<string, string|null> $cachePaymentMethods
* @param array<string, string|null> $cacheSalesChannels
*/
public function __construct(
private readonly EntityRepository $customerGroupRepository,
private readonly EntityRepository $paymentMethodRepository,
private readonly EntityRepository $salesChannelRepository
private readonly EntityRepository $salesChannelRepository,
private array $cacheCustomerGroups = [],
private array $cachePaymentMethods = [],
private array $cacheSalesChannels = [],
) {
}

Expand All @@ -55,7 +47,6 @@ public function deserialize(Config $config, EntityDefinition $definition, $entit
$id = $entity['group']['id'] ?? $this->getCustomerGroupId($name, $context);

if ($id) {
$deserialized['groupId'] = $id;
$deserialized['group']['id'] = $id;
}
}
Expand All @@ -65,7 +56,6 @@ public function deserialize(Config $config, EntityDefinition $definition, $entit
$id = $entity['defaultPaymentMethod']['id'] ?? $this->getDefaultPaymentMethodId($name, $context);

if ($id) {
$deserialized['defaultPaymentMethodId'] = $id;
$deserialized['defaultPaymentMethod']['id'] = $id;
}
}
Expand All @@ -75,11 +65,19 @@ public function deserialize(Config $config, EntityDefinition $definition, $entit
$id = $entity['salesChannel']['id'] ?? $this->getSalesChannelId($name, $context);

if ($id) {
$deserialized['salesChannelId'] = $id;
$deserialized['salesChannel']['id'] = $id;
}
}

if (!isset($deserialized['boundSalesChannelId']) && isset($entity['boundSalesChannel'])) {
$name = $entity['boundSalesChannel']['translations']['DEFAULT']['name'] ?? null;
$id = $entity['boundSalesChannel']['id'] ?? $this->getSalesChannelId($name, $context);

if ($id) {
$deserialized['boundSalesChannel']['id'] = $id;
}
}

yield from $deserialized;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Content\Test\ImportExport\DataAbstractionLayer\Serializer\Entity;
namespace Shopware\Tests\Integration\Core\Content\ImportExport\DataAbstractionLayer\Serializer\Entity;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Customer\CustomerDefinition;
use Shopware\Core\Content\ImportExport\DataAbstractionLayer\Serializer\Entity\CustomerSerializer;
Expand All @@ -18,6 +19,7 @@
* @internal
*/
#[Package('services-settings')]
#[CoversClass(CustomerSerializer::class)]
class CustomerSerializerTest extends TestCase
{
use IntegrationTestBehaviour;
Expand Down Expand Up @@ -82,6 +84,13 @@ public function testSimple(): void
],
],
],
'boundSalesChannel' => [
'translations' => [
'DEFAULT' => [
'name' => $salesChannel['name'],
],
],
],
];

$deserialized = $this->serializer->deserialize($config, $this->customerRepository->getDefinition(), $customer);
Expand All @@ -90,12 +99,10 @@ public function testSimple(): void

$deserialized = \iterator_to_array($deserialized);

static::assertSame($this->customerGroupId, $deserialized['groupId']);
static::assertSame($this->customerGroupId, $deserialized['group']['id']);
static::assertSame($this->paymentMethodId, $deserialized['defaultPaymentMethodId']);
static::assertSame($this->paymentMethodId, $deserialized['defaultPaymentMethod']['id']);
static::assertSame($salesChannel['id'], $deserialized['salesChannelId']);
static::assertSame($salesChannel['id'], $deserialized['salesChannel']['id']);
static::assertSame($salesChannel['id'], $deserialized['boundSalesChannel']['id']);
}

public function testSupportsOnlyCountry(): void
Expand Down
105 changes: 68 additions & 37 deletions tests/integration/Core/Content/ImportExport/ImportExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
use Shopware\Core\Checkout\Customer\CustomerCollection;
use Shopware\Core\Checkout\Customer\CustomerDefinition;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Checkout\Promotion\Aggregate\PromotionDiscount\PromotionDiscountCollection;
use Shopware\Core\Checkout\Promotion\Aggregate\PromotionDiscount\PromotionDiscountDefinition;
Expand Down Expand Up @@ -131,8 +132,6 @@ public function testExportEvents(): void
{
$this->listener->addSubscriber(new StockSubscriber());

$filesystem = static::getContainer()->get('shopware.filesystem.private');

$productId = Uuid::randomHex();
$product = $this->getTestProduct($productId);
$newStock = (int) $product['stock'] + 1;
Expand All @@ -145,10 +144,7 @@ public function testExportEvents(): void
static::assertContains(ImportExportBeforeExportRecordEvent::class, $events);
static::assertNotContains(ImportExportExceptionExportRecordEvent::class, $events);

$logfile = $this->getLogEntity($progress->getLogId())->getFile();
static::assertInstanceOf(ImportExportFileEntity::class, $logfile);
$csv = $filesystem->read($logfile->getPath());
static::assertIsString($csv);
$csv = $this->getCsvContent($progress->getLogId());
static::assertStringContainsString(sprintf(';%s;', $newStock), $csv);
}

Expand Down Expand Up @@ -421,10 +417,7 @@ public function testImportExportAdvancedPrices(): void

static::assertImportExportSucceeded($progress);

$filesystem = static::getContainer()->get('shopware.filesystem.private');
$logfile = $this->getLogEntity($progress->getLogId())->getFile();
static::assertInstanceOf(ImportExportFileEntity::class, $logfile);
$csv = $filesystem->read($logfile->getPath());
$csv = $this->getCsvContent($progress->getLogId());

static::assertStringEqualsFile(__DIR__ . '/fixtures/advanced_prices.csv', $csv);
}
Expand Down Expand Up @@ -1114,11 +1107,7 @@ public function testProductImportExportWithCustomField(): void

static::assertImportExportSucceeded($progress, $this->getInvalidLogContent($progress->getInvalidRecordsLogId()));

$filesystem = static::getContainer()->get('shopware.filesystem.private');
$logfile = $this->getLogEntity($progress->getLogId())->getFile();
static::assertInstanceOf(ImportExportFileEntity::class, $logfile);
$csv = $filesystem->read($logfile->getPath());
static::assertIsString($csv);
$csv = $this->getCsvContent($progress->getLogId());
$resource = fopen('data://text/plain;base64,' . base64_encode($csv), 'r');
static::assertIsResource($resource);
$reader = new CsvReader();
Expand Down Expand Up @@ -1269,12 +1258,8 @@ public function testCrossSellingCsv(): void

static::assertImportExportSucceeded($progress, $this->getInvalidLogContent($progress->getInvalidRecordsLogId()));

$filesystem = static::getContainer()->get('shopware.filesystem.private');
$logfile = $this->getLogEntity($progress->getLogId())->getFile();
static::assertInstanceOf(ImportExportFileEntity::class, $logfile);
$csv = $filesystem->read($logfile->getPath());
$csv = $this->getCsvContent($progress->getLogId());

static::assertIsString($csv);
static::assertStringContainsString(
'f26b0d8f252a76f2f99337cced08314b|c1ace7586faa4342a4d3b33e6dd33b7c|c9a70321b66449abb54ba9306ad02835',
$csv
Expand Down Expand Up @@ -1344,12 +1329,7 @@ public function testCustomersCsv(): void

static::assertImportExportSucceeded($progress, $this->getInvalidLogContent($progress->getInvalidRecordsLogId()));

$filesystem = static::getContainer()->get('shopware.filesystem.private');
$logfile = $this->getLogEntity($progress->getLogId())->getFile();
static::assertInstanceOf(ImportExportFileEntity::class, $logfile);
$csv = $filesystem->read($logfile->getPath());

static::assertIsString($csv);
$csv = $this->getCsvContent($progress->getLogId());
static::assertStringContainsString($salesChannel['name'], $csv);
static::assertStringContainsString('shopware AG', $csv);
static::assertStringContainsString('en-GB', $csv);
Expand Down Expand Up @@ -1395,6 +1375,54 @@ public function testImportWithCreateAndUpdateConfig(): void
static::assertSame(0, $mockRepo->updateCalls);
}

public function testCustomerImportExportWithBoundSalesChannel(): void
{
$customerRepository = self::getContainer()->get('customer.repository');
$customers = $customerRepository->search(new Criteria(), Context::createDefaultContext());
static::assertCount(0, $customers);

$context = Context::createDefaultContext();
$context->addState(EntityIndexerRegistry::DISABLE_INDEXING);

$profile = $this->cloneDefaultProfile(CustomerDefinition::ENTITY_NAME);
$mapping = $profile->getMapping();
$mapping[] = [
'key' => 'boundSalesChannelId',
'mappedKey' => 'boundsaleschannel_id',
];
$this->updateProfileMapping($profile->getId(), $mapping);

$salesChannel = $this->createSalesChannel([
'id' => '01902502a01172ad948f5a50096da0bd',
'name' => 'Sales-Channel-Name',
]);

$progress = $this->import(
$context,
CustomerDefinition::ENTITY_NAME,
'/fixtures/customers_boundsaleschannel.csv',
'customers_boundsaleschannel.csv',
$profile->getId()
);

static::assertImportExportSucceeded($progress, $this->getInvalidLogContent($progress->getInvalidRecordsLogId()));

$customers = $customerRepository->search(new Criteria(), Context::createDefaultContext());
static::assertCount(1, $customers);
$customer = $customers->getEntities()->first();
static::assertInstanceOf(CustomerEntity::class, $customer);

$boundSalesChannelId = $customer->getBoundSalesChannelId();
static::assertIsString($boundSalesChannelId);
static::assertSame($salesChannel['id'], $boundSalesChannelId);

$progress = $this->export($context, CustomerDefinition::ENTITY_NAME, null, null, $profile->getId());
static::assertImportExportSucceeded($progress, $this->getInvalidLogContent($progress->getInvalidRecordsLogId()));

$csvContent = $this->getCsvContent($progress->getLogId());
static::assertStringContainsString($salesChannel['id'], $csvContent);
}

public function testPromotionCodeImportExport(): void
{
$connection = static::getContainer()->get(Connection::class);
Expand Down Expand Up @@ -1442,12 +1470,8 @@ public function testPromotionCodeImportExport(): void

static::assertImportExportSucceeded($progress, $this->getInvalidLogContent($progress->getInvalidRecordsLogId()));

$filesystem = static::getContainer()->get('shopware.filesystem.private');
$logfile = $this->getLogEntity($progress->getLogId())->getFile();
static::assertInstanceOf(ImportExportFileEntity::class, $logfile);
$csv = $filesystem->read($logfile->getPath());
$csv = $this->getCsvContent($progress->getLogId());

static::assertIsString($csv);
// validate export
foreach ($individualCodes as $promoCodeResult) {
static::assertStringContainsString($promoCodeResult->getId(), $csv);
Expand Down Expand Up @@ -1510,14 +1534,9 @@ public function testPromotionDiscountImportExport(): void
static::assertContains($ruleId, $lastDiscountRules->getIds());

$progress = $this->export($context, PromotionDiscountDefinition::ENTITY_NAME);

static::assertImportExportSucceeded($progress);

$filesystem = static::getContainer()->get('shopware.filesystem.private');
$logfile = $this->getLogEntity($progress->getLogId())->getFile();
static::assertInstanceOf(ImportExportFileEntity::class, $logfile);
$csv = $filesystem->read($logfile->getPath());

$csv = $this->getCsvContent($progress->getLogId());
static::assertStringEqualsFile(__DIR__ . '/fixtures/promotion_discounts_export.csv', $csv);
}

Expand Down Expand Up @@ -1683,4 +1702,16 @@ private function createCategoryProfileMock(): array
],
];
}

private function getCsvContent(string $logId): string
{
$filesystem = static::getContainer()->get('shopware.filesystem.private');
$logfile = $this->getLogEntity($logId)->getFile();
static::assertInstanceOf(ImportExportFileEntity::class, $logfile);

$csv = $filesystem->read($logfile->getPath());
static::assertIsString($csv);

return $csv;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sales_channel;boundsaleschannel_id;id;account_type;salutation;customer_number;first_name;last_name;email;active;guest;customer_group;language;payment_method;billing_id;billing_salutation;billing_title;billing_first_name;billing_last_name;billing_company;billing_street;billing_zipcode;billing_city;billing_country;billing_phone_number;shipping_id;shipping_salutation;shipping_title;shipping_first_name;shipping_last_name;shipping_company;shipping_street;shipping_zipcode;shipping_city;shipping_country;shipping_phone_number
Sales-Channel-Name;01902502a01172ad948f5a50096da0bd;6c97534c2c0747f39e8751e43cb2b013;private;mr;SWDEMO10010;Max;Mustermann;[email protected];1;0;Standard customer group;en-GB;Invoice;d8f0dff7ef3947979a83c42f6509f22c;mr;;Max;Mustermann;;Musterstraße 1;12345;Musterstadt;DE;;d8f0dff7ef3947979a83c42f6509f22c;mr;;Max;Mustermann;;Musterstraße 1;12345;Musterstadt;DE;
Loading

0 comments on commit 55e1c69

Please sign in to comment.