Skip to content

Commit

Permalink
Adjust filter plugin requirements to comply with 4.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
kabalin committed Oct 24, 2024
1 parent cd11318 commit 8b2d4c3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
12 changes: 12 additions & 0 deletions src/PluginValidate/Requirements/AbstractRequirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
21 changes: 17 additions & 4 deletions src/PluginValidate/Requirements/FilterRequirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
];
}

Expand Down
62 changes: 53 additions & 9 deletions tests/PluginValidate/Requirements/FilterRequirementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 8b2d4c3

Please sign in to comment.