Skip to content

Commit 2abe44c

Browse files
authored
Merge pull request #49 from stronk7/enable_skipped_tests
Enable skipped tests
2 parents 2cae0e6 + 5a9d329 commit 2abe44c

23 files changed

+1801
-77
lines changed

moodle/Sniffs/Commenting/InlineCommentSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ public function process(File $phpcsFile, $stackPtr) {
406406

407407
if ($commentText === '') {
408408
$error = 'Blank comments are not allowed';
409-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty');
409+
$fix = $phpcsFile->addFixableError($error, $lastCommentToken, 'Empty');
410410
if ($fix === true) {
411-
$phpcsFile->fixer->replaceToken($stackPtr, '');
411+
$phpcsFile->fixer->replaceToken($lastCommentToken, '');
412412
}
413413

414414
return ($lastCommentToken + 1);

moodle/Sniffs/PHP/IncludingFileSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function process(File $file, $stackptr) {
4646
if ($tokens[$stackptr + 1]['code'] !== T_OPEN_PARENTHESIS) {
4747
$error = '"%s" must be immediately followed by an open parenthesis';
4848
$data = array($tokens[$stackptr]['content']);
49-
$file->addError($error, $stackptr, 'BracketsNotRequired', $data);
49+
$file->addError($error, $stackptr, 'BracketsRequired', $data);
5050
}
5151

5252
$incondition = (count($tokens[$stackptr]['conditions']) !== 0) ? true : false;

moodle/Tests/MoodleStandardTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public function test_moodle_commenting_inlinecomment() {
106106
135 => 0,
107107
136 => 0,
108108
137 => 0,
109+
143 => '@Message: Tab found before comment',
110+
144 => '@Source: moodle.Commenting.InlineComment.SpacingBefore',
111+
146 => '@Message: Blank comments are not allowed',
109112
]);
110113
$this->set_warnings([
111114
4 => 0,

moodle/Tests/MoodleUtilTest.php

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ public function test_calculateAllComponents() {
5757
"plugin,local_codechecker,{$moodleRoot}/local/codechecker\n" .// All ok.
5858
"plugin,mod_forum,{$moodleRoot}/mod/forum\n"; // All ok.
5959

60-
vfsStream::create([
61-
'components.txt' => $components,
62-
]);
60+
vfsStream::create(
61+
['components.txt' => $components,],
62+
$vfs
63+
);
6364

6465
// Set codechecker config to point to it.
6566
Config::setConfigData('moodleComponentsListPath', $vfs->url() . '/components.txt', true);
@@ -108,42 +109,42 @@ public function getMoodleComponentProvider() {
108109
'return' => ['value' => null],
109110
'reset' => true,
110111
'selfPath' => false,
111-
'requireRealMoodle' => false,
112+
'requireMockMoodle' => false,
112113
],
113114
'moodleComponent_file_without_component_class' => [
114115
'config' => ['file' => dirname(__FILE__) . '/fixtures/moodleutil/good/lib/lib.php'],
115116
'return' => ['value' => null],
116117
'reset' => true,
117118
'selfPath' => false,
118-
'requireRealMoodle' => false,
119+
'requireMockMoodle' => false,
119120
],
120121
'moodleComponent_file_valid' => [
121-
'config' => ['file' => __FILE__],
122-
'return' => ['value' => 'local_codechecker'],
122+
'config' => ['file' => 'local/invented/lib.php'],
123+
'return' => ['value' => 'local_invented'],
123124
'reset' => false, // Prevent resetting cached information to verify next works.
124125
'selfPath' => false,
125-
'requireRealMoodle' => true,
126+
'requireMockMoodle' => true,
126127
],
127128
'moodleComponent_file_already_cached' => [
128-
'config' => ['file' => dirname(__FILE__) . '/fixtures/moodleutil/good/lib/lib.php'],
129-
'return' => ['value' => 'local_codechecker'],
129+
'config' => ['file' => 'lib/lib.php'],
130+
'return' => ['value' => 'core'],
130131
'reset' => true,
131132
'selfPath' => false,
132-
'requireRealMoodle' => true,
133+
'requireMockMoodle' => true,
133134
],
134135
'moodleComponent_file_cache_cleaned' => [
135136
'config' => ['file' => dirname(__FILE__) . '/fixtures/moodleutil/good/lib/lib.php'],
136137
'return' => ['value' => null],
137138
'reset' => true,
138139
'selfPath' => false,
139-
'requireRealMoodle' => false,
140+
'requireMockMoodle' => false,
140141
],
141142
'moodleComponent_file_without_component' => [
142143
'config' => ['file' => dirname(__FILE__, 5) . '/userpix/index.php'],
143144
'return' => ['value' => null],
144145
'reset' => true,
145146
'selfPath' => false,
146-
'requireRealMoodle' => false,
147+
'requireMockMoodle' => false,
147148
],
148149
];
149150
}
@@ -155,7 +156,7 @@ public function getMoodleComponentProvider() {
155156
* @param array $return expected result of the test.
156157
* @param bool $reset to decide if static caches should be reset before the test.
157158
* @param bool $selfPath to decide if codechecker own path is good to find a valid moodle root.
158-
* @param bool $requireRealMoodle Whether a real Moodle root is required for this test.
159+
* @param bool $requireMockMoodle Whether a mock Moodle root is required for this test.
159160
*
160161
* @dataProvider getMoodleComponentProvider
161162
*/
@@ -164,10 +165,18 @@ public function test_getMoodleComponent(
164165
array $return,
165166
bool $reset = true,
166167
bool $selfPath = true,
167-
bool $requireRealMoodle = false
168+
bool $requireMockMoodle = false
168169
) {
169-
if ($requireRealMoodle) {
170+
if ($requireMockMoodle) {
171+
// We have to mock the passed moodleRoot.
172+
$vfs = vfsStream::setup('mocksite', null, []);
173+
vfsStream::copyFromFileSystem(__DIR__ . '/fixtures/moodleutil/complete', $vfs);
174+
$config['moodleRoot'] = $vfs->url(); // Let's add it to the standard config and immediately use it.
175+
Config::setConfigData('moodleRoot', $config['moodleRoot'], true);
170176
$this->requireRealMoodleRoot();
177+
178+
// Also, we need to set the config['file'] to point to the vfs one.
179+
$config['file'] = $vfs->url() . '/' . $config['file'];
171180
}
172181

173182
$file = null;
@@ -176,9 +185,9 @@ public function test_getMoodleComponent(
176185
foreach ($config as $key => $value) {
177186
if ($key === 'file') {
178187
// We are passing a real File, prepare it.
179-
$config = new Config();
180-
$ruleset = new Ruleset($config);
181-
$file = new File($value, $ruleset, $config);
188+
$phpcsConfig = new Config();
189+
$phpcsRuleset = new Ruleset($phpcsConfig);
190+
$file = new File($value, $phpcsRuleset, $phpcsConfig);
182191
} else {
183192
// Normal config.
184193
Config::setConfigData($key, $value, true);
@@ -208,9 +217,7 @@ public function test_getMoodleComponent(
208217
// We need to unset all config options when passed.
209218
if ($config) {
210219
foreach ($config as $key => $value) {
211-
if ($key !== 'file') {
212-
Config::setConfigData($key, null, true);
213-
}
220+
Config::setConfigData($key, null, true);
214221
}
215222
}
216223
}
@@ -274,9 +281,9 @@ public function test_getMoodleBranch(array $config, array $return, bool $reset =
274281
foreach ($config as $key => $value) {
275282
if ($key === 'file') {
276283
// We are passing a real File, prepare it.
277-
$config = new Config();
278-
$ruleset = new Ruleset($config);
279-
$file = new File($value, $ruleset, $config);
284+
$phpcsConfig = new Config();
285+
$phpcsRuleset = new Ruleset($phpcsConfig);
286+
$file = new File($value, $phpcsRuleset, $phpcsConfig);
280287
} else {
281288
// Normal config.
282289
Config::setConfigData($key, $value, true);
@@ -306,9 +313,7 @@ public function test_getMoodleBranch(array $config, array $return, bool $reset =
306313
// We need to unset all config options when passed.
307314
if ($config) {
308315
foreach ($config as $key => $value) {
309-
if ($key !== 'file') {
310-
Config::setConfigData($key, null, true);
311-
}
316+
Config::setConfigData($key, null, true);
312317
}
313318
}
314319
}
@@ -330,14 +335,14 @@ public function getMoodleRootProvider() {
330335
'requireRealMoodle' => false,
331336
],
332337
'moodleRoot_valid' => [
333-
'config' => ['moodleRoot' => dirname(__FILE__, 5)],
334-
'return' => ['value' => dirname(__FILE__, 5)],
338+
'config' => ['moodleRoot' => 'some_valid_moodle_root'],
339+
'return' => ['value' => 'some_valid_moodle_root'],
335340
'requireRealMoodle' => true,
336341
'reset' => false, // Prevent resetting cached information to verify next works.
337342
],
338343
'moodleRoot_already_cached' => [
339344
'config' => ['moodleRoot' => '/does/not/exist'],
340-
'return' => ['value' => dirname(__FILE__, 5)],
345+
'return' => ['value' => 'some_valid_moodle_root'],
341346
'requireRealMoodle' => true,
342347
],
343348
'moodleRoot_cache_cleaned' => [ // Verify that previous has cleaned cached information.
@@ -358,11 +363,11 @@ public function getMoodleRootProvider() {
358363
'requireRealMoodle' => false,
359364
],
360365

361-
// Passing nothing, defaults to this file.
366+
// Passing nothing, defaults to this file, that leads to not valid moodle root.
362367
'moodleRoot_pass_nothing' => [
363368
'config' => [],
364-
'return' => ['value' => dirname(__FILE__, 5)],
365-
'requireRealMoodle' => true,
369+
'return' => ['value' => null],
370+
'requireRealMoodle' => false,
366371
],
367372
];
368373
}
@@ -373,20 +378,33 @@ public function getMoodleRootProvider() {
373378
* @param array $config get the Config from provider.
374379
* @param array $return expected result of the test.
375380
* @param bool $reset to decide if static caches should be reset before the test.
376-
* @param bool $selfPath to decide if codechecker own path is good to find a valid moodle root.
377-
* @param bool $requireRealMoodle Whether a real Moodle root is required for this test.
381+
* @param bool $selfPath to decide if moodle-cs own path is good to find a valid moodle root.
382+
* @param bool $requireMockMoodle Whether a mock Moodle root is required for this test.
378383
*
379384
* @dataProvider getMoodleRootProvider
380385
*/
381386
public function test_getMoodleRoot(
382-
array $config,
383-
array $return,
384-
bool $requireRealMoodle = false,
385-
bool $reset = true,
386-
bool $selfPath = true
387+
array $config,
388+
array $return,
389+
bool $requireMockMoodle = false,
390+
bool $reset = true,
391+
bool $selfPath = true
387392
) {
388-
if ($requireRealMoodle) {
389-
$this->requireRealMoodleRoot();
393+
if ($requireMockMoodle) {
394+
if (isset($config['moodleRoot']) && isset($return['value'])) {
395+
// We have to mock the passed moodleRoot.
396+
$vfs = vfsStream::setup($config['moodleRoot'], null, [
397+
'version.php' => 'some version contents, not important for this test',
398+
'config-dist.php' => 'come config contents, not important for this test',
399+
]);
400+
$config['moodleRoot'] = $vfs->url(); // Let's add it to the standard config and immediately use it.
401+
Config::setConfigData('moodleRoot', $config['moodleRoot'], true);
402+
$this->requireRealMoodleRoot();
403+
404+
// We also have to mock the passed expectation for the test.
405+
$returnVfs = vfsStream::setup($return['value'], null, []);
406+
$return['value'] = $returnVfs->url();
407+
}
390408
}
391409

392410
$file = null;
@@ -395,9 +413,9 @@ public function test_getMoodleRoot(
395413
foreach ($config as $key => $value) {
396414
if ($key === 'file') {
397415
// We are passing a real File, prepare it.
398-
$config = new Config();
399-
$ruleset = new Ruleset($config);
400-
$file = new File($value, $ruleset, $config);
416+
$phpcsConfig = new Config();
417+
$phpcsRuleset = new Ruleset($phpcsConfig);
418+
$file = new File($value, $phpcsRuleset, $phpcsConfig);
401419
} else {
402420
// Normal config.
403421
Config::setConfigData($key, $value, true);
@@ -427,9 +445,7 @@ public function test_getMoodleRoot(
427445
// We need to unset all config options when passed.
428446
if ($config) {
429447
foreach ($config as $key => $value) {
430-
if ($key !== 'file') {
431-
Config::setConfigData($key, null, true);
432-
}
448+
Config::setConfigData($key, null, true);
433449
}
434450
}
435451
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace MoodleHQ\MoodleCS\moodle\Tests;
18+
19+
// phpcs:disable moodle.NamingConventions
20+
21+
/**
22+
* Test the IncludingFile sniff.
23+
*
24+
* @package local_codechecker
25+
* @category test
26+
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*
29+
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\PHP\IncludingFileSniff
30+
*/
31+
class PHPIncludingFileTest extends MoodleCSBaseTest {
32+
33+
public function test_php_includingfile() {
34+
// Define the standard, sniff and fixture to use.
35+
$this->set_standard('moodle');
36+
$this->set_sniff('moodle.PHP.IncludingFile');
37+
$this->set_fixture(__DIR__ . '/fixtures/php/includingfile.php');
38+
39+
// Define expected results (errors and warnings). Format, array of:
40+
// - line => number of problems, or
41+
// - line => array of contents for message / source problem matching.
42+
// - line => string of contents for message / source problem matching (only 1).
43+
$this->set_errors([
44+
9 => '@Message: "require" must be immediately followed by an open parenthesis',
45+
10 => '@Source: moodle.PHP.IncludingFile.BracketsRequired',
46+
13 => 1,
47+
14 => 1,
48+
17 => '@Source: moodle.PHP.IncludingFile.UseRequire',
49+
18 => '@Source: moodle.PHP.IncludingFile.UseRequireOnce',
50+
]);
51+
$this->set_warnings([]);
52+
53+
// Let's do all the hard work!
54+
$this->verify_cs_results();
55+
}
56+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace MoodleHQ\MoodleCS\moodle\Tests;
18+
19+
// phpcs:disable moodle.NamingConventions
20+
21+
/**
22+
* Test the IncludingFile sniff.
23+
*
24+
* @package local_codechecker
25+
* @category test
26+
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*
29+
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\PHP\MemberVarScopeSniff
30+
*/
31+
class PHPMemberVarScopeTest extends MoodleCSBaseTest {
32+
33+
public function test_php_membervarscope() {
34+
// Define the standard, sniff and fixture to use.
35+
$this->set_standard('moodle');
36+
$this->set_sniff('moodle.PHP.MemberVarScope');
37+
$this->set_fixture(__DIR__ . '/fixtures/php/membervarscope.php');
38+
39+
// Define expected results (errors and warnings). Format, array of:
40+
// - line => number of problems, or
41+
// - line => array of contents for message / source problem matching.
42+
// - line => string of contents for message / source problem matching (only 1).
43+
$this->set_errors([
44+
8 => '@Message: Scope modifier not specified for member variable "$missingprop"',
45+
9 => '@Source: moodle.PHP.MemberVarScope.Missing',
46+
]);
47+
$this->set_warnings([]);
48+
49+
// Let's do all the hard work!
50+
$this->verify_cs_results();
51+
}
52+
}

0 commit comments

Comments
 (0)