Commit cc4e74ff authored by Carsten  Rose's avatar Carsten Rose

Add test for basic form save

parent 116d7be5
Pipeline #2469 passed with stages
in 2 minutes and 45 seconds
......@@ -146,6 +146,10 @@ class Evaluate {
*/
public function parse($line, $sqlMode = ROW_IMPLODE_ALL, $recursion = 0, &$debugStack = array(), &$foundInStore = '') {
if ($line === '') {
return '';
}
$flagTokenReplaced = false;
if ($recursion > 4) {
......
......@@ -348,56 +348,58 @@ class FormAction {
$rcStatus = ACTION_ELEMENT_NO_CHANGE;
// Get the slaveId
$slaveId = $this->evaluate->parse($fe[FE_SLAVE_ID]);
if (isset($fe[FE_SLAVE_ID])) {
// Get the slaveId
$slaveId = $this->evaluate->parse($fe[FE_SLAVE_ID]);
if ($flagFeAction && $slaveId === '' && $fe[FE_NAME] !== '') {
// If the current action element has the same name as a real master record column: take that value as an id.
$slaveId = $this->store->getVar($fe[FE_NAME], STORE_RECORD);
}
if ($flagFeAction && $slaveId === '' && $fe[FE_NAME] !== '') {
// If the current action element has the same name as a real master record column: take that value as an id.
$slaveId = $this->store->getVar($fe[FE_NAME], STORE_RECORD);
}
if ($slaveId === '' || $slaveId === false) {
$slaveId = 0;
}
if ($slaveId === '' || $slaveId === false) {
$slaveId = 0;
}
// Store the slaveId: it's used and replaced in the update statement.
$this->store->setVar(VAR_SLAVE_ID, $slaveId, STORE_VAR, true);
// Store the slaveId: it's used and replaced in the update statement.
$this->store->setVar(VAR_SLAVE_ID, $slaveId, STORE_VAR, true);
$doInsert = ($slaveId == 0);
$doUpdate = ($slaveId != 0);
$doDelete = ($slaveId != 0) && !empty($fe[FE_SQL_DELETE]);
$doInsert = ($slaveId == 0);
$doUpdate = ($slaveId != 0);
$doDelete = ($slaveId != 0) && !empty($fe[FE_SQL_DELETE]);
if (!empty($fe[FE_SQL_HONOR_FORM_ELEMENTS])) {
$filled = $this->checkFormElements($fe[FE_SQL_HONOR_FORM_ELEMENTS]);
$doInsert = $filled && $doInsert;
$doUpdate = $filled && $doUpdate;
$doDelete = !$filled && $doDelete;
}
if (!empty($fe[FE_SQL_HONOR_FORM_ELEMENTS])) {
$filled = $this->checkFormElements($fe[FE_SQL_HONOR_FORM_ELEMENTS]);
$doInsert = $filled && $doInsert;
$doUpdate = $filled && $doUpdate;
$doDelete = !$filled && $doDelete;
}
// Fire slave query
if ($doInsert) {
$slaveId = $this->evaluate->parse($fe[FE_SQL_INSERT]);
// Store the slaveId: might be used later
$this->store->setVar(VAR_SLAVE_ID, $slaveId, STORE_VAR, true);
$rcStatus = ACTION_ELEMENT_MODIFIED;
}
// Fire slave query
if ($doInsert) {
$slaveId = $this->evaluate->parse($fe[FE_SQL_INSERT]);
// Store the slaveId: might be used later
$this->store->setVar(VAR_SLAVE_ID, $slaveId, STORE_VAR, true);
$rcStatus = ACTION_ELEMENT_MODIFIED;
}
if ($doUpdate) {
$this->evaluate->parse($fe[FE_SQL_UPDATE]);
$rcStatus = ACTION_ELEMENT_MODIFIED;
}
if ($doUpdate) {
$this->evaluate->parse($fe[FE_SQL_UPDATE]);
$rcStatus = ACTION_ELEMENT_MODIFIED;
}
// Fire a delete query
if ($doDelete) {
$this->evaluate->parse($fe[FE_SQL_DELETE]);
$slaveId = 0;
$rcStatus = ACTION_ELEMENT_DELETED;
}
// Fire a delete query
if ($doDelete) {
$this->evaluate->parse($fe[FE_SQL_DELETE]);
$slaveId = 0;
$rcStatus = ACTION_ELEMENT_DELETED;
}
// Check if there is a column with the same name as the 'action'-FormElement.
if ($flagFeAction && false !== $this->store->getVar($fe[FE_NAME], STORE_RECORD)) {
// After an insert or update, propagate the (new) slave id to the master record.
$this->db->sql("UPDATE " . $this->primaryTableName . " SET " . $fe[FE_NAME] . " = $slaveId WHERE id = ? LIMIT 1", ROW_REGULAR, [$recordId]);
// Check if there is a column with the same name as the 'action'-FormElement.
if ($flagFeAction && false !== $this->store->getVar($fe[FE_NAME], STORE_RECORD)) {
// After an insert or update, propagate the (new) slave id to the master record.
$this->db->sql("UPDATE " . $this->primaryTableName . " SET " . $fe[FE_NAME] . " = $slaveId WHERE id = ? LIMIT 1", ROW_REGULAR, [$recordId]);
}
}
// If given: fire a $sqlAfter query
......
......@@ -50,7 +50,8 @@ class HelperFormElement {
*/
public static function formElementSetDefault(array $elements, array $formSpec) {
$default = [FE_SLAVE_ID => '', FE_SQL_BEFORE => '', FE_SQL_INSERT => '', FE_SQL_UPDATE => '', FE_SQL_DELETE => '', FE_SQL_AFTER => ''];
// Do not add FE_SLAVE_ID - it's necessary to detect if a value is given or not.
$default = [FE_SQL_BEFORE => '', FE_SQL_INSERT => '', FE_SQL_UPDATE => '', FE_SQL_DELETE => '', FE_SQL_AFTER => ''];
foreach ($elements AS $key => $element) {
$elements[$key][FE_TG_INDEX] = 0;
......
# FormEditor: Small
# Form: Small
INSERT INTO Form (id, name, title, noteInternal, tableName, permitNew, permitEdit, render, multiSql, parameter) VALUES
(3, 'phpunit_person',
'Person {{SELECT ": ", firstName, " ", name, " (", id, ")" FROM Person WHERE id = {{recordId:S0}}}}',
......@@ -6,8 +6,8 @@ INSERT INTO Form (id, name, title, noteInternal, tableName, permitNew, permitEdi
'Person', 'always', 'always', 'bootstrap', '', '');
# FormEditor: FormElements
INSERT INTO FormElement (id, formId, name, label, mode, type, class, ord, size, maxLength, note, clientJs, value, sql1, sql2, parameter, feIdContainer, debug, modeSql)
INSERT INTO FormElement (id, formId, name, label, mode, type, class, ord, size, maxLength, note, clientJs, value, sql1,
parameter, feIdContainer, modeSql)
VALUES
(200, 3, 'name', 'Name', 'show', 'text', 'native', 10, 50, 255, '', '', '', '', '', '', 0, 'no', ''),
(201, 3, 'firstName', 'Firstname', 'show', 'text', 'native', 10, 50, 255, '', '', '', '', '', '', 0, 'no', '');
(200, 3, 'name', 'Name', 'show', 'text', 'native', 10, 50, 255, '', '', '', '', '', 0, ''),
(201, 3, 'firstName', 'Firstname', 'show', 'text', 'native', 10, 50, 255, '', '', '', '', '', 0, '');
......@@ -311,7 +311,6 @@ class FormActionTest extends AbstractDatabaseTest {
$this->store->setVar('r', '2', STORE_SIP, true);
// slaveId: 0
$feSpecAction[FE_SLAVE_ID] = '0';
$result = $this->dbArray[DB_INDEX_DEFAULT]->sql('TRUNCATE Address');
......
......@@ -8,7 +8,8 @@
namespace IMATHUZH\Qfq\Tests\Unit\Core;
use IMATHUZH\Qfq\Core\Helper\HelperFormElement;
use IMATHUZH\Qfq\Core\Save;
use IMATHUZH\Qfq\Tests\Unit\Core\Database\AbstractDatabaseTest;
......@@ -21,6 +22,7 @@ class SaveTest extends AbstractDatabaseTest {
/**
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
......@@ -44,6 +46,7 @@ class SaveTest extends AbstractDatabaseTest {
/**
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
......@@ -61,6 +64,80 @@ class SaveTest extends AbstractDatabaseTest {
$this->assertEquals($values, $result[0]);
}
/**
* Very basic test: Fake a form with two text inputs.
*
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
public function testSaveBasicText() {
$formSpec = [
F_DB_INDEX => DB_INDEX_DEFAULT,
F_TABLE_NAME => 'Person',
F_MULTI_SQL => '',
F_PRIMARY_KEY => F_PRIMARY_KEY_DEFAULT,
];
$feSpecNative = [
[
FE_ID => 1,
FE_TYPE => FE_TYPE_TEXT,
FE_NAME => 'name',
],
[
FE_ID => 2,
FE_TYPE => FE_TYPE_TEXT,
FE_NAME => 'firstName',
]
];
// Clean
$this->dbArray[DB_INDEX_DEFAULT]->sql('DELETE FROM Person WHERE name LIKE "DoeTmp"');
// Prepare Setup
$tableDefinition = $this->dbArray[DB_INDEX_DEFAULT]->getTableDefinition('Person');
$this->store->fillStoreTableDefaultColumnType($tableDefinition);
// Init FE
$feSpecNative = HelperFormElement::formElementSetDefault($feSpecNative, $formSpec);
// Fake POST value
$this->store::setStore(['name' => 'DoeTmp', 'firstName' => 'John'], STORE_FORM, true);
// ------------- Check -------------
// 1) Expect INSERT into Person
$save = new Save($formSpec, [], $feSpecNative, $feSpecNative);
$save->process();
// Get newly save record
$sql = "SELECT name, firstName FROM Person WHERE name like 'DoeTmp'";
$result = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_IMPLODE_ALL);
// Compare
$this->assertEquals('DoeTmpJohn', $result);
// // ------------- Check -------------
// $id=$this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id FROM Person WHERE name="DoeTmp"');
// // Fake POST value
// $this->store::setStore(['name' => 'DoeTmp', 'firstName' => 'JohnUpdate'], STORE_FORM, true);
// $this->store::setVar(SIP_RECORD_ID, $id,STORE_SIP, true);
//
// // 1) Expect UPDATE Person
// $save = new Save($formSpec, [], $feSpecNative, $feSpecNative);
// $save->process();
//
// // Get newly save record
// $sql = "SELECT name, firstName FROM Person WHERE name like 'DoeTmp'";
// $result = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_IMPLODE_ALL);
//
// // Compare
// $this->assertEquals('DoeTmpJohnUpdate' , $result);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment