-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1650 from cmfcmf/unique-trees-2.4
[2.4] Fix unique slugs with the TreeSlugHandler
- Loading branch information
Showing
4 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
lib/Gedmo/Sluggable/Handler/SlugHandlerWithUniqueCallbackInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Gedmo\Sluggable\Handler; | ||
|
||
use Gedmo\Sluggable\Mapping\Event\SluggableAdapter; | ||
|
||
/** | ||
* This adds the ability to a SlugHandler to change the slug just before its | ||
* uniqueness is ensured. It is also called if the unique options is _not_ | ||
* set. | ||
* | ||
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com> | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
interface SlugHandlerWithUniqueCallbackInterface extends SlugHandlerInterface | ||
{ | ||
/** | ||
* Callback for slug handlers before it is made unique | ||
* | ||
* @param SluggableAdapter $ea | ||
* @param array $config | ||
* @param object $object | ||
* @param string $slug | ||
* | ||
* @return void | ||
*/ | ||
public function beforeMakingUnique(SluggableAdapter $ea, array &$config, $object, &$slug); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
tests/Gedmo/Sluggable/Handlers/TreeSlugHandlerUniqueTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Gedmo\Sluggable; | ||
|
||
use Doctrine\Common\EventManager; | ||
use Gedmo\Tree\TreeListener; | ||
use Sluggable\Fixture\Handler\TreeSlug; | ||
use Tool\BaseTestCaseORM; | ||
|
||
class TreeSlugHandlerUniqueTest extends BaseTestCaseORM | ||
{ | ||
const TARGET = "Sluggable\\Fixture\\Handler\\TreeSlug"; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$evm = new EventManager(); | ||
$evm->addEventSubscriber(new SluggableListener()); | ||
$evm->addEventSubscriber(new TreeListener()); | ||
|
||
$this->getMockSqliteEntityManager($evm); | ||
} | ||
|
||
public function testUniqueRoot() | ||
{ | ||
$foo1 = new TreeSlug(); | ||
$foo1->setTitle('Foo'); | ||
|
||
$foo2 = new TreeSlug(); | ||
$foo2->setTitle('Foo'); | ||
|
||
$this->em->persist($foo1); | ||
$this->em->persist($foo2); | ||
|
||
$this->em->flush(); | ||
|
||
$this->assertEquals('foo', $foo1->getSlug()); | ||
$this->assertEquals('foo-1', $foo2->getSlug()); | ||
} | ||
|
||
public function testUniqueLeaf() | ||
{ | ||
$root = new TreeSlug(); | ||
$root->setTitle('root'); | ||
|
||
$foo1 = new TreeSlug(); | ||
$foo1->setTitle('Foo'); | ||
$foo1->setParent($root); | ||
|
||
$foo2 = new TreeSlug(); | ||
$foo2->setTitle('Foo'); | ||
$foo2->setParent($root); | ||
|
||
$this->em->persist($root); | ||
$this->em->persist($foo1); | ||
$this->em->persist($foo2); | ||
|
||
$this->em->flush(); | ||
|
||
$this->assertEquals('root/foo', $foo1->getSlug()); | ||
$this->assertEquals('root/foo-1', $foo2->getSlug()); | ||
} | ||
|
||
protected function getUsedEntityFixtures() | ||
{ | ||
return array( | ||
self::TARGET, | ||
); | ||
} | ||
} |