AssertionError: assert(is_numeric($result) && !empty($result)) in assert() (line 744 of modules/contrib/opigno_module/src/Entity/OpignoModule.php)

Created on 17 August 2023, over 1 year ago

Problem/Motivation

I've got a fatal error, when a user just enroll in a training and go to the catalog page.

AssertionError: assert(is_numeric($result) && !empty($result)) in assert() (line 744 of modules/contrib/opigno_module/src/Entity/OpignoModule.php)

This error came from OpignoModule:getGroupIdCurrentTraining($group_id) method.

Steps to reproduce

Create a training. User can enroll to the training.
Login with a simple user.
Enroll the training. Don't start the training.
Go to the catalog page.

The website encountered an unexpected error. Please try again later.
AssertionError: assert(is_numeric($result) && !empty($result)) in assert() (line 744 of modules/contrib/opigno_module/src/Entity/OpignoModule.php).

assert(, 'assert(is_numeric($result) && !empty($result))') (Line: 744)
Drupal\opigno_module\Entity\OpignoModule->getGroupIdCurrentTraining(NULL) (Line: 434)
Drupal\opigno_module\Entity\OpignoModule->getModuleAttempts(Object, NULL, NULL, , NULL) (Line: 2839)
opigno_learning_path_get_module_activities('1', '12', , NULL, NULL) (Line: 2964)
opigno_learning_path_get_step_progress(Array, '12', , NULL, NULL) (Line: 3016)
opigno_learning_path_get_step_status(Array, '12', ) (Line: 3159)

If the user start the training then tere is no more fatal error.

Proposed resolution

Looks like the OpignoModule:getGroupIdCurrentTraining($group_id) method can be called with a group ID NULL so the assert check can cause this fatal error.

In this case, this is because the method

opigno_learning_path_is_passed($step, $uid, $current_attempt = FALSE, $expired = FALSE) call the method

opigno_learning_path_get_step_status($step, $uid, $step_state_counting = FALSE, $latest_cert_date = NULL, $group_id = NULL)

without a group_id with this

$step['status'] = opigno_learning_path_get_step_status($step, $uid, $current_attempt);

The group_id is then NULL.

And this this call this method
$progress = opigno_learning_path_get_step_progress($step, $uid, $step_state_counting, $latest_cert_date, $group_id);
which call
$activities = opigno_learning_path_get_module_activities($step['id'], $uid, $step_state_counting, $latest_cert_date, $group_id);
which call
$attempts = $module->getModuleAttempts($user, NULL, $latest_cert_date, FALSE, $group_id);
which call
$group_id = $this->getGroupIdCurrentTraining($group_id);
and then the assert fails in

public function getGroupIdCurrentTraining($group_id) {
    $context_id = OpignoGroupContext::getCurrentGroupId();
    $group = $group_id ? Group::load($group_id) : NULL;
    if (
      $group instanceof Group &&
      $group->getGroupType()->id() == 'opigno_course' &&
      !empty($context_id) &&
      $group_id != $context_id
    ) {
      // If group is opigno_course, get parent training id.
      $group_id = $context_id;
    }
    $result = $group_id ?: $context_id;
    assert(is_numeric($result) && !empty($result));
    return $result;
  }

Remaining tasks

Looks like the assert here shouldn't be. The group ID and context ID can be NULL.

Remove it ?

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Active

Version

3.1

Component

Code

Created by

πŸ‡«πŸ‡·France flocondetoile Lyon

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

Comments & Activities

  • Issue created by @flocondetoile
  • πŸ‡«πŸ‡·France flocondetoile Lyon

    Let's try this

  • Status changed to Needs review over 1 year ago
  • πŸ‡¨πŸ‡¦Canada joelseguin Ontario, Canada

    I can confirm that on Drupal 9.5.10 the patch worked perfectly for me. I added several users to a training and was getting a WSOD when visiting the catalog page. Patch applied, no errors in the even log and users can now access the page as expected.

  • Status changed to RTBC 23 days ago
  • πŸ‡ΊπŸ‡ΈUnited States scott.allison

    I'm also having the same issue. Removing the assertion obviously removes the AssertionError, but I'd like to know more about why the assertion was added in the first place.

    According to PHP docs, "assertions should be used as a debugging feature only."

    Should a condition be added to return NULL if $result is not numeric or is empty? Or is it safe to remove the assertion completely? It doesn't seem to cause any issues in my testing, so this patch can be RTBC if a maintainer doesn't have any concerns.

Production build 0.71.5 2024