Skip to content

Commit

Permalink
Support re-enabling actions if they have been disabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
dragosprotung committed Nov 29, 2024
1 parent 605c7e4 commit dd5fa35
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Config/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ public function disable(string ...$disabledActionNames): self
return $this;
}

public function enable(string ...$enabledActionNames): self
{
$this->dto->enableActions($enabledActionNames);

return $this;
}

public function getAsDto(?string $pageName): ActionConfigDto
{
$this->dto->setPageName($pageName);
Expand Down
5 changes: 5 additions & 0 deletions src/Dto/ActionConfigDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public function disableActions(array $actionNames): void
}
}

public function enableActions(array $actionNames): void
{
$this->disabledActions = array_values(array_diff($this->disabledActions, $actionNames));
}

public function getActions(): ActionCollection|array
{
return null === $this->pageName ? $this->actions : ActionCollection::new($this->actions[$this->pageName]);
Expand Down
32 changes: 32 additions & 0 deletions tests/Dto/ActionConfigDtoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Dto;

use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionConfigDto;
use PHPUnit\Framework\TestCase;

final class ActionConfigDtoTest extends TestCase
{
public static function provideLabels(): \Generator
{
yield 'nothing to enable' => [[], ['foo', 'bar', 'baz', 'qux']];
yield 'enable action not disabled' => [['not-disabled'], ['foo', 'bar', 'baz', 'qux']];
yield 'enable 1 action' => [['bar'], ['foo', 'baz', 'qux']];
yield 'enable multiple action' => [['foo', 'baz'], ['bar', 'qux']];
yield 'enable all' => [['foo', 'bar', 'baz', 'qux'], []];
}

/**
* @dataProvider provideLabels
*/
public function testEnableActions(array $actionsToEnable, array $expectedDisabledActions): void
{
$actionConfigDto = new ActionConfigDto();
$actionConfigDto->disableActions(['foo', 'bar', 'baz', 'qux']);

$this->assertSame(['foo', 'bar', 'baz', 'qux'], $actionConfigDto->getDisabledActions());

$actionConfigDto->enableActions($actionsToEnable);
$this->assertSame($expectedDisabledActions, $actionConfigDto->getDisabledActions());
}
}

0 comments on commit dd5fa35

Please sign in to comment.