| 
 | 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 | +}  | 
0 commit comments