diff --git a/application/helpers/admin/import_helper.php b/application/helpers/admin/import_helper.php index 77ade49b6ae..c0273b80bc8 100644 --- a/application/helpers/admin/import_helper.php +++ b/application/helpers/admin/import_helper.php @@ -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
"); } $results['responses']++; diff --git a/application/helpers/expressions/em_manager_helper.php b/application/helpers/expressions/em_manager_helper.php index eec4fac0d0f..d4e94f6653d 100644 --- a/application/helpers/expressions/em_manager_helper.php +++ b/application/helpers/expressions/em_manager_helper.php @@ -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); diff --git a/application/models/SurveyDynamic.php b/application/models/SurveyDynamic.php index 2e8a39e3ac1..a98b16a0268 100644 --- a/application/models/SurveyDynamic.php +++ b/application/models/SurveyDynamic.php @@ -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; } /** diff --git a/tests/TestBaseClass.php b/tests/TestBaseClass.php index 8f6613e74c7..1d4728ba233 100644 --- a/tests/TestBaseClass.php +++ b/tests/TestBaseClass.php @@ -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, diff --git a/tests/unit/models/SurveyDynamicTest.php b/tests/unit/models/SurveyDynamicTest.php new file mode 100644 index 00000000000..905e175f832 --- /dev/null +++ b/tests/unit/models/SurveyDynamicTest.php @@ -0,0 +1,50 @@ +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')); + } +}