Skip to content

Commit

Permalink
Fix EZP-22228: NotFoundException thrown when not all requested langua…
Browse files Browse the repository at this point in the history
…ges are present
  • Loading branch information
lolautruche committed Jan 28, 2014
1 parent fe53fbf commit 48f64b2
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 27 deletions.
7 changes: 7 additions & 0 deletions eZ/Publish/Core/Persistence/InMemory/ContentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,13 @@ public function load( $id, $version, $translations = null )
if ( isset( $translations ) )
$fieldMatch["languageCode"] = $translations;
$content->fields = $this->backend->find( 'Content\\Field', $fieldMatch );
if ( empty( $content->fields ) )
{
throw new NotFoundException(
'Content',
"contentId:{$id}, versionNo:{$version}" . ( $translations ? ', ' . implode( ',', $translations ) : '' )
);
}

$content->versionInfo = $versions[0];
return $content;
Expand Down
23 changes: 0 additions & 23 deletions eZ/Publish/Core/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,29 +393,6 @@ public function internalLoadContent( $id, array $languages = null, $versionNo =
);
}

if ( !empty( $languages ) )
{
foreach ( $languages as $languageCode )
{
if (
!in_array(
$this->persistenceHandler->contentLanguageHandler()->loadByLanguageCode( $languageCode )->id,
$spiContent->versionInfo->languageIds
)
)
{
throw new NotFoundException(
"Content",
array(
$isRemoteId ? "remoteId" : "id" => $id,
"languages" => $languages,
"versionNo" => $versionNo
)
);
}
}
}

return $this->domainMapper->buildContentDomainObject( $spiContent );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,10 @@ public function testLoadContentArgumentsProvider()
return array(
array( 4, null, null ),
array( 4, array( "eng-US" ), null ),
array( 4, array( "eng-US", "fre-FR" ), null ),
array( 4, null, 1 ),
array( 4, array( "eng-US" ), 1 )
array( 4, array( "eng-US", "fre-FR", "nor-NO", "eng-DE" ), 1 ),
array( 4, array( "eng-US" ), 1 ),
);
}

Expand Down Expand Up @@ -524,23 +526,27 @@ public function testLoadContentThrowsNotFoundExceptionLanguageNotFound()
$contentService = $this->repository->getContentService();

// Throws an exception because content does not exists in "eng-GB" language
$content = $contentService->loadContent( 4, array( "eng-GB" ) );
$content = $contentService->loadContent( 4, array( "fre-FR" ) );
/* END: Use Case */
}

/**
* Test for the loadContent() method.
*
* @covers \eZ\Publish\Core\Repository\ContentService::loadContent
* @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
public function testLoadContentThrowsNotFoundExceptionLanguageNotFoundVariation()
{
/* BEGIN: Use Case */
$contentService = $this->repository->getContentService();

// Throws an exception because content does not exists in "eng-GB" language
// Content only exists in eng-US, so we should only have it in eng-US.
$content = $contentService->loadContent( 4, array( "eng-US", "eng-GB" ) );
$this->assertInstanceOf(
"eZ\\Publish\\API\\Repository\\Values\\Content\\Content",
$content
);
$this->assertContentValues( $content, array( "eng-US" ) );
/* END: Use Case */
}

Expand Down
223 changes: 223 additions & 0 deletions eZ/Publish/Core/Repository/Tests/Service/Mock/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,229 @@ public function testLoadVersionInfo()
$this->assertEquals( "result", $result );
}

public function testLoadContent()
{
$repository = $this->getRepositoryMock();
$contentService = $this->getPartlyMockedContentService( array( 'internalLoadContent' ) );
$content = $this->getMock( 'eZ\Publish\API\Repository\Values\Content\Content' );
$versionInfo = $this
->getMockBuilder( 'eZ\Publish\API\Repository\Values\Content\VersionInfo' )
->setConstructorArgs( array( array( 'status' => APIVersionInfo::STATUS_PUBLISHED ) ) )
->getMockForAbstractClass();
$content
->expects( $this->once() )
->method( 'getVersionInfo' )
->will( $this->returnValue( $versionInfo ) );
$contentId = 123;
$contentService
->expects( $this->once() )
->method( 'internalLoadContent' )
->with( $contentId )
->will( $this->returnValue( $content ) );

$repository
->expects( $this->once() )
->method( 'canUser' )
->with( 'content', 'read', $content )
->will( $this->returnValue( true ) );

$this->assertSame( $content, $contentService->loadContent( $contentId ) );
}

public function testLoadContentNonPublished()
{
$repository = $this->getRepositoryMock();
$contentService = $this->getPartlyMockedContentService( array( 'internalLoadContent' ) );
$content = $this->getMock( 'eZ\Publish\API\Repository\Values\Content\Content' );
$versionInfo = $this
->getMockBuilder( 'eZ\Publish\API\Repository\Values\Content\VersionInfo' )
->setConstructorArgs( array( array( 'status' => APIVersionInfo::STATUS_DRAFT ) ) )
->getMockForAbstractClass();
$content
->expects( $this->once() )
->method( 'getVersionInfo' )
->will( $this->returnValue( $versionInfo ) );
$contentId = 123;
$contentService
->expects( $this->once() )
->method( 'internalLoadContent' )
->with( $contentId )
->will( $this->returnValue( $content ) );

$repository
->expects( $this->exactly( 2 ) )
->method( 'canUser' )
->will(
$this->returnValueMap(
array(
array( 'content', 'read', $content, null, true ),
array( 'content', 'versionread', $content, null, true ),
)
)
);

$this->assertSame( $content, $contentService->loadContent( $contentId ) );
}

/**
* @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
*/
public function testLoadContentUnauthorized()
{
$repository = $this->getRepositoryMock();
$contentService = $this->getPartlyMockedContentService( array( 'internalLoadContent' ) );
$content = $this->getMock( 'eZ\Publish\API\Repository\Values\Content\Content' );
$contentId = 123;
$contentService
->expects( $this->once() )
->method( 'internalLoadContent' )
->with( $contentId )
->will( $this->returnValue( $content ) );

$repository
->expects( $this->once() )
->method( 'canUser' )
->with( 'content', 'read', $content )
->will( $this->returnValue( false ) );

$contentService->loadContent( $contentId );
}

/**
* @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException
*/
public function testLoadContentNotPublishedStatusUnauthorized()
{
$repository = $this->getRepositoryMock();
$contentService = $this->getPartlyMockedContentService( array( 'internalLoadContent' ) );
$content = $this->getMock( 'eZ\Publish\API\Repository\Values\Content\Content' );
$versionInfo = $this
->getMockBuilder( 'eZ\Publish\API\Repository\Values\Content\VersionInfo' )
->setConstructorArgs( array( array( 'status' => APIVersionInfo::STATUS_DRAFT ) ) )
->getMockForAbstractClass();
$content
->expects( $this->once() )
->method( 'getVersionInfo' )
->will( $this->returnValue( $versionInfo ) );
$contentId = 123;
$contentService
->expects( $this->once() )
->method( 'internalLoadContent' )
->with( $contentId )
->will( $this->returnValue( $content ) );

$repository
->expects( $this->exactly( 2 ) )
->method( 'canUser' )
->will(
$this->returnValueMap(
array(
array( 'content', 'read', $content, null, true ),
array( 'content', 'versionread', $content, null, false ),
)
)
);

$contentService->loadContent( $contentId );
}

/**
* @dataProvider internalLoadContentProvider
*/
public function testInternalLoadContent( $id, $languages, $versionNo, $isRemoteId )
{
$contentService = $this->getPartlyMockedContentService();
/** @var \PHPUnit_Framework_MockObject_MockObject $contentHandler */
$contentHandler = $this->getPersistenceMock()->contentHandler();
$realVersionNo = $versionNo;
$realId = $id;

if ( $isRemoteId )
{
$realVersionNo = $versionNo ?: 7;
$realId = 123;
$spiContentInfo = new SPIContentInfo( array( 'currentVersionNo' => $realVersionNo, 'id' => $realId ) );
$contentHandler
->expects( $this->once() )
->method( 'loadContentInfoByRemoteId' )
->with( $id )
->will( $this->returnValue( $spiContentInfo ) );
}
else if ( $versionNo === null )
{
$realVersionNo = 7;
$spiContentInfo = new SPIContentInfo( array( 'currentVersionNo' => $realVersionNo ) );
$contentHandler
->expects( $this->once() )
->method( 'loadContentInfo' )
->with( $id )
->will( $this->returnValue( $spiContentInfo ) );
}

$spiContent = new SPIContent();
$contentHandler
->expects( $this->once() )
->method( 'load' )
->with( $realId, $realVersionNo, $languages )
->will( $this->returnValue( $spiContent ) );
$content = $this->getMock( 'eZ\Publish\API\Repository\Values\Content\Content' );
$this->getDomainMapperMock()
->expects( $this->once() )
->method( 'buildContentDomainObject' )
->with( $spiContent )
->will( $this->returnValue( $content ) );

$this->assertSame(
$content,
$contentService->internalLoadContent( $id, $languages, $versionNo, $isRemoteId )
);
}

public function internalLoadContentProvider()
{
return array(
array( 123, null, null, false ),
array( 123, null, 456, false ),
array( 456, null, 123, false ),
array( 456, null, 2, false ),
array( 456, array( 'eng-GB' ), 2, false ),
array( 456, array( 'eng-GB', 'fre-FR' ), null, false ),
array( 456, array( 'eng-GB', 'fre-FR', 'nor-NO' ), 2, false ),
// With remoteId
array( 123, null, null, true ),
array( 'someRemoteId', null, 456, true ),
array( 456, null, 123, true ),
array( 'someRemoteId', null, 2, true ),
array( 'someRemoteId', array( 'eng-GB' ), 2, true ),
array( 456, array( 'eng-GB', 'fre-FR' ), null, true ),
array( 'someRemoteId', array( 'eng-GB', 'fre-FR', 'nor-NO' ), 2, true ),
);
}

/**
* @expectedException \eZ\Publish\Core\Base\Exceptions\NotFoundException
*/
public function testInternalLoadContentNotFound()
{
$contentService = $this->getPartlyMockedContentService();
/** @var \PHPUnit_Framework_MockObject_MockObject $contentHandler */
$contentHandler = $this->getPersistenceMock()->contentHandler();
$id = 123;
$versionNo = 7;
$languages = null;
$contentHandler
->expects( $this->once() )
->method( 'load' )
->with( $id, $versionNo, $languages )
->will(
$this->throwException(
$this->getMock( 'eZ\Publish\API\Repository\Exceptions\NotFoundException' )
)
);

$contentService->internalLoadContent( $id, $languages, $versionNo );
}

/**
* Test for the loadContentByContentInfo() method.
*
Expand Down

0 comments on commit 48f64b2

Please sign in to comment.