From ad7bd37c2e3b5c52aa402e21d6fb07388690f6e2 Mon Sep 17 00:00:00 2001 From: Thijs van den Anker Date: Fri, 16 May 2025 13:42:39 +0200 Subject: [PATCH] Add failing test: global scopes on union queries are not applied correctly --- .../Database/EloquentCursorPaginateTest.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tests/Integration/Database/EloquentCursorPaginateTest.php b/tests/Integration/Database/EloquentCursorPaginateTest.php index fb6d83b899b4..d358279d7e96 100644 --- a/tests/Integration/Database/EloquentCursorPaginateTest.php +++ b/tests/Integration/Database/EloquentCursorPaginateTest.php @@ -19,6 +19,13 @@ protected function afterRefreshingDatabase() $table->timestamps(); }); + Schema::create('test_post_with_global_scopes', function (Blueprint $table) { + $table->increments('id'); + $table->string('title')->nullable(); + $table->unsignedInteger('user_id')->nullable(); + $table->timestamps(); + }); + Schema::create('test_users', function ($table) { $table->increments('id'); $table->string('name')->nullable(); @@ -54,6 +61,50 @@ public function testPaginationWithUnion() $this->assertSame(['user_id'], $result->getOptions()['parameters']); } + public function testPaginationWithUnionAndGlobalScopes() + { + TestPostWithGlobalScope::create(['title' => 'Hello world', 'user_id' => 1]); + TestPostWithGlobalScope::create(['title' => 'Goodbye world', 'user_id' => 2]); + TestPostWithGlobalScope::create(['title' => 'Howdy', 'user_id' => 3]); + TestPostWithGlobalScope::create(['title' => '4th', 'user_id' => 4]); + + $table1 = TestPostWithGlobalScope::select('id')->whereIn('user_id', [1, 2]); + $table2 = TestPostWithGlobalScope::select('id')->whereIn('user_id', [3, 4]); + + $columns = ['id']; + $cursorName = 'cursor-name'; + $cursor = new Cursor(['id' => 1]); + + $result = $table1 + ->union($table2) + ->orderBy('id', 'asc') + ->cursorPaginate(1, $columns, $cursorName, $cursor); + + $this->assertSame(['id'], $result->getOptions()['parameters']); + } + + public function testPaginationWithUnionAndGlobalScopesToBase() + { + TestPostWithGlobalScope::create(['title' => 'Hello world', 'user_id' => 1]); + TestPostWithGlobalScope::create(['title' => 'Goodbye world', 'user_id' => 2]); + TestPostWithGlobalScope::create(['title' => 'Howdy', 'user_id' => 3]); + TestPostWithGlobalScope::create(['title' => '4th', 'user_id' => 4]); + + $table1 = TestPostWithGlobalScope::select('id')->whereIn('user_id', [1, 2]); + $table2 = TestPostWithGlobalScope::select('id')->whereIn('user_id', [3, 4]); + + $columns = ['id']; + $cursorName = 'cursor-name'; + $cursor = new Cursor(['id' => 1]); + + $result = $table1 + ->union($table2->toBase()) + ->orderBy('id', 'asc') + ->cursorPaginate(1, $columns, $cursorName, $cursor); + + $this->assertSame(['id'], $result->getOptions()['parameters']); + } + public function testPaginationWithDistinct() { for ($i = 1; $i <= 3; $i++) { @@ -284,6 +335,20 @@ class TestPost extends Model protected $guarded = []; } +class TestPostWithGlobalScope extends Model +{ + protected $guarded = []; + + public static function boot() + { + parent::boot(); + + static::addGlobalScope('global', function ($builder) { + $builder->where('id', '>', 0); + }); + } +} + class TestUser extends Model { protected $guarded = [];