Commit c9d81811 authored by Carsten  Rose's avatar Carsten Rose

Implements #9352 - add unit tests

parent cc4e74ff
Pipeline #2470 passed with stages
in 2 minutes and 38 seconds
......@@ -343,11 +343,9 @@ class FormAction {
*/
public function doSqlBeforeSlaveAfter(array $fe, $recordId, $flagFeAction) {
// If given: fire a sqlBefore query
$this->evaluate->parse($fe[FE_SQL_BEFORE]);
$rcStatus = ACTION_ELEMENT_NO_CHANGE;
// slaveId might be used in sqlBefore: get it first.
if (isset($fe[FE_SLAVE_ID])) {
// Get the slaveId
$slaveId = $this->evaluate->parse($fe[FE_SLAVE_ID]);
......@@ -363,7 +361,12 @@ class FormAction {
// Store the slaveId: it's used and replaced in the update statement.
$this->store->setVar(VAR_SLAVE_ID, $slaveId, STORE_VAR, true);
}
// If given: fire a sqlBefore query
$this->evaluate->parse($fe[FE_SQL_BEFORE]);
if (isset($fe[FE_SLAVE_ID])) {
$doInsert = ($slaveId == 0);
$doUpdate = ($slaveId != 0);
$doDelete = ($slaveId != 0) && !empty($fe[FE_SQL_DELETE]);
......
......@@ -107,7 +107,7 @@ class SaveTest extends AbstractDatabaseTest {
// Fake POST value
$this->store::setStore(['name' => 'DoeTmp', 'firstName' => 'John'], STORE_FORM, true);
// ------------- Check -------------
// ------------- Check: Insert -------------
// 1) Expect INSERT into Person
$save = new Save($formSpec, [], $feSpecNative, $feSpecNative);
$save->process();
......@@ -120,27 +120,109 @@ class SaveTest extends AbstractDatabaseTest {
$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);
// ------------- Check: Update -------------
$id = $this->dbArray[DB_INDEX_DEFAULT]->sql('SELECT id FROM Person WHERE name="DoeTmp"', ROW_IMPLODE_ALL);
// 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->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);
}
/**
* Test 'slaveId': Fake a form with two text inputs.
*
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
* @throws \UserReportException
*/
public function testSaveSlaveId() {
$this->dbArray[DB_INDEX_DEFAULT]->sql('DELETE FROM Address WHERE street="deleteMe"');
$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',
FE_SQL_BEFORE => '{{UPDATE Address SET city=CONCAT(city, "-") WHERE id={{slaveId:V}} }}',
FE_SLAVE_ID => '{{SELECT id FROM Address WHERE street="deleteMe"}}',
FE_SQL_INSERT => '{{INSERT INTO Address (street, city) VALUES ("deleteMe","Zurich")}}',
FE_SQL_UPDATE => '{{UPDATE Address SET city=CONCAT(city, " Oerlikon") WHERE id={{slaveId:V}} }}',
FE_SQL_AFTER => '{{UPDATE Address SET city=CONCAT(city, "+") WHERE id={{slaveId:V}} }}',
]
];
// 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: sqlBefore/sqlInsert/sqlAfter
*
* slaveId becomes '0', cause there is no Address record 'street="deleteMe"'.
* sqlBefore does nothing, cause there is no Address record 'street="deleteMe"'.
* sqlInsert will create an Address record 'street="deleteMe"'.
* sqlAfter adds '+' after city name.
*/
$save = new Save($formSpec, [], $feSpecNative, $feSpecNative);
$save->process();
// Get newly save record
$sql = "SELECT city FROM Address WHERE street like 'deleteMe'";
$result = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_IMPLODE_ALL);
// Compare
$this->assertEquals('Zurich+', $result);
/*
* Check: sqlBefore/sqlUpdate/sqlAfter - the previous 'deleteMe' record have to exist.
*
* slaveId becomes id of the previous created Address record 'street="deleteMe"'.
* sqlBefore adds '-' after city name.
* sqlUpdate adds ' Oerlikon' after city name.
* sqlAfter adds '+' after city name.
*/
$save->process();
// Get newly save record
$sql = "SELECT city FROM Address WHERE street like 'deleteMe'";
$result = $this->dbArray[DB_INDEX_DEFAULT]->sql($sql, ROW_IMPLODE_ALL);
// Compare
$this->assertEquals('Zurich+- Oerlikon+', $result);
}
/**
* UNIT Tests
*
* @throws \CodeException
* @throws \DbException
* @throws \UserFormException
......
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