|
4 | 4 | use Spatie\Sluggable\SlugOptions;
|
5 | 5 | use Spatie\Sluggable\Tests\TestSupport\TestModel;
|
6 | 6 | use Spatie\Sluggable\Tests\TestSupport\TestModelSoftDeletes;
|
| 7 | +use Spatie\Sluggable\Tests\TestSupport\TranslatableModel; |
7 | 8 |
|
8 | 9 | it('will save a slug when saving a model', function () {
|
9 | 10 | $model = TestModel::create(['name' => 'this is a test']);
|
@@ -349,3 +350,61 @@ public function getSlugOptions(): SlugOptions
|
349 | 350 |
|
350 | 351 | expect($savedModel->id)->toEqual($model->id);
|
351 | 352 | });
|
| 353 | + |
| 354 | +it('can customize query using additionalQuery parameter in findBySlug', function () { |
| 355 | + $model = new class () extends TestModel { |
| 356 | + public function getSlugOptions(): SlugOptions |
| 357 | + { |
| 358 | + return parent::getSlugOptions()->saveSlugsTo('url'); |
| 359 | + } |
| 360 | + }; |
| 361 | + |
| 362 | + $model->url = 'custom-slug'; |
| 363 | + $model->other_field = 'active'; |
| 364 | + $model->save(); |
| 365 | + |
| 366 | + $savedModel = $model::findBySlug('custom-slug', ['*'], function ($query) { |
| 367 | + $query->where('other_field', 'active'); |
| 368 | + }); |
| 369 | + |
| 370 | + expect($savedModel) |
| 371 | + ->not->toBeNull() |
| 372 | + ->and($savedModel->id) |
| 373 | + ->toEqual($model->id); |
| 374 | + |
| 375 | + $noMatch = $model::findBySlug('custom-slug', ['*'], function ($query) { |
| 376 | + $query->where('status', 'inactive'); |
| 377 | + }); |
| 378 | + |
| 379 | + expect($noMatch)->toBeNull(); |
| 380 | +}); |
| 381 | + |
| 382 | +it('can find models using findBySlug with fallback locale', function () { |
| 383 | + config()->set('app.fallback_locale', 'en'); |
| 384 | + app()->setLocale('tr'); |
| 385 | + |
| 386 | + $model = new class () extends TranslatableModel { |
| 387 | + public function getSlugOptions(): SlugOptions |
| 388 | + { |
| 389 | + return parent::getSlugOptions()->saveSlugsTo('slug'); |
| 390 | + } |
| 391 | + }; |
| 392 | + |
| 393 | + $model->slug = ['en' => 'english-slug', 'tr' => 'turkish-slug']; |
| 394 | + $model->name = ['en' => 'English Name', 'tr' => 'Turkish Name']; |
| 395 | + $model->save(); |
| 396 | + |
| 397 | + $foundModelInCurrentLocale = $model::findBySlug('turkish-slug'); |
| 398 | + |
| 399 | + expect($foundModelInCurrentLocale) |
| 400 | + ->not->toBeNull() |
| 401 | + ->and($foundModelInCurrentLocale->id) |
| 402 | + ->toEqual($model->id); |
| 403 | + |
| 404 | + $foundModelInFallbackLocale = $model::findBySlug('english-slug'); |
| 405 | + |
| 406 | + expect($foundModelInFallbackLocale) |
| 407 | + ->not->toBeNull() |
| 408 | + ->and($foundModelInFallbackLocale->id) |
| 409 | + ->toEqual($model->id); |
| 410 | +}); |
0 commit comments