Skip to content

Commit

Permalink
Merge pull request #34 from EscolaLMS/bugfix/SUD-446
Browse files Browse the repository at this point in the history
SUD-446 Quote regular expression characters
  • Loading branch information
daVitekPL authored Oct 10, 2024
2 parents 63406a2 + 36120de commit 418b2ef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Strategies/MultipleChoiceQuestionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getFeedbackByAnswer(string $answer): string
{
$answerBlock = $this->service->getAnswerFromQuestion($this->questionPlainText);

$pattern = chr(58) . "$answer(.*?)(~|$)" . chr(58);
$pattern = chr(58) . preg_quote($answer, '/') . "(.*?)(~|$)" . chr(58);
if (preg_match($pattern, $answerBlock, $matches)) {
return trim(Str::replace('#', ' ', $matches[1]));
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Api/AttemptAnswerSaveApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ public function questionDataProvider(): array
'score' => 1,
'resultScore' => 0.75,
],
[
'question' => 'Who\'s buried in Grant\'s tomb?{~a) no one ~b) Napoleon =c) Grant ~d) Churchill ~e) Mother Teresa }',
'type' => QuestionTypeEnum::MULTIPLE_CHOICE,
'answer' => [AnswerKeyEnum::TEXT => 'c) Grant'],
],
[
'question' => '::Jesus hometown::Jesus Christ was from {
~a) Jerusalem#This was an important city, but the wrong answer.
~%25%b) Bethlehem#He was born here, but not raised here.
~%50%c) Galilee#You need to be more specific.
=d) Nazareth#Yes! That\'s right!
}.',
'type' => QuestionTypeEnum::MULTIPLE_CHOICE_WITH_MULTIPLE_RIGHT_ANSWERS,
'answer' => [AnswerKeyEnum::MULTIPLE => ['b) Bethlehem', 'c) Galilee']],
'score' => 1,
'resultScore' => 0.75,
],
];
}
}
19 changes: 17 additions & 2 deletions tests/GiftQuestionTesting.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function questionDataProvider(): array
'no one',
'Napoleon',
'Churchill',
'Mother Teresa'
'Mother Teresa',
],
]
],
Expand All @@ -94,7 +94,7 @@ public function questionDataProvider(): array
'No one',
'Napoleon',
'Churchill',
'Mother Teresa'
'Mother Teresa',
],
]
],
Expand Down Expand Up @@ -272,6 +272,21 @@ public function questionDataProvider(): array
'questionForStudent' => 'Jesus Christ was from',
'options' => [],
],
[
'question' => 'Who\'s buried in Grant\'s tomb\?{=a) Grant ~b) no one ~c) Napoleon ~d) Churchill ~e) Mother Teresa }',
'type' => QuestionTypeEnum::MULTIPLE_CHOICE,
'title' => '',
'questionForStudent' => 'Who\'s buried in Grant\'s tomb?',
'options' => [
'answers' => [
'a) Grant',
'b) no one',
'c) Napoleon',
'd) Churchill',
'e) Mother Teresa',
],
]
],
];
}
}

0 comments on commit 418b2ef

Please sign in to comment.