Skip to content

Commit

Permalink
Merge branch 'sw-27055/add-test' into '5.7'
Browse files Browse the repository at this point in the history
SW-27055 - Add test to ensure cart attributes are correctly synchronzied into order detail attributes

See merge request shopware/5/product/shopware!994
  • Loading branch information
mitelg committed Mar 17, 2023
2 parents da87ad9 + b5688b3 commit 670a492
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 67 deletions.
5 changes: 0 additions & 5 deletions .phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -38770,11 +38770,6 @@ parameters:
count: 1
path: tests/Functional/Components/Cart/BasketQueryHelperTest.php

-
message: "#^Access to an undefined property sSystem\\:\\:\\$_POST\\.$#"
count: 1
path: tests/Functional/Components/Cart/CartMigrationTest.php

-
message: "#^Method Shopware\\\\Tests\\\\Functional\\\\Components\\\\Cart\\\\PaymentTokenServiceTest\\:\\:paymentTokenProviders\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down
5 changes: 2 additions & 3 deletions engine/Shopware/Core/sBasket.php
Original file line number Diff line number Diff line change
Expand Up @@ -1499,13 +1499,12 @@ public function sGetBasketData()
$result[CartKey::POSITIONS][$key]['tax'] = $calcDifference;
}
}
$result = $this->eventManager->filter(

return $this->eventManager->filter(
'Shopware_Modules_Basket_GetBasket_FilterResult',
$result,
['subject' => $this]
);

return $result;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions tests/Functional/Components/Cart/CartMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ public function testMigrateOnLoginWithFilledCart(): void
static::assertEquals($currentBasketAmount, Shopware()->Session()->get('sBasketAmount'));
}

protected function loginFrontendUser(): void
private function loginFrontendUser(): void
{
Shopware()->Front()->setRequest(new Enlight_Controller_Request_RequestHttp());
$user = Shopware()->Db()->fetchRow(
'SELECT `id`, `email`, `password`, `subshopID`, `language` FROM s_user WHERE `id` = 1'
);
Expand All @@ -75,11 +74,13 @@ protected function loginFrontendUser(): void
static::assertNotNull($shop);
Shopware()->Container()->get(ShopRegistrationServiceInterface::class)->registerResources($shop);

Shopware()->Session()->set('Admin', true);
Shopware()->System()->_POST = [
$request = new Enlight_Controller_Request_RequestHttp();
$request->setPost([
'email' => $user['email'],
'passwordMD5' => $user['password'],
];
]);
Shopware()->Front()->setRequest($request);
Shopware()->Session()->set('Admin', true);
Shopware()->Modules()->Admin()->sLogin(true);
}
}
205 changes: 205 additions & 0 deletions tests/Functional/Components/Cart/CartToOrderAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?php

declare(strict_types=1);
/**
* Shopware 5
* Copyright (c) shopware AG
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* "Shopware" is a registered trademark of shopware AG.
* The licensing of the program under the AGPLv3 does not imply a
* trademark license. Therefore any rights, title and interest in
* our trademarks remain entirely with us.
*/

namespace Shopware\Tests\Functional\Components\Cart;

use Doctrine\DBAL\Connection;
use Enlight_Controller_Request_RequestHttp;
use Enlight_Controller_Response_ResponseTestCase;
use Enlight_Event_EventArgs;
use Enlight_Template_Manager;
use Enlight_View_Default;
use PHPUnit\Framework\TestCase;
use Shopware\Bundle\AttributeBundle\Service\CrudService;
use Shopware\Bundle\AttributeBundle\Service\TypeMappingInterface;
use Shopware\Components\Model\ModelManager;
use Shopware\Models\Order\Order;
use Shopware\Tests\Functional\Traits\ContainerTrait;
use Shopware\Tests\Functional\Traits\CustomerLoginTrait;
use Shopware_Controllers_Frontend_Checkout;
use Symfony\Component\HttpFoundation\Request;

class CartToOrderAttributeTest extends TestCase
{
use ContainerTrait;
use CustomerLoginTrait;

private const TEST_ATTRIBUTE_NAME = 'test_attr';
private const TEST_ATTRIBUTE_VALUE = 'test';
private const CUSTOMER_ID = 1;

private CrudService $attributeService;

private Connection $connection;

private ModelManager $modelManager;

protected function setUp(): void
{
$this->attributeService = $this->getContainer()->get('shopware_attribute.crud_service');
$this->connection = $this->getContainer()->get(Connection::class);
$this->modelManager = $this->getContainer()->get(ModelManager::class);

$this->prepareAttributeTables();
$this->createListenerToFillCartAttribute();
}

protected function tearDown(): void
{
$this->removeAttributeColumn();
}

public function testSyncCartAttributeToOrderDetailAttribute(): void
{
$this->preparePaymentMethod();
$controller = $this->createController();

$addProductRequest = new Enlight_Controller_Request_RequestHttp();
$addProductRequest->setMethod(Request::METHOD_POST);
$addProductRequest->setPost([
'sAdd' => 'SW10064',
'sQuantity' => 1,
]);
$controller->setRequest($addProductRequest);
$controller->Front()->setRequest($addProductRequest);
$controller->addArticleAction();

$confirmRequest = new Enlight_Controller_Request_RequestHttp();
$controller->setRequest($confirmRequest);
$controller->Front()->setRequest($confirmRequest);
$controller->confirmAction();

$cartPositionsConfirmPage = $controller->View()->getAssign('sBasket')['content'];
static::assertCount(3, $cartPositionsConfirmPage);
static::assertSame('SW10064', $cartPositionsConfirmPage[0]['ordernumber']);
static::assertSame('SHIPPINGDISCOUNT', $cartPositionsConfirmPage[1]['ordernumber']);
static::assertSame('sw-payment', $cartPositionsConfirmPage[2]['ordernumber']);

$controller->View()->loadTemplate('frontend/checkout/finish.tpl');
$finishRequest = new Enlight_Controller_Request_RequestHttp([], [
'sAGB' => true,
]);
$controller->setRequest($finishRequest);
$controller->Front()->setRequest($finishRequest);
$controller->finishAction();

$order = $this->connection->executeQuery(
'SELECT id, ordernumber
FROM s_order
ORDER BY id DESC
LIMIT 1'
)->fetchAssociative();
static::assertIsArray($order);
static::assertGreaterThan(0, $order['ordernumber']);

$orderDetailAttributes = $this->connection->executeQuery(
'SELECT detailAttributes.*
FROM s_order AS `order`
INNER JOIN s_order_details details on `order`.id = details.orderID
INNER JOIN s_order_details_attributes detailAttributes on details.id = detailAttributes.detailID
WHERE `order`.id = :orderId',
['orderId' => $order['id']]
)->fetchAllAssociative();

foreach ($orderDetailAttributes as $orderDetailAttribute) {
static::assertSame(self::TEST_ATTRIBUTE_VALUE, $orderDetailAttribute['test_attr']);
}

$this->restorePaymentMethod();
$orderObject = $this->modelManager->find(Order::class, $order['id']);
static::assertInstanceOf(Order::class, $orderObject);
$this->modelManager->remove($orderObject);
$this->modelManager->flush($orderObject);
}

private function prepareAttributeTables(): void
{
$this->attributeService->update(
's_order_basket_attributes',
self::TEST_ATTRIBUTE_NAME,
TypeMappingInterface::TYPE_STRING,
[],
null,
true
);
$this->modelManager->generateAttributeModels([
's_order_basket_attributes',
's_order_details_attributes',
]);
}

private function createListenerToFillCartAttribute(): void
{
$this->getContainer()->get('events')->addListener('Shopware_Modules_Basket_GetBasket_FilterResult',
function (Enlight_Event_EventArgs $eventArgs) {
foreach ($eventArgs->getReturn()['content'] as $lineItem) {
$this->connection->update(
's_order_basket_attributes',
[self::TEST_ATTRIBUTE_NAME => self::TEST_ATTRIBUTE_VALUE],
['basketID' => $lineItem['id']],
);
}
});
}

private function createController(): Shopware_Controllers_Frontend_Checkout
{
$controller = new Shopware_Controllers_Frontend_Checkout();
$controller->setContainer($this->getContainer());
$controller->setResponse(new Enlight_Controller_Response_ResponseTestCase());
$request = new Enlight_Controller_Request_RequestHttp();
$controller->setRequest($request);
$controller->setView(new Enlight_View_Default(new Enlight_Template_Manager()));

$controller->Front()->setRequest($request);
$this->getContainer()->get('request_stack')->push($request);
$this->loginCustomer(null, self::CUSTOMER_ID);

$controller->init();
$controller->preDispatch();

return $controller;
}

private function removeAttributeColumn(): void
{
$this->attributeService->delete('s_order_basket_attributes', self::TEST_ATTRIBUTE_NAME, true);
$this->modelManager->generateAttributeModels([
's_order_basket_attributes',
's_order_details_attributes',
]);
}

private function preparePaymentMethod(): void
{
$this->connection->update('s_core_paymentmeans', ['debit_percent' => 10], ['name' => 'prepayment']);
}

private function restorePaymentMethod(): void
{
$this->connection->update('s_core_paymentmeans', ['debit_percent' => 0], ['name' => 'prepayment']);
}
}
6 changes: 3 additions & 3 deletions tests/Functional/Components/Cart/fixture/cart_migration_1.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
INSERT INTO `s_order_basket` (`id`, `sessionID`, `userID`, `articlename`, `articleID`, `ordernumber`, `shippingfree`, `quantity`, `price`, `netprice`, `tax_rate`, `datum`, `modus`, `esdarticle`, `partnerID`, `lastviewport`, `useragent`, `config`, `currencyFactor`) VALUES
(NULL, '52f0cf0e9f3737fd42f8371ec79fbd5a', 1, 'Münsterländer Aperitif 16%', 3, 'SW10003', 0, 1, 14.95, 12.563025210084, 19, '2019-04-05 09:09:54', 0, 0, '', 'account', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36', '', 1),
(NULL, '52f0cf0e9f3737fd42f8371ec79fbd5a', 0, 'Warenkorbrabatt', 0, 'SHIPPINGDISCOUNT', 0, 1, -2, -1.68, 19, '2019-04-05 09:10:30', 4, 0, '', '', '', '', 1);
INSERT INTO `s_order_basket` (`sessionID`, `userID`, `articlename`, `articleID`, `ordernumber`, `shippingfree`, `quantity`, `price`, `netprice`, `tax_rate`, `datum`, `modus`, `esdarticle`, `partnerID`, `lastviewport`, `useragent`, `config`, `currencyFactor`) VALUES
('52f0cf0e9f3737fd42f8371ec79fbd5a', 1, 'Münsterländer Aperitif 16%', 3, 'SW10003', 0, 1, 14.95, 12.563025210084, 19, '2019-04-05 09:09:54', 0, 0, '', 'account', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36', '', 1),
('52f0cf0e9f3737fd42f8371ec79fbd5a', 1, 'Warenkorbrabatt', 0, 'SHIPPINGDISCOUNT', 0, 1, -2, -1.68, 19, '2019-04-05 09:10:30', 4, 0, '', '', '', '', 1);
22 changes: 3 additions & 19 deletions tests/Functional/Components/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@

use Doctrine\DBAL\Connection;
use Enlight_Components_Test_Controller_TestCase;
use Enlight_Controller_Request_RequestHttp;
use Shopware\Components\Random;
use Shopware\Components\ShopRegistrationServiceInterface;
use Shopware\Models\Shop\Shop;
use Shopware\Tests\Functional\Bundle\StoreFrontBundle\Helper;
use Shopware\Tests\Functional\Traits\ContainerTrait;
use Shopware\Tests\Functional\Traits\CustomerLoginTrait;
use Shopware\Tests\Functional\Traits\DatabaseTransactionBehaviour;

abstract class CheckoutTest extends Enlight_Components_Test_Controller_TestCase
{
use ContainerTrait;
use CustomerLoginTrait;
use DatabaseTransactionBehaviour;

public const USER_AGENT = 'Mozilla/5.0 (Android; Tablet; rv:14.0) Gecko/14.0 Firefox/14.0';
Expand Down Expand Up @@ -207,22 +206,7 @@ protected function loginFrontendCustomer(string $group = 'EK'): void
$group
);

$request = new Enlight_Controller_Request_RequestHttp();
$request->setPost([
'email' => $customer['email'],
'passwordMD5' => $customer['password'],
]);
Shopware()->Front()->setRequest($request);

$shop = Shopware()->Models()->getRepository(Shop::class)->getActiveById($customer['language']);
static::assertInstanceOf(Shop::class, $shop);

$this->getContainer()->get(ShopRegistrationServiceInterface::class)->registerShop($shop);

Shopware()->Session()->set('Admin', true);
$result = Shopware()->Modules()->Admin()->sLogin(true);
static::assertIsArray($result);
static::assertNull($result['sErrorMessages']);
$this->loginCustomer(null, (int) $customer['id'], $customer['email'], null, 2, 3, $group);
}

protected function addProduct(string $productNumber, int $quantity = 1): void
Expand Down
1 change: 0 additions & 1 deletion tests/Functional/Controllers/Frontend/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use Enlight_Template_Manager;
use Enlight_View_Default;
use PHPUnit\Framework\TestCase;
use Shopware\Components\DependencyInjection\Bridge\Config;
use Shopware\Tests\Functional\Traits\ContainerTrait;
use Shopware\Tests\Functional\Traits\CustomerLoginTrait;
use Shopware_Components_Config;
Expand Down
Loading

0 comments on commit 670a492

Please sign in to comment.