Test for Simple Scoring is not correct

Created on 8 November 2024, about 1 month ago

Problem/Motivation

The multiple choice and match question types allow the user to setup the question to use "simple scoring" or not.

The code in SimpleScoringResponseTrait.php does not correctly determine the status of the simple scoring flag and always follows the path as if it were NOT set.

The setScore function in the trait is passed the question entity and the code then checks to see if the question entity is an instanceof "SimpleScoringQuestionInterface". The issue is that the question entity never has this trait. The trait is set on the Question response Plugin Type classes (MatchingChoiceResponse, MultipleChoiceResponse) so the test will always fail.

Proposed resolution

The proposed solution is to modify the test in the trait so that the plugin instance is checked for the trait and then checked to see if the flag is checked or not.

    /** @var \Drupal\quiz_maker\Entity\Question $question */
    $question_plugin = $question->getPluginInstance();
    $is_simple_score = $question_plugin instanceof SimpleScoringQuestionInterface ? $question_plugin->isSimpleScore() : FALSE;

In addition the SimpleScoringQuestionTrait.php needs to be changed as below so it checks for the existence and value of the simple scoring field on the question entity and not on the plugin which is what it is doing now.

  public function isSimpleScore(): bool {

    if ($this->entity instanceof Question && $this->entity->hasField('field_simple_scoring')) {
      return (bool) $this->entity->get('field_simple_scoring')->getString();
    }
    return FALSE;
  }
πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom arcaic Milton Keynes

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024