Correct p[roblem with getTags returning Null
Unit test added.
--- a/DataFixtures/ORM/LoadDocumentData.php Mon Feb 27 11:45:38 2012 +0100
+++ b/DataFixtures/ORM/LoadDocumentData.php Thu Mar 01 18:43:05 2012 +0100
@@ -38,6 +38,7 @@
# create new document
$doc_def_list = array(
+ array('title'=>'Title 0', 'description'=>'Description 0', 'tags' => null, 'categories' => array()),
array('title'=>'Title 1', 'description'=>'Description 1', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'categories' => array_values($cat_def_list)),
array('title'=>'Title 2', 'description'=>'Description 2', 'tags' => array('tag2', 'tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'], $cat_def_list['cat2'])),
array('title'=>'Title 3', 'description'=>'Description 3', 'tags' => array('tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'])),
--- a/Entity/DocumentRepository.php Mon Feb 27 11:45:38 2012 +0100
+++ b/Entity/DocumentRepository.php Thu Mar 01 18:43:05 2012 +0100
@@ -406,15 +406,19 @@
public function copyTags($src_doc, $tgt_doc)
{
//remove the previous tags
- foreach ($tgt_doc->getTags() as $doctag) {
- $this->getEntityManager()->remove($doctag);
+ if(!is_null($tgt_doc->getTags())) {
+ foreach ($tgt_doc->getTags() as $doctag) {
+ $this->getEntityManager()->remove($doctag);
+ }
}
// add the new ones
- foreach ($src_doc->getTags() as $doctag) {
- $new_doctag = clone $doctag;
- $new_doctag->setDocument($tgt_doc);
- $this->getEntityManager()->persist($new_doctag);
+ if(!is_null($src_doc->getTags())) {
+ foreach ($src_doc->getTags() as $doctag) {
+ $new_doctag = clone $doctag;
+ $new_doctag->setDocument($tgt_doc);
+ $this->getEntityManager()->persist($new_doctag);
+ }
}
$tgt_doc->setManualOrder(false);
--- a/Services/DocumentService.php Mon Feb 27 11:45:38 2012 +0100
+++ b/Services/DocumentService.php Thu Mar 01 18:43:05 2012 +0100
@@ -66,13 +66,13 @@
$src_doc = $doc_rep->findOneByExternalId($id_doc_src);
if(is_null($src_doc))
{
- throw new Exception("cloneTags: no source doc");
+ throw new \Exception("cloneTags: no source doc");
}
$tgt_doc = $doc_rep->findOneByExternalId($id_doc_tgt);
if(is_null($tgt_doc))
{
- throw new Exception("cloneTags: no target doc");
+ throw new \Exception("cloneTags: no target doc");
}
@@ -92,6 +92,9 @@
*/
public function addTags($doc, $tag_labels)
{
+ if(is_null($tag_labels) || (!is_string($tag_labels) && !is_array($tag_labels)) ) {
+ return;
+ }
// We get the DocumentTags
$em = $this->getDoctrine()->getEntityManager();
--- a/Tests/Services/DocumentServiceTest.php Mon Feb 27 11:45:38 2012 +0100
+++ b/Tests/Services/DocumentServiceTest.php Thu Mar 01 18:43:05 2012 +0100
@@ -169,6 +169,45 @@
}
+
+ public function testCopyTagsEmpty()
+ {
+ $doc_service = $this->get("wiki_tag.document");
+
+ $doc0 = $this->getDoctrine()->getRepository("CompanyBaseBundle:Document")->findOneByTitle("Title 0");
+
+ $this->assertEquals(0,count($doc_service->getTagLabels($doc0->getId())));
+
+ $doc1 = $this->getDoctrine()->getRepository("CompanyBaseBundle:Document")->findOneByTitle("Title 1");
+
+ $this->assertEquals(4,count($doc_service->getTagLabels($doc1->getId())));
+
+ $doc_service->copyTags($doc0->getId(), $doc1->getId());
+
+ $this->assertEquals(0,count($doc_service->getTagLabels($doc1->getId())));
+
+ }
+
+ public function testCopyNoPersist()
+ {
+ $doc_service = $this->get("wiki_tag.document");
+ $doc1 = $this->getDoctrine()->getRepository("CompanyBaseBundle:Document")->findOneByTitle("Title 1");
+
+ $newdoc = new \Company\BaseBundle\Entity\Document();
+ $newdoc->setTitle('a title');
+ $newdoc->setDescription('a description');
+
+ $this->getDoctrine()->getEntityManager()->persist($newdoc);
+ $this->getDoctrine()->getEntityManager()->flush();
+
+ $doc_service->copyTags($doc1->getId(), $newdoc->getId());
+
+ $this->getDoctrine()->getEntityManager()->flush();
+
+ $this->assertEquals(4,count($doc_service->getTagLabels($newdoc->getId())));
+
+ }
+
public function setUp()
{
$this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->_kernel);