Skip to content

Commit

Permalink
Fix EZP-24399: Cross-siteaccess links do not take tree_root into account
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Inacio committed Jun 5, 2015
1 parent ea02371 commit 22c31eb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 17 deletions.
Expand Up @@ -79,11 +79,13 @@ public function doGenerate( $location, array $parameters )
// We generate for a different SiteAccess, so potentially in a different language.
$languages = $this->configResolver->getParameter( 'languages', null, $parameters['siteaccess'] );
$urlAliases = $urlAliasService->listLocationAliases( $location, false, null, null, $languages );

// Use the target SiteAccess root location
$rootLocationId = $this->configResolver->getParameter( 'content.tree_root.location_id', null, $parameters['siteaccess'] );
unset( $parameters['siteaccess'] );
}
else
{
$rootLocationId = $this->rootLocationId;
$urlAliases = $urlAliasService->listLocationAliases( $location, false );
}

Expand All @@ -97,9 +99,9 @@ public function doGenerate( $location, array $parameters )
{
$path = $urlAliases[0]->path;
// Remove rootLocation's prefix if needed.
if ( $this->rootLocationId !== null )
if ( $rootLocationId !== null )
{
$pathPrefix = $this->getPathPrefixByRootLocationId( $this->rootLocationId );
$pathPrefix = $this->getPathPrefixByRootLocationId( $rootLocationId );
// "/" cannot be considered as a path prefix since it's root, so we ignore it.
if ( $pathPrefix !== '/' && mb_stripos( $path, $pathPrefix ) === 0 )
{
Expand All @@ -109,7 +111,7 @@ public function doGenerate( $location, array $parameters )
// This is most likely an error (from content edition or link generation logic).
else if ( $pathPrefix !== '/' && !$this->isUriPrefixExcluded( $path ) && $this->logger !== null )
{
$this->logger->warning( "Generating a link to a location outside root content tree: '$path' is outside tree starting to location #$this->rootLocationId" );
$this->logger->warning( "Generating a link to a location outside root content tree: '$path' is outside tree starting to location #$rootLocationId" );
}
}
}
Expand Down
81 changes: 68 additions & 13 deletions eZ/Publish/Core/MVC/Symfony/Routing/Tests/UrlAliasGeneratorTest.php
Expand Up @@ -175,19 +175,58 @@ public function testDoGenerate( URLAlias $urlAlias, array $parameters, $expected
$this->assertSame( $expected, $this->urlAliasGenerator->doGenerate( $location, $parameters ) );
}

public function providerTestDoGenerate()
{
return array(
array(
new URLAlias( array( 'path' => '/foo/bar' ) ),
array(),
'/foo/bar'
),
array(
new URLAlias( array( 'path' => '/foo/bar' ) ),
array( 'some' => 'thing' ),
'/foo/bar?some=thing'
),
array(
new URLAlias( array( 'path' => '/foo/bar' ) ),
array( 'some' => 'thing', 'truc' => 'muche' ),
'/foo/bar?some=thing&truc=muche'
),
);
}

/**
* @dataProvider providerTestDoGenerate
* @dataProvider providerTestDoGenerateWithSiteaccess
*/
public function testDoGenerateWithSiteAccessParam( URLAlias $urlAlias, array $parameters, $expected )
{
$siteaccessName = 'foo';
$parameters += array( 'siteaccess' => $siteaccessName );
$languages = array( 'esl-ES', 'fre-FR', 'eng-GB' );

$saRootLocations = array(
'foo' => 2,
'bar' => 100
);
$treeRootUrlAlias = array(
2 => new URLAlias( array( 'path' => '/' ) ),
100 => new URLAlias( array( 'path' => '/foo/bar' ) ),
);

$this->configResolver
->expects( $this->once() )
->expects( $this->any() )
->method( 'getParameter' )
->with( 'languages', null, $siteaccessName )
->will( $this->returnValue( $languages ) );
->will(
$this->returnValueMap(
array(
array( 'languages', null, 'foo', $languages ),
array( 'languages', null, 'bar', $languages ),
array( 'content.tree_root.location_id', null, 'foo', $saRootLocations['foo'] ),
array( 'content.tree_root.location_id', null, 'bar', $saRootLocations['bar'] ),
)
)
);

$location = new Location( array( 'id' => 123 ) );
$this->urlAliasService
Expand All @@ -201,12 +240,33 @@ public function testDoGenerateWithSiteAccessParam( URLAlias $urlAlias, array $pa
)
);

$this->locationService
->expects( $this->once() )
->method( 'loadLocation' )
->will(
$this->returnCallback(
function( $locationId ) {
return new Location( array( 'id' => $locationId ) );
}
)
);
$this->urlAliasService
->expects( $this->exactly( 1 ) )
->method( 'reverseLookup' )
->will(
$this->returnCallback(
function( $location ) use ( $treeRootUrlAlias ) {
return $treeRootUrlAlias[$location->id];
}
)
);

$this->urlAliasGenerator->setSiteAccess( new SiteAccess( 'test', 'fake', $this->getMock( 'eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\URILexer' ) ) );

$this->assertSame( $expected, $this->urlAliasGenerator->doGenerate( $location, $parameters ) );
}

public function providerTestDoGenerate()
public function providerTestDoGenerateWithSiteaccess()
{
return array(
array(
Expand All @@ -215,14 +275,9 @@ public function providerTestDoGenerate()
'/foo/bar'
),
array(
new URLAlias( array( 'path' => '/foo/bar' ) ),
array( 'some' => 'thing' ),
'/foo/bar?some=thing'
),
array(
new URLAlias( array( 'path' => '/foo/bar' ) ),
array( 'some' => 'thing', 'truc' => 'muche' ),
'/foo/bar?some=thing&truc=muche'
new URLAlias( array( 'path' => '/foo/bar/baz' ) ),
array( 'siteaccess' => 'bar' ),
'/baz'
),
);
}
Expand Down

0 comments on commit 22c31eb

Please sign in to comment.