Skip to content

AC-666: Copyright test for another extensions files #269

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
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
52 changes: 52 additions & 0 deletions Magento2/Sniffs/Legacy/CopyrightAnotherExtensionsFilesSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento2\Sniffs\Legacy;

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

class CopyrightAnotherExtensionsFilesSniff 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/';

/**
* @inheritDoc
*/
public function register(): array
{
return [
T_INLINE_HTML
];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($stackPtr > 0) {
return;
}

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

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

$phpcsFile->addWarningOnLine(
'Copyright is missing or has wrong format',
null,
self::WARNING_CODE
);
}
}
33 changes: 33 additions & 0 deletions Magento2/Tests/Legacy/AbstractJsSniffUnitTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Implements an abstract base for unit tests that cover less sniffs.
*/
abstract class AbstractJsSniffUnitTestCase extends AbstractSniffUnitTest
{
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();

$config = new Config();
$config->extensions = array_merge(
$config->extensions,
[
'js' => 'PHP'
]
);

$GLOBALS['PHP_CODESNIFFER_CONFIG'] = $config;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights another text to test.
* See COPYING.txt for license details.
*/
-->
<tag>

</tag>
10 changes: 10 additions & 0 deletions Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright Adobe.
* See COPYING.txt for license details.
*/

define([
'jquery'
], function (){

});
10 changes: 10 additions & 0 deletions Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'jquery'
], function (){

});
41 changes: 41 additions & 0 deletions Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

class CopyrightAnotherExtensionsFilesUnitTest extends AbstractJsSniffUnitTestCase
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList($testFile = '')
{
if ($testFile === 'CopyrightAnotherExtensionsFilesUnitTest.1.xml') {
return [
null => 1,
];
}
if ($testFile === 'CopyrightAnotherExtensionsFilesUnitTest.2.js') {
return [
null => 1,
];
}
if ($testFile === 'CopyrightAnotherExtensionsFilesUnitTest.3.xml') {
return [];
}
if ($testFile === 'CopyrightAnotherExtensionsFilesUnitTest.4.js') {
return [];
}
return [];
}
}
10 changes: 10 additions & 0 deletions Magento2/Tests/Legacy/CopyrightAnotherExtensionsFilesUnitTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright 2020 Adobe
* See COPYING.txt for license details.
*/
-->
<tag>

</tag>
6 changes: 5 additions & 1 deletion Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<description>Magento Coding Standard</description>

<!-- File extensions to be checked. -->
<arg name="extensions" value="php,phtml,graphqls/GraphQL,less/CSS,html/PHP,xml"/>
<arg name="extensions" value="php,phtml,graphqls/GraphQL,less/CSS,html/PHP,xml,js/PHP"/>

<!-- Severity 10 errors: Critical code issues. -->
<rule ref="Generic.Functions.CallTimePassByReference">
Expand Down Expand Up @@ -634,6 +634,10 @@
<exclude-pattern>*/Test/*</exclude-pattern>
<exclude-pattern>*Test.php</exclude-pattern>
</rule>
<rule ref="Magento2.Legacy.CopyrightAnotherExtensionsFiles">
<severity>5</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.Copyright">
<severity>5</severity>
<type>warning</type>
Expand Down