Skip to content

Commit dd15952

Browse files
committed
Merge branch '2.4-develop' of https://github.com/magento-commerce/magento2ce into PR-2021-12-21
2 parents 7cdd325 + f69a8ee commit dd15952

File tree

82 files changed

+2267
-386
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2267
-386
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Analytics\Plugin;
10+
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Integration\Model\Integration;
13+
use Magento\Integration\Model\Validator\BearerTokenValidator;
14+
15+
/**
16+
* Overrides authorization config to always allow analytics token to be used as bearer
17+
*/
18+
class BearerTokenValidatorPlugin
19+
{
20+
/**
21+
* @var ScopeConfigInterface
22+
*/
23+
private ScopeConfigInterface $config;
24+
25+
/**
26+
* @param ScopeConfigInterface $config
27+
*/
28+
public function __construct(ScopeConfigInterface $config)
29+
{
30+
$this->config = $config;
31+
}
32+
33+
/***
34+
* Always allow access token for analytics to be used as bearer
35+
*
36+
* @param BearerTokenValidator $subject
37+
* @param bool $result
38+
* @param Integration $integration
39+
* @return bool
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
*/
42+
public function afterIsIntegrationAllowedAsBearerToken(
43+
BearerTokenValidator $subject,
44+
bool $result,
45+
Integration $integration
46+
): bool {
47+
return $result || $integration->getName() === $this->config->getValue('analytics/integration_name');
48+
}
49+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\Analytics\Test\Unit\Plugin;
10+
11+
use Magento\Analytics\Plugin\BearerTokenValidatorPlugin;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Integration\Model\Integration;
14+
use Magento\Integration\Model\Validator\BearerTokenValidator;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class BearerTokenValidatorPluginTest extends TestCase
19+
{
20+
/**
21+
* @var BearerTokenValidatorPlugin
22+
*/
23+
private BearerTokenValidatorPlugin $plugin;
24+
25+
/**
26+
* @var BearerTokenValidator|MockObject
27+
*/
28+
private $validator;
29+
30+
public function setUp(): void
31+
{
32+
$config = $this->createMock(ScopeConfigInterface::class);
33+
$config->method('getValue')
34+
->with('analytics/integration_name')
35+
->willReturn('abc');
36+
$this->plugin = new BearerTokenValidatorPlugin($config);
37+
$this->validator = $this->createMock(BearerTokenValidator::class);
38+
}
39+
40+
public function testTrueIsPassedThrough()
41+
{
42+
$integration = $this->createMock(Integration::class);
43+
$integration->method('__call')
44+
->with('getName')
45+
->willReturn('invalid');
46+
47+
$result = $this->plugin->afterIsIntegrationAllowedAsBearerToken($this->validator, true, $integration);
48+
self::assertTrue($result);
49+
}
50+
51+
public function testFalseWhenIntegrationDoesntMatch()
52+
{
53+
$integration = $this->createMock(Integration::class);
54+
$integration->method('__call')
55+
->with('getName')
56+
->willReturn('invalid');
57+
58+
$result = $this->plugin->afterIsIntegrationAllowedAsBearerToken($this->validator, false, $integration);
59+
self::assertFalse($result);
60+
}
61+
62+
public function testTrueWhenIntegrationMatches()
63+
{
64+
$integration = $this->createMock(Integration::class);
65+
$integration->method('__call')
66+
->with('getName')
67+
->willReturn('abc');
68+
69+
$result = $this->plugin->afterIsIntegrationAllowedAsBearerToken($this->validator, true, $integration);
70+
self::assertTrue($result);
71+
}
72+
}

app/code/Magento/Analytics/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,7 @@
271271
<argument name="connectionFactory" xsi:type="object">Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactory</argument>
272272
</arguments>
273273
</type>
274+
<type name="Magento\Integration\Model\Validator\BearerTokenValidator">
275+
<plugin name="allow_bearer_token" type="Magento\Analytics\Plugin\BearerTokenValidatorPlugin"/>
276+
</type>
274277
</config>

app/code/Magento/CatalogInventory/Model/StockManagement.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
1515
use Magento\Catalog\Api\ProductRepositoryInterface;
1616
use Magento\CatalogInventory\Model\ResourceModel\Stock as ResourceStock;
17+
use Magento\Framework\Exception\LocalizedException;
1718

1819
/**
1920
* Implements a few interfaces for backward compatibility
21+
*
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2023
*/
2124
class StockManagement implements StockManagementInterface, RegisterProductSaleInterface, RevertProductSaleInterface
2225
{
@@ -91,7 +94,8 @@ public function __construct(
9194
* @param string[] $items
9295
* @param int $websiteId
9396
* @return StockItemInterface[]
94-
* @throws \Magento\Framework\Exception\LocalizedException
97+
* @throws StockStateException
98+
* @throws LocalizedException
9599
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
96100
*/
97101
public function registerProductsSale($items, $websiteId = null)
@@ -118,7 +122,7 @@ public function registerProductsSale($items, $websiteId = null)
118122
&& !$this->stockState->checkQty($productId, $orderedQty, $stockItem->getWebsiteId())
119123
) {
120124
$this->getResource()->commit();
121-
throw new \Magento\Framework\Exception\LocalizedException(
125+
throw new StockStateException(
122126
__('Some of the products are out of stock.')
123127
);
124128
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogInventory\Model;
7+
8+
use Magento\Framework\Exception\LocalizedException;
9+
10+
/**
11+
* Exception class reflecting when an operation cannot be completed due to the current stock status of an inventory item
12+
*
13+
* @api
14+
*/
15+
class StockStateException extends LocalizedException
16+
{
17+
}

app/code/Magento/CatalogInventory/Test/Unit/Model/StockManagementTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function testRegisterProductsSale(
203203
*/
204204
public function testRegisterProductsSaleException(array $items, array $lockedItems)
205205
{
206-
$this->expectException('Magento\Framework\Exception\LocalizedException');
206+
$this->expectException('Magento\CatalogInventory\Model\StockStateException');
207207
$this->expectExceptionMessage('Some of the products are out of stock.');
208208
$this->stockResourceMock
209209
->expects($this->once())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\CatalogInventoryGraphQl\Helper\Error\MessageFormatters;
9+
10+
use Magento\CatalogInventory\Model\StockStateException;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\GraphQl\Helper\Error\ExceptionMessageFormatterInterface;
17+
18+
/**
19+
* Check if an internally-thrown exception is from an item stock status issue and re-throw with the message intact if so
20+
*/
21+
class StockStateExceptionMessageFormatter implements ExceptionMessageFormatterInterface
22+
{
23+
/**
24+
* If the thrown exception was from an item stock status issue, allow the message to go through
25+
*
26+
* @param LocalizedException $e
27+
* @param string $messagePrefix
28+
* @param Field $field
29+
* @param ContextInterface $context
30+
* @param ResolveInfo $info
31+
*
32+
* @return GraphQlInputException|null
33+
*/
34+
public function getFormatted(
35+
LocalizedException $e,
36+
string $messagePrefix,
37+
Field $field,
38+
ContextInterface $context,
39+
ResolveInfo $info
40+
): ?GraphQlInputException {
41+
if ($e instanceof StockStateException) {
42+
return new GraphQlInputException(__("$messagePrefix: %message", ['message' => $e->getMessage()]), $e);
43+
}
44+
return null;
45+
}
46+
}

app/code/Magento/CatalogInventoryGraphQl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"magento/framework": "*",
88
"magento/module-store": "*",
99
"magento/module-catalog": "*",
10-
"magento/module-catalog-inventory": "*"
10+
"magento/module-catalog-inventory": "*",
11+
"magento/module-graph-ql": "*"
1112
},
1213
"license": [
1314
"OSL-3.0",

app/code/Magento/ConfigurableProductGraphQl/Model/Cart/BuyRequest/SuperAttributeDataProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
1212
use Magento\CatalogInventory\Api\StockStateInterface;
13+
use Magento\CatalogInventory\Model\StockStateException;
1314
use Magento\ConfigurableProductGraphQl\Model\Options\Collection as OptionCollection;
1415
use Magento\Framework\EntityManager\MetadataPool;
1516
use Magento\Framework\Exception\LocalizedException;
@@ -146,12 +147,13 @@ private function checkProductStock(string $sku, float $qty, int $scopeId): void
146147
*
147148
* @param string $parentSku
148149
* @param array $superAttributesData
150+
* @throws StockStateException
149151
* @throws LocalizedException
150152
*/
151153
private function checkSuperAttributeData(string $parentSku, array $superAttributesData): void
152154
{
153155
if (empty($superAttributesData)) {
154-
throw new LocalizedException(
156+
throw new StockStateException(
155157
__('The product with SKU %sku is out of stock.', ['sku' => $parentSku])
156158
);
157159
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\GraphQl\Helper\Error;
9+
10+
use GraphQL\Error\ClientAware;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Framework\Phrase;
17+
18+
/**
19+
* Class for formatting internally-thrown errors if they match allowed exception types or using a default message if not
20+
*/
21+
class AggregateExceptionMessageFormatter
22+
{
23+
/**
24+
* @var ExceptionMessageFormatterInterface[]
25+
*/
26+
private $messageFormatters;
27+
28+
/**
29+
* @param ExceptionMessageFormatterInterface[] $messageFormatters
30+
*/
31+
public function __construct(array $messageFormatters)
32+
{
33+
$this->messageFormatters = $messageFormatters;
34+
}
35+
36+
/**
37+
* Format a thrown exception message if it matches one of the supplied formatters, otherwise use a default message
38+
*
39+
* @param LocalizedException $e
40+
* @param Phrase $defaultMessage
41+
* @param string $messagePrefix
42+
* @param Field $field
43+
* @param ContextInterface $context
44+
* @param ResolveInfo $info
45+
*
46+
* @return ClientAware
47+
*/
48+
public function getFormatted(
49+
LocalizedException $e,
50+
Phrase $defaultMessage,
51+
string $messagePrefix,
52+
Field $field,
53+
ContextInterface $context,
54+
ResolveInfo $info
55+
): ClientAware {
56+
foreach ($this->messageFormatters as $formatter) {
57+
$formatted = $formatter->getFormatted($e, $messagePrefix, $field, $context, $info);
58+
if ($formatted) {
59+
return $formatted;
60+
}
61+
}
62+
return new GraphQlInputException($defaultMessage, $e);
63+
}
64+
}

0 commit comments

Comments
 (0)