Safer slugs (#135)

* Safer slugs

* fix test case for slug
This commit is contained in:
formsdev 2023-06-19 18:20:31 +05:30 committed by GitHub
parent 5087724847
commit e47dea7936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -10,6 +10,7 @@ use Database\Factories\FormFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Spatie\Sluggable\HasSlug; use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions; use Spatie\Sluggable\SlugOptions;
use Stevebauman\Purify\Facades\Purify; use Stevebauman\Purify\Facades\Purify;
@ -245,7 +246,9 @@ class Form extends Model
{ {
return SlugOptions::create() return SlugOptions::create()
->doNotGenerateSlugsOnUpdate() ->doNotGenerateSlugsOnUpdate()
->generateSlugsFrom('title') ->generateSlugsFrom(function (Form $form) {
return $form->title . ' ' . Str::random(6);
})
->saveSlugsTo('slug'); ->saveSlugsTo('slug');
} }

View File

@ -86,7 +86,9 @@ it('can regenerate a form url', function () {
->assertSuccessful() ->assertSuccessful()
->assertJson(function (AssertableJson $json) use ($newSlug) { ->assertJson(function (AssertableJson $json) use ($newSlug) {
return $json->where('type', 'success') return $json->where('type', 'success')
->where('form.slug', $newSlug) ->where('form.slug', function ($slug) use ($newSlug) {
return substr($slug, 0, -6) == substr($newSlug, 0, -6);
})
->etc(); ->etc();
}); });