diff --git a/.phpstan-baseline.neon b/.phpstan-baseline.neon index 37b8bd57771..b9873a80bc7 100644 --- a/.phpstan-baseline.neon +++ b/.phpstan-baseline.neon @@ -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 diff --git a/engine/Shopware/Core/sBasket.php b/engine/Shopware/Core/sBasket.php index 83ed16aaf7a..04183248da3 100644 --- a/engine/Shopware/Core/sBasket.php +++ b/engine/Shopware/Core/sBasket.php @@ -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; } /** diff --git a/tests/Functional/Components/Cart/CartMigrationTest.php b/tests/Functional/Components/Cart/CartMigrationTest.php index 704fd92d81b..f635bc2af3e 100644 --- a/tests/Functional/Components/Cart/CartMigrationTest.php +++ b/tests/Functional/Components/Cart/CartMigrationTest.php @@ -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' ); @@ -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); } } diff --git a/tests/Functional/Components/Cart/CartToOrderAttributeTest.php b/tests/Functional/Components/Cart/CartToOrderAttributeTest.php new file mode 100644 index 00000000000..2c209563f6d --- /dev/null +++ b/tests/Functional/Components/Cart/CartToOrderAttributeTest.php @@ -0,0 +1,205 @@ +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']); + } +} diff --git a/tests/Functional/Components/Cart/fixture/cart_migration_1.sql b/tests/Functional/Components/Cart/fixture/cart_migration_1.sql index ad3fa5afece..67c25e2ed04 100644 --- a/tests/Functional/Components/Cart/fixture/cart_migration_1.sql +++ b/tests/Functional/Components/Cart/fixture/cart_migration_1.sql @@ -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); diff --git a/tests/Functional/Components/CheckoutTest.php b/tests/Functional/Components/CheckoutTest.php index d70b4e19664..15007fc0a21 100644 --- a/tests/Functional/Components/CheckoutTest.php +++ b/tests/Functional/Components/CheckoutTest.php @@ -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'; @@ -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 diff --git a/tests/Functional/Controllers/Frontend/AddressTest.php b/tests/Functional/Controllers/Frontend/AddressTest.php index 3e5451cebb0..1a64f8914fe 100644 --- a/tests/Functional/Controllers/Frontend/AddressTest.php +++ b/tests/Functional/Controllers/Frontend/AddressTest.php @@ -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; diff --git a/tests/Functional/Controllers/Frontend/CheckoutTest.php b/tests/Functional/Controllers/Frontend/CheckoutTest.php index 23514fe7fb7..b7e00ef580d 100644 --- a/tests/Functional/Controllers/Frontend/CheckoutTest.php +++ b/tests/Functional/Controllers/Frontend/CheckoutTest.php @@ -28,7 +28,6 @@ use Doctrine\DBAL\Connection; use Enlight_Components_Test_Plugin_TestCase; -use Enlight_Controller_Request_RequestHttp; use Enlight_Controller_Request_RequestTestCase; use Enlight_Controller_Response_ResponseTestCase; use Enlight_Template_Manager; @@ -39,7 +38,6 @@ use Shopware\Bundle\CartBundle\CheckoutKey; use Shopware\Bundle\OrderBundle\Service\CalculationServiceInterface; use Shopware\Components\Model\ModelManager; -use Shopware\Components\ShopRegistrationServiceInterface; use Shopware\Models\Customer\Group as CustomerGroup; use Shopware\Models\Order\Order; use Shopware\Models\Shop\Shop; @@ -169,7 +167,7 @@ public function testAddBasketOverGetFails(): void public function testRequestPaymentWithoutAGB(): void { // Login - $this->loginFrontendUser(); + $this->loginCustomer(); // Add product to basket $this->addBasketProduct(self::USER_AGENT, 5); @@ -198,7 +196,7 @@ public function testRequestPaymentWithoutAGB(): void public function testRequestPaymentWithoutServiceAgreement(): void { // Login - $this->loginFrontendUser(); + $this->loginCustomer(); // Add product to basket $this->addBasketProduct(self::USER_AGENT, 5); @@ -268,7 +266,7 @@ public function testCartActionWithEnglishTranslationAndWithTranslatedCountrySett public function testRedirectShippingPaymentPageOnEmptyBasket(): void { - $this->loginFrontendUser(); + $this->loginCustomer(); $this->getContainer()->get('modules')->Basket()->sDeleteBasket(); $this->Request()->setMethod('GET'); @@ -370,7 +368,7 @@ private function runCheckoutTest(bool $net): void // Simulate checkout in frontend // Login - $this->loginFrontendUser(); + $this->loginCustomer(); // Add product to basket $this->addBasketProduct(self::USER_AGENT, 5); @@ -423,29 +421,6 @@ private function runCheckoutTest(bool $net): void $this->getContainer()->get('modules')->Basket()->sDeleteBasket(); } - /** - * Login as a frontend user - */ - private function loginFrontendUser(): void - { - $user = $this->connection->fetchAssociative( - 'SELECT id, email, password, subshopID, language FROM s_user WHERE id = 1' - ); - static::assertIsArray($user); - - $shop = $this->getContainer()->get(ModelManager::class)->getRepository(Shop::class)->getActiveById($user['language']); - static::assertInstanceOf(Shop::class, $shop); - - $this->getContainer()->get(ShopRegistrationServiceInterface::class)->registerShop($shop); - - $request = new Enlight_Controller_Request_RequestHttp(); - $request->setPost('email', $user['email']); - $request->setPost('passwordMD5', $user['password']); - $this->getContainer()->get('front')->setRequest($request); - $this->getContainer()->get('session')->set('Admin', true); - $this->getContainer()->get('modules')->Admin()->sLogin(true); - } - /** * Fires the add product request with the given user agent * diff --git a/tests/Functional/Traits/CustomerLoginTrait.php b/tests/Functional/Traits/CustomerLoginTrait.php index 9ed1ebc001b..ea69dfdce77 100644 --- a/tests/Functional/Traits/CustomerLoginTrait.php +++ b/tests/Functional/Traits/CustomerLoginTrait.php @@ -32,7 +32,7 @@ trait CustomerLoginTrait { public function loginCustomer( - string $sessionId = 'sessionId', + ?string $sessionId = null, int $customerId = 1, string $email = 'test@example.com', ?string $passwordChangeDate = null, @@ -56,6 +56,10 @@ public function loginCustomer( ); } + if (empty($sessionId)) { + $sessionId = $session->getId(); + } + $session->offsetSet('sessionId', $sessionId); $session->offsetSet('sUserId', $customerId); $session->offsetSet('sUserMail', $email); diff --git a/tests/Functional/Traits/FixtureBehaviour.php b/tests/Functional/Traits/FixtureBehaviour.php index fe49236bd31..dedb5aae167 100644 --- a/tests/Functional/Traits/FixtureBehaviour.php +++ b/tests/Functional/Traits/FixtureBehaviour.php @@ -24,6 +24,7 @@ namespace Shopware\Tests\Functional\Traits; +use Doctrine\DBAL\Connection; use RuntimeException; trait FixtureBehaviour @@ -36,6 +37,6 @@ protected static function executeFixture(string $name): void throw new RuntimeException(sprintf('Could not read fixture "%s"', $name)); } - Shopware()->Container()->get(\Doctrine\DBAL\Connection::class)->exec($sql); + Shopware()->Container()->get(Connection::class)->executeStatement($sql); } }