Skip to content

Commit

Permalink
Merge pull request #60 from synolia/fix/issue-57
Browse files Browse the repository at this point in the history
export customer without cart
  • Loading branch information
oallain authored Sep 22, 2023
2 parents dc3f1b7 + 7476434 commit 9ada78a
Show file tree
Hide file tree
Showing 2 changed files with 81 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);
}
}
}
42 changes: 42 additions & 0 deletions tests/PHPUnit/Controller/ExportDataControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Tests\Synolia\SyliusGDPRPlugin\PHPUnit\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpKernel\Kernel;

class ExportDataControllerTest extends WebTestCase
{
public function testExportDataWithCart(): void
{
if (Kernel::MAJOR_VERSION < 6) {
$this->markTestSkipped('Test on for Symfony 6+');
}

$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 9ada78a

Please sign in to comment.