diff --git a/tests/src/Core/Addon/Model/AddonLoaderTest.php b/tests/src/Core/Addon/Model/AddonLoaderTest.php new file mode 100644 index 0000000000..4496cd42f6 --- /dev/null +++ b/tests/src/Core/Addon/Model/AddonLoaderTest.php @@ -0,0 +1,212 @@ +. + * + */ + +namespace Friendica\Test\src\Core\Addon\Model; + +use Friendica\Core\Addon\Exception\AddonInvalidConfigFileException; +use Friendica\Core\Addon\Model\AddonLoader; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Test\MockedTest; +use Friendica\Test\Util\VFSTrait; +use org\bovigo\vfs\vfsStream; + +class AddonLoaderTest extends MockedTest +{ + use VFSTrait; + + protected $structure = [ + 'addon' => [ + 'testaddon1' => [ + 'static' => [], + ], + 'testaddon2' => [ + 'static' => [], + ], + 'testaddon3' => [], + ] + ]; + + protected $addons = [ + 'testaddon1', + 'testaddon2', + 'testaddon3', + ]; + + protected $content = << [ + \Psr\Log\LoggerInterface::class => [ + \Psr\Log\NullLogger::class => [''], + ], + ], +]; +EOF; + + protected function setUp(): void + { + parent::setUp(); + + $this->setUpVfsDir(); + } + + public function dataHooks(): array + { + return [ + 'normal' => [ + 'structure' => $this->structure, + 'enabled' => $this->addons, + 'files' => [ + 'addon/testaddon1/static/hooks.config.php' => $this->content, + ], + 'assertion' => [ + \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [ + \Psr\Log\LoggerInterface::class => [ + \Psr\Log\NullLogger::class => [''], + ], + ], + ], + ], + 'double' => [ + 'structure' => $this->structure, + 'enabled' => $this->addons, + 'files' => [ + 'addon/testaddon1/static/hooks.config.php' => $this->content, + 'addon/testaddon2/static/hooks.config.php' => $this->content, + ], + 'assertion' => [ + \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [ + \Psr\Log\LoggerInterface::class => [ + \Psr\Log\NullLogger::class => ['', ''], + ], + ], + ], + ], + 'wrongName' => [ + 'structure' => $this->structure, + 'enabled' => $this->addons, + 'files' => [ + 'addon/testaddon1/static/wrong.config.php' => $this->content, + ], + 'assertion' => [ + ], + ], + 'doubleNutOnlyOneEnabled' => [ + 'structure' => $this->structure, + 'enabled' => ['testaddon1'], + 'files' => [ + 'addon/testaddon1/static/hooks.config.php' => $this->content, + 'addon/testaddon2/static/hooks.config.php' => $this->content, + ], + 'assertion' => [ + \Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [ + \Psr\Log\LoggerInterface::class => [ + \Psr\Log\NullLogger::class => [''], + ], + ], + ], + ] + ]; + } + + /** + * @dataProvider dataHooks + */ + public function testAddonLoader(array $structure, array $enabledAddons, array $files, array $assertion) + { + vfsStream::create($structure)->at($this->root); + + foreach ($files as $file => $content) { + vfsStream::newFile($file) + ->withContent($content) + ->at($this->root); + } + + $configArray = []; + foreach ($enabledAddons as $enabledAddon) { + $configArray[$enabledAddon] = ['test' => []]; + } + + $config = \Mockery::mock(IManageConfigValues::class); + $config->shouldReceive('get')->with('addons')->andReturn($configArray)->once(); + + $addonLoader = new AddonLoader($this->root->url(), $config); + + self::assertEquals($assertion, $addonLoader->getActiveAddonConfig('hooks')); + } + + /** + * Test the exception in case of a wrong addon content + */ + public function testWrongContent() + { + $filename = 'addon/testaddon1/static/hooks.config.php'; + $wrongContent = "structure)->at($this->root); + + vfsStream::newFile($filename) + ->withContent($wrongContent) + ->at($this->root); + + $configArray = []; + foreach ($this->addons as $enabledAddon) { + $configArray[$enabledAddon] = ['test' => []]; + } + + $config = \Mockery::mock(IManageConfigValues::class); + $config->shouldReceive('get')->with('addons')->andReturn($configArray)->once(); + + $addonLoader = new AddonLoader($this->root->url(), $config); + + self::expectException(AddonInvalidConfigFileException::class); + self::expectExceptionMessage(sprintf('Error loading config file %s', $this->root->getChild($filename)->url())); + + $addonLoader->getActiveAddonConfig('hooks'); + } + + /** + * Test that nothing happens in case there are wrong addons files, but they're not used + */ + public function testNoHooksConfig() + { + $filename = 'addon/testaddon1/static/hooks.config.php'; + $wrongContent = "structure)->at($this->root); + + vfsStream::newFile($filename) + ->withContent($wrongContent) + ->at($this->root); + + $configArray = []; + foreach ($this->addons as $enabledAddon) { + $configArray[$enabledAddon] = ['test' => []]; + } + + $config = \Mockery::mock(IManageConfigValues::class); + $config->shouldReceive('get')->with('addons')->andReturn($configArray)->once(); + + $addonLoader = new AddonLoader($this->root->url(), $config); + self::assertEmpty($addonLoader->getActiveAddonConfig('anythingElse')); + } +}