From 11bf683e3e904cddf82677685c79f9ef63d33a84 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 2 Aug 2016 15:09:12 +0200 Subject: [PATCH] Fixing issue when a QtiIdentifiable component is renamed. --- src/qtism/data/QtiIdentifiableCollection.php | 17 ++++++--------- ....php => QtiIdentifiableCollectionTest.php} | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) rename test/qtismtest/data/{QTIIdentifiableCollectionTest.php => QtiIdentifiableCollectionTest.php} (84%) diff --git a/src/qtism/data/QtiIdentifiableCollection.php b/src/qtism/data/QtiIdentifiableCollection.php index 7f50e3747..7852ec0e7 100644 --- a/src/qtism/data/QtiIdentifiableCollection.php +++ b/src/qtism/data/QtiIdentifiableCollection.php @@ -14,7 +14,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * Copyright (c) 2013-2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); + * Copyright (c) 2013-2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); * * @author Jérôme Bogaerts * @license GPLv2 @@ -29,8 +29,9 @@ use \SplSubject; /** - * This extension of QtiComponentCollection can retrieve items it contains - * by identifier. + * This extension of QtiComponentCollection can retrieve items it contains by QTI identifier. + * + * This collection implementation aims at storing QTI components having an "identifier" attribute. * * @author Jérôme Bogaerts * @@ -177,7 +178,7 @@ public function replace($object, $replacement) if (($search = array_search($object, $this->dataPlaceHolder, true)) !== false) { - $objectKey = $object->getIdentifier(); + $objectKey = $search; $replacementKey = $replacement->getIdentifier(); if ($objectKey === $replacementKey) { @@ -215,13 +216,7 @@ public function update(SplSubject $subject) { // -- case 1 (QtiIdentifiable) // If it is a QtiIdentifiable, it has changed its identifier. - $data = &$this->getDataPlaceHolder(); - foreach (array_keys($data) as $k) { - if ($data[$k] === $subject && $k !== $subject->getIdentifier()) { - unset($data[$k]); - $this->offsetSet(null, $subject); - } - } + $this->replace($subject, $subject); } public function __clone() diff --git a/test/qtismtest/data/QTIIdentifiableCollectionTest.php b/test/qtismtest/data/QtiIdentifiableCollectionTest.php similarity index 84% rename from test/qtismtest/data/QTIIdentifiableCollectionTest.php rename to test/qtismtest/data/QtiIdentifiableCollectionTest.php index d94c4125e..1f8e1bcc8 100644 --- a/test/qtismtest/data/QTIIdentifiableCollectionTest.php +++ b/test/qtismtest/data/QtiIdentifiableCollectionTest.php @@ -103,4 +103,25 @@ public function testEventsUnset() $weight1->setIdentifier('weight2'); $this->assertFalse($weight1 === $weights['weight2']); } + + public function testRenamingOrder() + { + $weight1 = new Weight('weight1', 1.0); + $weight2 = new Weight('weight2', 1.2); + $weight3 = new Weight('weight3', 1.2); + $weights = new WeightCollection(array($weight1, $weight2, $weight3)); + + // If weight2 gets a new identifier "weight4", it should still be in second position in the collection. + $this->assertSame( + array('weight1', 'weight2', 'weight3'), + $weights->getKeys() + ); + + $weight2->setIdentifier('weight4'); + + $this->assertSame( + array('weight1', 'weight4', 'weight3'), + $weights->getKeys() + ); + } }