Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-82960

get_config('backup') throws php error if called immediately after course deletion

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Minor Minor
    • None
    • 4.3.6, 4.3.7, 4.3.8, 4.4.2, 4.4.3, 4.4.4
    • Administration, Backup, Course
    • None
    • MOODLE_403_STABLE, MOODLE_404_STABLE

      The following unit test fails, showing the problem clearly:

      /**
       * Unit tests for the bug.
       *
       */
      final class bug_test extends \advanced_testcase {
          /**
           * Setup.
           */
          public function setUp(): void {
              $this->resetAfterTest();
          }
       
          /**
           * Test bug reproduction.
           * @return void
           */
          public function test_bug_reproduction(): void {
              $course = $this->getDataGenerator()->create_course();
              delete_course($course->id, false);
              $backupsettings = get_config('backup');
              $this->assertInstanceOf(\stdClass::class, $backupsettings);
          }
      }
      

      The test above is broken with the following output:

      foreach() argument must be of type array|object, null given
      /www/src/moodle/lib/moodlelib.php:1069
      /www/src/moodle/admin/tool/playground/tests/bug_test.php:48
      /www/src/moodle/lib/phpunit/classes/advanced_testcase.php:72

      since v4.3.6 and 4.4.2 there has been a modification in

      • admin/tool/recyclebin/classes/category_bin.php
      • admin/tool/recyclebin/classes/course_bin.php

      via MDL-81119 tool_recyclebin: Stop overriding unrelated forced config

      as follows:

      $forcedbackupsettings = $CFG->forced_plugin_settings['backup'] ?? null;
      $CFG->forced_plugin_settings['backup']['backup_auto_storage'] = 0;
      $CFG->forced_plugin_settings['backup']['backup_auto_files'] = 1;
      ...
      ...
      $CFG->forced_plugin_settings['backup'] = $forcedbackupsettings;
      

      See:
      https://github.com/moodle/moodle/blob/cc2edf3b8ccaa116e19ea2bb5daec7ed8adb3acd/admin/tool/recyclebin/classes/category_bin.php#L138

      https://github.com/moodle/moodle/commit/d5b719f6a0f5e3504ccf797ab17e9ae6001ec9f9

      benjaminwalker
      as soon as a course is deleted it is per default backuped in the category via category_bin.php.
      in case $CFG->forced_plugin_settings['backup'] not set, it is set to null as follows:

      $CFG->forced_plugin_settings['backup'] = $forcedbackupsettings; // Which is null.
      

      get_config('backup') expects $CFG->forced_plugin_settings['backup'] to be an array though. If you call it immediately after course deletion you get the php error mentioned above.

      I suggest to set the $CFG->forced_plugin_settings back to initial value as follows to fix the bug:

      if (!is_null($forcedbackupsettings)) {
          $CFG->forced_plugin_settings['backup'] = $forcedbackupsettings;
      } else {
          unset($CFG->forced_plugin_settings['backup']);
      }
      

            Unassigned Unassigned
            Taraghi Behnam
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.