From 8b2d4c362c77530b5c7d487ccce0e9caac7b92f2 Mon Sep 17 00:00:00 2001 From: Ruslan Kabalin Date: Fri, 11 Oct 2024 22:17:32 +0100 Subject: [PATCH] Adjust filter plugin requirements to comply with 4.5. Per https://moodledev.io/docs/4.5/devupdate#filter-plugins Fixes #321 --- .../Requirements/AbstractRequirements.php | 12 ++++ .../Requirements/FilterRequirements.php | 21 +++++-- .../Requirements/FilterRequirementsTest.php | 62 ++++++++++++++++--- 3 files changed, 82 insertions(+), 13 deletions(-) diff --git a/src/PluginValidate/Requirements/AbstractRequirements.php b/src/PluginValidate/Requirements/AbstractRequirements.php index 9978dd2f..32c1e154 100644 --- a/src/PluginValidate/Requirements/AbstractRequirements.php +++ b/src/PluginValidate/Requirements/AbstractRequirements.php @@ -64,6 +64,18 @@ protected function behatTagsFactory(array $tags): array return $fileTokens; } + /** + * Helper method to check file existence. + * + * @param string $file + * + * @return bool + */ + protected function fileExists(string $file): bool + { + return file_exists($this->plugin->directory . '/' . $file); + } + /** * An array of required files, paths are relative to the plugin directory. * diff --git a/src/PluginValidate/Requirements/FilterRequirements.php b/src/PluginValidate/Requirements/FilterRequirements.php index cbed68db..c6c2ed90 100644 --- a/src/PluginValidate/Requirements/FilterRequirements.php +++ b/src/PluginValidate/Requirements/FilterRequirements.php @@ -21,15 +21,28 @@ class FilterRequirements extends GenericRequirements { public function getRequiredFiles(): array { - return array_merge(parent::getRequiredFiles(), [ - 'filter.php', - ]); + $files = []; + if ($this->moodleVersion >= 405) { + $files[] = 'classes/text_filter.php'; + } else { + // This must exist in 4.5 if plugin supports older version, but we don't identify support range to validate it. + $files[] = 'filter.php'; + } + + return array_merge(parent::getRequiredFiles(), $files); } public function getRequiredClasses(): array { + if ($this->moodleVersion <= 404 && !$this->fileExists('classes/text_filter.php')) { + // Plugin does not support 4.5, check class presence in filter.php + return [ + FileTokens::create('filter.php')->mustHave('filter_' . $this->plugin->name), + ]; + } + return [ - FileTokens::create('filter.php')->mustHave('filter_' . $this->plugin->name), + FileTokens::create('classes/text_filter.php')->mustHave("filter_{$this->plugin->name}\\text_filter"), ]; } diff --git a/tests/PluginValidate/Requirements/FilterRequirementsTest.php b/tests/PluginValidate/Requirements/FilterRequirementsTest.php index de2b2d4d..7f44ed44 100644 --- a/tests/PluginValidate/Requirements/FilterRequirementsTest.php +++ b/tests/PluginValidate/Requirements/FilterRequirementsTest.php @@ -23,14 +23,21 @@ class FilterRequirementsTest extends \PHPUnit\Framework\TestCase */ private $requirements; + /** + * @var FilterRequirements + */ + private $requirements404; + protected function setUp(): void { - $this->requirements = new FilterRequirements(new Plugin('filter_activitynames', 'filter', 'activitynames', ''), 29); + $this->requirements404 = new FilterRequirements(new Plugin('filter_activitynames', 'filter', 'activitynames', ''), 404); + $this->requirements = new FilterRequirements(new Plugin('filter_activitynames', 'filter', 'activitynames', ''), 405); } protected function tearDown(): void { - $this->requirements = null; + $this->requirements404 = null; + $this->requirements = null; } public function testResolveRequirements() @@ -39,29 +46,66 @@ public function testResolveRequirements() $this->assertInstanceOf( 'MoodlePluginCI\PluginValidate\Requirements\FilterRequirements', - $resolver->resolveRequirements(new Plugin('', 'filter', '', ''), 29) + $resolver->resolveRequirements(new Plugin('', 'filter', '', ''), 404) ); } + public function testGetRequiredFiles404() + { + $files = $this->requirements404->getRequiredFiles(); + + $this->assertTrue(in_array('filter.php', $files, true)); + $this->assertFalse(in_array('classes/text_filter.php', $files, true)); + foreach ($files as $file) { + $this->assertIsString($file); + } + } + public function testGetRequiredFiles() { $files = $this->requirements->getRequiredFiles(); - $this->assertNotEmpty($files); - $this->assertTrue(in_array('filter.php', $files, true)); + $this->assertFalse(in_array('filter.php', $files, true)); + $this->assertTrue(in_array('classes/text_filter.php', $files, true)); foreach ($files as $file) { $this->assertIsString($file); } } + public function testGetRequiredClasses404() + { + $requirements = $this->getMockBuilder('MoodlePluginCI\PluginValidate\Requirements\FilterRequirements') + ->setConstructorArgs([new Plugin('filter_activitynames', 'filter', 'activitynames', ''), 404]) + ->onlyMethods(['fileExists']) + ->getMock(); + // On first call fileExists return false, on second call return true. + $requirements->method('fileExists') + ->with($this->identicalTo('classes/text_filter.php')) + ->willReturn(false, true); + + // If classes/text_filter.php does not exist, expect class presence in filter.php. + $classes = $requirements->getRequiredClasses(); + $this->assertCount(1, $classes); + $class = reset($classes); + $this->assertInstanceOf('MoodlePluginCI\PluginValidate\Finder\FileTokens', $class); + $this->assertSame('filter.php', $class->file); + + // If classes/text_filter.php exists, expect class presence in it (4.5 plugin backward compatibility). + $classes = $requirements->getRequiredClasses(); + $this->assertCount(1, $classes); + $class = reset($classes); + $this->assertInstanceOf('MoodlePluginCI\PluginValidate\Finder\FileTokens', $class); + $this->assertSame('classes/text_filter.php', $class->file); + } + public function testGetRequiredClasses() { $classes = $this->requirements->getRequiredClasses(); - $this->assertNotEmpty($classes); - foreach ($classes as $class) { - $this->assertInstanceOf('MoodlePluginCI\PluginValidate\Finder\FileTokens', $class); - } + $this->assertCount(1, $classes); + $class = reset($classes); + $this->assertInstanceOf('MoodlePluginCI\PluginValidate\Finder\FileTokens', $class); + $this->assertSame('classes/text_filter.php', $class->file); } public function testGetRequiredStrings()