Skip to content

Commit 670a492

Browse files
committed
Merge branch 'sw-27055/add-test' into '5.7'
SW-27055 - Add test to ensure cart attributes are correctly synchronzied into order detail attributes See merge request shopware/5/product/shopware!994
2 parents da87ad9 + b5688b3 commit 670a492

File tree

10 files changed

+230
-67
lines changed

10 files changed

+230
-67
lines changed

.phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38770,11 +38770,6 @@ parameters:
3877038770
count: 1
3877138771
path: tests/Functional/Components/Cart/BasketQueryHelperTest.php
3877238772

38773-
-
38774-
message: "#^Access to an undefined property sSystem\\:\\:\\$_POST\\.$#"
38775-
count: 1
38776-
path: tests/Functional/Components/Cart/CartMigrationTest.php
38777-
3877838773
-
3877938774
message: "#^Method Shopware\\\\Tests\\\\Functional\\\\Components\\\\Cart\\\\PaymentTokenServiceTest\\:\\:paymentTokenProviders\\(\\) return type has no value type specified in iterable type array\\.$#"
3878038775
count: 1

engine/Shopware/Core/sBasket.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,13 +1499,12 @@ public function sGetBasketData()
14991499
$result[CartKey::POSITIONS][$key]['tax'] = $calcDifference;
15001500
}
15011501
}
1502-
$result = $this->eventManager->filter(
1502+
1503+
return $this->eventManager->filter(
15031504
'Shopware_Modules_Basket_GetBasket_FilterResult',
15041505
$result,
15051506
['subject' => $this]
15061507
);
1507-
1508-
return $result;
15091508
}
15101509

15111510
/**

tests/Functional/Components/Cart/CartMigrationTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ public function testMigrateOnLoginWithFilledCart(): void
6464
static::assertEquals($currentBasketAmount, Shopware()->Session()->get('sBasketAmount'));
6565
}
6666

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

78-
Shopware()->Session()->set('Admin', true);
79-
Shopware()->System()->_POST = [
77+
$request = new Enlight_Controller_Request_RequestHttp();
78+
$request->setPost([
8079
'email' => $user['email'],
8180
'passwordMD5' => $user['password'],
82-
];
81+
]);
82+
Shopware()->Front()->setRequest($request);
83+
Shopware()->Session()->set('Admin', true);
8384
Shopware()->Modules()->Admin()->sLogin(true);
8485
}
8586
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* Shopware 5
6+
* Copyright (c) shopware AG
7+
*
8+
* According to our dual licensing model, this program can be used either
9+
* under the terms of the GNU Affero General Public License, version 3,
10+
* or under a proprietary license.
11+
*
12+
* The texts of the GNU Affero General Public License with an additional
13+
* permission and of our proprietary license can be found at and
14+
* in the LICENSE file you have received along with this program.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* "Shopware" is a registered trademark of shopware AG.
22+
* The licensing of the program under the AGPLv3 does not imply a
23+
* trademark license. Therefore any rights, title and interest in
24+
* our trademarks remain entirely with us.
25+
*/
26+
27+
namespace Shopware\Tests\Functional\Components\Cart;
28+
29+
use Doctrine\DBAL\Connection;
30+
use Enlight_Controller_Request_RequestHttp;
31+
use Enlight_Controller_Response_ResponseTestCase;
32+
use Enlight_Event_EventArgs;
33+
use Enlight_Template_Manager;
34+
use Enlight_View_Default;
35+
use PHPUnit\Framework\TestCase;
36+
use Shopware\Bundle\AttributeBundle\Service\CrudService;
37+
use Shopware\Bundle\AttributeBundle\Service\TypeMappingInterface;
38+
use Shopware\Components\Model\ModelManager;
39+
use Shopware\Models\Order\Order;
40+
use Shopware\Tests\Functional\Traits\ContainerTrait;
41+
use Shopware\Tests\Functional\Traits\CustomerLoginTrait;
42+
use Shopware_Controllers_Frontend_Checkout;
43+
use Symfony\Component\HttpFoundation\Request;
44+
45+
class CartToOrderAttributeTest extends TestCase
46+
{
47+
use ContainerTrait;
48+
use CustomerLoginTrait;
49+
50+
private const TEST_ATTRIBUTE_NAME = 'test_attr';
51+
private const TEST_ATTRIBUTE_VALUE = 'test';
52+
private const CUSTOMER_ID = 1;
53+
54+
private CrudService $attributeService;
55+
56+
private Connection $connection;
57+
58+
private ModelManager $modelManager;
59+
60+
protected function setUp(): void
61+
{
62+
$this->attributeService = $this->getContainer()->get('shopware_attribute.crud_service');
63+
$this->connection = $this->getContainer()->get(Connection::class);
64+
$this->modelManager = $this->getContainer()->get(ModelManager::class);
65+
66+
$this->prepareAttributeTables();
67+
$this->createListenerToFillCartAttribute();
68+
}
69+
70+
protected function tearDown(): void
71+
{
72+
$this->removeAttributeColumn();
73+
}
74+
75+
public function testSyncCartAttributeToOrderDetailAttribute(): void
76+
{
77+
$this->preparePaymentMethod();
78+
$controller = $this->createController();
79+
80+
$addProductRequest = new Enlight_Controller_Request_RequestHttp();
81+
$addProductRequest->setMethod(Request::METHOD_POST);
82+
$addProductRequest->setPost([
83+
'sAdd' => 'SW10064',
84+
'sQuantity' => 1,
85+
]);
86+
$controller->setRequest($addProductRequest);
87+
$controller->Front()->setRequest($addProductRequest);
88+
$controller->addArticleAction();
89+
90+
$confirmRequest = new Enlight_Controller_Request_RequestHttp();
91+
$controller->setRequest($confirmRequest);
92+
$controller->Front()->setRequest($confirmRequest);
93+
$controller->confirmAction();
94+
95+
$cartPositionsConfirmPage = $controller->View()->getAssign('sBasket')['content'];
96+
static::assertCount(3, $cartPositionsConfirmPage);
97+
static::assertSame('SW10064', $cartPositionsConfirmPage[0]['ordernumber']);
98+
static::assertSame('SHIPPINGDISCOUNT', $cartPositionsConfirmPage[1]['ordernumber']);
99+
static::assertSame('sw-payment', $cartPositionsConfirmPage[2]['ordernumber']);
100+
101+
$controller->View()->loadTemplate('frontend/checkout/finish.tpl');
102+
$finishRequest = new Enlight_Controller_Request_RequestHttp([], [
103+
'sAGB' => true,
104+
]);
105+
$controller->setRequest($finishRequest);
106+
$controller->Front()->setRequest($finishRequest);
107+
$controller->finishAction();
108+
109+
$order = $this->connection->executeQuery(
110+
'SELECT id, ordernumber
111+
FROM s_order
112+
ORDER BY id DESC
113+
LIMIT 1'
114+
)->fetchAssociative();
115+
static::assertIsArray($order);
116+
static::assertGreaterThan(0, $order['ordernumber']);
117+
118+
$orderDetailAttributes = $this->connection->executeQuery(
119+
'SELECT detailAttributes.*
120+
FROM s_order AS `order`
121+
INNER JOIN s_order_details details on `order`.id = details.orderID
122+
INNER JOIN s_order_details_attributes detailAttributes on details.id = detailAttributes.detailID
123+
WHERE `order`.id = :orderId',
124+
['orderId' => $order['id']]
125+
)->fetchAllAssociative();
126+
127+
foreach ($orderDetailAttributes as $orderDetailAttribute) {
128+
static::assertSame(self::TEST_ATTRIBUTE_VALUE, $orderDetailAttribute['test_attr']);
129+
}
130+
131+
$this->restorePaymentMethod();
132+
$orderObject = $this->modelManager->find(Order::class, $order['id']);
133+
static::assertInstanceOf(Order::class, $orderObject);
134+
$this->modelManager->remove($orderObject);
135+
$this->modelManager->flush($orderObject);
136+
}
137+
138+
private function prepareAttributeTables(): void
139+
{
140+
$this->attributeService->update(
141+
's_order_basket_attributes',
142+
self::TEST_ATTRIBUTE_NAME,
143+
TypeMappingInterface::TYPE_STRING,
144+
[],
145+
null,
146+
true
147+
);
148+
$this->modelManager->generateAttributeModels([
149+
's_order_basket_attributes',
150+
's_order_details_attributes',
151+
]);
152+
}
153+
154+
private function createListenerToFillCartAttribute(): void
155+
{
156+
$this->getContainer()->get('events')->addListener('Shopware_Modules_Basket_GetBasket_FilterResult',
157+
function (Enlight_Event_EventArgs $eventArgs) {
158+
foreach ($eventArgs->getReturn()['content'] as $lineItem) {
159+
$this->connection->update(
160+
's_order_basket_attributes',
161+
[self::TEST_ATTRIBUTE_NAME => self::TEST_ATTRIBUTE_VALUE],
162+
['basketID' => $lineItem['id']],
163+
);
164+
}
165+
});
166+
}
167+
168+
private function createController(): Shopware_Controllers_Frontend_Checkout
169+
{
170+
$controller = new Shopware_Controllers_Frontend_Checkout();
171+
$controller->setContainer($this->getContainer());
172+
$controller->setResponse(new Enlight_Controller_Response_ResponseTestCase());
173+
$request = new Enlight_Controller_Request_RequestHttp();
174+
$controller->setRequest($request);
175+
$controller->setView(new Enlight_View_Default(new Enlight_Template_Manager()));
176+
177+
$controller->Front()->setRequest($request);
178+
$this->getContainer()->get('request_stack')->push($request);
179+
$this->loginCustomer(null, self::CUSTOMER_ID);
180+
181+
$controller->init();
182+
$controller->preDispatch();
183+
184+
return $controller;
185+
}
186+
187+
private function removeAttributeColumn(): void
188+
{
189+
$this->attributeService->delete('s_order_basket_attributes', self::TEST_ATTRIBUTE_NAME, true);
190+
$this->modelManager->generateAttributeModels([
191+
's_order_basket_attributes',
192+
's_order_details_attributes',
193+
]);
194+
}
195+
196+
private function preparePaymentMethod(): void
197+
{
198+
$this->connection->update('s_core_paymentmeans', ['debit_percent' => 10], ['name' => 'prepayment']);
199+
}
200+
201+
private function restorePaymentMethod(): void
202+
{
203+
$this->connection->update('s_core_paymentmeans', ['debit_percent' => 0], ['name' => 'prepayment']);
204+
}
205+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
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
2-
(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),
3-
(NULL, '52f0cf0e9f3737fd42f8371ec79fbd5a', 0, 'Warenkorbrabatt', 0, 'SHIPPINGDISCOUNT', 0, 1, -2, -1.68, 19, '2019-04-05 09:10:30', 4, 0, '', '', '', '', 1);
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
2+
('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),
3+
('52f0cf0e9f3737fd42f8371ec79fbd5a', 1, 'Warenkorbrabatt', 0, 'SHIPPINGDISCOUNT', 0, 1, -2, -1.68, 19, '2019-04-05 09:10:30', 4, 0, '', '', '', '', 1);

tests/Functional/Components/CheckoutTest.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,16 @@
2828

2929
use Doctrine\DBAL\Connection;
3030
use Enlight_Components_Test_Controller_TestCase;
31-
use Enlight_Controller_Request_RequestHttp;
3231
use Shopware\Components\Random;
33-
use Shopware\Components\ShopRegistrationServiceInterface;
34-
use Shopware\Models\Shop\Shop;
3532
use Shopware\Tests\Functional\Bundle\StoreFrontBundle\Helper;
3633
use Shopware\Tests\Functional\Traits\ContainerTrait;
34+
use Shopware\Tests\Functional\Traits\CustomerLoginTrait;
3735
use Shopware\Tests\Functional\Traits\DatabaseTransactionBehaviour;
3836

3937
abstract class CheckoutTest extends Enlight_Components_Test_Controller_TestCase
4038
{
4139
use ContainerTrait;
40+
use CustomerLoginTrait;
4241
use DatabaseTransactionBehaviour;
4342

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

210-
$request = new Enlight_Controller_Request_RequestHttp();
211-
$request->setPost([
212-
'email' => $customer['email'],
213-
'passwordMD5' => $customer['password'],
214-
]);
215-
Shopware()->Front()->setRequest($request);
216-
217-
$shop = Shopware()->Models()->getRepository(Shop::class)->getActiveById($customer['language']);
218-
static::assertInstanceOf(Shop::class, $shop);
219-
220-
$this->getContainer()->get(ShopRegistrationServiceInterface::class)->registerShop($shop);
221-
222-
Shopware()->Session()->set('Admin', true);
223-
$result = Shopware()->Modules()->Admin()->sLogin(true);
224-
static::assertIsArray($result);
225-
static::assertNull($result['sErrorMessages']);
209+
$this->loginCustomer(null, (int) $customer['id'], $customer['email'], null, 2, 3, $group);
226210
}
227211

228212
protected function addProduct(string $productNumber, int $quantity = 1): void

tests/Functional/Controllers/Frontend/AddressTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
use Enlight_Template_Manager;
3232
use Enlight_View_Default;
3333
use PHPUnit\Framework\TestCase;
34-
use Shopware\Components\DependencyInjection\Bridge\Config;
3534
use Shopware\Tests\Functional\Traits\ContainerTrait;
3635
use Shopware\Tests\Functional\Traits\CustomerLoginTrait;
3736
use Shopware_Components_Config;

0 commit comments

Comments
 (0)