Browse Source

reorder batch field only if exists

Mike Meier 12 years ago
parent
commit
7767eaeb4e

+ 3 - 1
Admin/FieldDescriptionCollection.php

@@ -117,7 +117,9 @@ class FieldDescriptionCollection implements \ArrayAccess, \Countable
      */
     public function reorder(array $keys)
     {
-        array_unshift($keys, 'batch');
+        if($this->has('batch')){
+            array_unshift($keys, 'batch');
+        }
         $this->elements = array_merge(array_flip($keys), $this->elements);
     }
 }

+ 44 - 1
Tests/Admin/FieldDescriptionCollectionTest.php

@@ -68,4 +68,47 @@ class FieldDescriptionCollectionTest extends \PHPUnit_Framework_TestCase
 
         $collection['foo'] = null;
     }
-}
+
+    public function testReorderListWithoutBatchField()
+    {
+        $collection = new FieldDescriptionCollection();
+
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title'));
+        $collection->add($fieldDescription);
+
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
+        $collection->add($fieldDescription);
+
+        $newOrder = array('position', 'title');
+        $collection->reorder($newOrder);
+
+        $actualElements = array_keys($collection->getElements());
+        $this->assertSame($newOrder, $actualElements, 'the order is wrong');
+    }
+
+    public function testReorderListWithBatchField()
+    {
+        $collection = new FieldDescriptionCollection();
+
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('title'));
+        $collection->add($fieldDescription);
+
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('position'));
+        $collection->add($fieldDescription);
+
+        $fieldDescription = $this->getMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
+        $fieldDescription->expects($this->once())->method('getName')->will($this->returnValue('batch'));
+        $collection->add($fieldDescription);
+
+        $newOrder = array('position', 'title');
+        $collection->reorder($newOrder);
+        array_unshift($newOrder, 'batch');
+
+        $actualElements = array_keys($collection->getElements());
+        $this->assertSame($newOrder, $actualElements, 'the order is wrong');
+    }
+}