Skip to content

Commit

Permalink
Test unique slug strings
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidgh83 committed Oct 29, 2024
1 parent cd66982 commit c09e23c
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions tests/HasSlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,164 @@ protected function getSlugConfig(): SlugOptions
$this->assertDatabaseHas('test_model', ['slugs' => json_encode($testModel->slugs)]);
$this->assertInstanceOf(Collection::class, $testModel->slugs);
}

public function testItGeneratesUniqiueSlugsOnDemand()
{
$model = new class extends TestModel {
protected $sluggables = ['name', 'title'];

protected function getSlugConfig(): SlugOptions
{
return SlugOptions::make()
->avoidDuplicates()
->saveSlugsTo('slugs')
;
}
};

$model->name = $name = 'some text goes here';
$model->title = $title = 'some title';
$model->save();

for ($i = 1; $i < 10; ++$i) {
$testModel = new $model();
$testModel->name = $name;
$testModel->title = $title;
$testModel->save();

// Ensure slug is different
$this->assertNotEquals($name, $testModel->slugs['name']);
$this->assertNotEquals($title, $testModel->slugs['title']);

// Ensure slug is unique in the database
$this->assertDatabaseHas('test_model', ['name' => $name, 'slugs' => json_encode([
'name' => 'some-text-goes-here-' . $i,
'title' => 'some-title-' . $i,
])]);
}

$this->assertDatabaseCount('test_model', 10);
}

public function testGeneratedSlugsAreTrimmed()
{
$model = new class extends TestModel {
protected $sluggables = ['name', 'title'];

protected function getSlugConfig(): SlugOptions
{
return SlugOptions::make()
->saveSlugsTo('slugs')
;
}
};

$model->name = 'some text with spaces at the end ';
$model->title = 'some title with hyphen at the end ---';
$model->save();

$this->assertEquals('some-text-with-spaces-at-the-end', $model->slugs['name']);
$this->assertEquals('some-title-with-hyphen-at-the-end', $model->slugs['title']);
$this->assertDatabaseHas('test_model', ['name' => $model->name, 'slugs' => json_encode([
'name' => 'some-text-with-spaces-at-the-end',
'title' => 'some-title-with-hyphen-at-the-end',
])]);
}

public function testEmptySluggableAttributesAreIgnoredWithEnablingUniqueSlugsOption()
{
$model = new class extends TestModel {
protected $sluggables = ['name', 'title'];

protected function getSlugConfig(): SlugOptions
{
return SlugOptions::make()
->avoidDuplicates()
->saveSlugsTo('slugs')
;
}
};

$testModel = new $model();
$testModel->name = $name = 'some text goes here';
$testModel->save();

for ($i = 1; $i < 10; ++$i) {
$testModel = new $model();
$testModel->name = $name;
$testModel->save();

$savedModel = $testModel::find($testModel->id);

$this->assertEquals(['name' => 'some-text-goes-here-' . $i, 'title' => ''], $savedModel->slugs);
}

$this->assertDatabaseCount('test_model', 10);
}

public function testEmptySluggableAttributesAreIgnoredWithoutEnablingUniqueSlugsOption()
{
$model = new class extends TestModel {
protected $sluggables = ['name', 'title'];

protected function getSlugConfig(): SlugOptions
{
return SlugOptions::make()
->saveSlugsTo('slugs')
;
}
};

$testModel = new $model();
$testModel->name = 'some text goes here';
$testModel->save();

$this->assertDatabaseHas('test_model', ['name' => 'some text goes here', 'slugs' => json_encode(['name' => 'some-text-goes-here', 'title' => ''])]);
$this->assertDatabaseCount('test_model', 1);

$testModel = new $model();
$testModel->name = 'some text goes here';
$testModel->save();

$this->assertDatabaseCount('test_model', 2);

$savedModel = $testModel::all();
$this->assertEquals($savedModel[0]->slugs, $savedModel[1]->slugs); // Duplicated slugs are allowed
}

public function testItGeneratesUniqueSlugsWithMaximumLength()
{
$model = new class extends TestModel {
protected $sluggables = ['name'];

protected function getSlugConfig(): SlugOptions
{
return SlugOptions::make()
->avoidDuplicates()
->slugShouldBeNoLongerThan(10)
->saveSlugsTo('slugs')
;
}
};

$testModel = new $model();
$testModel->name = $name = $this->faker->sentence(5);
$testModel->save();

$generatedSlug = $testModel->slugs['name'];

$this->assertLessThanOrEqual(10, strlen($generatedSlug));
$this->assertDatabaseHas('test_model', ['name' => $testModel->name, 'slugs' => json_encode(['name' => $generatedSlug])]);

for ($i = 1; $i < 10; ++$i) {
$testModel = new $model();
$testModel->name = $name;
$testModel->save();

$this->assertLessThanOrEqual(10, strlen($testModel->slugs['name']));
$this->assertMatchesRegularExpression('/-' . $i . '$/', $testModel->slugs['name']);
}

$this->assertDatabaseCount('test_model', 10);
}
}

0 comments on commit c09e23c

Please sign in to comment.