|
| 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\Config\Controller\Adminhtml\System; |
| 9 | + |
| 10 | +use Magento\Config\Model\Config\Loader; |
| 11 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 12 | +use Magento\Framework\App\ScopeInterface; |
| 13 | +use Magento\Framework\App\ScopeResolverPool; |
| 14 | +use Magento\Framework\Message\MessageInterface; |
| 15 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 16 | + |
| 17 | +/** |
| 18 | + * Checks saving and updating of configuration data |
| 19 | + * |
| 20 | + * @see \Magento\Config\Controller\Adminhtml\System\Config\Save |
| 21 | + * @magentoAppArea adminhtml |
| 22 | + */ |
| 23 | +class SaveTest extends AbstractBackendController |
| 24 | +{ |
| 25 | + /** @var Loader */ |
| 26 | + private $configLoader; |
| 27 | + |
| 28 | + /** @var ScopeResolverPool */ |
| 29 | + private $scopeResolverPool; |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritdoc |
| 33 | + */ |
| 34 | + public function setUp(): void |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->configLoader = $this->_objectManager->get(Loader::class); |
| 39 | + $this->scopeResolverPool = $this->_objectManager->get(ScopeResolverPool::class); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @dataProvider saveConfigDataProvider |
| 44 | + * @param array $params |
| 45 | + * @param array $post |
| 46 | + * @magentoDbIsolation enabled |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public function testSaveConfig(array $params, array $post): void |
| 50 | + { |
| 51 | + $expectedPathValue = $this->prepareExpectedPathValue($params['section'], $post['groups']); |
| 52 | + $this->dispatchWithParams($params, $post); |
| 53 | + $this->assertSessionMessages( |
| 54 | + $this->equalTo([(string)__('You saved the configuration.')]), |
| 55 | + MessageInterface::TYPE_SUCCESS |
| 56 | + ); |
| 57 | + $this->assertPathValue($expectedPathValue); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return array |
| 62 | + */ |
| 63 | + public function saveConfigDataProvider(): array |
| 64 | + { |
| 65 | + return [ |
| 66 | + 'configure_shipping_origin' => [ |
| 67 | + 'params' => ['section' => 'shipping'], |
| 68 | + 'post' => [ |
| 69 | + 'groups' => [ |
| 70 | + 'origin' => [ |
| 71 | + 'fields' => [ |
| 72 | + 'country_id' => ['value' => 'CH'], |
| 73 | + 'region_id' => ['value' => '107'], |
| 74 | + 'postcode' => ['value' => '3005'], |
| 75 | + 'city' => ['value' => 'Bern'], |
| 76 | + 'street_line1' => ['value' => 'Weinbergstrasse 4'], |
| 77 | + 'street_line2' => ['value' => 'Suite 1'], |
| 78 | + ], |
| 79 | + ], |
| 80 | + ], |
| 81 | + ], |
| 82 | + ], |
| 83 | + 'configure_multi_shipping_options' => [ |
| 84 | + 'params' => ['section' => 'multishipping'], |
| 85 | + 'post' => [ |
| 86 | + 'groups' => [ |
| 87 | + 'options' => [ |
| 88 | + 'fields' => [ |
| 89 | + 'checkout_multiple' => ['value' => '1'], |
| 90 | + 'checkout_multiple_maximum_qty' => ['value' => '99'], |
| 91 | + ], |
| 92 | + ], |
| 93 | + ], |
| 94 | + ], |
| 95 | + ], |
| 96 | + 'configure_flat_rate_shipping_method' => [ |
| 97 | + 'params' => ['section' => 'carriers'], |
| 98 | + 'post' => [ |
| 99 | + 'groups' => [ |
| 100 | + 'flatrate' => [ |
| 101 | + 'fields' => [ |
| 102 | + 'active' => ['value' => '1'], |
| 103 | + 'type' => ['value' => 'I'], |
| 104 | + 'price' => ['value' => '5.00'], |
| 105 | + 'sallowspecific' => ['value' => '0'], |
| 106 | + ], |
| 107 | + ], |
| 108 | + ], |
| 109 | + ], |
| 110 | + ], |
| 111 | + ]; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Prepare expected path value array. |
| 116 | + * |
| 117 | + * @param string $section |
| 118 | + * @param array $groups |
| 119 | + * @return array |
| 120 | + */ |
| 121 | + private function prepareExpectedPathValue(string $section, array $groups): array |
| 122 | + { |
| 123 | + $expectedData = []; |
| 124 | + foreach ($groups as $groupId => $groupData) { |
| 125 | + $groupPath = $section . '/' . $groupId; |
| 126 | + foreach ($groupData['fields'] as $fieldId => $fieldData) { |
| 127 | + $path = $groupPath . '/' . $fieldId; |
| 128 | + $expectedData[$groupPath][$path] = $fieldData['value']; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return $expectedData; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Check that the values for the paths in the config data were saved successfully. |
| 137 | + * |
| 138 | + * @param array $expectedPathValue |
| 139 | + * @return void |
| 140 | + */ |
| 141 | + private function assertPathValue(array $expectedPathValue): void |
| 142 | + { |
| 143 | + $scope = $this->scopeResolverPool->get(ScopeInterface::SCOPE_DEFAULT)->getScope(); |
| 144 | + foreach ($expectedPathValue as $groupPath => $groupData) { |
| 145 | + $actualPathValue = $this->configLoader->getConfigByPath( |
| 146 | + $groupPath, |
| 147 | + $scope->getScopeType(), |
| 148 | + $scope->getId(), |
| 149 | + false |
| 150 | + ); |
| 151 | + $filteredActualPathValue = []; |
| 152 | + foreach ($groupData as $fieldPath => $fieldValue) { |
| 153 | + if (isset($actualPathValue[$fieldPath])) { |
| 154 | + $filteredActualPathValue[$fieldPath] = $actualPathValue[$fieldPath]; |
| 155 | + } |
| 156 | + } |
| 157 | + $this->assertEquals($groupData, $filteredActualPathValue); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Dispatch request with params |
| 163 | + * |
| 164 | + * @param array $params |
| 165 | + * @param array $postParams |
| 166 | + * @return void |
| 167 | + */ |
| 168 | + private function dispatchWithParams(array $params = [], array $postParams = []): void |
| 169 | + { |
| 170 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST) |
| 171 | + ->setParams($params) |
| 172 | + ->setPostValue($postParams); |
| 173 | + $this->dispatch('backend/admin/system_config/save'); |
| 174 | + } |
| 175 | +} |
0 commit comments