Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support re-enabling actions if they have been disabled. #6210

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Config/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ public function disable(string ...$disabledActionNames): self
return $this;
}

public function enable(string ...$enabledActionNames): self
{
// if 'delete' or 'batch delete' is enabled, both are enabled automatically.
// This is the most common case, but user can re-disable the action if needed manually
if (\in_array(Action::DELETE, $enabledActionNames, true) || \in_array(Action::BATCH_DELETE, $enabledActionNames, true)) {
$enabledActionNames[] = Action::DELETE;
$enabledActionNames[] = Action::BATCH_DELETE;
}

$this->dto->enableActions($enabledActionNames);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also re enable Action::BATCH_DELETE in case Action::DELETE is enabled?
(to be consistent with the disable method)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I update the code to reflect this.
I also re-enable Action::DELETE if Action::BATCH_DELETE is re-enabled.


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
43 changes: 43 additions & 0 deletions tests/Config/ActionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Config;

use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use PHPUnit\Framework\TestCase;

class ActionsTest extends TestCase
{
public function testEnableDeleteAction(): void
{
$actions = Actions::new();
$actions->disable(Action::DELETE);

$this->assertSame(['delete', 'batchDelete'], $actions->getAsDto(null)->getDisabledActions());

$actions->enable(Action::DELETE);
$this->assertSame([], $actions->getAsDto(null)->getDisabledActions());
}

public function testEnableBatchDeleteAction(): void
{
$actions = Actions::new();
$actions->disable(Action::BATCH_DELETE);

$this->assertSame(['batchDelete'], $actions->getAsDto(null)->getDisabledActions());

$actions->enable(Action::BATCH_DELETE);
$this->assertSame([], $actions->getAsDto(null)->getDisabledActions());
}

public function testEnableBatchDeleteActionWillEnableDeleteAsWell(): void
{
$actions = Actions::new();
$actions->disable(Action::DELETE, Action::BATCH_DELETE);

$this->assertSame(['delete', 'batchDelete'], $actions->getAsDto(null)->getDisabledActions());

$actions->enable(Action::BATCH_DELETE);
$this->assertSame([], $actions->getAsDto(null)->getDisabledActions());
}
}
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());
}
}
Loading