/** * Test validations */ public function testRecordValidate() { $project = new Phprojekt_Project(array('db' => $this->sharedFixture)); $db = new Phprojekt_DatabaseManager($project, array('db' => $this->sharedFixture)); $message = 'The Module must contain at least one field'; $this->assertFalse($db->recordValidate(array())); $db->recordValidate(array()); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'Please enter a name for this module'; $this->assertFalse($db->recordValidate(array(array('tableName' => '')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'The module name must start with a letter'; $this->assertFalse($db->recordValidate(array(array('tableName' => '212')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'All the fields must have a table name'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => '', 'tableField' => '', 'tableLength' => 0, 'formType' => ''), array('tableName' => '', 'tableType' => '', 'tableField' => '', 'tableLength' => 0, 'formType' => '')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'There are two fields with the same Field Name'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => '', 'tableField' => 'project_id', 'tableLength' => 0, 'formType' => ''), array('tableName' => 'Project', 'tableType' => '', 'tableField' => 'project_id', 'tableLength' => 0, 'formType' => '')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'Invalid parameters'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'varchar', 'tableField' => 'project_id', 'tableLength' => 11)))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'The length of the varchar fields must be between 1 and 255'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'varchar', 'tableField' => 'project_id', 'tableLength' => 260, 'formType' => '')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'The length of the int fields must be between 1 and 11'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'int', 'tableField' => 'project_id', 'tableLength' => 12, 'formType' => '')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'Invalid form Range for the select field'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'int', 'tableField' => 'project_id', 'tableLength' => 11, 'formType' => 'selectValues')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'Invalid form Range for the select field'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'int', 'tableField' => 'project_id', 'tableLength' => 11, 'formType' => 'selectValues', 'formRange' => '')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'Invalid form Range for the select field'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'int', 'tableField' => 'project_id', 'tableLength' => 11, 'formType' => 'selectValues', 'formRange' => 'Project#id', 'selectType' => 'project')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'Invalid form Range for the select field'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'int', 'tableField' => 'project_id', 'tableLength' => 11, 'formType' => 'selectValues', 'formRange' => '1#e', 'selectType' => 'custom')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'Invalid form Range for the select field'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'int', 'tableField' => 'project_id', 'tableLength' => 11, 'formType' => 'selectValues', 'formRange' => '1#e | 2', 'selectType' => 'custom')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'The module must have a project selector called project_id'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'int', 'tableField' => 'project', 'tableLength' => 11, 'formType' => 'selectValues', 'formRange' => '1#Gustavo | 2#Solt', 'selectType' => 'custom')))); $error = $db->getError(); $this->assertEquals($message, $error['message']); $message = 'The module must have at least one field with the list position greater than 0'; $this->assertFalse($db->recordValidate(array(array('tableName' => 'Project', 'tableType' => 'int', 'tableField' => 'project_id', 'tableLength' => 11, 'formType' => 'selectValues', 'formRange' => '1#Gustavo | 2#Solt', 'selectType' => 'custom', 'listPosition' => 0)))); $error = $db->getError(); $this->assertEquals($message, $error['message']); }
private function _handleDatabaseChange($model, $module, $data, $saveType, $id) { $ret = array('type' => null, 'message' => null, 'id' => $id); if ($model instanceof Phprojekt_Item_Abstract || $id == 0) { $databaseManager = new Phprojekt_DatabaseManager($model); $data = Zend_Json::decode($data); // Validate if ($databaseManager->recordValidate($data, $saveType)) { // Update Table Structure $tableData = $this->_getTableData($data); if (!$databaseManager->syncTable($data, $module, $tableData)) { $ret['type'] = 'error'; $ret['message'] = Phprojekt::getInstance()->translate('There was an error writing the table'); } else { // remove possible id's as we are not allowed to change id's foreach ($data as $key => $value) { unset($data[$key]['id']); } // Update DatabaseManager Table $databaseManager->saveData($module, $data, $tableData); $ret = null; } } else { $error = $databaseManager->getError(); $ret['message'] = $error['label'] . ': ' . $error['message']; $ret['type'] = 'error'; } } else { $ret = null; } return $ret; }
/** * Saves the design of all the fields in the module. * * If the request parameter "id" is null or 0, the function will add a new module, * if the "id" is an existing module, the function will update it. * * The save action will try to add or update the module table itself and the database_manager. * * REQUIRES request parameters: * <pre> * - integer <b>id</b> id of the module to save. * - string <b>designerData</b> Data of the fields. * - string <b>name</b> Name of the module. * - string <b>label</b> Display of the module. * </pre> * * The return is a string in JSON format with: * <pre> * - type => 'success' or 'error'. * - message => Success or error message. * - code => 0. * - id => id of the module. * </pre> * * @throws Phprojekt_PublishedException On error in the action save. * * @return void */ public function jsonSaveAction() { $id = (int) $this->getRequest()->getParam('id'); $data = $this->getRequest()->getParam('designerData'); $saveType = (int) $this->getRequest()->getParam('saveType'); $model = null; $module = Cleaner::sanitize('alnum', $this->getRequest()->getParam('name', null)); $this->setCurrentProjectId(); if (empty($module)) { $module = Cleaner::sanitize('alnum', $this->getRequest()->getParam('label')); } $module = ucfirst(str_replace(" ", "", $module)); if ($id > 0) { $model = Phprojekt_Loader::getModel($module, $module); } if ($model instanceof Phprojekt_Item_Abstract || $id == 0) { $databaseManager = new Phprojekt_DatabaseManager($model); $data = Zend_Json_Decoder::decode($data); // Validate if ($databaseManager->recordValidate($data, $saveType)) { // Update Table Structure $tableData = $this->_getTableData($data); if (!$databaseManager->syncTable($data, $module, $tableData)) { $type = 'error'; $message = Phprojekt::getInstance()->translate('There was an error writing the table'); } else { // Update DatabaseManager Table $databaseManager->saveData($module, $data, $tableData); if (empty($id)) { $message = Phprojekt::getInstance()->translate('The table module was created correctly'); } else { $message = Phprojekt::getInstance()->translate('The table module was edited correctly'); } $type = 'success'; } } else { $error = $databaseManager->getError(); $message = $error['label'] . ': ' . $error['message']; $type = 'error'; } } else { $type = 'success'; $message = null; } $return = array('type' => $type, 'message' => $message, 'code' => 0, 'id' => $id); Phprojekt_Converter_Json::echoConvert($return); }