mirror of
https://github.com/flarum/core.git
synced 2025-08-05 16:07:34 +02:00
feat: add whenExtensionDisabled
to Conditional
extender (#3847)
* feat: add to extender * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
@@ -30,6 +30,16 @@ class Conditional implements ExtenderInterface
|
|||||||
}, $extenders);
|
}, $extenders);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ExtenderInterface[] $extenders
|
||||||
|
*/
|
||||||
|
public function whenExtensionDisabled(string $extensionId, array $extenders): self
|
||||||
|
{
|
||||||
|
return $this->when(function (ExtensionManager $extensions) use ($extensionId) {
|
||||||
|
return ! $extensions->isEnabled($extensionId);
|
||||||
|
}, $extenders);
|
||||||
|
}
|
||||||
|
|
||||||
public function when(callable|bool $condition, array $extenders): self
|
public function when(callable|bool $condition, array $extenders): self
|
||||||
{
|
{
|
||||||
$this->conditions[] = [
|
$this->conditions[] = [
|
||||||
|
@@ -159,4 +159,119 @@ class ConditionalTest extends TestCase
|
|||||||
|
|
||||||
$this->app();
|
$this->app();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function conditional_disabled_extension_not_enabled_applies_extender_array()
|
||||||
|
{
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\Conditional())
|
||||||
|
->whenExtensionDisabled('flarum-dummy-extension', [
|
||||||
|
(new Extend\ApiSerializer(ForumSerializer::class))
|
||||||
|
->attributes(function () {
|
||||||
|
return [
|
||||||
|
'customConditionalAttribute' => true
|
||||||
|
];
|
||||||
|
})
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api', [
|
||||||
|
'authenticatedAs' => 1,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$payload = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function conditional_disabled_extension_enabled_does_not_apply_extender_array()
|
||||||
|
{
|
||||||
|
$this->extension('flarum-tags');
|
||||||
|
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\Conditional())
|
||||||
|
->whenExtensionDisabled('flarum-tags', [
|
||||||
|
(new Extend\ApiSerializer(ForumSerializer::class))
|
||||||
|
->attributes(function () {
|
||||||
|
return [
|
||||||
|
'customConditionalAttribute' => true
|
||||||
|
];
|
||||||
|
})
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api', [
|
||||||
|
'authenticatedAs' => 1,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$payload = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
$this->assertArrayNotHasKey('customConditionalAttribute', $payload['data']['attributes']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function conditional_enabled_extension_disabled_does_not_apply_extender_array()
|
||||||
|
{
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\Conditional())
|
||||||
|
->whenExtensionEnabled('flarum-dummy-extension', [
|
||||||
|
(new Extend\ApiSerializer(ForumSerializer::class))
|
||||||
|
->attributes(function () {
|
||||||
|
return [
|
||||||
|
'customConditionalAttribute' => true
|
||||||
|
];
|
||||||
|
})
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api', [
|
||||||
|
'authenticatedAs' => 1,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$payload = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
$this->assertArrayNotHasKey('customConditionalAttribute', $payload['data']['attributes']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function conditional_enabled_extension_enabled_applies_extender_array()
|
||||||
|
{
|
||||||
|
$this->extension('flarum-tags');
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\Conditional())
|
||||||
|
->whenExtensionEnabled('flarum-tags', [
|
||||||
|
(new Extend\ApiSerializer(ForumSerializer::class))
|
||||||
|
->attributes(function () {
|
||||||
|
return [
|
||||||
|
'customConditionalAttribute' => true
|
||||||
|
];
|
||||||
|
})
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('GET', '/api', [
|
||||||
|
'authenticatedAs' => 1,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$payload = json_decode($response->getBody()->getContents(), true);
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user