|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote; |
| 9 | + |
| 10 | +use Magento\Framework\App\Request\Http; |
| 11 | +use Magento\Framework\Message\MessageInterface; |
| 12 | +use Magento\SalesRule\Model\ResourceModel\Rule\Collection; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test class for \Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Save |
| 18 | + * |
| 19 | + * @magentoAppArea adminhtml |
| 20 | + */ |
| 21 | +class SaveTest extends AbstractBackendController |
| 22 | +{ |
| 23 | + public function testCreateRuleWithOnlyFormkey(): void |
| 24 | + { |
| 25 | + $requestData = []; |
| 26 | + $this->getRequest()->setMethod(Http::METHOD_POST); |
| 27 | + $this->getRequest()->setPostValue($requestData); |
| 28 | + |
| 29 | + $this->dispatch('backend/sales_rule/promo_quote/save'); |
| 30 | + $this->assertSessionMessages( |
| 31 | + self::equalTo(['You saved the rule.']), |
| 32 | + MessageInterface::TYPE_SUCCESS |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function testCreateRuleWithFreeShipping(): void |
| 37 | + { |
| 38 | + $ruleCollection = Bootstrap::getObjectManager()->create(Collection::class); |
| 39 | + $resource = $ruleCollection->getResource(); |
| 40 | + $select = $resource->getConnection()->select(); |
| 41 | + $select->from($resource->getTable('salesrule'), [new \Zend_Db_Expr('MAX(rule_id)')]); |
| 42 | + $maxId = (int)$resource->getConnection()->fetchOne($select); |
| 43 | + |
| 44 | + $requestData = [ |
| 45 | + 'simple_free_shipping' => 1, |
| 46 | + ]; |
| 47 | + $this->getRequest()->setMethod(Http::METHOD_POST); |
| 48 | + $this->getRequest()->setPostValue($requestData); |
| 49 | + |
| 50 | + $this->dispatch('backend/sales_rule/promo_quote/save'); |
| 51 | + $this->assertSessionMessages( |
| 52 | + self::equalTo(['You saved the rule.']), |
| 53 | + MessageInterface::TYPE_SUCCESS |
| 54 | + ); |
| 55 | + |
| 56 | + $select = $resource->getConnection()->select(); |
| 57 | + $select |
| 58 | + ->from($resource->getTable('salesrule'), ['simple_free_shipping']) |
| 59 | + ->where('rule_id > ?', $maxId); |
| 60 | + $simpleFreeShipping = (int)$resource->getConnection()->fetchOne($select); |
| 61 | + |
| 62 | + $this->assertEquals(1, $simpleFreeShipping); |
| 63 | + } |
| 64 | + |
| 65 | + public function testCreateRuleWithWrongDates(): void |
| 66 | + { |
| 67 | + $requestData = [ |
| 68 | + 'from_date' => '2023-02-02', |
| 69 | + 'to_date' => '2023-01-01', |
| 70 | + ]; |
| 71 | + $this->getRequest()->setMethod(Http::METHOD_POST); |
| 72 | + $this->getRequest()->setPostValue($requestData); |
| 73 | + |
| 74 | + $this->dispatch('backend/sales_rule/promo_quote/save'); |
| 75 | + $this->assertSessionMessages( |
| 76 | + self::equalTo(['End Date must follow Start Date.']), |
| 77 | + MessageInterface::TYPE_ERROR |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
0 commit comments