diff --git a/app/code/Magento/Paypal/Test/Unit/Model/Payflow/TransparentTest.php b/app/code/Magento/Paypal/Test/Unit/Model/Payflow/TransparentTest.php index f6df35ae272b7..9587600203561 100644 --- a/app/code/Magento/Paypal/Test/Unit/Model/Payflow/TransparentTest.php +++ b/app/code/Magento/Paypal/Test/Unit/Model/Payflow/TransparentTest.php @@ -29,6 +29,8 @@ use PHPUnit_Framework_MockObject_MockObject as MockObject; /** + * Paypal transparent test class + * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class TransparentTest extends \PHPUnit\Framework\TestCase @@ -194,7 +196,7 @@ private function getPaymentExtensionInterfaceFactory() ->disableOriginalConstructor() ->getMock(); $orderPaymentExtension = $this->getMockBuilder(OrderPaymentExtensionInterface::class) - ->setMethods(['setVaultPaymentToken']) + ->setMethods(['setVaultPaymentToken', 'getVaultPaymentToken']) ->disableOriginalConstructor() ->getMock(); diff --git a/app/code/Magento/Reports/Model/ResourceModel/Product/Collection.php b/app/code/Magento/Reports/Model/ResourceModel/Product/Collection.php index d81f6c6f362dc..aa41759baac68 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Product/Collection.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Product/Collection.php @@ -3,12 +3,12 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); -/** - * @author Magento Core Team - */ namespace Magento\Reports\Model\ResourceModel\Product; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; + /** * Products Report collection. * @@ -90,6 +90,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection * @param \Magento\Catalog\Model\Product\Type $productType * @param \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteResource * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection + * @param ProductLimitationFactory|null $productLimitationFactory * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -117,7 +118,8 @@ public function __construct( \Magento\Reports\Model\Event\TypeFactory $eventTypeFactory, \Magento\Catalog\Model\Product\Type $productType, \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteResource, - \Magento\Framework\DB\Adapter\AdapterInterface $connection = null + \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, + ProductLimitationFactory $productLimitationFactory = null ) { $this->setProductEntityId($product->getEntityIdField()); $this->setProductEntityTableName($product->getEntityTable()); @@ -142,7 +144,8 @@ public function __construct( $customerSession, $dateTime, $groupManagement, - $connection + $connection, + $productLimitationFactory ); $this->_eventTypeFactory = $eventTypeFactory; $this->_productType = $productType; diff --git a/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/CollectionTest.php b/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/CollectionTest.php index c8a16c2476824..3f1857b352dbc 100644 --- a/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/CollectionTest.php +++ b/app/code/Magento/Reports/Test/Unit/Model/ResourceModel/Product/CollectionTest.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace Magento\Reports\Test\Unit\Model\ResourceModel\Product; @@ -12,6 +13,8 @@ use Magento\Catalog\Model\Product\Type as ProductType; use Magento\Catalog\Model\ResourceModel\Helper; use Magento\Catalog\Model\ResourceModel\Product as ResourceProduct; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation; +use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory; use Magento\Catalog\Model\ResourceModel\Url; use Magento\Customer\Api\GroupManagementInterface; use Magento\Customer\Model\Session; @@ -75,6 +78,11 @@ class CollectionTest extends \PHPUnit\Framework\TestCase */ private $selectMock; + /** + * SetUp method + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ protected function setUp() { $this->objectManager = new ObjectManager($this); @@ -138,31 +146,44 @@ protected function setUp() $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->connectionMock); $this->connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock); - $this->collection = new ProductCollection( - $entityFactoryMock, - $loggerMock, - $fetchStrategyMock, - $eventManagerMock, - $eavConfigMock, - $this->resourceMock, - $eavEntityFactoryMock, - $resourceHelperMock, - $universalFactoryMock, - $storeManagerMock, - $moduleManagerMock, - $productFlatStateMock, - $scopeConfigMock, - $optionFactoryMock, - $catalogUrlMock, - $localeDateMock, - $customerSessionMock, - $dateTimeMock, - $groupManagementMock, - $productMock, - $this->eventTypeFactoryMock, - $productTypeMock, - $quoteResourceMock, - $this->connectionMock + $productLimitationFactoryMock = $this->createPartialMock( + ProductLimitationFactory::class, + ['create'] + ); + $productLimitation = $this->createMock(ProductLimitation::class); + $productLimitationFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($productLimitation); + + $this->collection = $this->objectManager->getObject( + ProductCollection::class, + [ + 'entityFactory' => $entityFactoryMock, + 'logger' => $loggerMock, + 'fetchStrategy' => $fetchStrategyMock, + 'eventManager' => $eventManagerMock, + 'eavConfig' => $eavConfigMock, + 'resource' => $this->resourceMock, + 'eavEntityFactory' => $eavEntityFactoryMock, + 'resourceHelper' => $resourceHelperMock, + 'universalFactory' => $universalFactoryMock, + 'storeManager' => $storeManagerMock, + 'moduleManager' => $moduleManagerMock, + 'catalogProductFlatState' => $productFlatStateMock, + 'scopeConfig' => $scopeConfigMock, + 'productOptionFactory' => $optionFactoryMock, + 'catalogUrl' => $catalogUrlMock, + 'localeDate' => $localeDateMock, + 'customerSession' => $customerSessionMock, + 'dateTime' => $dateTimeMock, + 'groupManagement' => $groupManagementMock, + 'product' => $productMock, + 'eventTypeFactory' => $this->eventTypeFactoryMock, + 'productType' => $productTypeMock, + 'quoteResource' => $quoteResourceMock, + 'connection' => $this->connectionMock, + 'productLimitationFactory' => $productLimitationFactoryMock + ] ); } diff --git a/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php b/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php index 00ec485b15692..7e3f9b67a58ec 100644 --- a/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php +++ b/app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php @@ -3,6 +3,8 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Vault\Test\Unit\Model\Method; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; @@ -25,6 +27,7 @@ /** * Class VaultTest + * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class VaultTest extends \PHPUnit\Framework\TestCase @@ -140,7 +143,7 @@ public function testAuthorize() ->disableOriginalConstructor() ->getMock(); $extensionAttributes = $this->getMockBuilder(OrderPaymentExtensionInterface::class) - ->setMethods(['setVaultPaymentToken']) + ->setMethods(['setVaultPaymentToken', 'getVaultPaymentToken']) ->getMockForAbstractClass(); $commandManagerPool = $this->createMock(CommandManagerPoolInterface::class); @@ -235,7 +238,7 @@ public function testCapture() } /** - * @covers \Magento\Vault\Model\Method\Vault::isAvailable + * @covers \Magento\Vault\Model\Method\Vault::isAvailable * @dataProvider isAvailableDataProvider */ public function testIsAvailable($isAvailableProvider, $isActive, $expected) @@ -251,7 +254,10 @@ public function testIsAvailable($isAvailableProvider, $isActive, $expected) $config->expects(static::any()) ->method('getValue') - ->with('active', $storeId) + ->with( + 'active', + $storeId + ) ->willReturn($isActive); $quote->expects(static::any()) @@ -259,10 +265,13 @@ public function testIsAvailable($isAvailableProvider, $isActive, $expected) ->willReturn($storeId); /** @var Vault $model */ - $model = $this->objectManager->getObject(Vault::class, [ - 'config' => $config, - 'vaultProvider' => $this->vaultProvider - ]); + $model = $this->objectManager->getObject( + Vault::class, + [ + 'config' => $config, + 'vaultProvider' => $this->vaultProvider + ] + ); $actual = $model->isAvailable($quote); static::assertEquals($expected, $actual); } @@ -296,19 +305,25 @@ public function testIsAvailableWithoutQuote() $config->expects(static::once()) ->method('getValue') - ->with('active', $quote) + ->with( + 'active', + $quote + ) ->willReturn(false); /** @var Vault $model */ - $model = $this->objectManager->getObject(Vault::class, [ - 'config' => $config, - 'vaultProvider' => $this->vaultProvider - ]); + $model = $this->objectManager->getObject( + Vault::class, + [ + 'config' => $config, + 'vaultProvider' => $this->vaultProvider + ] + ); static::assertFalse($model->isAvailable($quote)); } /** - * @covers \Magento\Vault\Model\Method\Vault::canUseInternal + * @covers \Magento\Vault\Model\Method\Vault::canUseInternal * @param bool|null $configValue * @param bool|null $paymentValue * @param bool $expected @@ -326,7 +341,10 @@ public function testCanUseInternal($configValue, $paymentValue, $expected) $handler->expects(static::once()) ->method('handle') - ->with(['field' => 'can_use_internal'], null) + ->with( + ['field' => 'can_use_internal'], + null + ) ->willReturn($configValue); $this->vaultProvider->expects(static::any()) @@ -334,10 +352,13 @@ public function testCanUseInternal($configValue, $paymentValue, $expected) ->willReturn($paymentValue); /** @var Vault $model */ - $model = $this->objectManager->getObject(Vault::class, [ - 'vaultProvider' => $this->vaultProvider, - 'valueHandlerPool' => $handlerPool, - ]); + $model = $this->objectManager->getObject( + Vault::class, + [ + 'vaultProvider' => $this->vaultProvider, + 'valueHandlerPool' => $handlerPool, + ] + ); static::assertEquals($expected, $model->canUseInternal()); } diff --git a/dev/tests/static/framework/Magento/TestFramework/Dependency/Route/RouteMapper.php b/dev/tests/static/framework/Magento/TestFramework/Dependency/Route/RouteMapper.php index a3e5b6927e72a..87cc0985a053b 100644 --- a/dev/tests/static/framework/Magento/TestFramework/Dependency/Route/RouteMapper.php +++ b/dev/tests/static/framework/Magento/TestFramework/Dependency/Route/RouteMapper.php @@ -9,6 +9,7 @@ use Magento\Framework\App\Area; use Magento\Framework\App\Utility\Files; +use Magento\Framework\Component\ComponentFile; use Magento\TestFramework\Exception\NoSuchActionException; /** @@ -231,7 +232,7 @@ private function processConfigFile(string $module, string $configFile) // Read module's routes.xml file $config = simplexml_load_file($configFile); - $routers = $config->xpath("/config/router"); + $routers = $config->xpath("/config/router"); foreach ($routers as $router) { $routerId = (string)$router['id']; foreach ($router->xpath('route') as $route) { @@ -254,13 +255,11 @@ private function processConfigFile(string $module, string $configFile) private function getListRoutesXml() { if (empty($this->routeConfigFiles)) { - $files = Files::init()->getConfigFiles('*/routes.xml', [], false); - $pattern = '/(?[A-Z][a-z]+)[_\/\\\\](?[A-Z][a-zA-Z]+)/'; - foreach ($files as $file) { - if (preg_match($pattern, $file, $matches)) { - $module = $matches['namespace'] . '\\' . $matches['module']; - $this->routeConfigFiles[$module][] = $file; - } + $files = Files::init()->getConfigFiles('*/routes.xml', [], false, true); + /** @var ComponentFile $componentFile */ + foreach ($files as $componentFile) { + $module = str_replace('_', '\\', $componentFile->getComponentName()); + $this->routeConfigFiles[$module][] = $componentFile->getFullPath(); } } return $this->routeConfigFiles; diff --git a/lib/internal/Magento/Framework/App/Utility/Files.php b/lib/internal/Magento/Framework/App/Utility/Files.php index 8bb59bb42ff49..3460faf854bac 100644 --- a/lib/internal/Magento/Framework/App/Utility/Files.php +++ b/lib/internal/Magento/Framework/App/Utility/Files.php @@ -3,15 +3,18 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\App\Utility; use Magento\Framework\App\ObjectManager; +use Magento\Framework\Component\ComponentFile; use Magento\Framework\Component\ComponentRegistrar; use Magento\Framework\Component\DirSearch; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Filesystem\Glob; use Magento\Framework\Serialize\Serializer\Json; use Magento\Framework\View\Design\Theme\ThemePackage; use Magento\Framework\View\Design\Theme\ThemePackageList; -use Magento\Framework\Filesystem\Glob; /** * A helper to gather specific kind of files in Magento application. @@ -143,13 +146,13 @@ public static function setInstance(Files $instance = null) * Getter for an instance of self * * @return \Magento\Framework\App\Utility\Files - * @throws \Exception when there is no instance set + * @throws LocalizedException when there is no instance set */ public static function init() { if (!self::$_instance) { // phpcs:ignore Magento2.Exceptions.DirectThrow.FoundDirectThrow - throw new \Exception('Instance is not set yet.'); + throw new LocalizedException(__('Instance is not set yet.')); } return self::$_instance; } @@ -410,21 +413,25 @@ public function getMainConfigFiles($asDataSet = true) * @param string $fileNamePattern * @param array $excludedFileNames * @param bool $asDataSet + * @param bool $collectWithContext * @return array * @codingStandardsIgnoreStart */ public function getConfigFiles( $fileNamePattern = '*.xml', $excludedFileNames = ['wsdl.xml', 'wsdl2.xml', 'wsi.xml'], - $asDataSet = true + $asDataSet = true, + $collectWithContext = false ) { $cacheKey = __METHOD__ . '|' . $this->serializer->serialize([$fileNamePattern, $excludedFileNames, $asDataSet]); if (!isset(self::$_cache[$cacheKey])) { - $files = $this->dirSearch->collectFiles(ComponentRegistrar::MODULE, "/etc/{$fileNamePattern}"); + $method = $collectWithContext ? 'collectFilesWithContext' : 'collectFiles'; + $files = $this->dirSearch->{$method}(ComponentRegistrar::MODULE, "/etc/{$fileNamePattern}"); $files = array_filter( $files, - function ($file) use ($excludedFileNames) { - return !in_array(basename($file), $excludedFileNames); + function ($file) use ($excludedFileNames, $collectWithContext) { + /** @var ComponentFile $file */ + return !in_array(basename($collectWithContext ? $file->getFullPath() : $file), $excludedFileNames); } ); self::$_cache[$cacheKey] = $files; @@ -615,8 +622,7 @@ protected function getLayoutXmlFiles($location, $incomingParams = [], $asDataSet $params[$key] = $incomingParams[$key]; } } - //phpcs:ignore Magento2.Security.InsecureFunction - $cacheKey = md5($location . '|' . implode('|', $params)); + $cacheKey = hash('sha256', $location . '|' . implode('|', $params)); if (!isset(self::$_cache[__METHOD__][$cacheKey])) { $files = []; @@ -769,8 +775,7 @@ public function getPageTypeFiles($incomingParams = [], $asDataSet = true) $params[$key] = $incomingParams[$key]; } } - //phpcs:ignore Magento2.Security.InsecureFunction - $cacheKey = md5(implode('|', $params)); + $cacheKey = hash('sha256', implode('|', $params)); if (!isset(self::$_cache[__METHOD__][$cacheKey])) { self::$_cache[__METHOD__][$cacheKey] = self::getFiles( @@ -1506,7 +1511,7 @@ public function getNamespaces() public function getModuleFile($namespace, $module, $file) { return $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $namespace . '_' . $module) . - '/' . $file; + '/' . $file; } /** @@ -1607,7 +1612,12 @@ public function readLists($globPattern) $result = array_merge($result, $files); } if (!empty($incorrectPatterns)) { - throw new \Exception("The following patterns didn't return any result:\n" . join("\n", $incorrectPatterns)); + throw new LocalizedException( + __( + "The following patterns didn't return any result:\n%1", + join("\n", $incorrectPatterns) + ) + ); } return $result; }