|
11 | 11 | use Filament\Tests\Actions\TestCase; |
12 | 12 | use Filament\Tests\Fixtures\Models\Post; |
13 | 13 | use Filament\Tests\Fixtures\Pages\Actions; |
| 14 | +use Illuminate\Auth\Access\Response; |
14 | 15 | use Illuminate\Support\Str; |
15 | 16 |
|
16 | 17 | use function Filament\Tests\livewire; |
|
1548 | 1549 | expect($action)->toBeInstanceOf(Action::class); |
1549 | 1550 | }); |
1550 | 1551 | }); |
| 1552 | + |
| 1553 | +describe('authorization', function (): void { |
| 1554 | + it('is visible by default when no `authorize()` is configured', function (): void { |
| 1555 | + $action = Action::make('test'); |
| 1556 | + |
| 1557 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeTrue(); |
| 1558 | + expect($action->isVisible())->toBeTrue(); |
| 1559 | + }); |
| 1560 | + |
| 1561 | + it('is hidden when `authorize()` returns `false` and no auth feedback method is set', function (): void { |
| 1562 | + $action = Action::make('test') |
| 1563 | + ->authorize(fn (): bool => false); |
| 1564 | + |
| 1565 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeFalse(); |
| 1566 | + expect($action->isVisible())->toBeFalse(); |
| 1567 | + }); |
| 1568 | + |
| 1569 | + it('accepts an `authorize()` closure returning a `Response`', function (): void { |
| 1570 | + $action = Action::make('test') |
| 1571 | + ->authorize(fn (): Response => Response::deny('Nope.')); |
| 1572 | + |
| 1573 | + expect($action->getAuthorizationResponse()->message())->toBe('Nope.'); |
| 1574 | + }); |
| 1575 | + |
| 1576 | + it('can chain `authorizationMessage()` to set a fallback message', function (): void { |
| 1577 | + $action = Action::make('test') |
| 1578 | + ->authorizationMessage('Custom fallback.'); |
| 1579 | + |
| 1580 | + expect($action->getAuthorizationMessage())->toBe('Custom fallback.'); |
| 1581 | + }); |
| 1582 | + |
| 1583 | + describe('with `authorizationNotification()`', function (): void { |
| 1584 | + it('shows the action when the user is allowed', function (): void { |
| 1585 | + $action = Action::make('test') |
| 1586 | + ->authorize(fn (): Response => Response::allow()) |
| 1587 | + ->authorizationNotification(); |
| 1588 | + |
| 1589 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeTrue(); |
| 1590 | + expect($action->isVisible())->toBeTrue(); |
| 1591 | + }); |
| 1592 | + |
| 1593 | + it('shows the action when denied with a message', function (): void { |
| 1594 | + $action = Action::make('test') |
| 1595 | + ->authorize(fn (): Response => Response::deny('You cannot do that.')) |
| 1596 | + ->authorizationNotification(); |
| 1597 | + |
| 1598 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeTrue(); |
| 1599 | + expect($action->isVisible())->toBeTrue(); |
| 1600 | + expect($action->hasAuthorizationNotification())->toBeTrue(); |
| 1601 | + expect($action->getAuthorizationResponseWithMessage()->message())->toBe('You cannot do that.'); |
| 1602 | + }); |
| 1603 | + |
| 1604 | + it('hides the action when denied with `Response::deny()` and no message', function (): void { |
| 1605 | + $action = Action::make('test') |
| 1606 | + ->authorize(fn (): Response => Response::deny()) |
| 1607 | + ->authorizationNotification(); |
| 1608 | + |
| 1609 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeFalse(); |
| 1610 | + expect($action->isVisible())->toBeFalse(); |
| 1611 | + }); |
| 1612 | + |
| 1613 | + it('hides the action when the policy returns bare `false`', function (): void { |
| 1614 | + $action = Action::make('test') |
| 1615 | + ->authorize(fn (): bool => false) |
| 1616 | + ->authorizationNotification(); |
| 1617 | + |
| 1618 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeFalse(); |
| 1619 | + expect($action->isVisible())->toBeFalse(); |
| 1620 | + }); |
| 1621 | + |
| 1622 | + it('shows the action when `authorizationMessage()` is set even if the policy returns bare `false`', function (): void { |
| 1623 | + $action = Action::make('test') |
| 1624 | + ->authorize(fn (): bool => false) |
| 1625 | + ->authorizationMessage('Explicit message.') |
| 1626 | + ->authorizationNotification(); |
| 1627 | + |
| 1628 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeTrue(); |
| 1629 | + expect($action->isVisible())->toBeTrue(); |
| 1630 | + expect($action->getAuthorizationResponseWithMessage()->message())->toBe('Explicit message.'); |
| 1631 | + }); |
| 1632 | + |
| 1633 | + it('is a no-op when `condition: false` is passed', function (): void { |
| 1634 | + $action = Action::make('test') |
| 1635 | + ->authorize(fn (): bool => false) |
| 1636 | + ->authorizationNotification(false); |
| 1637 | + |
| 1638 | + expect($action->hasAuthorizationNotification())->toBeFalse(); |
| 1639 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeFalse(); |
| 1640 | + expect($action->isVisible())->toBeFalse(); |
| 1641 | + }); |
| 1642 | + |
| 1643 | + it('stays hidden when `visible(false)` is also set', function (): void { |
| 1644 | + $action = Action::make('test') |
| 1645 | + ->authorize(fn (): Response => Response::deny('Has a message.')) |
| 1646 | + ->authorizationNotification() |
| 1647 | + ->visible(false); |
| 1648 | + |
| 1649 | + expect($action->isVisible())->toBeFalse(); |
| 1650 | + }); |
| 1651 | + }); |
| 1652 | + |
| 1653 | + describe('with `authorizationTooltip()`', function (): void { |
| 1654 | + it('shows the action when the user is allowed', function (): void { |
| 1655 | + $action = Action::make('test') |
| 1656 | + ->authorize(fn (): Response => Response::allow()) |
| 1657 | + ->authorizationTooltip(); |
| 1658 | + |
| 1659 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeTrue(); |
| 1660 | + expect($action->isVisible())->toBeTrue(); |
| 1661 | + }); |
| 1662 | + |
| 1663 | + it('shows the action with the deny message as a tooltip when denied with a message', function (): void { |
| 1664 | + $action = Action::make('test') |
| 1665 | + ->authorize(fn (): Response => Response::deny('You cannot do that.')) |
| 1666 | + ->authorizationTooltip(); |
| 1667 | + |
| 1668 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeTrue(); |
| 1669 | + expect($action->isVisible())->toBeTrue(); |
| 1670 | + expect($action->hasAuthorizationTooltip())->toBeTrue(); |
| 1671 | + expect($action->getTooltip())->toBe('You cannot do that.'); |
| 1672 | + }); |
| 1673 | + |
| 1674 | + it('hides the action when denied with `Response::deny()` and no message', function (): void { |
| 1675 | + $action = Action::make('test') |
| 1676 | + ->authorize(fn (): Response => Response::deny()) |
| 1677 | + ->authorizationTooltip(); |
| 1678 | + |
| 1679 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeFalse(); |
| 1680 | + expect($action->isVisible())->toBeFalse(); |
| 1681 | + }); |
| 1682 | + |
| 1683 | + it('hides the action when the policy returns bare `false`', function (): void { |
| 1684 | + $action = Action::make('test') |
| 1685 | + ->authorize(fn (): bool => false) |
| 1686 | + ->authorizationTooltip(); |
| 1687 | + |
| 1688 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeFalse(); |
| 1689 | + expect($action->isVisible())->toBeFalse(); |
| 1690 | + }); |
| 1691 | + |
| 1692 | + it('shows the action when `authorizationMessage()` is set even if the policy returns bare `false`', function (): void { |
| 1693 | + $action = Action::make('test') |
| 1694 | + ->authorize(fn (): bool => false) |
| 1695 | + ->authorizationMessage('Explicit message.') |
| 1696 | + ->authorizationTooltip(); |
| 1697 | + |
| 1698 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeTrue(); |
| 1699 | + expect($action->isVisible())->toBeTrue(); |
| 1700 | + expect($action->getTooltip())->toBe('Explicit message.'); |
| 1701 | + }); |
| 1702 | + |
| 1703 | + it('is a no-op when `condition: false` is passed', function (): void { |
| 1704 | + $action = Action::make('test') |
| 1705 | + ->authorize(fn (): bool => false) |
| 1706 | + ->authorizationTooltip(false); |
| 1707 | + |
| 1708 | + expect($action->hasAuthorizationTooltip())->toBeFalse(); |
| 1709 | + expect($action->isAuthorizedOrNotHiddenWhenUnauthorized())->toBeFalse(); |
| 1710 | + expect($action->isVisible())->toBeFalse(); |
| 1711 | + }); |
| 1712 | + |
| 1713 | + it('stays hidden when `visible(false)` is also set', function (): void { |
| 1714 | + $action = Action::make('test') |
| 1715 | + ->authorize(fn (): Response => Response::deny('Has a message.')) |
| 1716 | + ->authorizationTooltip() |
| 1717 | + ->visible(false); |
| 1718 | + |
| 1719 | + expect($action->isVisible())->toBeFalse(); |
| 1720 | + }); |
| 1721 | + }); |
| 1722 | +}); |
0 commit comments