Skip to content

Commit

Permalink
export customer with cart
Browse files Browse the repository at this point in the history
  • Loading branch information
oallain committed Sep 22, 2023
1 parent dc3f1b7 commit 5a3c4bd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/EventSubscriber/RemoveCartBeforeExportCustomerSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Synolia\SyliusGDPRPlugin\EventSubscriber;

use Sylius\Component\Core\Model\OrderInterface as CoreOrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Order\Model\OrderInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Synolia\SyliusGDPRPlugin\Event\BeforeExportCustomerData;

class RemoveCartBeforeExportCustomerSubscriber implements EventSubscriberInterface
{
public function __construct(
private OrderRepositoryInterface $orderRepository,
) {
}

public static function getSubscribedEvents(): array
{
return [
BeforeExportCustomerData::class => ['process'],
];
}

public function process(BeforeExportCustomerData $beforeExportCustomerData): void
{
$carts = $this->orderRepository->findBy([
'customer' => $beforeExportCustomerData->getCustomer(),
'state' => OrderInterface::STATE_CART,
]);

/** @var CoreOrderInterface $cart */
foreach ($carts as $cart) {
$this->orderRepository->remove($cart);
}
}
}
37 changes: 37 additions & 0 deletions tests/PHPUnit/Controller/ExportDataControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Tests\Synolia\SyliusGDPRPlugin\PHPUnit\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ExportDataControllerTest extends WebTestCase
{
public function testExportDataWithCart(): void
{
$client = static::createClient();

// login a user
$shopUser = static::getContainer()->get('sylius.repository.shop_user')->findOneBy([]);
$client->loginUser($shopUser);

// go to product page
$client->request('GET', '/en_US/products/sport-basic-white-t-shirt');
$this->assertSelectorTextContains('h1', 'Sport basic white T-Shirt');

// add product to cart
$client->submitForm('Add to cart');
$client->request('GET', '/en_US/cart/');
$this->assertSelectorTextContains('h1', 'Your shopping cart');

// login as admin and go to customer page
$adminUser = static::getContainer()->get('sylius.repository.admin_user')->findOneBy([]);
$client->loginUser($adminUser, 'admin');
$client->request('GET', sprintf('/admin/customers/%s', $shopUser->getId()));

// export data for this user
$client->clickLink('Export data');
$this->assertResponseIsSuccessful();
}
}

0 comments on commit 5a3c4bd

Please sign in to comment.