Skip to content

Commit

Permalink
slug should have a consecutive number
Browse files Browse the repository at this point in the history
fixes NightJar#11

1
12
123
isn't that consecutive, is it?
  • Loading branch information
wernerkrauss committed Apr 29, 2020
1 parent 974fa9f commit 00cee2c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ public function onBeforeWrite()
}

$count = 1;
$origSlug = $owner->URLSlug;
while ($collisionList->filter($filter)->exists()) {
$owner->URLSlug = implode('-', [$owner->URLSlug, $count++]);
$owner->URLSlug = implode('-', [$origSlug, $count++]);
$filter['URLSlug'] = $owner->URLSlug;
}
} elseif ($slugHasChanged) {
Expand Down
10 changes: 9 additions & 1 deletion tests/SlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,17 @@ public function testSlugCollisionsCorrectThemselves()
'Title' => 'First news',
'ParentID' => $newsPage->ID,
]);
$newArticle->extend('onBeforeWrite');
$newArticle->write();
$this->assertEquals('first-news-1', $newArticle->URLSlug);
$this->assertEquals('First news', $newArticle->Title);

$anotherArticle = Article::create()->update([
'Title' => 'First news',
'ParentID' => $newsPage->ID,
]);
$anotherArticle->write();
$this->assertEquals('first-news-2', $anotherArticle->URLSlug);
$this->assertEquals('First news', $anotherArticle->Title);
}

/**
Expand Down

0 comments on commit 00cee2c

Please sign in to comment.