ltsecurityinc安防产品在线商城代码
This commit is contained in:
+179
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Config\Controller\Adminhtml\System\Config;
|
||||
|
||||
use Magento\Config\Model\Config\Loader;
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
use Magento\Framework\App\ScopeInterface;
|
||||
use Magento\Framework\App\ScopeResolverPool;
|
||||
use Magento\Framework\Message\MessageInterface;
|
||||
use Magento\TestFramework\TestCase\AbstractBackendController;
|
||||
|
||||
/**
|
||||
* Checks saving and updating of configuration data
|
||||
*
|
||||
* @see \Magento\Config\Controller\Adminhtml\System\Config\Save
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class SaveTest extends AbstractBackendController
|
||||
{
|
||||
/** @var Loader */
|
||||
private $configLoader;
|
||||
|
||||
/** @var ScopeResolverPool */
|
||||
private $scopeResolverPool;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->configLoader = $this->_objectManager->get(Loader::class);
|
||||
$this->scopeResolverPool = $this->_objectManager->get(ScopeResolverPool::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider saveConfigDataProvider
|
||||
* @magentoDbIsolation enabled
|
||||
* @param array $params
|
||||
* @param array $post
|
||||
* @return void
|
||||
*/
|
||||
public function testSaveConfig(array $params, array $post): void
|
||||
{
|
||||
$expectedPathValue = $this->prepareExpectedPathValue($params['section'], $post['groups']);
|
||||
$this->dispatchWithParams($params, $post);
|
||||
$this->assertSessionMessages(
|
||||
$this->containsEqual((string)__('You saved the configuration.')),
|
||||
MessageInterface::TYPE_SUCCESS
|
||||
);
|
||||
$this->assertPathValue($expectedPathValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function saveConfigDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'configure_shipping_origin' => [
|
||||
'params' => ['section' => 'shipping'],
|
||||
'post' => [
|
||||
'groups' => [
|
||||
'origin' => [
|
||||
'fields' => [
|
||||
'country_id' => ['value' => 'CH'],
|
||||
'region_id' => ['value' => '107'],
|
||||
'postcode' => ['value' => '3005'],
|
||||
'city' => ['value' => 'Bern'],
|
||||
'street_line1' => ['value' => 'Weinbergstrasse 4'],
|
||||
'street_line2' => ['value' => 'Suite 1'],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'configure_multi_shipping_options' => [
|
||||
'params' => ['section' => 'multishipping'],
|
||||
'post' => [
|
||||
'groups' => [
|
||||
'options' => [
|
||||
'fields' => [
|
||||
'checkout_multiple' => ['value' => '1'],
|
||||
'checkout_multiple_maximum_qty' => ['value' => '99'],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'configure_flat_rate_shipping_method' => [
|
||||
'params' => ['section' => 'carriers'],
|
||||
'post' => [
|
||||
'groups' => [
|
||||
'flatrate' => [
|
||||
'fields' => [
|
||||
'active' => ['value' => '1'],
|
||||
'type' => ['value' => 'I'],
|
||||
'price' => ['value' => '5.00'],
|
||||
'sallowspecific' => ['value' => '0'],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare expected path value array.
|
||||
*
|
||||
* @param string $section
|
||||
* @param array $groups
|
||||
* @return array
|
||||
*/
|
||||
private function prepareExpectedPathValue(string $section, array $groups): array
|
||||
{
|
||||
foreach ($groups as $groupId => $groupData) {
|
||||
$groupPath = $section . '/' . $groupId;
|
||||
foreach ($groupData['fields'] as $fieldId => $fieldData) {
|
||||
$path = $groupPath . '/' . $fieldId;
|
||||
$expectedData[$groupPath][$path] = $fieldData['value'];
|
||||
}
|
||||
}
|
||||
|
||||
return $expectedData ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the values for the paths in the config data were saved successfully.
|
||||
*
|
||||
* @param array $expectedPathValue
|
||||
* @return void
|
||||
*/
|
||||
private function assertPathValue(array $expectedPathValue): void
|
||||
{
|
||||
$scope = $this->scopeResolverPool->get(ScopeInterface::SCOPE_DEFAULT)->getScope();
|
||||
foreach ($expectedPathValue as $groupPath => $groupData) {
|
||||
$actualPathValue = $this->configLoader->getConfigByPath(
|
||||
$groupPath,
|
||||
$scope->getScopeType(),
|
||||
$scope->getId(),
|
||||
false
|
||||
);
|
||||
foreach ($groupData as $fieldPath => $fieldValue) {
|
||||
$this->assertArrayHasKey(
|
||||
$fieldPath,
|
||||
$actualPathValue,
|
||||
sprintf('The expected config setting was not saved in the database. Path: %s', $fieldPath)
|
||||
);
|
||||
$this->assertEquals(
|
||||
$fieldValue,
|
||||
$actualPathValue[$fieldPath],
|
||||
sprintf('The expected value of the config setting is not correct. Path: %s', $fieldPath)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch request with params
|
||||
*
|
||||
* @param array $params
|
||||
* @param array $postParams
|
||||
* @return void
|
||||
*/
|
||||
private function dispatchWithParams(array $params = [], array $postParams = []): void
|
||||
{
|
||||
$this->getRequest()->setMethod(HttpRequest::METHOD_POST)
|
||||
->setParams($params)
|
||||
->setPostValue($postParams);
|
||||
$this->dispatch('backend/admin/system_config/save');
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Config\Controller\Adminhtml\System;
|
||||
|
||||
use Magento\Config\Controller\Adminhtml\System\Config\Save;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class ConfigTest extends \Magento\TestFramework\TestCase\AbstractBackendController
|
||||
{
|
||||
/**
|
||||
* Test Configuration page existing.
|
||||
*/
|
||||
public function testEditAction()
|
||||
{
|
||||
$this->dispatch('backend/admin/system_config/edit');
|
||||
$this->assertStringContainsString('<div id="system_config_tabs"', $this->getResponse()->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test redirect after changing base URL.
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testChangeBaseUrl()
|
||||
{
|
||||
$defaultHost = Bootstrap::getObjectManager()->create(\Magento\Framework\Url::class)->getBaseUrl();
|
||||
$newHost = 'm2test123.loc';
|
||||
$request = $this->getRequest();
|
||||
$request->setPostValue(
|
||||
[
|
||||
'groups' =>
|
||||
['unsecure' =>
|
||||
['fields' =>
|
||||
['base_url' =>
|
||||
['value' => 'http://' . $newHost . '/']
|
||||
]
|
||||
]
|
||||
],
|
||||
'config_state' => ['web_unsecure' => 1]
|
||||
]
|
||||
)->setParam(
|
||||
'section',
|
||||
'web'
|
||||
)->setMethod(
|
||||
HttpRequest::METHOD_POST
|
||||
);
|
||||
$this->dispatch('backend/admin/system_config/save');
|
||||
|
||||
$this->assertTrue($this->getResponse()->isRedirect(), 'Redirect was expected, but none was performed.');
|
||||
|
||||
/** @var array|bool $url */
|
||||
$url = parse_url($this->getResponse()->getHeader('Location')->getFieldValue());
|
||||
$this->assertArrayNotHasKey(
|
||||
'query',
|
||||
$url,
|
||||
'No GET params, including "SID", were expected, but somewhat exists'
|
||||
);
|
||||
$this->assertEquals($newHost, $url['host'], 'A new host in the url expected, but there is old one');
|
||||
$this->resetBaseUrl($defaultHost);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test saving undeclared configs.
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testSavingUndeclared()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
$request->setPostValue([
|
||||
'groups' => [
|
||||
'non_existing' => [
|
||||
'fields' => [
|
||||
'non_existing_field' => [
|
||||
'value' => 'some_value'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
$request->setParam('section', 'web');
|
||||
$request->setMethod(HttpRequest::METHOD_POST);
|
||||
/** @var Save $controller */
|
||||
$controller = Bootstrap::getObjectManager()->create(Save::class);
|
||||
$controller->execute();
|
||||
|
||||
$this->assertSessionMessages($this->equalTo(['You saved the configuration.']));
|
||||
/** @var ScopeConfigInterface $scopeConfig */
|
||||
$scopeConfig = Bootstrap::getObjectManager()->get(ScopeConfigInterface::class);
|
||||
$this->assertNull($scopeConfig->getValue('web/non_existing/non_existing_field'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset test framework default base url.
|
||||
*
|
||||
* @param string $defaultHost
|
||||
*/
|
||||
protected function resetBaseUrl($defaultHost)
|
||||
{
|
||||
$baseUrlData = [
|
||||
'section' => 'web',
|
||||
'website' => null,
|
||||
'store' => null,
|
||||
'groups' => [
|
||||
'unsecure' => [
|
||||
'fields' => [
|
||||
'base_url' => [
|
||||
'value' => $defaultHost
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
Bootstrap::getObjectManager()->create(\Magento\Config\Model\Config\Factory::class)
|
||||
->create()
|
||||
->addData($baseUrlData)
|
||||
->save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user