@@ -50,8 +50,15 @@ protected function defineDatabaseMigrations(): void
50
50
$ table ->softDeletes ();
51
51
});
52
52
53
+ Schema::create ('posts ' , function (Blueprint $ table ) {
54
+ $ table ->increments ('id ' );
55
+ $ table ->integer ('user_id ' );
56
+ $ table ->timestamps ();
57
+ });
58
+
53
59
$ this ->beforeApplicationDestroyed (function () {
54
60
Schema::dropIfExists ('users ' );
61
+ Schema::dropIfExists ('posts ' );
55
62
});
56
63
}
57
64
@@ -60,14 +67,14 @@ public function testWithRouteCachingEnabled()
60
67
$ this ->defineCacheRoutes (<<<PHP
61
68
<?php
62
69
63
- use Illuminate\Tests\Integration\Routing\ImplicitBindingModel ;
70
+ use Illuminate\Tests\Integration\Routing\ImplicitBindingUser ;
64
71
65
- Route::post('/user/{user}', function (ImplicitBindingModel \$user) {
72
+ Route::post('/user/{user}', function (ImplicitBindingUser \$user) {
66
73
return \$user;
67
74
})->middleware('web');
68
75
PHP );
69
76
70
- $ user = ImplicitBindingModel ::create (['name ' => 'Dries ' ]);
77
+ $ user = ImplicitBindingUser ::create (['name ' => 'Dries ' ]);
71
78
72
79
$ response = $ this ->postJson ("/user/ {$ user ->id }" );
73
80
@@ -79,11 +86,11 @@ public function testWithRouteCachingEnabled()
79
86
80
87
public function testWithoutRouteCachingEnabled ()
81
88
{
82
- $ user = ImplicitBindingModel ::create (['name ' => 'Dries ' ]);
89
+ $ user = ImplicitBindingUser ::create (['name ' => 'Dries ' ]);
83
90
84
91
config (['app.key ' => str_repeat ('a ' , 32 )]);
85
92
86
- Route::post ('/user/{user} ' , function (ImplicitBindingModel $ user ) {
93
+ Route::post ('/user/{user} ' , function (ImplicitBindingUser $ user ) {
87
94
return $ user ;
88
95
})->middleware (['web ' ]);
89
96
@@ -97,13 +104,13 @@ public function testWithoutRouteCachingEnabled()
97
104
98
105
public function testSoftDeletedModelsAreNotRetrieved ()
99
106
{
100
- $ user = ImplicitBindingModel ::create (['name ' => 'Dries ' ]);
107
+ $ user = ImplicitBindingUser ::create (['name ' => 'Dries ' ]);
101
108
102
109
$ user ->delete ();
103
110
104
111
config (['app.key ' => str_repeat ('a ' , 32 )]);
105
112
106
- Route::post ('/user/{user} ' , function (ImplicitBindingModel $ user ) {
113
+ Route::post ('/user/{user} ' , function (ImplicitBindingUser $ user ) {
107
114
return $ user ;
108
115
})->middleware (['web ' ]);
109
116
@@ -114,13 +121,13 @@ public function testSoftDeletedModelsAreNotRetrieved()
114
121
115
122
public function testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethod ()
116
123
{
117
- $ user = ImplicitBindingModel ::create (['name ' => 'Dries ' ]);
124
+ $ user = ImplicitBindingUser ::create (['name ' => 'Dries ' ]);
118
125
119
126
$ user ->delete ();
120
127
121
128
config (['app.key ' => str_repeat ('a ' , 32 )]);
122
129
123
- Route::post ('/user/{user} ' , function (ImplicitBindingModel $ user ) {
130
+ Route::post ('/user/{user} ' , function (ImplicitBindingUser $ user ) {
124
131
return $ user ;
125
132
})->middleware (['web ' ])->withTrashed ();
126
133
@@ -131,13 +138,96 @@ public function testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethod()
131
138
'name ' => $ user ->name ,
132
139
]);
133
140
}
141
+
142
+ public function testEnforceScopingImplicitRouteBindings ()
143
+ {
144
+ $ user = ImplicitBindingUser::create (['name ' => 'Dries ' ]);
145
+ $ post = ImplicitBindingPost::create (['user_id ' => 2 ]);
146
+ $ this ->assertEmpty ($ user ->posts );
147
+
148
+ config (['app.key ' => str_repeat ('a ' , 32 )]);
149
+
150
+ Route::scopeBindings ()->group (function () {
151
+ Route::get ('/user/{user}/post/{post} ' , function (ImplicitBindingUser $ user , ImplicitBindingPost $ post ) {
152
+ return [$ user , $ post ];
153
+ })->middleware (['web ' ]);
154
+ });
155
+
156
+ $ response = $ this ->getJson ("/user/ {$ user ->id }/post/ {$ post ->id }" );
157
+
158
+ $ response ->assertNotFound ();
159
+ }
160
+
161
+ public function testEnforceScopingImplicitRouteBindingsWithRouteCachingEnabled ()
162
+ {
163
+ $ user = ImplicitBindingUser::create (['name ' => 'Dries ' ]);
164
+ $ post = ImplicitBindingPost::create (['user_id ' => 2 ]);
165
+ $ this ->assertEmpty ($ user ->posts );
166
+
167
+ $ this ->defineCacheRoutes (<<<PHP
168
+ <?php
169
+
170
+ use Illuminate\Tests\Integration\Routing\ImplicitBindingUser;
171
+ use Illuminate\Tests\Integration\Routing\ImplicitBindingPost;
172
+
173
+ Route::group(['scoping' => true], function () {
174
+ Route::get('/user/{user}/post/{post}', function (ImplicitBindingUser \$user, ImplicitBindingPost \$post) {
175
+ return [ \$user, \$post];
176
+ })->middleware(['web']);
177
+ });
178
+ PHP );
179
+
180
+ $ response = $ this ->getJson ("/user/ {$ user ->id }/post/ {$ post ->id }" );
181
+
182
+ $ response ->assertNotFound ();
183
+ }
184
+
185
+ public function testWithoutEnforceScopingImplicitRouteBindings ()
186
+ {
187
+ $ user = ImplicitBindingUser::create (['name ' => 'Dries ' ]);
188
+ $ post = ImplicitBindingPost::create (['user_id ' => 2 ]);
189
+ $ this ->assertEmpty ($ user ->posts );
190
+
191
+ config (['app.key ' => str_repeat ('a ' , 32 )]);
192
+
193
+ Route::group (['scoping ' => false ], function () {
194
+ Route::get ('/user/{user}/post/{post} ' , function (ImplicitBindingUser $ user , ImplicitBindingPost $ post ) {
195
+ return [$ user , $ post ];
196
+ })->middleware (['web ' ]);
197
+ });
198
+
199
+ $ response = $ this ->getJson ("/user/ {$ user ->id }/post/ {$ post ->id }" );
200
+ $ response ->assertOk ();
201
+ $ response ->assertJson ([
202
+ [
203
+ 'id ' => $ user ->id ,
204
+ 'name ' => $ user ->name ,
205
+ ],
206
+ [
207
+ 'id ' => 1 ,
208
+ 'user_id ' => 2 ,
209
+ ],
210
+ ]);
211
+ }
134
212
}
135
213
136
- class ImplicitBindingModel extends Model
214
+ class ImplicitBindingUser extends Model
137
215
{
138
216
use SoftDeletes;
139
217
140
218
public $ table = 'users ' ;
141
219
142
220
protected $ fillable = ['name ' ];
221
+
222
+ public function posts ()
223
+ {
224
+ return $ this ->hasMany (ImplicitBindingPost::class, 'user_id ' );
225
+ }
226
+ }
227
+
228
+ class ImplicitBindingPost extends Model
229
+ {
230
+ public $ table = 'posts ' ;
231
+
232
+ protected $ fillable = ['user_id ' ];
143
233
}
0 commit comments