Skip to content

Commit d1d764e

Browse files
committed
Add failing test: global scopes on union queries are not applied correctly
1 parent 718c09e commit d1d764e

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

tests/Integration/Database/EloquentCursorPaginateTest.php

+68-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ protected function afterRefreshingDatabase()
1919
$table->timestamps();
2020
});
2121

22+
Schema::create('test_post_with_global_scopes', function (Blueprint $table) {
23+
$table->increments('id');
24+
$table->string('title')->nullable();
25+
$table->unsignedInteger('user_id')->nullable();
26+
$table->timestamps();
27+
});
28+
2229
Schema::create('test_users', function ($table) {
2330
$table->increments('id');
2431
$table->string('name')->nullable();
@@ -54,6 +61,50 @@ public function testPaginationWithUnion()
5461
$this->assertSame(['user_id'], $result->getOptions()['parameters']);
5562
}
5663

64+
public function testPaginationWithUnionAndGlobalScopes()
65+
{
66+
TestPostWithGlobalScope::create(['title' => 'Hello world', 'user_id' => 1]);
67+
TestPostWithGlobalScope::create(['title' => 'Goodbye world', 'user_id' => 2]);
68+
TestPostWithGlobalScope::create(['title' => 'Howdy', 'user_id' => 3]);
69+
TestPostWithGlobalScope::create(['title' => '4th', 'user_id' => 4]);
70+
71+
$table1 = TestPostWithGlobalScope::select('id')->whereIn('user_id', [1, 2]);
72+
$table2 = TestPostWithGlobalScope::select('id')->whereIn('user_id', [3, 4]);
73+
74+
$columns = ['id'];
75+
$cursorName = 'cursor-name';
76+
$cursor = new Cursor(['id' => 1]);
77+
78+
$result = $table1
79+
->union($table2)
80+
->orderBy('id', 'asc')
81+
->cursorPaginate(1, $columns, $cursorName, $cursor);
82+
83+
$this->assertSame(['id'], $result->getOptions()['parameters']);
84+
}
85+
86+
public function testPaginationWithUnionAndGlobalScopesToBase()
87+
{
88+
TestPostWithGlobalScope::create(['title' => 'Hello world', 'user_id' => 1]);
89+
TestPostWithGlobalScope::create(['title' => 'Goodbye world', 'user_id' => 2]);
90+
TestPostWithGlobalScope::create(['title' => 'Howdy', 'user_id' => 3]);
91+
TestPostWithGlobalScope::create(['title' => '4th', 'user_id' => 4]);
92+
93+
$table1 = TestPostWithGlobalScope::select('id')->whereIn('user_id', [1, 2]);
94+
$table2 = TestPostWithGlobalScope::select('id')->whereIn('user_id', [3, 4]);
95+
96+
$columns = ['id'];
97+
$cursorName = 'cursor-name';
98+
$cursor = new Cursor(['id' => 1]);
99+
100+
$result = $table1
101+
->union($table2->toBase())
102+
->orderBy('id', 'asc')
103+
->cursorPaginate(1, $columns, $cursorName, $cursor);
104+
105+
$this->assertSame(['id'], $result->getOptions()['parameters']);
106+
}
107+
57108
public function testPaginationWithDistinct()
58109
{
59110
for ($i = 1; $i <= 3; $i++) {
@@ -181,9 +232,9 @@ public function testPaginationWithMultipleUnionAndMultipleWhereClauses()
181232
$cursorName = 'cursor-name';
182233
$cursor = new Cursor(['id' => 1]);
183234

184-
$result = $table1->toBase()
185-
->union($table2->toBase())
186-
->union($table3->toBase())
235+
$result = $table1
236+
->union($table2)
237+
->union($table3)
187238
->orderBy('id', 'asc')
188239
->cursorPaginate(1, $columns, $cursorName, $cursor);
189240

@@ -284,6 +335,20 @@ class TestPost extends Model
284335
protected $guarded = [];
285336
}
286337

338+
class TestPostWithGlobalScope extends Model
339+
{
340+
protected $guarded = [];
341+
342+
public static function boot()
343+
{
344+
parent::boot();
345+
346+
static::addGlobalScope('global', function ($builder) {
347+
$builder->where('id', '>', 0);
348+
});
349+
}
350+
}
351+
287352
class TestUser extends Model
288353
{
289354
protected $guarded = [];

0 commit comments

Comments
 (0)