|
17 | 17 | namespace local_codechecker;
|
18 | 18 |
|
19 | 19 | use MoodleCodeSniffer\moodle\Util\MoodleUtil;
|
| 20 | +use org\bovigo\vfs\vfsStream; |
20 | 21 | use PHP_CodeSniffer\Config;
|
21 | 22 | use PHP_CodeSniffer\Exceptions\DeepExitException;
|
22 | 23 | use PHP_CodeSniffer\Files\File;
|
|
41 | 42 | */
|
42 | 43 | class moodleutil_test extends local_codechecker_testcase {
|
43 | 44 |
|
| 45 | + /** |
| 46 | + * Unit test for calculateAllComponents. |
| 47 | + * |
| 48 | + * Not 100% orthodox because {@see calculateAllComponents()} is protected, |
| 49 | + * and it's already indirectly tested by {@see test_getMoodleComponent()} |
| 50 | + * but it has some feature that we need to test individually here. |
| 51 | + */ |
| 52 | + public function test_calculateAllComponents() { |
| 53 | + // Let's calculate moodleRoot. |
| 54 | + $moodleRoot = MoodleUtil::getMoodleRoot(); |
| 55 | + |
| 56 | + // Let's prepare a components file, with some correct and incorrect entries. |
| 57 | + $components = |
| 58 | + "nonono,mod_forum,{$moodleRoot}/mod_forum\n" . // Wrong type. |
| 59 | + "plugin,mod__nono,{$moodleRoot}/mod_forum\n" . // Wrong component. |
| 60 | + "plugin,mod_forum,/no/no/no/no//mod_forum\n" . // Wrong path. |
| 61 | + "plugin,local_codechecker,{$moodleRoot}/local/codechecker\n" .// All ok. |
| 62 | + "plugin,mod_forum,{$moodleRoot}/mod/forum\n"; // All ok. |
| 63 | + |
| 64 | + // Let's use virtual filesystem instead of real one. |
| 65 | + $vfs = vfsStream::setup('root', null, ['components.txt' => $components]); |
| 66 | + |
| 67 | + // Set codechecker config to point to it. |
| 68 | + Config::setConfigData('moodleComponentsListPath', $vfs->url() . '/components.txt', true); |
| 69 | + |
| 70 | + // Let's run calculateAllComponents() and evaluate results. |
| 71 | + $method = new \ReflectionMethod(MoodleUtil::class, 'calculateAllComponents'); |
| 72 | + $method->setAccessible(true); |
| 73 | + $method->invokeArgs(null, [$moodleRoot]); |
| 74 | + |
| 75 | + // Let's inspect which components have been loaded. |
| 76 | + $property = new \ReflectionProperty(MoodleUtil::class, 'moodleComponents'); |
| 77 | + $property->setAccessible(true); |
| 78 | + $loadedComponents = $property->getValue(); |
| 79 | + |
| 80 | + $this->assertCount(2, $loadedComponents); |
| 81 | + $this->assertSame(['mod_forum', 'local_codechecker'], |
| 82 | + array_keys($loadedComponents)); // Verify they are ordered in ascending order. |
| 83 | + $this->assertSame(["{$moodleRoot}/mod/forum", "{$moodleRoot}/local/codechecker"], |
| 84 | + array_values($loadedComponents)); // Verify component paths are also the expected ones. |
| 85 | + |
| 86 | + // Ensure cached information doesn't affect other tests. |
| 87 | + $this->cleanMoodleUtilCaches(); |
| 88 | + Config::setConfigData('moodleComponentsListPath', null, true); |
| 89 | + } |
| 90 | + |
44 | 91 | /**
|
45 | 92 | * Provider for test_getMoodleComponent.
|
46 | 93 | */
|
|
0 commit comments