public function testAddPage()
 {
     //GIVEN
     $oPage = AM_Model_Db_Table_Abstract::factory('page')->findOneBy(array('id' => 2));
     $oTemplate = AM_Model_Db_Table_Abstract::factory('template')->findOneBy(array('id' => 1));
     $aUser = array('id' => 1);
     $oHandler = new AM_Handler_Page();
     //WHEN
     $oNewPage = $oHandler->addPage($oPage, $oTemplate, AM_Model_Db_Page::LINK_RIGHT, $aUser, false);
     //THEN
     $oGivenDataSet = $this->getConnection()->createQueryTable('page', 'SELECT id, title, revision, user, template, connections FROM page ORDER BY id');
     $oExpectedDataSet = $this->createFlatXMLDataSet(dirname(__FILE__) . '/_dataset/HandlerPageAddTest.xml')->getTable('page');
     $this->assertTablesEqual($oExpectedDataSet, $oGivenDataSet);
     $oGivenDataSet = $this->getConnection()->createQueryTable('page_imposition', 'SELECT page, is_linked_to, link_type FROM page_imposition ORDER BY id');
     $oExpectedDataSet = $this->createFlatXMLDataSet(dirname(__FILE__) . '/_dataset/HandlerPageAddTest.xml')->getTable('page_imposition');
     $this->assertTablesEqual($oExpectedDataSet, $oGivenDataSet);
 }
 /**
  * PageMap add-page action
  */
 public function addPageAction()
 {
     $aMessage = array('success' => false);
     try {
         $iRevisionId = intval($this->_getParam('rid'));
         $iPageId = intval($this->_getParam('pid'));
         $iTemplateId = intval($this->_getParam('tid'));
         $sLinkType = (string) $this->_getParam('type');
         $bBetween = (bool) $this->_getParam('between');
         if (!AM_Model_Db_Table_Abstract::factory('page')->checkAccess($iPageId, $this->_aUserInfo)) {
             throw new AM_Controller_Exception_Forbidden('Access denied');
         }
         $oPage = AM_Model_Db_Table_Abstract::factory('page')->findOneBy(array('id' => $iPageId));
         /* @var $oPage AM_Model_Db_Page */
         if (is_null($oPage)) {
             throw new AM_Controller_Exception_BadRequest('Incorrect parameters were given. Parent page not found.');
         }
         $oTemplate = AM_Model_Db_Table_Abstract::factory('template')->findOneBy(array('id' => $iTemplateId));
         /* @var $oTemplate AM_Model_Db_Template */
         if (is_null($oTemplate)) {
             throw new AM_Controller_Exception_BadRequest('Incorrect parameters were given. Template not found.');
         }
         $oPageHandler = new AM_Handler_Page();
         $oPageNew = $oPageHandler->addPage($oPage, $oTemplate, $sLinkType, $this->getUser(), $bBetween);
         if (is_null($oPageNew)) {
             throw new AM_Controller_Exception('Can\'t add page');
         }
         $oPage->getRevision()->exportRevision();
         $aMessage['page'] = AM_Handler_Page::parsePage($oPageNew);
         $aMessage['pid'] = $oPageNew->id;
         $aMessage['success'] = true;
     } catch (Exception $oException) {
         $aMessage['message'] = $oException->getMessage();
     }
     return $this->getHelper('Json')->sendJson($aMessage);
 }