Skip to content

Added copyright verification for graphqls files #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento2Framework\Sniffs\Header;

use Magento2\Sniffs\Less\TokenizerSymbolsInterface;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

Expand All @@ -23,7 +22,7 @@ class CopyrightAnotherExtensionsFilesSniff implements Sniff
*
* @var array
*/
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS, 'PHP'];
public $supportedTokenizers = ['CSS', 'PHP', 'JS'];

/**
* @inheritDoc
Expand All @@ -46,9 +45,10 @@ public function process(File $phpcsFile, $stackPtr)
}

$fileText = $phpcsFile->getTokensAsString($stackPtr, count($phpcsFile->getTokens()));
$adobeCopyrightFound = preg_match(self::COPYRIGHT_ADOBE, $fileText);

if (strpos($fileText, self::COPYRIGHT_MAGENTO_TEXT) !== false || $adobeCopyrightFound) {
if (strpos($fileText, self::COPYRIGHT_MAGENTO_TEXT) !== false
|| preg_match(self::COPYRIGHT_ADOBE, $fileText)
) {
return;
}

Expand Down
57 changes: 57 additions & 0 deletions Magento2Framework/Sniffs/Header/CopyrightGraphQLSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento2Framework\Sniffs\Header;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class CopyrightGraphQLSniff implements Sniff
{
private const WARNING_CODE = 'FoundCopyrightMissingOrWrongFormat';

private const COPYRIGHT_MAGENTO_TEXT = 'Copyright © Magento, Inc. All rights reserved.';
private const COPYRIGHT_ADOBE = '/Copyright \d+ Adobe/';

private const FILE_EXTENSION = 'graphqls';

/**
* @var string[]
*/
public $supportedTokenizers = ['GRAPHQL'];

/**
* @inheritdoc
*/
public function register()
{
return [T_OPEN_TAG];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false) {
return;
}

// @phpcs:ignore Magento2.Functions.DiscouragedFunction.Discouraged
$content = file_get_contents($phpcsFile->getFilename());

if (strpos($content, self::COPYRIGHT_MAGENTO_TEXT) !== false || preg_match(self::COPYRIGHT_ADOBE, $content)) {
return;
}

$phpcsFile->addWarningOnLine(
'Copyright is missing or has wrong format',
null,
self::WARNING_CODE
);
}
}
5 changes: 2 additions & 3 deletions Magento2Framework/Sniffs/Header/LicenseSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento2Framework\Sniffs\Header;

use Magento2\Sniffs\Less\TokenizerSymbolsInterface;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

Expand All @@ -18,7 +17,7 @@ class LicenseSniff implements Sniff
*
* @var array
*/
public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS, 'PHP'];
public $supportedTokenizers = ['CSS', 'PHP'];

private const WARNING_CODE = 'FoundLegacyTextInCopyright';

Expand Down Expand Up @@ -49,7 +48,7 @@ public function process(File $phpcsFile, $stackPtr)
if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) {
$content = $phpcsFile->getTokensAsString($stackPtr, 1);
}
if ($content != null) {
if ($content !== null) {
$this->checkLicense($content, $stackPtr, $phpcsFile);
}
}
Expand Down
34 changes: 34 additions & 0 deletions Magento2Framework/Tests/Header/CopyrightGraphQLUnitTest.1.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

enum PriceAdjustmentCodesEnum {
WEEE @deprecated(reason: "WEEE code is deprecated, use fixed_product_taxes.label")
WEEE_TAX @deprecated(reason: "Use fixed_product_taxes. PriceAdjustmentCodesEnum is deprecated. Tax is included or excluded in price. Tax is not shown separtely in Catalog")
}

type ProductPrice {
fixed_product_taxes: [FixedProductTax] @doc(description: "The multiple FPTs that can be applied to a product price.") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\FixedProductTax")
}

type CartItemPrices {
fixed_product_taxes: [FixedProductTax] @doc(description: "Applied FPT to the cart item.") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\Quote\\FixedProductTax")
}

type FixedProductTax @doc(description: "A single FPT that can be applied to a product price.") {
amount: Money @doc(description: "Amount of the FPT as a money object.")
label: String @doc(description: "The label assigned to the FPT to be displayed on the frontend.")
}

type StoreConfig {
product_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices On Product View Page' field. It indicates how FPT information is displayed on product pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
category_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices In Product Lists' field. It indicates how FPT information is displayed on category pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
sales_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices In Sales Modules' field. It indicates how FPT information is displayed on cart, checkout, and order pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
}

enum FixedProductTaxDisplaySettings @doc(description: "This enumeration display settings for the fixed product tax") {
INCLUDE_FPT_WITHOUT_DETAILS @doc(description: "The displayed price includes the FPT amount without displaying the ProductPrice.fixed_product_taxes values. This value corresponds to 'Including FPT only'")
INCLUDE_FPT_WITH_DETAILS @doc(description: "The displayed price includes the FPT amount while displaying the values of ProductPrice.fixed_product_taxes separately. This value corresponds to 'Including FPT and FPT description'")
EXCLUDE_FPT_AND_INCLUDE_WITH_DETAILS @doc(description: "The displayed price does not include the FPT amount. The values of ProductPrice.fixed_product_taxes and the price including the FPT are displayed separately. This value corresponds to 'Excluding FPT, Including FPT description and final price'")
EXCLUDE_FPT_WITHOUT_DETAILS @doc(description: "The displayed price does not include the FPT amount. The values from ProductPrice.fixed_product_taxes are not displayed. This value corresponds to 'Excluding FPT'")
FPT_DISABLED @doc(description: "The FPT feature is not enabled. You can omit ProductPrice.fixed_product_taxes from your query")
}
34 changes: 34 additions & 0 deletions Magento2Framework/Tests/Header/CopyrightGraphQLUnitTest.2.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2020 Adobe
# See COPYING.txt for license details.

enum PriceAdjustmentCodesEnum {
WEEE @deprecated(reason: "WEEE code is deprecated, use fixed_product_taxes.label")
WEEE_TAX @deprecated(reason: "Use fixed_product_taxes. PriceAdjustmentCodesEnum is deprecated. Tax is included or excluded in price. Tax is not shown separtely in Catalog")
}

type ProductPrice {
fixed_product_taxes: [FixedProductTax] @doc(description: "The multiple FPTs that can be applied to a product price.") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\FixedProductTax")
}

type CartItemPrices {
fixed_product_taxes: [FixedProductTax] @doc(description: "Applied FPT to the cart item.") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\Quote\\FixedProductTax")
}

type FixedProductTax @doc(description: "A single FPT that can be applied to a product price.") {
amount: Money @doc(description: "Amount of the FPT as a money object.")
label: String @doc(description: "The label assigned to the FPT to be displayed on the frontend.")
}

type StoreConfig {
product_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices On Product View Page' field. It indicates how FPT information is displayed on product pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
category_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices In Product Lists' field. It indicates how FPT information is displayed on category pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
sales_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices In Sales Modules' field. It indicates how FPT information is displayed on cart, checkout, and order pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
}

enum FixedProductTaxDisplaySettings @doc(description: "This enumeration display settings for the fixed product tax") {
INCLUDE_FPT_WITHOUT_DETAILS @doc(description: "The displayed price includes the FPT amount without displaying the ProductPrice.fixed_product_taxes values. This value corresponds to 'Including FPT only'")
INCLUDE_FPT_WITH_DETAILS @doc(description: "The displayed price includes the FPT amount while displaying the values of ProductPrice.fixed_product_taxes separately. This value corresponds to 'Including FPT and FPT description'")
EXCLUDE_FPT_AND_INCLUDE_WITH_DETAILS @doc(description: "The displayed price does not include the FPT amount. The values of ProductPrice.fixed_product_taxes and the price including the FPT are displayed separately. This value corresponds to 'Excluding FPT, Including FPT description and final price'")
EXCLUDE_FPT_WITHOUT_DETAILS @doc(description: "The displayed price does not include the FPT amount. The values from ProductPrice.fixed_product_taxes are not displayed. This value corresponds to 'Excluding FPT'")
FPT_DISABLED @doc(description: "The FPT feature is not enabled. You can omit ProductPrice.fixed_product_taxes from your query")
}
34 changes: 34 additions & 0 deletions Magento2Framework/Tests/Header/CopyrightGraphQLUnitTest.3.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2020 Magento
# See COPYING.txt for license details.

enum PriceAdjustmentCodesEnum {
WEEE @deprecated(reason: "WEEE code is deprecated, use fixed_product_taxes.label")
WEEE_TAX @deprecated(reason: "Use fixed_product_taxes. PriceAdjustmentCodesEnum is deprecated. Tax is included or excluded in price. Tax is not shown separtely in Catalog")
}

type ProductPrice {
fixed_product_taxes: [FixedProductTax] @doc(description: "The multiple FPTs that can be applied to a product price.") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\FixedProductTax")
}

type CartItemPrices {
fixed_product_taxes: [FixedProductTax] @doc(description: "Applied FPT to the cart item.") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\Quote\\FixedProductTax")
}

type FixedProductTax @doc(description: "A single FPT that can be applied to a product price.") {
amount: Money @doc(description: "Amount of the FPT as a money object.")
label: String @doc(description: "The label assigned to the FPT to be displayed on the frontend.")
}

type StoreConfig {
product_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices On Product View Page' field. It indicates how FPT information is displayed on product pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
category_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices In Product Lists' field. It indicates how FPT information is displayed on category pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
sales_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices In Sales Modules' field. It indicates how FPT information is displayed on cart, checkout, and order pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
}

enum FixedProductTaxDisplaySettings @doc(description: "This enumeration display settings for the fixed product tax") {
INCLUDE_FPT_WITHOUT_DETAILS @doc(description: "The displayed price includes the FPT amount without displaying the ProductPrice.fixed_product_taxes values. This value corresponds to 'Including FPT only'")
INCLUDE_FPT_WITH_DETAILS @doc(description: "The displayed price includes the FPT amount while displaying the values of ProductPrice.fixed_product_taxes separately. This value corresponds to 'Including FPT and FPT description'")
EXCLUDE_FPT_AND_INCLUDE_WITH_DETAILS @doc(description: "The displayed price does not include the FPT amount. The values of ProductPrice.fixed_product_taxes and the price including the FPT are displayed separately. This value corresponds to 'Excluding FPT, Including FPT description and final price'")
EXCLUDE_FPT_WITHOUT_DETAILS @doc(description: "The displayed price does not include the FPT amount. The values from ProductPrice.fixed_product_taxes are not displayed. This value corresponds to 'Excluding FPT'")
FPT_DISABLED @doc(description: "The FPT feature is not enabled. You can omit ProductPrice.fixed_product_taxes from your query")
}
31 changes: 31 additions & 0 deletions Magento2Framework/Tests/Header/CopyrightGraphQLUnitTest.4.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
enum PriceAdjustmentCodesEnum {
WEEE @deprecated(reason: "WEEE code is deprecated, use fixed_product_taxes.label")
WEEE_TAX @deprecated(reason: "Use fixed_product_taxes. PriceAdjustmentCodesEnum is deprecated. Tax is included or excluded in price. Tax is not shown separtely in Catalog")
}

type ProductPrice {
fixed_product_taxes: [FixedProductTax] @doc(description: "The multiple FPTs that can be applied to a product price.") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\FixedProductTax")
}

type CartItemPrices {
fixed_product_taxes: [FixedProductTax] @doc(description: "Applied FPT to the cart item.") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\Quote\\FixedProductTax")
}

type FixedProductTax @doc(description: "A single FPT that can be applied to a product price.") {
amount: Money @doc(description: "Amount of the FPT as a money object.")
label: String @doc(description: "The label assigned to the FPT to be displayed on the frontend.")
}

type StoreConfig {
product_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices On Product View Page' field. It indicates how FPT information is displayed on product pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
category_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices In Product Lists' field. It indicates how FPT information is displayed on category pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
sales_fixed_product_tax_display_setting : FixedProductTaxDisplaySettings @doc(description: "Corresponds to the 'Display Prices In Sales Modules' field. It indicates how FPT information is displayed on cart, checkout, and order pages") @resolver(class: "Magento\\WeeeGraphQl\\Model\\Resolver\\StoreConfig")
}

enum FixedProductTaxDisplaySettings @doc(description: "This enumeration display settings for the fixed product tax") {
INCLUDE_FPT_WITHOUT_DETAILS @doc(description: "The displayed price includes the FPT amount without displaying the ProductPrice.fixed_product_taxes values. This value corresponds to 'Including FPT only'")
INCLUDE_FPT_WITH_DETAILS @doc(description: "The displayed price includes the FPT amount while displaying the values of ProductPrice.fixed_product_taxes separately. This value corresponds to 'Including FPT and FPT description'")
EXCLUDE_FPT_AND_INCLUDE_WITH_DETAILS @doc(description: "The displayed price does not include the FPT amount. The values of ProductPrice.fixed_product_taxes and the price including the FPT are displayed separately. This value corresponds to 'Excluding FPT, Including FPT description and final price'")
EXCLUDE_FPT_WITHOUT_DETAILS @doc(description: "The displayed price does not include the FPT amount. The values from ProductPrice.fixed_product_taxes are not displayed. This value corresponds to 'Excluding FPT'")
FPT_DISABLED @doc(description: "The FPT feature is not enabled. You can omit ProductPrice.fixed_product_taxes from your query")
}
37 changes: 37 additions & 0 deletions Magento2Framework/Tests/Header/CopyrightGraphQLUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2Framework\Tests\Header;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class CopyrightGraphQLUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList(): array
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList($testFile = ''): array
{
if ($testFile === 'CopyrightGraphQLUnitTest.1.inc' || $testFile === 'CopyrightGraphQLUnitTest.2.inc') {
return [];
}

if ($testFile === 'CopyrightGraphQLUnitTest.3.inc' || $testFile === 'CopyrightGraphQLUnitTest.4.inc') {
return [
1 => 1
];
}

return [];
}
}
12 changes: 12 additions & 0 deletions Magento2Framework/ruleset.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0"?>
<ruleset name="Magento2Framework">
<description>Magento Coding Standard sniffs applicable for the framework testing only</description>

<arg name="extensions" value="php,phtml,graphqls/GRAPHQL,less/CSS,html/CSS,css/CSS,xml,js/JS"/>

<rule ref="Magento2Framework.Header.License">
<severity>5</severity>
<type>warning</type>
Expand All @@ -17,4 +20,13 @@
<include-pattern>*\.php$</include-pattern>
<include-pattern>*\.phtml$</include-pattern>
</rule>
<rule ref="Magento2Framework.Header.CopyrightGraphQL">
<severity>5</severity>
<type>warning</type>
<include-pattern>*\.graphqls$</include-pattern>
</rule>

<rule ref="Internal.NoCodeFound">
<severity>0</severity>
</rule>
</ruleset>