Merge branch 'develop' into new-addonproxy

This commit is contained in:
Art4 2025-02-05 19:20:27 +00:00
commit d086a982f9
27 changed files with 1267 additions and 107 deletions

View file

@ -0,0 +1,176 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Core\Hooks;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Hooks\HookEventBridge;
use Friendica\Event\ArrayFilterEvent;
use Friendica\Event\ConfigLoadedEvent;
use Friendica\Event\Event;
use Friendica\Event\HtmlFilterEvent;
use PHPUnit\Framework\TestCase;
class HookEventBridgeTest extends TestCase
{
public function testGetStaticSubscribedEventsReturnsStaticMethods(): void
{
$expected = [
Event::INIT => 'onNamedEvent',
ConfigLoadedEvent::CONFIG_LOADED => 'onConfigLoadedEvent',
ArrayFilterEvent::APP_MENU => 'onArrayFilterEvent',
ArrayFilterEvent::NAV_INFO => 'onArrayFilterEvent',
ArrayFilterEvent::FEATURE_ENABLED => 'onArrayFilterEvent',
ArrayFilterEvent::FEATURE_GET => 'onArrayFilterEvent',
HtmlFilterEvent::HEAD => 'onHtmlFilterEvent',
HtmlFilterEvent::FOOTER => 'onHtmlFilterEvent',
HtmlFilterEvent::PAGE_HEADER => 'onHtmlFilterEvent',
HtmlFilterEvent::PAGE_CONTENT_TOP => 'onHtmlFilterEvent',
HtmlFilterEvent::PAGE_END => 'onHtmlFilterEvent',
];
$this->assertSame(
$expected,
HookEventBridge::getStaticSubscribedEvents()
);
foreach ($expected as $methodName) {
$this->assertTrue(
method_exists(HookEventBridge::class, $methodName),
$methodName . '() is not defined'
);
$this->assertTrue(
(new \ReflectionMethod(HookEventBridge::class, $methodName))->isStatic(),
$methodName . '() is not static'
);
}
}
public static function getNamedEventData(): array
{
return [
['test', 'test'],
[Event::INIT, 'init_1'],
];
}
/**
* @dataProvider getNamedEventData
*/
public function testOnNamedEventCallsHook($name, $expected): void
{
$event = new Event($name);
$reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, function (string $name, $data) use ($expected) {
$this->assertSame($expected, $name);
$this->assertSame('', $data);
return $data;
});
HookEventBridge::onNamedEvent($event);
}
public static function getConfigLoadedEventData(): array
{
return [
['test', 'test'],
[ConfigLoadedEvent::CONFIG_LOADED, 'load_config'],
];
}
/**
* @dataProvider getConfigLoadedEventData
*/
public function testOnConfigLoadedEventCallsHookWithCorrectValue($name, $expected): void
{
$config = $this->createStub(ConfigFileManager::class);
$event = new ConfigLoadedEvent($name, $config);
$reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, function (string $name, $data) use ($expected, $config) {
$this->assertSame($expected, $name);
$this->assertSame($config, $data);
return $data;
});
HookEventBridge::onConfigLoadedEvent($event);
}
public static function getArrayFilterEventData(): array
{
return [
['test', 'test'],
[ArrayFilterEvent::APP_MENU, 'app_menu'],
[ArrayFilterEvent::NAV_INFO, 'nav_info'],
[ArrayFilterEvent::FEATURE_ENABLED, 'isEnabled'],
[ArrayFilterEvent::FEATURE_GET, 'get'],
];
}
/**
* @dataProvider getArrayFilterEventData
*/
public function testOnArrayFilterEventCallsHookWithCorrectValue($name, $expected): void
{
$event = new ArrayFilterEvent($name, ['original']);
$reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, function (string $name, $data) use ($expected) {
$this->assertSame($expected, $name);
$this->assertSame(['original'], $data);
return $data;
});
HookEventBridge::onArrayFilterEvent($event);
}
public static function getHtmlFilterEventData(): array
{
return [
['test', 'test'],
[HtmlFilterEvent::HEAD, 'head'],
[HtmlFilterEvent::FOOTER, 'footer'],
[HtmlFilterEvent::PAGE_HEADER, 'page_header'],
[HtmlFilterEvent::PAGE_CONTENT_TOP, 'page_content_top'],
[HtmlFilterEvent::PAGE_END, 'page_end'],
];
}
/**
* @dataProvider getHtmlFilterEventData
*/
public function testOnHtmlFilterEventCallsHookWithCorrectValue($name, $expected): void
{
$event = new HtmlFilterEvent($name, 'original');
$reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue(null, function (string $name, $data) use ($expected) {
$this->assertSame($expected, $name);
$this->assertSame('original', $data);
return $data;
});
HookEventBridge::onHtmlFilterEvent($event);
}
}

View file

@ -0,0 +1,66 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Event;
use Friendica\Event\ArrayFilterEvent;
use Friendica\Event\NamedEvent;
use PHPUnit\Framework\TestCase;
class ArrayFilterEventTest extends TestCase
{
public function testImplementationOfInstances(): void
{
$event = new ArrayFilterEvent('test', []);
$this->assertInstanceOf(NamedEvent::class, $event);
}
public static function getPublicConstants(): array
{
return [
[ArrayFilterEvent::APP_MENU, 'friendica.data.app_menu'],
];
}
/**
* @dataProvider getPublicConstants
*/
public function testPublicConstantsAreAvailable($value, $expected): void
{
$this->assertSame($expected, $value);
}
public function testGetNameReturnsName(): void
{
$event = new ArrayFilterEvent('test', []);
$this->assertSame('test', $event->getName());
}
public function testGetArrayReturnsCorrectString(): void
{
$data = ['original'];
$event = new ArrayFilterEvent('test', $data);
$this->assertSame($data, $event->getArray());
}
public function testSetArrayUpdatesHtml(): void
{
$event = new ArrayFilterEvent('test', ['original']);
$expected = ['updated'];
$event->setArray($expected);
$this->assertSame($expected, $event->getArray());
}
}

View file

@ -0,0 +1,56 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Event;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Event\ConfigLoadedEvent;
use Friendica\Event\NamedEvent;
use PHPUnit\Framework\TestCase;
class ConfigLoadedEventTest extends TestCase
{
public function testImplementationOfInstances(): void
{
$event = new ConfigLoadedEvent('test', $this->createStub(ConfigFileManager::class));
$this->assertInstanceOf(NamedEvent::class, $event);
}
public static function getPublicConstants(): array
{
return [
[ConfigLoadedEvent::CONFIG_LOADED, 'friendica.config_loaded'],
];
}
/**
* @dataProvider getPublicConstants
*/
public function testPublicConstantsAreAvailable($value, $expected): void
{
$this->assertSame($expected, $value);
}
public function testGetNameReturnsName(): void
{
$event = new ConfigLoadedEvent('test', $this->createStub(ConfigFileManager::class));
$this->assertSame('test', $event->getName());
}
public function testGetConfigReturnsCorrectString(): void
{
$config = $this->createStub(ConfigFileManager::class);
$event = new ConfigLoadedEvent('test', $config);
$this->assertSame($config, $event->getConfig());
}
}

View file

@ -0,0 +1,37 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Event;
use Friendica\Event\Event;
use Friendica\Event\EventDispatcher;
use Friendica\Event\NamedEvent;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
class EventDispatcherTest extends TestCase
{
public function testImplementationOfInstances(): void
{
$eventDispatcher = new EventDispatcher();
$this->assertInstanceOf(EventDispatcherInterface::class, $eventDispatcher);
}
public function testDispatchANamedEventUsesNameAsEventName(): void
{
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('test', function (NamedEvent $event) {
$this->assertSame('test', $event->getName());
});
$eventDispatcher->dispatch(new Event('test'));
}
}

View file

@ -0,0 +1,46 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Event;
use Friendica\Event\Event;
use Friendica\Event\NamedEvent;
use PHPUnit\Framework\TestCase;
class EventTest extends TestCase
{
public function testImplementationOfInstances(): void
{
$event = new Event('test');
$this->assertInstanceOf(NamedEvent::class, $event);
}
public static function getPublicConstants(): array
{
return [
[Event::INIT, 'friendica.init'],
];
}
/**
* @dataProvider getPublicConstants
*/
public function testPublicConstantsAreAvailable($value, $expected): void
{
$this->assertSame($expected, $value);
}
public function testGetNameReturnsName(): void
{
$event = new Event('test');
$this->assertSame('test', $event->getName());
}
}

View file

@ -0,0 +1,69 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Test\Unit\Event;
use Friendica\Event\HtmlFilterEvent;
use Friendica\Event\NamedEvent;
use PHPUnit\Framework\TestCase;
class HtmlFilterEventTest extends TestCase
{
public function testImplementationOfInstances(): void
{
$event = new HtmlFilterEvent('test', 'original');
$this->assertInstanceOf(NamedEvent::class, $event);
}
public static function getPublicConstants(): array
{
return [
[HtmlFilterEvent::HEAD, 'friendica.html.head'],
[HtmlFilterEvent::FOOTER, 'friendica.html.footer'],
[HtmlFilterEvent::PAGE_CONTENT_TOP, 'friendica.html.page_content_top'],
[HtmlFilterEvent::PAGE_END, 'friendica.html.page_end'],
];
}
/**
* @dataProvider getPublicConstants
*/
public function testPublicConstantsAreAvailable($value, $expected): void
{
$this->assertSame($expected, $value);
}
public function testGetNameReturnsName(): void
{
$event = new HtmlFilterEvent('test', '');
$this->assertSame('test', $event->getName());
}
public function testGetHtmlReturnsCorrectString(): void
{
$data = 'original';
$event = new HtmlFilterEvent('test', $data);
$this->assertSame($data, $event->getHtml());
}
public function testSetHtmlUpdatesHtml(): void
{
$event = new HtmlFilterEvent('test', 'original');
$expected = 'updated';
$event->setHtml($expected);
$this->assertSame($expected, $event->getHtml());
}
}

View file

@ -5,7 +5,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types = 1);
declare(strict_types=1);
namespace Friendica\Test\Unit\Util;
@ -16,48 +16,48 @@ class BasePathTest extends TestCase
{
public static function getDataPaths(): array
{
$basePath = dirname(__DIR__, 3);
$basePath = dirname(__DIR__, 3);
$configPath = $basePath . DIRECTORY_SEPARATOR . 'config';
return [
'fullPath' => [
'server' => [],
'baseDir' => $configPath,
'server' => [],
'baseDir' => $configPath,
'expected' => $configPath,
],
'relative' => [
'server' => [],
'baseDir' => 'config',
'server' => [],
'baseDir' => 'config',
'expected' => $configPath,
],
'document_root' => [
'server' => [
'DOCUMENT_ROOT' => $configPath,
],
'baseDir' => '/noooop',
'baseDir' => '/noooop',
'expected' => $configPath,
],
'pwd' => [
'server' => [
'PWD' => $configPath,
],
'baseDir' => '/noooop',
'baseDir' => '/noooop',
'expected' => $configPath,
],
'no_overwrite' => [
'server' => [
'DOCUMENT_ROOT' => $basePath,
'PWD' => $basePath,
'PWD' => $basePath,
],
'baseDir' => 'config',
'baseDir' => 'config',
'expected' => $configPath,
],
'no_overwrite_if_invalid' => [
'server' => [
'DOCUMENT_ROOT' => '/nopopop',
'PWD' => $configPath,
'PWD' => $configPath,
],
'baseDir' => '/noatgawe22fafa',
'baseDir' => '/noatgawe22fafa',
'expected' => $configPath,
]
];

View file

@ -5,7 +5,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types = 1);
declare(strict_types=1);
namespace Friendica\Test\Unit\Util;