Commit cc4e74ff authored by Carsten  Rose's avatar Carsten Rose
Browse files

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 { ...@@ -146,6 +146,10 @@ class Evaluate {
*/ */
public function parse($line, $sqlMode = ROW_IMPLODE_ALL, $recursion = 0, &$debugStack = array(), &$foundInStore = '') { public function parse($line, $sqlMode = ROW_IMPLODE_ALL, $recursion = 0, &$debugStack = array(), &$foundInStore = '') {
if ($line === '') {
return '';
}
$flagTokenReplaced = false; $flagTokenReplaced = false;
if ($recursion > 4) { if ($recursion > 4) {
......
...@@ -348,6 +348,7 @@ class FormAction { ...@@ -348,6 +348,7 @@ class FormAction {
$rcStatus = ACTION_ELEMENT_NO_CHANGE; $rcStatus = ACTION_ELEMENT_NO_CHANGE;
if (isset($fe[FE_SLAVE_ID])) {
// Get the slaveId // Get the slaveId
$slaveId = $this->evaluate->parse($fe[FE_SLAVE_ID]); $slaveId = $this->evaluate->parse($fe[FE_SLAVE_ID]);
...@@ -399,6 +400,7 @@ class FormAction { ...@@ -399,6 +400,7 @@ class FormAction {
// After an insert or update, propagate the (new) slave id to the master 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]); $this->db->sql("UPDATE " . $this->primaryTableName . " SET " . $fe[FE_NAME] . " = $slaveId WHERE id = ? LIMIT 1", ROW_REGULAR, [$recordId]);
} }
}
// If given: fire a $sqlAfter query // If given: fire a $sqlAfter query
$this->evaluate->parse($fe[FE_SQL_AFTER]); $this->evaluate->parse($fe[FE_SQL_AFTER]);
......
...@@ -50,7 +50,8 @@ class HelperFormElement { ...@@ -50,7 +50,8 @@ class HelperFormElement {
*/ */
public static function formElementSetDefault(array $elements, array $formSpec) { 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) { foreach ($elements AS $key => $element) {
$elements[$key][FE_TG_INDEX] = 0; $elements[$key][FE_TG_INDEX] = 0;
......
# FormEditor: Small # Form: Small
INSERT INTO Form (id, name, title, noteInternal, tableName, permitNew, permitEdit, render, multiSql, parameter) VALUES INSERT INTO Form (id, name, title, noteInternal, tableName, permitNew, permitEdit, render, multiSql, parameter) VALUES
(3, 'phpunit_person', (3, 'phpunit_person',
'Person {{SELECT ": ", firstName, " ", name, " (", id, ")" FROM Person WHERE id = {{recordId:S0}}}}', '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 ...@@ -6,8 +6,8 @@ INSERT INTO Form (id, name, title, noteInternal, tableName, permitNew, permitEdi
'Person', 'always', 'always', 'bootstrap', '', ''); 'Person', 'always', 'always', 'bootstrap', '', '');
# FormEditor: FormElements # 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 VALUES
(200, 3, 'name', 'Name', 'show', 'text', 'native', 10, 50, 255, '', '', '', '', '', 0, ''),
(200, 3, 'name', 'Name', 'show', 'text', 'native', 10, 50, 255, '', '', '', '', '', '', 0, 'no', ''), (201, 3, 'firstName', 'Firstname', 'show', 'text', 'native', 10, 50, 255, '', '', '', '', '', 0, '');
(201, 3, 'firstName', 'Firstname', 'show', 'text', 'native', 10, 50, 255, '', '', '', '', '', '', 0, 'no', '');
...@@ -311,7 +311,6 @@ class FormActionTest extends AbstractDatabaseTest { ...@@ -311,7 +311,6 @@ class FormActionTest extends AbstractDatabaseTest {
$this->store->setVar('r', '2', STORE_SIP, true); $this->store->setVar('r', '2', STORE_SIP, true);
// slaveId: 0 // slaveId: 0
$feSpecAction[FE_SLAVE_ID] = '0'; $feSpecAction[FE_SLAVE_ID] = '0';
$result = $this->dbArray[DB_INDEX_DEFAULT]->sql('TRUNCATE Address'); $result = $this->dbArray[DB_INDEX_DEFAULT]->sql('TRUNCATE Address');
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
namespace IMATHUZH\Qfq\Tests\Unit\Core; namespace IMATHUZH\Qfq\Tests\Unit\Core;
use IMATHUZH\Qfq\Core\Helper\HelperFormElement;
use IMATHUZH\Qfq\Core\Save; use IMATHUZH\Qfq\Core\Save;
use IMATHUZH\Qfq\Tests\Unit\Core\Database\AbstractDatabaseTest; use IMATHUZH\Qfq\Tests\Unit\Core\Database\AbstractDatabaseTest;
...@@ -21,6 +22,7 @@ class SaveTest extends AbstractDatabaseTest { ...@@ -21,6 +22,7 @@ class SaveTest extends AbstractDatabaseTest {
/** /**
* @throws \CodeException * @throws \CodeException
* @throws \DbException
* @throws \UserFormException * @throws \UserFormException
* @throws \UserReportException * @throws \UserReportException
*/ */
...@@ -44,6 +46,7 @@ class SaveTest extends AbstractDatabaseTest { ...@@ -44,6 +46,7 @@ class SaveTest extends AbstractDatabaseTest {
/** /**
* @throws \CodeException * @throws \CodeException
* @throws \DbException
* @throws \UserFormException * @throws \UserFormException
* @throws \UserReportException * @throws \UserReportException
*/ */
...@@ -61,6 +64,80 @@ class SaveTest extends AbstractDatabaseTest { ...@@ -61,6 +64,80 @@ class SaveTest extends AbstractDatabaseTest {
$this->assertEquals($values, $result[0]); $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);
} }
/** /**
......
Supports Markdown
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