function test1()
 {
     $user = new User();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("ouhosu");
     $this->em->persist($user);
     $tree = new Tree();
     $tree->setTitleAdmin('Tree');
     $tree->setPublicId('tree');
     $tree->setOwner($user);
     $this->em->persist($tree);
     $treeVersion = new TreeVersion();
     $treeVersion->setTree($tree);
     $treeVersion->setPublicId('version');
     $treeVersion->setFeatureLibraryContent(true);
     $this->em->persist($treeVersion);
     $startNode = new Node();
     $startNode->setTreeVersion($treeVersion);
     $startNode->setTitle("START HERE");
     $startNode->setPublicId('start');
     $this->em->persist($startNode);
     $libraryContent = new LibraryContent();
     $libraryContent->setTreeVersion($treeVersion);
     $libraryContent->setBodyText('TREE CONTENT');
     $this->em->persist($libraryContent);
     $nodeHasLibraryContent = new NodeHasLibraryContent();
     $nodeHasLibraryContent->setSort(0);
     $nodeHasLibraryContent->setNode($startNode);
     $nodeHasLibraryContent->setLibraryContent($libraryContent);
     $this->em->persist($nodeHasLibraryContent);
     $treeVersionPublished = new TreeVersionPublished();
     $treeVersionPublished->setTreeVersion($treeVersion);
     $treeVersionPublished->setPublishedBy($user);
     $this->em->flush();
     $tvsn = new TreeVersionStartingNode();
     $tvsn->setNode($startNode);
     $tvsn->setTreeVersion($treeVersion);
     $this->em->persist($tvsn);
     $published = new TreeVersionPublished();
     $published->setTreeVersion($treeVersion);
     $this->em->persist($published);
     $this->em->flush();
     // ######################################################## LOAD PAGE
     $this->driver->get('http://localhost/app_dev.php/tree/tree/demo');
     $startLink = $this->driver->findElement(WebDriverBy::id('StartTreeLink'));
     $this->assertEquals('Start Tree!', $startLink->getText());
     // ######################################################## Start Tree
     $startLink->click();
     sleep($this->sleepOnActionWithNetwork);
     $nodeTitle = $this->driver->findElement(WebDriverBy::id('DemoHere'))->findElement(WebDriverBy::className('node'))->findElement(WebDriverBy::className('title'));
     $this->assertEquals('START HERE', $nodeTitle->getText());
     $nodeBody = $this->driver->findElement(WebDriverBy::id('DemoHere'))->findElement(WebDriverBy::className('node'))->findElement(WebDriverBy::className('body'));
     $this->assertEquals('TREE CONTENT', $nodeBody->getText());
 }
 public function addLibraryContentToNode(LibraryContent $libraryContent, Node $node)
 {
     $nodeHasLibraryContent = $this->findOneBy(array('node' => $node, 'libraryContent' => $libraryContent));
     if (!$nodeHasLibraryContent) {
         $currentMax = $this->getEntityManager()->createQuery(' SELECT MAX(nhlc.sort) FROM QuestionKeyBundle:NodeHasLibraryContent nhlc' . ' WHERE nhlc.node = :node')->setParameter('node', $node)->getScalarResult();
         $nodeHasLibraryContent = new NodeHasLibraryContent();
         $nodeHasLibraryContent->setNode($node);
         $nodeHasLibraryContent->setLibraryContent($libraryContent);
         $nodeHasLibraryContent->setSort(is_null($currentMax[0][1]) ? 0 : $currentMax[0][1] + 1);
         $this->getEntityManager()->persist($nodeHasLibraryContent);
         $this->getEntityManager()->flush($nodeHasLibraryContent);
     }
 }
 public function setUp()
 {
     parent::setUp();
     // ################################################ Basic Details
     $user = new User();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("ouhosu");
     $this->em->persist($user);
     $tree = new Tree();
     $tree->setTitleAdmin('Tree');
     $tree->setPublicId('tree');
     $tree->setOwner($user);
     $this->em->persist($tree);
     $treeVersion = new TreeVersion();
     $treeVersion->setTree($tree);
     $treeVersion->setPublicId('version');
     $treeVersion->setFeatureLibraryContent(true);
     $treeVersion->setFeatureVariables(true);
     $this->em->persist($treeVersion);
     // ################################################ Variable
     $variable = new Variable();
     $variable->setTreeVersion($treeVersion);
     $variable->setName('score');
     $variable->setType('Integer');
     $this->em->persist($variable);
     // ################################################ Nodes
     $startNode = new Node();
     $startNode->setTreeVersion($treeVersion);
     $startNode->setTitle("START HERE");
     $startNode->setPublicId('start');
     $this->em->persist($startNode);
     $endNode = new Node();
     $endNode->setTreeVersion($treeVersion);
     $endNode->setTitle("END HERE");
     $endNode->setPublicId('end');
     $this->em->persist($endNode);
     // ################################################ Node option for points with variable action
     $nodeOptionPoints = new NodeOption();
     $nodeOptionPoints->setTitle("POINTS");
     $nodeOptionPoints->setTreeVersion($treeVersion);
     $nodeOptionPoints->setNode($startNode);
     $nodeOptionPoints->setDestinationNode($endNode);
     $nodeOptionPoints->setSort(10);
     $nodeOptionPoints->setPublicId('points');
     $this->em->persist($nodeOptionPoints);
     $nodeOptionPointsAction = new NodeOptionVariableAction();
     $nodeOptionPointsAction->setNodeOption($nodeOptionPoints);
     $nodeOptionPointsAction->setVariable($variable);
     $nodeOptionPointsAction->setValue(10);
     $nodeOptionPointsAction->setAction('assign');
     $this->em->persist($nodeOptionPointsAction);
     // ################################################ Node option for no points
     $nodeOptionNoPoints = new NodeOption();
     $nodeOptionNoPoints->setTitle("NO POINTS");
     $nodeOptionNoPoints->setTreeVersion($treeVersion);
     $nodeOptionNoPoints->setNode($startNode);
     $nodeOptionNoPoints->setDestinationNode($endNode);
     $nodeOptionNoPoints->setPublicId('nopoints');
     $nodeOptionNoPoints->setSort(100);
     $this->em->persist($nodeOptionNoPoints);
     // ################################################ Library Content for points
     $libraryContentPoints = new LibraryContent();
     $libraryContentPoints->setTreeVersion($treeVersion);
     $libraryContentPoints->setBodyText('POINTS');
     $this->em->persist($libraryContentPoints);
     $nodeHasLibraryContentPoints = new NodeHasLibraryContent();
     $nodeHasLibraryContentPoints->setSort(0);
     $nodeHasLibraryContentPoints->setNode($endNode);
     $nodeHasLibraryContentPoints->setLibraryContent($libraryContentPoints);
     $this->em->persist($nodeHasLibraryContentPoints);
     $nodeHasLibraryContentPointsIfVariable = new NodeHasLibraryContentIfVariable();
     $nodeHasLibraryContentPointsIfVariable->setPublicId('points');
     $nodeHasLibraryContentPointsIfVariable->setLibraryContent($libraryContentPoints);
     $nodeHasLibraryContentPointsIfVariable->setNode($endNode);
     $nodeHasLibraryContentPointsIfVariable->setVariable($variable);
     $nodeHasLibraryContentPointsIfVariable->setAction('>');
     $nodeHasLibraryContentPointsIfVariable->setValue(0);
     $this->em->persist($nodeHasLibraryContentPointsIfVariable);
     // ################################################ Library Content for NO points
     $libraryContentNoPoints = new LibraryContent();
     $libraryContentNoPoints->setTreeVersion($treeVersion);
     $libraryContentNoPoints->setBodyText('NO POINTS');
     $this->em->persist($libraryContentNoPoints);
     $nodeHasLibraryContentNoPoints = new NodeHasLibraryContent();
     $nodeHasLibraryContentNoPoints->setSort(0);
     $nodeHasLibraryContentNoPoints->setNode($endNode);
     $nodeHasLibraryContentNoPoints->setLibraryContent($libraryContentNoPoints);
     $this->em->persist($nodeHasLibraryContentNoPoints);
     $nodeHasLibraryContentNoPointsIfVariable = new NodeHasLibraryContentIfVariable();
     $nodeHasLibraryContentNoPointsIfVariable->setPublicId('nopoints');
     $nodeHasLibraryContentNoPointsIfVariable->setLibraryContent($libraryContentNoPoints);
     $nodeHasLibraryContentNoPointsIfVariable->setNode($endNode);
     $nodeHasLibraryContentNoPointsIfVariable->setVariable($variable);
     $nodeHasLibraryContentNoPointsIfVariable->setAction('==');
     $nodeHasLibraryContentNoPointsIfVariable->setValue(0);
     $this->em->persist($nodeHasLibraryContentNoPointsIfVariable);
     // ################################################ MISC
     $treeVersionPublished = new TreeVersionPublished();
     $treeVersionPublished->setTreeVersion($treeVersion);
     $treeVersionPublished->setPublishedBy($user);
     $this->em->flush();
     $tvsn = new TreeVersionStartingNode();
     $tvsn->setNode($startNode);
     $tvsn->setTreeVersion($treeVersion);
     $this->em->persist($tvsn);
     $published = new TreeVersionPublished();
     $published->setTreeVersion($treeVersion);
     $this->em->persist($published);
     $this->em->flush();
 }
 public function go()
 {
     // Tree Option
     $this->newVersion->setFromOldVersion($this->oldVersion);
     // As we call flush in this function, make sure new version will definetly be part of that.
     $this->doctrine->persist($this->newVersion);
     // repos
     $nodeRepo = $this->doctrine->getRepository('QuestionKeyBundle:Node');
     $nodeOptionRepo = $this->doctrine->getRepository('QuestionKeyBundle:NodeOption');
     $libraryContentRepo = $this->doctrine->getRepository('QuestionKeyBundle:LibraryContent');
     $nodeHasLibraryContentRepo = $this->doctrine->getRepository('QuestionKeyBundle:NodeHasLibraryContent');
     $treeStartingNodeRepo = $this->doctrine->getRepository('QuestionKeyBundle:TreeVersionStartingNode');
     // Library Content
     $libraryContents = array();
     foreach ($libraryContentRepo->findByTreeVersion($this->oldVersion) as $libraryContent) {
         $libraryContents[$libraryContent->getPublicId()] = new LibraryContent();
         $libraryContents[$libraryContent->getPublicId()]->setTitleAdmin($libraryContent->getTitleAdmin());
         $libraryContents[$libraryContent->getPublicId()]->setBodyHTML($libraryContent->getBodyHTML());
         $libraryContents[$libraryContent->getPublicId()]->setBodyText($libraryContent->getBodyText());
         $libraryContents[$libraryContent->getPublicId()]->setTreeVersion($this->newVersion);
         $libraryContents[$libraryContent->getPublicId()]->setPublicId($libraryContent->getPublicId());
         $this->doctrine->persist($libraryContents[$libraryContent->getPublicId()]);
     }
     // Nodes
     $nodes = $nodeRepo->findByTreeVersion($this->oldVersion);
     $newNodes = array();
     foreach ($nodes as $node) {
         $newNodes[$node->getId()] = new Node();
         $newNodes[$node->getId()]->setTreeVersion($this->newVersion);
         $newNodes[$node->getId()]->setTitle($node->getTitle());
         $newNodes[$node->getId()]->setBodyText($node->getBodyText());
         $newNodes[$node->getId()]->setBodyHTML($node->getBodyHTML());
         $newNodes[$node->getId()]->setPublicId($node->getPublicId());
         $newNodes[$node->getId()]->setFromOldVersion($node);
         $this->doctrine->persist($newNodes[$node->getId()]);
         $sort = 0;
         foreach ($libraryContentRepo->findForNode($node) as $oldLibraryContent) {
             // We create a NodeHasLibraryContent record directly instead of using
             //     $nodeHasLibraryContentRepo->addLibraryContentToNode($libraryContents[$oldLibraryContent->getPublicId()],  $newNodes[$node->getId()]);
             // because it's much more efficient in DB queries. Also, given some records might not have saved I'm not sure addLibraryContentToNode() would find the correct sort values anyway.
             $nodeHasLibraryContent = new NodeHasLibraryContent();
             $nodeHasLibraryContent->setLibraryContent($libraryContents[$oldLibraryContent->getPublicId()]);
             $nodeHasLibraryContent->setNode($newNodes[$node->getId()]);
             $nodeHasLibraryContent->setSort($sort++);
             $this->doctrine->persist($nodeHasLibraryContent);
         }
     }
     // Node Options
     $nodeOptions = $nodeOptionRepo->findAllNodeOptionsForTreeVersion($this->oldVersion);
     foreach ($nodeOptions as $nodeOption) {
         $newNodeOption = new NodeOption();
         $newNodeOption->setTreeVersion($this->newVersion);
         $newNodeOption->setNode($newNodes[$nodeOption->getNode()->getId()]);
         $newNodeOption->setDestinationNode($newNodes[$nodeOption->getDestinationNode()->getId()]);
         $newNodeOption->setTitle($nodeOption->getTitle());
         $newNodeOption->setBodyHTML($nodeOption->getBodyHTML());
         $newNodeOption->setBodyText($nodeOption->getBodyText());
         $newNodeOption->setSort($nodeOption->getSort());
         $newNodeOption->setPublicId($nodeOption->getPublicId());
         $newNodeOption->setFromOldVersion($nodeOption);
         $this->doctrine->persist($newNodeOption);
     }
     // Tree Start
     $treeStartingNode = $treeStartingNodeRepo->findOneByTreeVersion($this->oldVersion);
     if ($treeStartingNode) {
         // we have to flush at this point otherwise when we try and persist at the next stage you get an error.
         $this->doctrine->flush();
         $newTreeVersionStartingNode = new TreeVersionStartingNode();
         $newTreeVersionStartingNode->setTreeVersion($this->newVersion);
         $newTreeVersionStartingNode->setNode($newNodes[$treeStartingNode->getNode()->getId()]);
         $this->doctrine->persist($newTreeVersionStartingNode);
     }
     // final flush
     $this->doctrine->flush();
 }