Skip to content

Commit

Permalink
Fix EZP-21680: As a developer, I want to easily check if a Field is e…
Browse files Browse the repository at this point in the history
…mpty
  • Loading branch information
lolautruche committed Oct 3, 2013
1 parent 1293cde commit 1efee5d
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 9 deletions.
5 changes: 5 additions & 0 deletions eZ/Bundle/EzPublishCoreBundle/Resources/config/helpers.yml
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]
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:

ezpublish.twig.extension.content:
class: %ezpublish.twig.extension.content.class%
arguments: [@service_container, @ezpublish.config.resolver, @ezpublish.translation_helper]
arguments: [@service_container, @ezpublish.config.resolver, @ezpublish.translation_helper, @ezpublish.field_helper]
tags:
- {name: twig.extension}

Expand Down
76 changes: 76 additions & 0 deletions eZ/Publish/Core/Helper/FieldHelper.php
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 );
}
}
104 changes: 104 additions & 0 deletions eZ/Publish/Core/Helper/Tests/FieldHelperTest.php
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 ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function getExtensions()
new ContentExtension(
$this->getContainerMock(),
$configResolver,
new TranslationHelper( $configResolver )
new TranslationHelper( $configResolver ),
$this->getMock( 'eZ\\Publish\\Core\\Helper\\FieldHelper' )
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension;

use eZ\Publish\Core\Helper\FieldHelper;
use eZ\Publish\Core\Helper\TranslationHelper;
use eZ\Publish\Core\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Field;
Expand Down Expand Up @@ -101,9 +102,14 @@ class ContentExtension extends Twig_Extension
/**
* @var \eZ\Publish\Core\Helper\TranslationHelper
*/
protected $contentHelper;
protected $translationHelper;

public function __construct( ContainerInterface $container, ConfigResolverInterface $resolver, TranslationHelper $contentHelper )
/**
* @var \eZ\Publish\Core\Helper\FieldHelper
*/
protected $fieldHelper;

public function __construct( ContainerInterface $container, ConfigResolverInterface $resolver, TranslationHelper $translationHelper, FieldHelper $fieldHelper )
{
$comp = function ( $a, $b )
{
Expand All @@ -119,7 +125,8 @@ public function __construct( ContainerInterface $container, ConfigResolverInterf
$this->blocks = array();
$this->container = $container;
$this->configResolver = $resolver;
$this->contentHelper = $contentHelper;
$this->translationHelper = $translationHelper;
$this->fieldHelper = $fieldHelper;
}

/**
Expand Down Expand Up @@ -162,7 +169,11 @@ public function getFunctions()
new Twig_SimpleFunction(
'ez_field_value',
array( $this, 'getTranslatedFieldValue' )
)
),
new Twig_SimpleFunction(
'ez_is_field_empty',
array( $this, 'isFieldEmpty' )
),
);
}

Expand Down Expand Up @@ -287,7 +298,7 @@ public function renderFieldDefinitionSettings( FieldDefinition $definition )
*/
public function renderField( Content $content, $fieldIdentifier, array $params = array() )
{
$field = $this->contentHelper->getTranslatedField( $content, $fieldIdentifier, isset( $params['lang'] ) ? $params['lang'] : null );
$field = $this->translationHelper->getTranslatedField( $content, $fieldIdentifier, isset( $params['lang'] ) ? $params['lang'] : null );

if ( !$field instanceof Field )
{
Expand Down Expand Up @@ -528,7 +539,7 @@ protected function getFieldTypeIdentifier( Content $content, Field $field )
*/
public function getTranslatedContentName( Content $content, $forcedLanguage = null )
{
return $this->contentHelper->getTranslatedName( $content, $forcedLanguage );
return $this->translationHelper->getTranslatedName( $content, $forcedLanguage );
}

/**
Expand All @@ -540,6 +551,20 @@ public function getTranslatedContentName( Content $content, $forcedLanguage = nu
*/
public function getTranslatedFieldValue( Content $content, $fieldDefIdentifier, $forcedLanguage = null )
{
return $this->contentHelper->getTranslatedField( $content, $fieldDefIdentifier, $forcedLanguage )->value;
return $this->translationHelper->getTranslatedField( $content, $fieldDefIdentifier, $forcedLanguage )->value;
}

/**
* Checks if a given field is considered empty.
*
* @param \eZ\Publish\Core\Repository\Values\Content\Content $content
* @param string $fieldDefIdentifier Identifier for the field we want to get the value from.
* @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale).
*
* @return bool
*/
public function isFieldEmpty( Content $content, $fieldDefIdentifier, $forcedLanguage = null )
{
return $this->fieldHelper->isFieldEmpty( $content, $fieldDefIdentifier, $forcedLanguage );
}
}

0 comments on commit 1efee5d

Please sign in to comment.