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

Core test_course_get_enrolled_courses_for_logged_in_user unit test sometimes requires fewer queries than expected

XMLWordPrintable

    • MOODLE_311_STABLE, MOODLE_400_STABLE
    • MOODLE_310_STABLE, MOODLE_311_STABLE
    • master-MDL-72239-v2
    • Hide

      Before the patch

      1. Confirm that the following unit tests fail:

        vendor/bin/phpunit --testsuite='core_course_testsuite' --filter='test_course_get_enrolled_courses_for_logged_in_user'

      After the patch

      1. Confirm that the same unit tests don't fail:

        vendor/bin/phpunit --testsuite='core_course_testsuite' --filter='test_course_get_enrolled_courses_for_logged_in_user'

      Show
      Before the patch Confirm that the following unit tests fail: vendor/bin/phpunit --testsuite= 'core_course_testsuite' --filter= 'test_course_get_enrolled_courses_for_logged_in_user' After the patch Confirm that the same unit tests don't fail: vendor/bin/phpunit --testsuite= 'core_course_testsuite' --filter= 'test_course_get_enrolled_courses_for_logged_in_user'

      Hi,

      I have a failing core test:

      root@013d65e07b28:/siteroot# vendor/bin/phpunit --testsuite='core_course_testsuite' --filter='test_course_get_enrolled_courses_for_logged_in_user'
      Moodle 3.11.1 (Build: 20210712), 0e295ea322b7fe8d37c364e9c26128fcf316d718
      Php: 7.4.3, pgsql: 10.15 (Debian 10.15-1.pgdg90+1), OS: Linux 5.4.0-77-generic x86_64
      PHPUnit 9.5.4 by Sebastian Bergmann and contributors.
       
      .FFFFFFFF                                                           9 / 9 (100%)
       
      Time: 00:05.884, Memory: 92.50 MB
       
      There were 8 failures:
       
      1) core_course_courselib_testcase::test_course_get_enrolled_courses_for_logged_in_user with data set "less than query limit" (3, 2, 0, 0, 2, array('testcourse0', 'testcourse1'))
      Failed asserting that 1 matches expected 2.
       
      /siteroot/course/tests/courselib_test.php:4679
      /siteroot/lib/phpunit/classes/advanced_testcase.php:80
       
      2) core_course_courselib_testcase::test_course_get_enrolled_courses_for_logged_in_user with data set "more than query limit" (3, 7, 0, 0, 4, array('testcourse0', 'testcourse1', 'testcourse2', 'testcourse3', 'testcourse4', 'testcourse5', 'testcourse6'))
      Failed asserting that 3 matches expected 4.
       
      /siteroot/course/tests/courselib_test.php:4679
      /siteroot/lib/phpunit/classes/advanced_testcase.php:80
       
      3) core_course_courselib_testcase::test_course_get_enrolled_courses_for_logged_in_user with data set "limit less than query limit" (3, 7, 2, 0, 2, array('testcourse0', 'testcourse1'))
      Failed asserting that 1 matches expected 2.
       
      /siteroot/course/tests/courselib_test.php:4679
      /siteroot/lib/phpunit/classes/advanced_testcase.php:80
       
      4) core_course_courselib_testcase::test_course_get_enrolled_courses_for_logged_in_user with data set "limit less than query limit with offset" (3, 7, 2, 2, 2, array('testcourse2', 'testcourse3'))
      Failed asserting that 1 matches expected 2.
       
      /siteroot/course/tests/courselib_test.php:4679
      /siteroot/lib/phpunit/classes/advanced_testcase.php:80
       
      5) core_course_courselib_testcase::test_course_get_enrolled_courses_for_logged_in_user with data set "limit less than total" (3, 9, 6, 0, 3, array('testcourse0', 'testcourse1', 'testcourse2', 'testcourse3', 'testcourse4', 'testcourse5'))
      Failed asserting that 2 matches expected 3.
       
      /siteroot/course/tests/courselib_test.php:4679
      /siteroot/lib/phpunit/classes/advanced_testcase.php:80
       
      6) core_course_courselib_testcase::test_course_get_enrolled_courses_for_logged_in_user with data set "less results than limit" (4, 9, 20, 0, 4, array('testcourse0', 'testcourse1', 'testcourse2', 'testcourse3', 'testcourse4', 'testcourse5', 'testcourse6', 'testcourse7', 'testcourse8'))
      Failed asserting that 3 matches expected 4.
       
      /siteroot/course/tests/courselib_test.php:4679
      /siteroot/lib/phpunit/classes/advanced_testcase.php:80
       
      7) core_course_courselib_testcase::test_course_get_enrolled_courses_for_logged_in_user with data set "less results than limit exact divisible" (3, 9, 20, 0, 5, array('testcourse0', 'testcourse1', 'testcourse2', 'testcourse3', 'testcourse4', 'testcourse5', 'testcourse6', 'testcourse7', 'testcourse8'))
      Failed asserting that 4 matches expected 5.
       
      /siteroot/course/tests/courselib_test.php:4679
      /siteroot/lib/phpunit/classes/advanced_testcase.php:80
       
      8) core_course_courselib_testcase::test_course_get_enrolled_courses_for_logged_in_user with data set "less results than limit with offset" (3, 9, 10, 5, 3, array('testcourse5', 'testcourse6', 'testcourse7', 'testcourse8'))
      Failed asserting that 2 matches expected 3.
       
      /siteroot/course/tests/courselib_test.php:4679
      /siteroot/lib/phpunit/classes/advanced_testcase.php:80
       
      FAILURES!
      Tests: 9, Assertions: 18, Failures: 8.
      
      

      It fails on asserting that $expecteddbqueries is equal to $DB->perf_get_queries() - $initialquerycount). In fact there are less db queries than expected.

      I turned out that a plugin that I have installed has an observer, that listens to user_enrolment_created events and then calls get_complete_user_data() which does a query to mdl_user_lastaccess table, so its columns get cached. Thus, when the test does moodle_database->get_columns() it gets columns for mdl_user_lastaccess from the cache. As a result, the test does less db calls than expected.

       

      Steps to triage the issue:

      1. Install a helper plaugin https://github.com/golenkovm/moodle-local_mdl_helper
      2. Checkout to MDL-72239 branch
      3. Run unit tests: 

        vendor/bin/phpunit --testsuite='core_course_testsuite' --filter='test_course_get_enrolled_courses_for_logged_in_user'
        

            mikhailgolenkov Misha Golenkov
            mikhailgolenkov Misha Golenkov
            Heena Agheda Heena Agheda
            Victor Déniz Falcón Victor Déniz Falcón
            Gladys Basiana Gladys Basiana
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:

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

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