diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/MergeCollectionListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/MergeCollectionListener.php index 35d086391b..2586f677ac 100644 --- a/src/Symfony/Component/Form/Extension/Core/EventListener/MergeCollectionListener.php +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/MergeCollectionListener.php @@ -130,7 +130,7 @@ class MergeCollectionListener implements EventSubscriberInterface } // Set preconfigured adder - if ($this->addMethod) { + if ($this->allowAdd && $this->addMethod) { $addMethod = $this->checkMethod($reflClass, $this->addMethod); if (!$addMethod) { @@ -143,7 +143,7 @@ class MergeCollectionListener implements EventSubscriberInterface } // Set preconfigured remover - if ($this->removeMethod) { + if ($this->allowDelete && $this->removeMethod) { $removeMethod = $this->checkMethod($reflClass, $this->removeMethod); if (!$removeMethod) { diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/EventListener/MergeCollectionListenerTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/EventListener/MergeCollectionListenerTest.php index 9f81626b4c..02fd3e7300 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/EventListener/MergeCollectionListenerTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/EventListener/MergeCollectionListenerTest.php @@ -475,6 +475,68 @@ abstract class MergeCollectionListenerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($this->getData($originalDataArray), $event->getData()); } + public function testDontCallAdderWithCustomNameIfDisallowed() + { + $parentData = $this->getMock(__CLASS__ . '_CarCustomNames'); + $parentForm = $this->getForm('car'); + $parentForm->setData($parentData); + $parentForm->add($this->form); + + $originalDataArray = array(1 => 'second'); + $originalData = $this->getData($originalDataArray); + $newData = $this->getData(array(0 => 'first')); + + $this->form->setData($originalData); + + $parentData->expects($this->never()) + ->method('foo'); + $parentData->expects($this->once()) + ->method('bar') + ->with('second'); + + $event = new FilterDataEvent($this->form, $newData); + $listener = new MergeCollectionListener(false, true, true, 'foo', 'bar'); + $listener->onBindNormData($event); + + if (is_object($originalData)) { + $this->assertSame($originalData, $event->getData()); + } + + // The data was not modified + $this->assertEquals($this->getData($originalDataArray), $event->getData()); + } + + public function testDontCallRemoverWithCustomNameIfDisallowed() + { + $parentData = $this->getMock(__CLASS__ . '_CarCustomNames'); + $parentForm = $this->getForm('car'); + $parentForm->setData($parentData); + $parentForm->add($this->form); + + $originalDataArray = array(1 => 'second'); + $originalData = $this->getData($originalDataArray); + $newData = $this->getData(array(0 => 'first')); + + $this->form->setData($originalData); + + $parentData->expects($this->once()) + ->method('foo') + ->with('first'); + $parentData->expects($this->never()) + ->method('bar'); + + $event = new FilterDataEvent($this->form, $newData); + $listener = new MergeCollectionListener(true, false, true, 'foo', 'bar'); + $listener->onBindNormData($event); + + if (is_object($originalData)) { + $this->assertSame($originalData, $event->getData()); + } + + // The data was not modified + $this->assertEquals($this->getData($originalDataArray), $event->getData()); + } + /** * @expectedException Symfony\Component\Form\Exception\FormException */