Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/3178' into develop
Browse files Browse the repository at this point in the history
Forward port #3178
  • Loading branch information
weierophinney committed Dec 11, 2012
2 parents 8711f05 + ae7e34e commit 8d6c7b5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
25 changes: 16 additions & 9 deletions library/Zend/Form/Fieldset.php
Expand Up @@ -608,17 +608,24 @@ protected function extract()
*/
public function __clone()
{
$this->iterator = new PriorityQueue();
$items = $this->iterator->toArray(PriorityQueue::EXTR_BOTH);

$this->byName = array();
$this->elements = array();
$this->fieldsets = array();
$this->iterator = new PriorityQueue();

foreach ($items as $item) {
$elementOrFieldset = clone $item['data'];
$name = $elementOrFieldset->getName();

foreach ($this->byName as $key => $value) {
$value = clone $value;
$this->byName[$key] = $value;
$this->iterator->insert($value);
$this->iterator->insert($elementOrFieldset, $item['priority']);
$this->byName[$name] = $elementOrFieldset;

if ($value instanceof FieldsetInterface) {
$this->fieldsets[$key] = $value;
} elseif ($value instanceof ElementInterface) {
$this->elements[$key] = $value;
if ($elementOrFieldset instanceof FieldsetInterface) {
$this->fieldsets[$name] = $elementOrFieldset;
} elseif ($elementOrFieldset instanceof ElementInterface) {
$this->elements[$name] = $elementOrFieldset;
}
}

Expand Down
39 changes: 39 additions & 0 deletions tests/ZendTest/Form/FieldsetTest.php
Expand Up @@ -335,6 +335,45 @@ public function testIteratingRespectsOrderPriorityProvidedWhenSetLater()
$this->assertEquals($expected, $test);
}

public function testIteratingRespectsOrderPriorityWhenCloned()
{
$this->fieldset->add(new Element('foo'), array('priority' => 10));
$this->fieldset->add(new Element('bar'), array('priority' => 20));
$this->fieldset->add(new Element('baz'), array('priority' => -10));
$this->fieldset->add(new Fieldset('barbaz'), array('priority' => 30));

$expected = array('barbaz', 'bar', 'foo', 'baz');

$testOrig = array();
$testClone = array();

$fieldsetClone = clone $this->fieldset;

foreach ($this->fieldset as $element) {
$testOrig[] = $element->getName();
}

foreach ($fieldsetClone as $element) {
$testClone[] = $element->getName();
}

$this->assertEquals($expected, $testClone);
$this->assertEquals($testOrig, $testClone);
}

public function testCloneDeepClonesElementsAndObject()
{
$this->fieldset->add(new Element('foo'));
$this->fieldset->add(new Element('bar'));
$this->fieldset->setObject(new \stdClass);

$fieldsetClone = clone $this->fieldset;

$this->assertNotSame($this->fieldset->get('foo'), $fieldsetClone->get('foo'));
$this->assertNotSame($this->fieldset->get('bar'), $fieldsetClone->get('bar'));
$this->assertNotSame($this->fieldset->getObject(), $fieldsetClone->getObject());
}

public function testSubFieldsetsBindObject()
{
$form = new Form();
Expand Down

0 comments on commit 8d6c7b5

Please sign in to comment.