diff options
Diffstat (limited to 'tests')
19 files changed, 428 insertions, 13 deletions
diff --git a/tests/data/app/code-checker/test-deprecated-constant-alias.php b/tests/data/app/code-checker/test-deprecated-constant-alias.php new file mode 100644 index 00000000000..b5a5bfdb762 --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-constant-alias.php @@ -0,0 +1,8 @@ +<?php + +use OCP\NamespaceName\ClassName as Alias; + +/** + * Class BadClass - creating an instance of a blacklisted class is not allowed + */ +Alias::CONSTANT_NAME; diff --git a/tests/data/app/code-checker/test-deprecated-constant-sub-alias.php b/tests/data/app/code-checker/test-deprecated-constant-sub-alias.php new file mode 100644 index 00000000000..9b1757aa683 --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-constant-sub-alias.php @@ -0,0 +1,8 @@ +<?php + +use OCP\NamespaceName as SubAlias; + +/** + * Class BadClass - creating an instance of a blacklisted class is not allowed + */ +SubAlias\ClassName::CONSTANT_NAME; diff --git a/tests/data/app/code-checker/test-deprecated-constant-sub.php b/tests/data/app/code-checker/test-deprecated-constant-sub.php new file mode 100644 index 00000000000..86e0ff52efe --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-constant-sub.php @@ -0,0 +1,8 @@ +<?php + +use OCP\NamespaceName; + +/** + * Class BadClass - creating an instance of a blacklisted class is not allowed + */ +NamespaceName\ClassName::CONSTANT_NAME; diff --git a/tests/data/app/code-checker/test-deprecated-constant.php b/tests/data/app/code-checker/test-deprecated-constant.php new file mode 100644 index 00000000000..170b1d9e5ad --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-constant.php @@ -0,0 +1,6 @@ +<?php + +/** + * Class BadClass - creating an instance of a blacklisted class is not allowed + */ +\OCP\NamespaceName\ClassName::CONSTANT_NAME; diff --git a/tests/data/app/code-checker/test-deprecated-function-alias.php b/tests/data/app/code-checker/test-deprecated-function-alias.php new file mode 100644 index 00000000000..28ed5051191 --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-function-alias.php @@ -0,0 +1,6 @@ +<?php + +use OCP\NamespaceName\ClassName as Alias; + +Alias::functionName(); +Alias::methodName(); diff --git a/tests/data/app/code-checker/test-deprecated-function-sub-alias.php b/tests/data/app/code-checker/test-deprecated-function-sub-alias.php new file mode 100644 index 00000000000..73dd5814531 --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-function-sub-alias.php @@ -0,0 +1,6 @@ +<?php + +use OCP\NamespaceName as SubAlias; + +SubAlias\ClassName::functionName(); +SubAlias\ClassName::methodName(); diff --git a/tests/data/app/code-checker/test-deprecated-function-sub.php b/tests/data/app/code-checker/test-deprecated-function-sub.php new file mode 100644 index 00000000000..c08d3bad8c0 --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-function-sub.php @@ -0,0 +1,6 @@ +<?php + +use OCP\NamespaceName; + +NamespaceName\ClassName::functionName(); +NamespaceName\ClassName::methodName(); diff --git a/tests/data/app/code-checker/test-deprecated-function.php b/tests/data/app/code-checker/test-deprecated-function.php new file mode 100644 index 00000000000..12a144a7118 --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-function.php @@ -0,0 +1,4 @@ +<?php + +\OCP\NamespaceName\ClassName::functionName(); +\OCP\NamespaceName\ClassName::methodName(); diff --git a/tests/data/app/code-checker/test-deprecated-method.php b/tests/data/app/code-checker/test-deprecated-method.php new file mode 100644 index 00000000000..ee2fdb642d4 --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-method.php @@ -0,0 +1,5 @@ +<?php + +$class = new \OCP\NamespaceName\ClassName(); +$class->methodName(); +$class::methodName(); diff --git a/tests/data/app/code-checker/test-deprecated-use-alias.php b/tests/data/app/code-checker/test-deprecated-use-alias.php new file mode 100644 index 00000000000..a92187fa880 --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-use-alias.php @@ -0,0 +1,9 @@ +<?php + +use OCP\AppFramework\IApi as OAFIA; + +/** + * Class BadClass - creating an instance of a blacklisted class is not allowed + */ +class BadClass implements OAFIA { +} diff --git a/tests/data/app/code-checker/test-deprecated-use-sub-alias.php b/tests/data/app/code-checker/test-deprecated-use-sub-alias.php new file mode 100644 index 00000000000..9da4b75d216 --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-use-sub-alias.php @@ -0,0 +1,9 @@ +<?php + +use OCP\AppFramework as OAF; + +/** + * Class BadClass - creating an instance of a blacklisted class is not allowed + */ +class BadClass implements OAF\IApi { +} diff --git a/tests/data/app/code-checker/test-deprecated-use-sub.php b/tests/data/app/code-checker/test-deprecated-use-sub.php new file mode 100644 index 00000000000..a53e9a7229e --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-use-sub.php @@ -0,0 +1,9 @@ +<?php + +use OCP\AppFramework; + +/** + * Class BadClass - creating an instance of a blacklisted class is not allowed + */ +class BadClass implements AppFramework\IApi { +} diff --git a/tests/data/app/code-checker/test-deprecated-use.php b/tests/data/app/code-checker/test-deprecated-use.php new file mode 100644 index 00000000000..ebf2c90bc5a --- /dev/null +++ b/tests/data/app/code-checker/test-deprecated-use.php @@ -0,0 +1,9 @@ +<?php + +use OCP\AppFramework\IApi; + +/** + * Class BadClass - creating an instance of a blacklisted class is not allowed + */ +class BadClass implements IApi { +} diff --git a/tests/data/app/code-checker/test-use.php b/tests/data/app/code-checker/test-use.php new file mode 100644 index 00000000000..9c4824f9767 --- /dev/null +++ b/tests/data/app/code-checker/test-use.php @@ -0,0 +1,12 @@ +<?php + +use OC_AppConfig as UseConfig; + +/** + * Class BadClass - creating an instance of a blacklisted class is not allowed + */ +class BadClass { + public function foo() { + $bar = new UseConfig(); + } +} diff --git a/tests/lib/app/codechecker.php b/tests/lib/app/codechecker/codecheckertest.php index f45ee02d185..cdbb7c17da5 100644 --- a/tests/lib/app/codechecker.php +++ b/tests/lib/app/codechecker/codecheckertest.php @@ -6,22 +6,26 @@ * See the COPYING-README file. */ -namespace Test\App; +namespace Test\App\CodeChecker; -use OC; +use OC\App\CodeChecker\CodeChecker; +use OC\App\CodeChecker\EmptyCheck; +use OC\App\CodeChecker\PrivateCheck; use Test\TestCase; -class CodeChecker extends TestCase { +class CodeCheckerTest extends TestCase { /** * @dataProvider providesFilesToCheck - * @param $expectedErrorToken - * @param $expectedErrorCode - * @param $fileToVerify + * @param string $expectedErrorToken + * @param int $expectedErrorCode + * @param string $fileToVerify */ public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { - $checker = new OC\App\CodeChecker(); - $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + $checker = new CodeChecker( + new PrivateCheck(new EmptyCheck()) + ); + $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); $this->assertEquals(1, count($errors)); $this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); @@ -35,18 +39,19 @@ class CodeChecker extends TestCase { ['OC_App', 1002, 'test-static-call.php'], ['OC_API', 1003, 'test-const.php'], ['OC_AppConfig', 1004, 'test-new.php'], - ['==', 1005, 'test-equal.php'], - ['!=', 1005, 'test-not-equal.php'], + ['OC_AppConfig', 1006, 'test-use.php'], ]; } /** * @dataProvider validFilesData - * @param $fileToVerify + * @param string $fileToVerify */ public function testPassValidUsage($fileToVerify) { - $checker = new OC\App\CodeChecker(); - $errors = $checker->analyseFile(OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + $checker = new CodeChecker( + new PrivateCheck(new EmptyCheck()) + ); + $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); $this->assertEquals(0, count($errors)); } diff --git a/tests/lib/app/codechecker/deprecationchecktest.php b/tests/lib/app/codechecker/deprecationchecktest.php new file mode 100644 index 00000000000..2cf64a9d186 --- /dev/null +++ b/tests/lib/app/codechecker/deprecationchecktest.php @@ -0,0 +1,70 @@ +<?php +/** + * Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\App\CodeChecker; + +use OC\App\CodeChecker\CodeChecker; +use OC\App\CodeChecker\DeprecationCheck; +use OC\App\CodeChecker\EmptyCheck; +use Test\TestCase; + +class DeprecationCheckTest extends TestCase { + + /** + * @dataProvider providesFilesToCheck + * @param string $expectedErrorToken + * @param int $expectedErrorCode + * @param string $fileToVerify + */ + public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { + $checker = new CodeChecker( + new DeprecationCheck(new EmptyCheck()) + ); + $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(1, count($errors)); + $this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); + $this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']); + } + + public function providesFilesToCheck() { + return [ + ['OCP\AppFramework\IApi', 1006, 'test-deprecated-use.php'], + ['OCP\AppFramework\IApi', 1006, 'test-deprecated-use-alias.php'], + ['AppFramework\IApi', 1001, 'test-deprecated-use-sub.php'], + ['OAF\IApi', 1001, 'test-deprecated-use-sub-alias.php'], + ]; + } + + /** + * @dataProvider validFilesData + * @param string $fileToVerify + */ + public function testPassValidUsage($fileToVerify) { + $checker = new CodeChecker( + new DeprecationCheck(new EmptyCheck()) + ); + $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(0, count($errors)); + } + + public function validFilesData() { + return [ + ['test-equal.php'], + ['test-not-equal.php'], + ['test-extends.php'], + ['test-implements.php'], + ['test-static-call.php'], + ['test-const.php'], + ['test-new.php'], + ['test-use.php'], + ['test-identical-operator.php'], + ]; + } +} diff --git a/tests/lib/app/codechecker/mock/testlist.php b/tests/lib/app/codechecker/mock/testlist.php new file mode 100644 index 00000000000..1fe83293acf --- /dev/null +++ b/tests/lib/app/codechecker/mock/testlist.php @@ -0,0 +1,92 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace Test\App\CodeChecker\Mock; + +use OC\App\CodeChecker\ICheck; + +class TestList implements ICheck { + /** @var ICheck */ + protected $check; + + /** + * @param ICheck $check + */ + public function __construct(ICheck $check) { + $this->check = $check; + } + + /** + * @param int $errorCode + * @param string $errorObject + * @return string + */ + public function getDescription($errorCode, $errorObject) { + return 'testing'; + } + + /** + * @return array E.g.: `'ClassName' => 'oc version',` + */ + public function getClasses() { + return [ + // Deprecated classes + 'OCP\AppFramework\IApi' => '8.0.0', + ]; + } + + /** + * @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',` + */ + public function getConstants() { + return [ + // Deprecated constants + 'OCP\NamespaceName\ClassName::CONSTANT_NAME' => '8.0.0', + ]; + } + + /** + * @return array E.g.: `'functionName' => 'oc version',` + */ + public function getFunctions() { + return [ + // Deprecated functions + 'OCP\NamespaceName\ClassName::functionName' => '8.0.0', + ]; + } + + /** + * @return array E.g.: `'ClassName::methodName' => 'oc version',` + */ + public function getMethods() { + return [ + // Deprecated methods + 'OCP\NamespaceName\ClassName::methodName' => '8.0.0', + ]; + } + + /** + * @return bool + */ + public function checkStrongComparisons() { + return true; + } +} diff --git a/tests/lib/app/codechecker/nodevisitortest.php b/tests/lib/app/codechecker/nodevisitortest.php new file mode 100644 index 00000000000..0b5aea28324 --- /dev/null +++ b/tests/lib/app/codechecker/nodevisitortest.php @@ -0,0 +1,73 @@ +<?php +/** + * Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\AppCodeChecker; + +use OC\App\CodeChecker\CodeChecker; +use OC\App\CodeChecker\EmptyCheck; +use Test\App\CodeChecker\Mock\TestList; +use Test\TestCase; + +class NodeVisitorTest extends TestCase { + + public function providesFilesToCheck() { + return [ + [[['OCP\AppFramework\IApi', 1006]], 'test-deprecated-use.php'], + [[['OCP\AppFramework\IApi', 1006]], 'test-deprecated-use-alias.php'], + [[['AppFramework\IApi', 1001]], 'test-deprecated-use-sub.php'], + [[['OAF\IApi', 1001]], 'test-deprecated-use-sub-alias.php'], + + [[['OCP\NamespaceName\ClassName::CONSTANT_NAME', 1003]], 'test-deprecated-constant.php'], + [[['Alias::CONSTANT_NAME', 1003]], 'test-deprecated-constant-alias.php'], + [[['NamespaceName\ClassName::CONSTANT_NAME', 1003]], 'test-deprecated-constant-sub.php'], + [[['SubAlias\ClassName::CONSTANT_NAME', 1003]], 'test-deprecated-constant-sub-alias.php'], + + [[ + ['OCP\NamespaceName\ClassName::functionName', 1002], + ['OCP\NamespaceName\ClassName::methodName', 1007], + ], 'test-deprecated-function.php'], + [[ + ['Alias::functionName', 1002], + ['Alias::methodName', 1007], + ], 'test-deprecated-function-alias.php'], + [[ + ['NamespaceName\ClassName::functionName', 1002], + ['NamespaceName\ClassName::methodName', 1007], + ], 'test-deprecated-function-sub.php'], + [[ + ['SubAlias\ClassName::functionName', 1002], + ['SubAlias\ClassName::methodName', 1007], + ], 'test-deprecated-function-sub-alias.php'], + + // TODO Failing to resolve variables to classes +// [[['OCP\NamespaceName\ClassName::methodName', 1007]], 'test-deprecated-method.php'], +// [[['Alias::methodName', 1002]], 'test-deprecated-method-alias.php'], +// [[['NamespaceName\ClassName::methodName', 1002]], 'test-deprecated-method-sub.php'], +// [[['SubAlias\ClassName::methodName', 1002]], 'test-deprecated-method-sub-alias.php'], + ]; + } + + /** + * @dataProvider providesFilesToCheck + * @param array $expectedErrors + * @param string $fileToVerify + */ + public function testMethodsToCheck($expectedErrors, $fileToVerify) { + $checker = new CodeChecker( + new TestList(new EmptyCheck()) + ); + $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertCount(sizeof($expectedErrors), $errors); + + foreach ($expectedErrors as $int => $expectedError) { + $this->assertEquals($expectedError[0], $errors[$int]['disallowedToken']); + $this->assertEquals($expectedError[1], $errors[$int]['errorCode']); + } + } +} diff --git a/tests/lib/app/codechecker/strongcomparisonchecktest.php b/tests/lib/app/codechecker/strongcomparisonchecktest.php new file mode 100644 index 00000000000..c73eae286ab --- /dev/null +++ b/tests/lib/app/codechecker/strongcomparisonchecktest.php @@ -0,0 +1,70 @@ +<?php +/** + * Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\App\CodeChecker; + +use OC\App\CodeChecker\CodeChecker; +use OC\App\CodeChecker\EmptyCheck; +use OC\App\CodeChecker\StrongComparisonCheck; +use Test\TestCase; + +class StrongComparisonCheckTest extends TestCase { + + /** + * @dataProvider providesFilesToCheck + * @param string $expectedErrorToken + * @param int $expectedErrorCode + * @param string $fileToVerify + */ + public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { + $checker = new CodeChecker( + new StrongComparisonCheck(new EmptyCheck()) + ); + $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(1, count($errors)); + $this->assertEquals($expectedErrorCode, $errors[0]['errorCode']); + $this->assertEquals($expectedErrorToken, $errors[0]['disallowedToken']); + } + + public function providesFilesToCheck() { + return [ + ['==', 1005, 'test-equal.php'], + ['!=', 1005, 'test-not-equal.php'], + ]; + } + + /** + * @dataProvider validFilesData + * @param string $fileToVerify + */ + public function testPassValidUsage($fileToVerify) { + $checker = new CodeChecker( + new StrongComparisonCheck(new EmptyCheck()) + ); + $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); + + $this->assertEquals(0, count($errors)); + } + + public function validFilesData() { + return [ + ['test-deprecated-use.php'], + ['test-deprecated-use-alias.php'], + ['test-deprecated-use-sub.php'], + ['test-deprecated-use-sub-alias.php'], + ['test-extends.php'], + ['test-implements.php'], + ['test-static-call.php'], + ['test-const.php'], + ['test-new.php'], + ['test-use.php'], + ['test-identical-operator.php'], + ]; + } +} |