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

Do not use empty static modnames

XMLWordPrintable

    • MOODLE_401_STABLE, MOODLE_402_STABLE, MOODLE_403_STABLE
    • MOODLE_402_STABLE, MOODLE_403_STABLE
    • 401_check_cached_empty_modnames
    • 403_check_cached_empty_modnames
    • master_check_cached_empty_modnames
    • Hide
      1. Install a fresh Moodle instance.
      2. Open a terminal and go to your Moodle instance's root folder.
      3. Install the report_editdates plugin from repo

        git clone https://github.com/opitz/moodle-report_editdates report/editdates
        cd report/editdates
        git checkout enabling_activities
        

      4. Run a PHPUnit test with lib/tests/navigation/views/secondary_test.php. e.g. using MDK:

        mdk phpunit -r -u lib/tests/navigation/views/secondary_test.php
        

      5. Confirm that the test is passing and that the following error is not shown:

        Undefined property: stdClass::$activitytype

      Show
      Install a fresh Moodle instance. Open a terminal and go to your Moodle instance's root folder. Install the report_editdates plugin from repo git clone https: //github .com /opitz/moodle-report_editdates report /editdates cd report /editdates git checkout enabling_activities Run a PHPUnit test with lib/tests/navigation/views/secondary_test.php. e.g. using MDK: mdk phpunit -r -u lib /tests/navigation/views/secondary_test .php Confirm that the test is passing and that the following error is not shown: Undefined property: stdClass::$activitytype

      When calling get_module_types_names() in course/lib.php before the modules have been loaded the result is an array of empty arrays which then is stored in a static $modnames.

      When after the modules have been installed the method is called again it then will return the cached values which will break things.

       

      To replicate the issue:

      Install a MOODLE_403_STABLE instance.

      Add the report_editdates plugin from here https://github.com/opitz/moodle-report_editdates/tree/enabling_activities

      Run a PHPUnit test with lib/tests/navigation/views/secondary_test.php

       

      You will get an “Undefined property: stdClass::$activitytype” error.

      This is because get_module_types_names() is 1st called during the installation of the plugin before any modules are installed. This produces empty arrays are stored in a static variable.

      The 2nd call during the finalising process of the installation - after all modules have been installed - will return the static variable instead of the now available values.

      To resolve this I suggest to check not only for a null value but also for an empty array before using cached values.

      Line 402 of course/lib.php should change from

      “if ($modnames === null || $resetcache) {“

      to

      if ($modnames === null || empty($modnames[0]) || $resetcache) {”

       

       

       

            opitz Matthias Opitz
            opitz Matthias Opitz
            dragos5436 dragos5436
            Jun Pataleta Jun Pataleta
            Ron Carl Alfon Yu Ron Carl Alfon Yu
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 1 hour, 35 minutes
                1h 35m

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