Merge branch '3.x' into 4.x

This commit is contained in:
Dan Harrin
2025-03-20 09:06:14 +00:00
10 changed files with 82 additions and 14 deletions
@@ -172,8 +172,6 @@ class EditProfile extends Page
$this->handleRecordUpdate($this->getUser(), $data);
$this->callHook('afterSave');
$this->commitDatabaseTransaction();
} catch (Halt $exception) {
$exception->shouldRollbackDatabaseTransaction() ?
$this->rollBackDatabaseTransaction() :
@@ -186,6 +184,8 @@ class EditProfile extends Page
throw $exception;
}
$this->commitDatabaseTransaction();
if (request()->hasSession() && array_key_exists('password', $data)) {
request()->session()->put([
'password_hash_' . Filament::getAuthGuard() => $data['password'],
@@ -123,8 +123,6 @@ abstract class EditTenantProfile extends Page
$this->handleRecordUpdate($this->tenant, $data);
$this->callHook('afterSave');
$this->commitDatabaseTransaction();
} catch (Halt $exception) {
$exception->shouldRollbackDatabaseTransaction() ?
$this->rollBackDatabaseTransaction() :
@@ -137,6 +135,8 @@ abstract class EditTenantProfile extends Page
throw $exception;
}
$this->commitDatabaseTransaction();
$this->getSavedNotification()?->send();
if ($redirectUrl = $this->getRedirectUrl()) {
@@ -88,8 +88,6 @@ abstract class RegisterTenant extends SimplePage
$this->form->model($this->tenant)->saveRelationships();
$this->callHook('afterRegister');
$this->commitDatabaseTransaction();
} catch (Halt $exception) {
$exception->shouldRollbackDatabaseTransaction() ?
$this->rollBackDatabaseTransaction() :
@@ -102,6 +100,8 @@ abstract class RegisterTenant extends SimplePage
throw $exception;
}
$this->commitDatabaseTransaction();
if ($redirectUrl = $this->getRedirectUrl()) {
$this->redirect($redirectUrl, navigate: FilamentView::hasSpaMode() && is_app_url($redirectUrl));
}
@@ -95,8 +95,6 @@ class CreateRecord extends Page
$this->form->model($this->getRecord())->saveRelationships();
$this->callHook('afterCreate');
$this->commitDatabaseTransaction();
} catch (Halt $exception) {
$exception->shouldRollbackDatabaseTransaction() ?
$this->rollBackDatabaseTransaction() :
@@ -109,6 +107,8 @@ class CreateRecord extends Page
throw $exception;
}
$this->commitDatabaseTransaction();
$this->rememberData();
$this->getCreatedNotification()?->send();
@@ -147,8 +147,6 @@ class EditRecord extends Page
$this->handleRecordUpdate($this->getRecord(), $data);
$this->callHook('afterSave');
$this->commitDatabaseTransaction();
} catch (Halt $exception) {
$exception->shouldRollbackDatabaseTransaction() ?
$this->rollBackDatabaseTransaction() :
@@ -161,6 +159,8 @@ class EditRecord extends Page
throw $exception;
}
$this->commitDatabaseTransaction();
$this->rememberData();
if ($shouldSendSavedNotification) {
@@ -196,8 +196,6 @@ class EditRecord extends Page
$this->handleRecordUpdate($this->getRecord(), $data);
$this->callHook('afterSave');
$this->commitDatabaseTransaction();
} catch (Halt $exception) {
$exception->shouldRollbackDatabaseTransaction() ?
$this->rollBackDatabaseTransaction() :
@@ -210,6 +208,8 @@ class EditRecord extends Page
throw $exception;
}
$this->commitDatabaseTransaction();
$this->rememberData();
}
@@ -86,8 +86,6 @@ class SettingsPage extends Page
$settings->save();
$this->callHook('afterSave');
$this->commitDatabaseTransaction();
} catch (Halt $exception) {
$exception->shouldRollbackDatabaseTransaction() ?
$this->rollBackDatabaseTransaction() :
@@ -100,6 +98,8 @@ class SettingsPage extends Page
throw $exception;
}
$this->commitDatabaseTransaction();
$this->rememberData();
$this->getSavedNotification()?->send();
@@ -5,6 +5,9 @@ namespace Filament\Tests\Fixtures\Resources\Posts\Pages;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Filament\Tests\Fixtures\Resources\Posts\PostResource;
use Filament\Tests\Fixtures\Models\Post;
use Illuminate\Support\Facades\DB;
use RuntimeException;
class EditPost extends EditRecord
{
@@ -17,6 +20,16 @@ class EditPost extends EditRecord
Actions\ActionGroup::make([
Actions\DeleteAction::make(),
]),
Actions\Action::make('randomize_title')
->databaseTransaction()
->action(action: function (Post $record) {
DB::afterCommit(function () {
throw new RuntimeException('This exception, happening after the successful commit of the current transaction, should not trigger a rollback by Filament.');
});
$record->title = 'Test';
$record->save();
}),
];
}
@@ -3,6 +3,7 @@
namespace Filament\Tests\Fixtures\Resources\Posts;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
@@ -15,6 +16,9 @@ use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tests\Fixtures\Models\Post;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use RuntimeException;
class PostResource extends Resource
{
@@ -62,6 +66,16 @@ class PostResource extends Resource
->actions([
ViewAction::make(),
EditAction::make(),
Action::make('randomize_title')
->databaseTransaction()
->action(action: function (Post $record) {
DB::afterCommit(function () {
throw new RuntimeException('This exception, happening after the successful commit of the current transaction, should not trigger a rollback by Filament.');
});
$record->title = Str::random(10);
$record->save();
}),
DeleteAction::make(),
])
->bulkActions([
@@ -18,6 +18,7 @@ use Filament\Tests\Fixtures\Resources\Tickets\Pages\EditTicket;
use Filament\Tests\Fixtures\Resources\Tickets\TicketResource;
use Filament\Tests\Panels\Resources\TestCase;
use Illuminate\Auth\Access\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use function Filament\Tests\livewire;
@@ -123,6 +124,27 @@ it('can refresh data', function (): void {
]);
});
test('actions will not interfere with database transactions on an error', function () {
$post = Post::factory()->create();
$transactionLevel = DB::transactionLevel();
try {
livewire(EditPost::class, [
'record' => $post->getKey(),
])
->callAction('randomize_title');
} catch (Exception $exception) {
// This can be caught and handled somewhere else, code continues...
}
// Original transaction level should be unaffected...
expect(DB::transactionLevel())
->toBe($transactionLevel);
});
it('can ticket messages page without a policy', function (): void {
$message = TicketMessage::factory()
->create();
@@ -24,6 +24,7 @@ use Filament\Tests\Fixtures\Resources\Tickets\TicketResource;
use Filament\Tests\Fixtures\Resources\Users\UserResource;
use Filament\Tests\Panels\Resources\TestCase;
use Illuminate\Auth\Access\Response;
use Illuminate\Support\Facades\DB;
use function Filament\Tests\livewire;
use function Pest\Laravel\assertSoftDeleted;
@@ -133,6 +134,24 @@ it('can bulk delete posts', function (): void {
}
});
test('table actions will not interfere with database transactions on an error', function () {
$post = Post::factory()->create();
$transactionLevel = DB::transactionLevel();
try {
livewire(ListPosts::class)
->callTableAction('randomize_title', $post);
} catch (Exception $exception) {
// This can be caught and handled somewhere else, code continues...
}
// Original transaction level should be unaffected...
expect(DB::transactionLevel())
->toBe($transactionLevel);
});
it('can render ticket messages page without a policy', function (): void {
TicketMessage::factory(10)
->create();