Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 418bbcc

Browse files
committed
Fixes bug with auto_increment_path and non-title updates
- We now safely ignore updates that do not change the route name.
1 parent 3030eea commit 418bbcc

File tree

4 files changed

+98
-17
lines changed

4 files changed

+98
-17
lines changed

AutoRoute/PathExists/AutoIncrementPath.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@ public function execute(RouteStack $routeStack)
3232

3333
$path = $routeStack->getFullPath();
3434

35+
$route = $this->dm->find(null, $path);
36+
$context = $routeStack->getContext();
37+
38+
if ($route->getRouteContent() === $context->getContent()) {
39+
$routeStack->addRoute($route);
40+
return;
41+
}
42+
3543
do {
3644
$newPath = sprintf('%s-%d', $path, $inc++);
3745
} while (null !== $this->dm->find(null, $newPath));
3846

3947
$routeStack->replaceLastPathElement(basename($newPath));
40-
4148
$this->routeMaker->make($routeStack);
4249
}
4350
}

Tests/AutoRoute/PathExists/AutoIncrementPathTest.php

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,81 @@ public function setUp()
2020
'Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStack'
2121
)->disableOriginalConstructor()->getMock();
2222

23+
$this->builderContext = $this->getMock('Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext');
24+
2325
$this->aiPath = new AutoIncrementPath($this->dm, $this->routeMaker);
26+
$this->route1 = $this->getMockBuilder('Symfony\Cmf\Bundle\RoutingExtraBundle\Document\Route')
27+
->disableOriginalConstructor()
28+
->getMock();
29+
$this->route2 = $this->getMockBuilder('Symfony\Cmf\Bundle\RoutingExtraBundle\Document\Route')
30+
->disableOriginalConstructor()
31+
->getMock();
32+
33+
$this->content1 = new \stdClass;
34+
$this->content2 = new \stdClass;
2435
}
2536

26-
public function testAutoIncrement()
37+
public function provideAutoIncrement()
38+
{
39+
return array(
40+
array(false),
41+
array(true),
42+
);
43+
}
44+
45+
/**
46+
* @dataProvider provideAutoIncrement
47+
*/
48+
public function testAutoIncrement($testUpdate)
2749
{
2850
$this->routeStack->expects($this->once())
2951
->method('getFullPath')
3052
->will($this->returnValue('/foo/bar'));
3153

54+
$this->routeStack->expects($this->once())
55+
->method('getContext')
56+
->will($this->returnValue($this->builderContext));
57+
58+
// if we test update we have the same content found
59+
// by the DM and set in the builder context
3260
$this->dm->expects($this->at(0))
3361
->method('find')
34-
->with(null, '/foo/bar-1')
35-
->will($this->returnValue(new \stdClass));
62+
->with(null, '/foo/bar')
63+
->will($this->returnValue($this->route1));
3664

37-
$this->dm->expects($this->at(1))
38-
->method('find')
39-
->with(null, '/foo/bar-2')
40-
->will($this->returnValue(null));
65+
$this->route1->expects($this->once())
66+
->method('getRouteContent')
67+
->will($this->returnValue($this->content1));
4168

42-
$this->routeStack->expects($this->once())
43-
->method('replaceLastPathElement')
44-
->with('bar-2');
69+
if (true === $testUpdate) {
70+
$this->builderContext->expects($this->once())
71+
->method('getContent')
72+
->will($this->returnValue($this->content1));
73+
} else {
74+
$this->builderContext->expects($this->once())
75+
->method('getContent')
76+
->will($this->returnValue($this->content2));
77+
}
78+
79+
if (false === $testUpdate) {
80+
$this->dm->expects($this->at(1))
81+
->method('find')
82+
->with(null, '/foo/bar-1')
83+
->will($this->returnValue(new \stdClass));
84+
85+
$this->dm->expects($this->at(2))
86+
->method('find')
87+
->with(null, '/foo/bar-2')
88+
->will($this->returnValue(null));
89+
90+
$this->routeStack->expects($this->once())
91+
->method('replaceLastPathElement')
92+
->with('bar-2');
4593

46-
$this->routeMaker->expects($this->once())
47-
->method('make')
48-
->with($this->routeStack);
94+
$this->routeMaker->expects($this->once())
95+
->method('make')
96+
->with($this->routeStack);
97+
}
4998

5099
$this->aiPath->execute($this->routeStack);
51100
}

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function provideTestUpdateBlog()
5555
/**
5656
* @dataProvider provideTestUpdateBlog
5757
*/
58-
public function testUpdateBlog($withPosts = false)
58+
public function testUpdateRenameBlog($withPosts = false)
5959
{
6060
$this->createBlog($withPosts);
6161

@@ -74,8 +74,6 @@ public function testUpdateBlog($withPosts = false)
7474
$this->assertCount(1, $routes);
7575
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute', $routes[0]);
7676

77-
$this->getDm()->refresh($routes[0]);
78-
7977
$this->assertEquals('foobar', $routes[0]->getName());
8078
$this->assertEquals('/test/auto-route/blog/foobar', $routes[0]->getId());
8179

@@ -90,6 +88,28 @@ public function testUpdateBlog($withPosts = false)
9088
}
9189
}
9290

91+
public function testUpdatePostNotChangingTitle()
92+
{
93+
$this->createBlog(true);
94+
95+
$post = $this->getDm()->find(null, '/test/test-blog/This is a post title');
96+
$this->assertNotNull($post);
97+
98+
$post->body = 'Test';
99+
100+
$this->getDm()->persist($post);
101+
$this->getDm()->flush();
102+
$this->getDm()->clear();
103+
104+
$post = $this->getDm()->find(null, '/test/test-blog/This is a post title');
105+
$routes = $post->routes;
106+
107+
$this->assertCount(1, $routes);
108+
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Document\AutoRoute', $routes[0]);
109+
110+
$this->assertEquals('this-is-a-post-title', $routes[0]->getName());
111+
}
112+
93113
public function testRemoveBlog()
94114
{
95115
$this->createBlog();

Tests/Functional/app/Document/Post.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class Post
3535
*/
3636
public $title;
3737

38+
/**
39+
* @PHPCR\String
40+
*/
41+
public $body;
42+
3843
public function getTitle()
3944
{
4045
return $this->title;

0 commit comments

Comments
 (0)