-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix EZP-21680: As a developer, I want to easily check if a Field is e…
…mpty
- Loading branch information
1 parent
1293cde
commit 1efee5d
Showing
6 changed files
with
220 additions
and
9 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
parameters: | ||
# Helpers | ||
ezpublish.translation_helper.class: eZ\Publish\Core\Helper\TranslationHelper | ||
ezpublish.field_helper.class: eZ\Publish\Core\Helper\FieldHelper | ||
|
||
services: | ||
# Helpers | ||
ezpublish.translation_helper: | ||
class: %ezpublish.translation_helper.class% | ||
arguments: [@ezpublish.config.resolver] | ||
|
||
ezpublish.field_helper: | ||
class: %ezpublish.field_helper.class% | ||
arguments: [@ezpublish.translation_helper, @ezpublish.api.service.content_type, @ezpublish.api.service.field_type] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* File containing the FieldHelper class. | ||
* | ||
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved. | ||
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 | ||
* @version //autogentag// | ||
*/ | ||
|
||
namespace eZ\Publish\Core\Helper; | ||
|
||
use eZ\Publish\API\Repository\ContentTypeService; | ||
use eZ\Publish\API\Repository\FieldTypeService; | ||
use eZ\Publish\API\Repository\Values\Content\Content; | ||
use eZ\Publish\API\Repository\Values\Content\ContentInfo; | ||
|
||
class FieldHelper | ||
{ | ||
/** | ||
* @var \eZ\Publish\API\Repository\ContentTypeService | ||
*/ | ||
private $contentTypeService; | ||
|
||
/** | ||
* @var \eZ\Publish\API\Repository\FieldTypeService | ||
*/ | ||
private $fieldTypeService; | ||
|
||
/** | ||
* @var TranslationHelper | ||
*/ | ||
private $translationHelper; | ||
|
||
public function __construct( TranslationHelper $translationHelper, ContentTypeService $contentTypeService, FieldTypeService $fieldTypeService ) | ||
{ | ||
$this->fieldTypeService = $fieldTypeService; | ||
$this->contentTypeService = $contentTypeService; | ||
$this->translationHelper = $translationHelper; | ||
} | ||
|
||
/** | ||
* Checks if provided field can be considered empty. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Content $content | ||
* @param string $fieldDefIdentifier | ||
* @param null $forcedLanguage | ||
* | ||
* @return bool | ||
*/ | ||
public function isFieldEmpty( Content $content, $fieldDefIdentifier, $forcedLanguage = null ) | ||
{ | ||
$field = $this->translationHelper->getTranslatedField( $content, $fieldDefIdentifier, $forcedLanguage ); | ||
$fieldDefinition = $this->getFieldDefinition( $content->contentInfo, $fieldDefIdentifier ); | ||
|
||
return $this | ||
->fieldTypeService | ||
->getFieldType( $fieldDefinition->fieldTypeIdentifier ) | ||
->isEmptyValue( $field->value ); | ||
} | ||
|
||
/** | ||
* Returns FieldDefinition object based on $contentInfo and $fieldDefIdentifier. | ||
* | ||
* @param ContentInfo $contentInfo | ||
* @param string $fieldDefIdentifier | ||
* | ||
* @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition | ||
*/ | ||
public function getFieldDefinition( ContentInfo $contentInfo, $fieldDefIdentifier ) | ||
{ | ||
return $this | ||
->contentTypeService | ||
->loadContentType( $contentInfo->contentTypeId ) | ||
->getFieldDefinition( $fieldDefIdentifier ); | ||
} | ||
} |
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,104 @@ | ||
<?php | ||
/** | ||
* File containing the FieldHelperTest class. | ||
* | ||
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved. | ||
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 | ||
* @version //autogentag// | ||
*/ | ||
|
||
namespace eZ\Publish\Core\Helper\Tests; | ||
|
||
use eZ\Publish\API\Repository\ContentTypeService; | ||
use eZ\Publish\API\Repository\FieldTypeService; | ||
use eZ\Publish\API\Repository\Values\Content\ContentInfo; | ||
use eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\Core\FieldType\TextLine\Type as TextLineType; | ||
use eZ\Publish\Core\FieldType\TextLine\Value; | ||
use eZ\Publish\Core\Helper\FieldHelper; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
class FieldHelperTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var FieldHelper | ||
*/ | ||
private $fieldHelper; | ||
|
||
/** | ||
* @var FieldTypeService|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $fieldTypeServiceMock; | ||
|
||
/** | ||
* @var ContentTypeService|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $contentTypeServiceMock; | ||
|
||
/** | ||
* @var \eZ\Publish\Core\Helper\TranslationHelper|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $translationHelper; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->fieldTypeServiceMock = $this->getMock( 'eZ\\Publish\\API\\Repository\\FieldTypeService' ); | ||
$this->contentTypeServiceMock = $this->getMock( 'eZ\\Publish\\API\\Repository\\ContentTypeService' ); | ||
$this->translationHelper = $this | ||
->getMockBuilder( 'eZ\\Publish\\Core\\Helper\\TranslationHelper' ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->fieldHelper = new FieldHelper( $this->translationHelper, $this->contentTypeServiceMock, $this->fieldTypeServiceMock ); | ||
} | ||
|
||
public function testIsFieldEmpty() | ||
{ | ||
$contentTypeId = 123; | ||
$contentInfo = new ContentInfo( array( 'contentTypeId' => $contentTypeId ) ); | ||
$content = $this->getMock( 'eZ\\Publish\\API\\Repository\\Values\\Content\\Content' ); | ||
$content | ||
->expects( $this->any() ) | ||
->method( '__get' ) | ||
->with( 'contentInfo' ) | ||
->will( $this->returnValue( $contentInfo ) ); | ||
|
||
$fieldDefIdentifier = 'my_field_definition'; | ||
$textLineFT = new TextLineType; | ||
$emptyValue = $textLineFT->getEmptyValue(); | ||
$emptyField = new Field( array( 'fieldDefIdentifier' => $fieldDefIdentifier, 'value' => $emptyValue ) ); | ||
|
||
$contentType = $this->getMockForAbstractClass( 'eZ\\Publish\\API\\Repository\\Values\\ContentType\\ContentType' ); | ||
$fieldDefinition = $this->getMockBuilder( 'eZ\\Publish\\API\\Repository\\Values\\ContentType\\FieldDefinition' ) | ||
->setConstructorArgs( array( array( 'fieldTypeIdentifier' => 'ezstring' ) ) ) | ||
->getMockForAbstractClass(); | ||
$contentType | ||
->expects( $this->once() ) | ||
->method( 'getFieldDefinition' ) | ||
->with( $fieldDefIdentifier ) | ||
->will( $this->returnValue( $fieldDefinition ) ); | ||
|
||
$this->contentTypeServiceMock | ||
->expects( $this->once() ) | ||
->method( 'loadContentType' ) | ||
->with( $contentTypeId ) | ||
->will( $this->returnValue( $contentType ) ); | ||
|
||
$this->translationHelper | ||
->expects( $this->once() ) | ||
->method( 'getTranslatedField' ) | ||
->with( $content, $fieldDefIdentifier ) | ||
->will( $this->returnValue( $emptyField ) ); | ||
|
||
$this->fieldTypeServiceMock | ||
->expects( $this->any() ) | ||
->method( 'getFieldType' ) | ||
->with( 'ezstring' ) | ||
->will( $this->returnValue( $textLineFT ) ); | ||
|
||
$this->assertTrue( $this->fieldHelper->isFieldEmpty( $content, $fieldDefIdentifier ) ); | ||
|
||
// $nonEmptyValue = new Field( array( 'fieldDefIdentifier' => 'ezstring', 'value' => new Value( 'Vive le sucre !!!' ) ) ); | ||
// $this->assertFalse( $this->fieldHelper->isFieldEmpty( $nonEmptyValue ) ); | ||
} | ||
} |
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