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

PHPUnit fails for external services requiring externallib.php

    XMLWordPrintable

Details

    • MOODLE_402_STABLE
    • MOODLE_402_STABLE
    • MDL-78049-MOODLE_402_STABLE
    • MDL-78049-master
    • Hide
      1. Use a plugin trying to maintain only one branch, having some webservices. The webservice classes are requiring externallib.php, because they extend the old \external_api class (which is now aliased in externallib.php which is why this needs to be included). For example use https://github.com/mebis-lp/mod_learningmap/commit/6f74502390d10e3f62b7901b4183182c78a11893:

        git clone --branch v0.9.3  https://github.com/mebis-lp/mod_learningmap mod/learningmap
        

      2. Initialize PHPUnit and run PHPUnit filtering after the plugin, in our example --filter=mod_learningmap:

        mdk phpunit -r --filter=mod_learningmap

      3. Confirm that PHPUnit is passing without any errors
      Show
      Use a plugin trying to maintain only one branch, having some webservices. The webservice classes are requiring externallib.php, because they extend the old \external_api class (which is now aliased in externallib.php which is why this needs to be included). For example use https://github.com/mebis-lp/mod_learningmap/commit/6f74502390d10e3f62b7901b4183182c78a11893: git clone --branch v0.9.3  https://github.com/mebis-lp/mod_learningmap mod/learningmap Initialize PHPUnit and run PHPUnit filtering after the plugin, in our example --filter=mod_learningmap: mdk phpunit -r --filter=mod_learningmap Confirm that PHPUnit is passing without any errors

    Description

      This issue has been requested by dobedobedoh after I posted a question about that problem in the developer matrix channel.

      With 4.2 the external api has been moved to new classes.

      In externallib.php aliases have been created to allow plugin maintainers to keep backwards compatibility and not forcing plugin developers to create a MOODLE_402_STABLE only branch.

      For the aliases to work one has to include require_once($CFG->libdir . '/externallib.php'); in the webservice class plugintype/myplugin/classes/external/get_some_info.php.

      However, that makes unit tests fail for this plugin. Important: It's about core unit tests which are failing, not unit tests in this plugin. This issue also appears, if the plugin itself does not provide any external api related unit tests.

      1) core_external\external_api_test::test_all_external_info with data set "plugintype_myplugin_get_some_info" (stdClass Object (...))
      coding_exception: Coding error detected, it must be fixed by a programmer: When including this file for a unit test, the test must be run in an isolated process. See the PHPUnit @runInSeparateProcess and @runTestsInSeparateProcesses annotations.
       
      /var/www/html/lib/setuplib.php:2204
      /var/www/html/lib/externallib.php:35
      /var/www/html/plugintype/myplpugin/classes/external/get_some_info.php:30 // this is the require_once line
      /var/www/html/lib/classes/component.php:144
      /var/www/html/lib/external/classes/external_api.php:61
      /var/www/html/lib/external/tests/external_api_test.php:305
      /var/www/html/lib/phpunit/classes/advanced_testcase.php:94
      

      A workaround for sure would be to make a separate branch and not rely on the aliases, so it's ok to remove the require statement for externallib. But this means that a plugin maintainer will be forced to use a separate branch for moodle 4.2 and above.

      Attachments

        Issue Links

          Activity

            People

              phmemmel PhMemmel
              phmemmel PhMemmel
              Marina Glancy Marina Glancy
              Sara Arjona (@sarjona) Sara Arjona (@sarjona)
              Kim Jared Lucas Kim Jared Lucas
              Votes:
              0 Vote for this issue
              Watchers:
              9 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                12/Jun/23

                Time Tracking

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