Skip to content

Commit b53691f

Browse files
AC-8119: Fedex Shipping Method Configuration Fix
1 parent 9e99eac commit b53691f

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Fedex\Model\Config\Backend;
10+
11+
use Magento\Framework\App\Cache\TypeListInterface;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\App\Config\Value;
14+
use Magento\Framework\Data\Collection\AbstractDb;
15+
use Magento\Framework\Exception\ValidatorException;
16+
use Magento\Framework\Model\Context;
17+
use Magento\Framework\Model\ResourceModel\AbstractResource;
18+
use Magento\Framework\Registry;
19+
use Magento\Framework\Validator\Url;
20+
use Magento\Framework\Model\AbstractModel;
21+
22+
/**
23+
* Represents a config URL that may point to a Fedex endpoint
24+
*/
25+
class FedexUrl extends Value
26+
{
27+
/**
28+
* @var Url
29+
*/
30+
private Url $url;
31+
32+
/**
33+
* @param Context $context
34+
* @param Registry $registry
35+
* @param ScopeConfigInterface $config
36+
* @param TypeListInterface $cacheTypeList
37+
* @param AbstractResource|null $resource
38+
* @param AbstractDb|null $resourceCollection
39+
* @param Url $url
40+
* @param array $data
41+
*/
42+
public function __construct(
43+
Context $context,
44+
Registry $registry,
45+
ScopeConfigInterface $config,
46+
TypeListInterface $cacheTypeList,
47+
AbstractResource $resource = null,
48+
AbstractDb $resourceCollection = null,
49+
Url $url,
50+
array $data = []
51+
) {
52+
$this->url = $url;
53+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
* @throws ValidatorException
59+
*/
60+
public function beforeSave(): AbstractModel
61+
{
62+
$isValid = $this->url->isValid($this->getValue());
63+
if ($isValid) {
64+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
65+
$host = parse_url((string)$this->getValue(), \PHP_URL_HOST);
66+
67+
if (!empty($host) && !preg_match('/(?:.+\.|^)fedex\.com$/i', $host)) {
68+
throw new ValidatorException(__('Fedex API endpoint URL\'s must use fedex.com'));
69+
}
70+
}
71+
72+
return parent::beforeSave();
73+
}
74+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Fedex\Test\Unit\Model\Config\Backend;
10+
11+
use Magento\Fedex\Model\Config\Backend\FedexUrl;
12+
use Magento\Framework\Exception\ValidatorException;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Verify behavior of FedexUrl backend type
18+
*/
19+
class FedexUrlTest extends TestCase
20+
{
21+
22+
/**
23+
* @var FedexUrl
24+
*/
25+
private $config;
26+
27+
protected function setUp(): void
28+
{
29+
$objectManager = new ObjectManager($this);
30+
/** @var FedexUrl $fedexUrl */
31+
$this->config = $objectManager->getObject(FedexUrl::class);
32+
}
33+
34+
/**
35+
* @dataProvider validDataProvider
36+
* @param string|null $data The valid data
37+
* @throws ValidatorException
38+
*/
39+
public function testBeforeSave(string $data = null)
40+
{
41+
$this->config->setValue($data);
42+
$this->config->beforeSave();
43+
}
44+
45+
/**
46+
* @dataProvider invalidDataProvider
47+
* @param string $data The invalid data
48+
*/
49+
public function testBeforeSaveErrors(string $data)
50+
{
51+
$this->expectException('Magento\Framework\Exception\ValidatorException');
52+
$this->expectExceptionMessage('Fedex API endpoint URL\'s must use fedex.com');
53+
$this->config->setValue($data);
54+
$this->config->beforeSave();
55+
}
56+
57+
/**
58+
* Validator Data Provider
59+
*
60+
* @return array
61+
*/
62+
public function validDataProvider(): array
63+
{
64+
return [
65+
[],
66+
[null],
67+
[''],
68+
['http://fedex.com'],
69+
['https://foo.fedex.com'],
70+
['http://foo.fedex.com/foo/bar?baz=bash&fizz=buzz'],
71+
];
72+
}
73+
74+
/**
75+
* @return \string[][]
76+
*/
77+
public function invalidDataProvider(): array
78+
{
79+
return [
80+
['http://fedexfoo.com'],
81+
['https://foofedex.com'],
82+
['https://fedex.com.fake.com'],
83+
['https://fedex.info'],
84+
['http://fedex.com.foo.com/foo/bar?baz=bash&fizz=buzz'],
85+
['http://foofedex.com/foo/bar?baz=bash&fizz=buzz'],
86+
];
87+
}
88+
}

app/code/Magento/Fedex/etc/adminhtml/system.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
</field>
4141
<field id="production_webservices_url" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1" canRestore="1">
4242
<label>Web-Services URL (Production)</label>
43+
<backend_model>Magento\Fedex\Model\Config\Backend\FedexUrl</backend_model>
4344
<depends>
4445
<field id="sandbox_mode">0</field>
4546
</depends>
4647
</field>
4748
<field id="sandbox_webservices_url" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" canRestore="1">
4849
<label>Web-Services URL (Sandbox)</label>
50+
<backend_model>Magento\Fedex\Model\Config\Backend\FedexUrl</backend_model>
4951
<depends>
5052
<field id="sandbox_mode">1</field>
5153
</depends>

app/code/Magento/Fedex/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ Debug,Debug
7878
"Show Method if Not Applicable","Show Method if Not Applicable"
7979
"Sort Order","Sort Order"
8080
"Can't convert a shipping cost from ""%1-%2"" for FedEx carrier.","Can't convert a shipping cost from ""%1-%2"" for FedEx carrier."
81+
"Fedex API endpoint URL\'s must use fedex.com","Fedex API endpoint URL\'s must use fedex.com"

0 commit comments

Comments
 (0)