Skip to content
This repository has been archived by the owner on Sep 23, 2020. It is now read-only.

Commit

Permalink
added node-name-slugifier to centralice additional node name replacer
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Apr 12, 2017
1 parent f51fc4c commit af07068
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 34 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
CHANGELOG for Sulu Document Manager
===================================

* dev-develop
* ENHANCEMENT #110 Added node-name-slugifier to centralice additional node name replacer

* 0.9.1 (2017-03-16)
* ENHANCEMENT #107 Added VersionNotFoundException

Expand Down
50 changes: 50 additions & 0 deletions lib/Slugifier/NodeNameSlugifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Component\DocumentManager\Slugifier;

use Symfony\Cmf\Api\Slugifier\SlugifierInterface;

/**
* Wraps default slugifier and add some additional node-name stuff.
*/
class NodeNameSlugifier implements SlugifierInterface
{
/**
* @var SlugifierInterface
*/
private $slugifier;

/**
* @param SlugifierInterface $slugifier
*/
public function __construct(SlugifierInterface $slugifier)
{
$this->slugifier = $slugifier;
}

/**
* Slugifies given string to a valid node-name.
*
* @param string $text
*
* @return string
*/
public function slugify($text)
{
$text = $this->slugifier->slugify($text);

// jackrabbit can not handle node-names which contains a number followed by "e" e.g. 10e
$text = preg_replace('((\d+)([eE]))', '$1-$2', $text);

return $text;
}
}
3 changes: 0 additions & 3 deletions lib/Subscriber/Behavior/Path/AutoNameSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,6 @@ private function getName(

$name = $this->slugifier->slugify($title);

// jackrabbit can not handle node-names which contains a number followed by "e" e.g. 10e
$name = preg_replace('((\d+)([eE]))', '$1-$2', $name);

return $this->resolver->resolveName($parentNode, $name, $node, $autoRename);
}

Expand Down
5 changes: 5 additions & 0 deletions symfony-di/core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<argument>Ferrandini\Urlizer::urlize</argument>
</service>

<service id="sulu_document_manager.node_name_slugifier"
class="Sulu\Component\DocumentManager\Slugifier\NodeNameSlugifier" public="false">
<argument type="service" id="sulu_document_manager.slugifier"/>
</service>

<service id="sulu_document_manager.namespace_registry" class="Sulu\Component\DocumentManager\NamespaceRegistry" public="false">
<argument>%sulu_document_manager.namespace_mapping%</argument>
</service>
Expand Down
2 changes: 1 addition & 1 deletion symfony-di/subscribers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<!-- Behavior Subscribers -->
<service id="sulu_document_manager.subscriber.behavior.auto_name" class="Sulu\Component\DocumentManager\Subscriber\Behavior\Path\AutoNameSubscriber">
<argument type="service" id="sulu_document_manager.document_registry"/>
<argument type="service" id="sulu_document_manager.slugifier"/>
<argument type="service" id="sulu_document_manager.node_name_slugifier"/>
<argument type="service" id="sulu_document_manager.name_resolver"/>
<argument type="service" id="sulu_document_manager.node_manager"/>
<tag name="sulu_document_manager.event_subscriber"/>
Expand Down
70 changes: 70 additions & 0 deletions tests/Unit/Slugifier/NodeNameSlugifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Component\DocumentManager\tests\Unit\Slugifier;

use Sulu\Component\DocumentManager\Slugifier\NodeNameSlugifier;
use Symfony\Cmf\Api\Slugifier\SlugifierInterface;

class NodeNameSlugifierTest extends \PHPUnit_Framework_TestCase
{
/**
* @var SlugifierInterface
*/
private $slugifier;

/**
* @var NodeNameSlugifier
*/
private $nodeNameSlugifier;

protected function setUp()
{
$this->slugifier = $this->prophesize(SlugifierInterface::class);
$this->nodeNameSlugifier = new NodeNameSlugifier($this->slugifier->reveal());
}

public function testSlugify()
{
$this->slugifier->slugify('Test article')->willReturn('test-article');

$this->assertEquals('test-article', $this->nodeNameSlugifier->slugify('Test article'));
}

public function provide10eData()
{
return [
['10e', '10-e'],
['.10e', '.10-e'],
['-10e', '-10-e'],
['%10e', '%10-e'],
['test-10e-name', 'test-10-e-name'],
['test.10e-name', 'test.10-e-name'],
['test%10e-name', 'test%10-e-name'],
['test-10E-name', 'test-10-E-name'],
['test.10E-name', 'test.10-E-name'],
['test%10E-name', 'test%10-E-name'],
['test10E-name', 'test10-E-name'],
['test-9e-name', 'test-9-e-name'],
['test-500e-name', 'test-500-e-name'],
];
}

/**
* @dataProvider provide10eData
*/
public function testSlugify10e($actual, $expected)
{
$this->slugifier->slugify($actual)->willReturn($actual);

$this->assertEquals($expected, $this->nodeNameSlugifier->slugify($actual));
}
}
30 changes: 0 additions & 30 deletions tests/Unit/Subscriber/Behavior/Path/AutoNameSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,36 +160,6 @@ public function testAutoName()
$this->subscriber->handlePersist($this->persistEvent->reveal());
}

public function provide10eData()
{
return [
['10e', '10-e'],
['.10e', '.10-e'],
['-10e', '-10-e'],
['%10e', '%10-e'],
['test-10e-name', 'test-10-e-name'],
['test.10e-name', 'test.10-e-name'],
['test%10e-name', 'test%10-e-name'],
['test-10E-name', 'test-10-E-name'],
['test.10E-name', 'test.10-E-name'],
['test%10E-name', 'test%10-E-name'],
['test10E-name', 'test10-E-name'],
['test-9e-name', 'test-9-e-name'],
['test-500e-name', 'test-500-e-name'],
];
}

/**
* It should assign a name based on the documents title.
*
* @dataProvider provide10eData
*/
public function testAutoName10e($title, $expectedName)
{
$this->doTestAutoName($title, $expectedName, true, false, $expectedName);
$this->subscriber->handlePersist($this->persistEvent->reveal());
}

/**
* It should not assign a new name, if the option says it is disabled.
*/
Expand Down

0 comments on commit af07068

Please sign in to comment.