Skip to content

Commit

Permalink
TASK: Simplify implementation and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaidelich committed Apr 19, 2018
1 parent 4de52b9 commit d51f567
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
24 changes: 10 additions & 14 deletions Neos.FluidAdaptor/Classes/ViewHelpers/Form/CheckboxViewHelper.php
Expand Up @@ -93,21 +93,17 @@ public function render($checked = null, $multiple = null)
}
if (is_array($propertyValue)) {
if ($checked === null) {
if (TypeHandling::isSimpleType(TypeHandling::getTypeForValue(current($propertyValue))) === false || Arrays::containsMultipleTypes($propertyValue)) {
$checked = false;
foreach ($propertyValue as $value) {
if (TypeHandling::isSimpleType(TypeHandling::getTypeForValue($value))) {
$checked = $valueAttribute === $value;
} else {
// assume an entity
$checked = $valueAttribute === $this->persistenceManager->getIdentifierByObject($value);
}
if ($checked === true) {
break;
}
$checked = false;
foreach ($propertyValue as $value) {
if (TypeHandling::isSimpleType(TypeHandling::getTypeForValue($value))) {
$checked = $valueAttribute === $value;
} else {
// assume an entity
$checked = $valueAttribute === $this->persistenceManager->getIdentifierByObject($value);
}
if ($checked === true) {
break;
}
} else {
$checked = in_array($valueAttribute, $propertyValue, true);
}
}
$this->arguments['multiple'] = true;
Expand Down
Expand Up @@ -11,8 +11,12 @@
* source code.
*/

use Doctrine\Common\Collections\ArrayCollection;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\FluidAdaptor\ViewHelpers\Fixtures\UserDomainClass;
use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;

require_once(__DIR__ . '/Fixtures/Fixture_UserDomainClass.php');
require_once(__DIR__ . '/FormFieldViewHelperBaseTestcase.php');

/**
Expand Down Expand Up @@ -173,6 +177,38 @@ public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyO
$this->viewHelper->render();
}

/**
* @test
*/
public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAnEntityCollection()
{
$this->mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('type', 'checkbox');
$this->mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('name', 'foo[]');
$this->mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('value', '1');
$this->mockTagBuilder->expects($this->at(5))->method('addAttribute')->with('checked', 'checked');

$user_kd = new UserDomainClass(1, 'Karsten', 'Dambekalns');
$user_bw = new UserDomainClass(2, 'Bastian', 'Waidelich');

$userCollection = new ArrayCollection([$user_kd, $user_bw]);

/** @var PersistenceManagerInterface|\PHPUnit_Framework_MockObject_MockObject $mockPersistenceManager */
$mockPersistenceManager = $this->createMock(PersistenceManagerInterface::class);
$mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->willReturnCallback(function(UserDomainClass $user) {
return (string)$user->getId();
});
$this->viewHelper->injectPersistenceManager($mockPersistenceManager);

$this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
$this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('1'));
$this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(true));
$this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue($userCollection));
$this->viewHelper->injectTagBuilder($this->mockTagBuilder);

$this->viewHelper->initialize();
$this->viewHelper->render();
}

/**
* @test
*/
Expand Down

0 comments on commit d51f567

Please sign in to comment.