Skip to content

Commit

Permalink
Fixed bug #18627: Missing SQL query errors in admin mail (SQL CODE TH…
Browse files Browse the repository at this point in the history
…AT FAILED) (#3372)

Co-authored-by: Lapiu Dev <devgit@lapiu.biz>
  • Loading branch information
gabrieljenik and lapiudevgit committed Sep 14, 2023
1 parent ea588db commit 19d1cad
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
7 changes: 6 additions & 1 deletion application/helpers/admin/import_helper.php
Expand Up @@ -2611,7 +2611,12 @@ function XMLImportResponses($sFullFilePath, $iSurveyID, $aFieldReMap = array())
}
}
}
if (!SurveyDynamic::model($iSurveyID)->insertRecords($aInsertData)) {
try {
$iNewID = SurveyDynamic::model($iSurveyID)->insertRecords($aInsertData);
if (!$iNewID) {
throw new Exception("Error, no entry id was returned.", 1);
}
} catch (Exception $e) {
throw new Exception(gT("Error") . ": Failed to insert data in response table<br />");
}
$results['responses']++;
Expand Down
14 changes: 10 additions & 4 deletions application/helpers/expressions/em_manager_helper.php
Expand Up @@ -5055,15 +5055,21 @@ private function _UpdateValuesInDatabase($finished = false)
SurveyDynamic::sid($this->sid);
$oSurvey = new SurveyDynamic();

$iNewID = $oSurvey->insertRecords($sdata);
if ($iNewID) { // Checked
try {
$iNewID = $oSurvey->insertRecords($sdata);
if (!$iNewID) {
throw new Exception("Error, no entry id was returned.", 1);
}
$srid = $iNewID;
$_SESSION[$this->sessid]['srid'] = $iNewID;
} else {
} catch (Exception $e) {
$srid = null;
$message .= $this->gT("Unable to insert record into survey table"); // TODO - add SQL error?
submitfailed($this->gT("Unable to insert record into survey table"));
$query = $e->getMessage();
$trace = $e->getTraceAsString();
submitfailed($this->gT("Unable to insert record into survey table"), $query . "\n\n" . $trace);
}

//Insert Row for Timings, if needed
if ($this->surveyOptions['savetimings']) {
SurveyTimingDynamic::sid($this->sid);
Expand Down
8 changes: 2 additions & 6 deletions application/models/SurveyDynamic.php
Expand Up @@ -120,12 +120,8 @@ public function insertRecords($data)
$record->$k = $v;
}

try {
$record->encryptSave();
return $record->id;
} catch (Exception $e) {
return false;
}
$res = $record->encryptSave();
return $res ? $record->id : $res;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/TestBaseClass.php
Expand Up @@ -140,6 +140,8 @@ public static function tearDownAfterClass(): void
\Yii::app()->session['loginID'] = 1;

if (self::$testSurvey) {
// Clear database cache.
\Yii::app()->db->schema->refresh();
if (!self::$testSurvey->delete()) {
self::assertTrue(
false,
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/models/SurveyDynamicTest.php
@@ -0,0 +1,50 @@
<?php

namespace ls\tests;

use Yii;

class SurveyDynamicTest extends TestBaseClass
{
public static function setUpBeforeClass(): void
{
parent::setupBeforeClass();

// Import survey.
$filename = self::$surveysFolder . '/limesurvey_survey_161359_quickTranslation.lss';
self::importSurvey($filename);

// Activate survey.
$activator = new \SurveyActivator(self::$testSurvey);
$activator->activate();
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
}

/**
* Testing that a new response can be correctly inserted.
*/
public function testInsertResponse()
{
$responseId = \SurveyDynamic::model(self::$surveyId)->insertRecords(array('startlanguage' => 'en'));
$response = \SurveyDynamic::model()->findByPk($responseId);

$this->assertIsNumeric($responseId, 'The newly inserted response id should have been returned.');
$this->assertInstanceOf('SurveyDynamic', $response, 'The newly inserted response should have been returned.');
}

/**
* Testing that an exception is thrown when
* response insertion fails.
*/
public function testErrorInsertingResponse()
{
$this->expectException(\CException::class);

// Table column name incorrectly spelled.
$responseId = \SurveyDynamic::model(self::$surveyId)->insertRecords(array('starlanguage' => 'en'));
}
}

0 comments on commit 19d1cad

Please sign in to comment.