Browse Source

[Tree] Materialized Path (MongoDB): If PathSource contains the Path separator, we throw an exception

comfortablynumb 13 years ago
parent
commit
254657fa6c

+ 9 - 0
lib/Gedmo/Tree/Strategy/AbstractMaterializedPath.php

@@ -9,6 +9,7 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo;
 use Doctrine\ORM\Query;
 use Doctrine\Common\Persistence\ObjectManager;
 use Gedmo\Mapping\Event\AdapterInterface;
+use Gedmo\Exception\RuntimeException;
 
 /**
  * This strategy makes tree using materialized path strategy
@@ -182,6 +183,14 @@ abstract class AbstractMaterializedPath implements Strategy
         $pathSourceProp = $meta->getReflectionProperty($config['path_source']);
         $pathSourceProp->setAccessible(true);
         $path = $pathSourceProp->getValue($node);
+
+        // We need to avoid the presence of the path separator in the path source
+        if (strpos($path, $config['path_separator']) !== false) {
+            $msg = 'You can\'t use the Path separator ("%s") as a character for your PathSource field value.';
+
+            throw new RuntimeException(sprintf($msg, $config['path_separator']));
+        }
+
         $fieldMapping = $meta->getFieldMapping($config['path_source']);
 
         // If PathSource field is a string, we append the ID to the path

+ 14 - 0
tests/Gedmo/Tree/MaterializedPathODMMongoDBTest.php

@@ -108,6 +108,20 @@ class MaterializedPathODMMongoDBTest extends BaseTestCaseMongoODM
         $this->assertEquals(1, $firstResult->getLevel());
     }
 
+    /**
+     * @test
+     */
+    public function useOfSeparatorInPathSourceShouldThrowAnException()
+    {
+        $this->setExpectedException('Gedmo\Exception\RuntimeException');
+
+        $category = $this->createCategory();
+        $category->setTitle('1|');
+
+        $this->dm->persist($category);
+        $this->dm->flush();
+    }
+
     public function createCategory()
     {
         $class = self::CATEGORY;