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

Behat: Allow CLI scripts to run on Behat instance

XMLWordPrintable

    • MOODLE_401_STABLE, MOODLE_404_STABLE, MOODLE_405_STABLE, MOODLE_500_STABLE
    • MOODLE_401_STABLE, MOODLE_404_STABLE, MOODLE_405_STABLE, MOODLE_500_STABLE
    • MDL-84793-m401
    • MDL-84793-m404
    • MDL-84793-m405
    • MDL-84793-m500
    • MDL-84793-main
    • Hide

      Prerequisite: You must have a working Behat setup and be able to edit files within the Moodle codebase. Also, you must be using a normal command shell (e.g. bash) that lets you set environment variables using the syntax shown below in step 8.

      1. If necessary, run Behat install - php admin/tool/behat/cli/init.php - and wait for it to complete.
      2. Visit the home page of your Behat installation - the URL is shown at the end of the Behat init output.
      3. Edit the CSS file mod/assign/styles.css (this was chosen at random, it can actually be any Moodle plugin CSS file).
      4. At the end of the file, add the section of CSS code shown below, and save the file.
      5. Hold down Shift and click Reload in your browser. Do this twice, waiting for each one to fully complete. (I don't know why it is needed twice but it seems to be when using Boost theme.)
        • EXPECTED (no change): The heading has not turned pink because the theme cache has not been cleared.
      6. Run this CLI command: php admin/cli/purge_caches.php --theme
        • EXPECTED (no change): The script does not output the 'BEHAT_CLI' line shown below in step 8.
      7. Hold down Shift and click Reload in your browser, twice.
        • EXPECTED (no change): The heading has not turned pink because the theme cache has been cleared for your normal Moodle instance but not the Behat one.
      8. Run this CLI command: BEHAT_CLI=1 php admin/cli/purge_caches.php --theme
        • EXPECTED: The script should output a line of text BEHAT_CLI: This command line script is running on the acceptance testing site.
      9. Hold down Shift and click Reload in your browser, twice.
        • EXPECTED: The heading 'Acceptance test site' should now be pink because the theme cache has been cleared for your Behat instance.
      10. On the command line, run the CLI commands where this feature has been dsiabled (list below).
        • EXPECTED: None of them should display the 'BEHAT_CLI' text. This confirms the behaviour is not activated for these specific scripts and they will work the same as always.

      Example CSS (step 4)

      h1 {
          color: pink !important;
      }
      

      Commands where this feature has been disabled (step 10)

      BEHAT_CLI=1 php admin/tool/phpunit/cli/init.php --help
      BEHAT_CLI=1 php admin/tool/phpunit/cli/util.php --help
      BEHAT_CLI=1 php admin/tool/behat/cli/init.php --help
      BEHAT_CLI=1 php admin/tool/behat/cli/util.php --help
      BEHAT_CLI=1 php admin/tool/behat/cli/util_single_run.php --help
      BEHAT_CLI=1 php admin/tool/behat/cli/run.php --help
      

      Note: We are using the '--help' parameter just to make the test easier because the command then does nothing; we only need to verify it doesn't show the warning text.
       

      Show
      Prerequisite: You must have a working Behat setup and be able to edit files within the Moodle codebase. Also, you must be using a normal command shell (e.g. bash) that lets you set environment variables using the syntax shown below in step 8. If necessary, run Behat install - php admin/tool/behat/cli/init.php - and wait for it to complete. Visit the home page of your Behat installation - the URL is shown at the end of the Behat init output. Edit the CSS file mod/assign/styles.css (this was chosen at random, it can actually be any Moodle plugin CSS file). At the end of the file, add the section of CSS code shown below, and save the file. Hold down Shift and click Reload in your browser. Do this twice, waiting for each one to fully complete. (I don't know why it is needed twice but it seems to be when using Boost theme.) EXPECTED (no change): The heading has not turned pink because the theme cache has not been cleared. Run this CLI command: php admin/cli/purge_caches.php --theme EXPECTED (no change): The script does not output the 'BEHAT_CLI' line shown below in step 8. Hold down Shift and click Reload in your browser, twice. EXPECTED (no change): The heading has not turned pink because the theme cache has been cleared for your normal Moodle instance but not the Behat one. Run this CLI command: BEHAT_CLI=1 php admin/cli/purge_caches.php --theme EXPECTED: The script should output a line of text BEHAT_CLI: This command line script is running on the acceptance testing site. Hold down Shift and click Reload in your browser, twice. EXPECTED: The heading 'Acceptance test site' should now be pink because the theme cache has been cleared for your Behat instance. On the command line, run the CLI commands where this feature has been dsiabled (list below). EXPECTED: None of them should display the 'BEHAT_CLI' text. This confirms the behaviour is not activated for these specific scripts and they will work the same as always. Example CSS (step 4) h 1 { color : pink !important ; } Commands where this feature has been disabled (step 10) BEHAT_CLI=1 php admin/tool/phpunit/cli/init.php --help BEHAT_CLI=1 php admin/tool/phpunit/cli/util.php --help BEHAT_CLI=1 php admin/tool/behat/cli/init.php --help BEHAT_CLI=1 php admin/tool/behat/cli/util.php --help BEHAT_CLI=1 php admin/tool/behat/cli/util_single_run.php --help BEHAT_CLI=1 php admin/tool/behat/cli/run.php --help Note: We are using the '--help' parameter just to make the test easier because the command then does nothing; we only need to verify it doesn't show the warning text.  
    • Hide

      Code verified against automated checks.

      Checked MDL-84793 using repository: https://github.com/sammarshallou/moodle.git

      More information about this report

      Built on: Mon Apr 28 10:25:28 UTC 2025

      Show
      Code verified against automated checks. Checked MDL-84793 using repository: https://github.com/sammarshallou/moodle.git MOODLE_405_STABLE (0 errors / 0 warnings) [branch: MDL-84793-m405 | CI Job ] MOODLE_500_STABLE (0 errors / 0 warnings) [branch: MDL-84793-m500 | CI Job ] main (0 errors / 0 warnings) [branch: MDL-84793-main | CI Job ] More information about this report Built on: Mon Apr 28 10:25:28 UTC 2025
    • Show
      Launching automatic jobs for branch MDL-84793 -m404 https://ci.moodle.org/view/Testing/job/DEV.02%20-%20Developer-requested%20PHPUnit/18641/ PHPUnit (sqlsrv) https://ci.moodle.org/view/Testing/job/DEV.01%20-%20Developer-requested%20Behat/63861/ Behat (NonJS - boost and classic) https://ci.moodle.org/view/Testing/job/DEV.01%20-%20Developer-requested%20Behat/63862/ Behat (Firefox - boost) Launching automatic jobs for branch MDL-84793 -m405 https://ci.moodle.org/view/Testing/job/DEV.02%20-%20Developer-requested%20PHPUnit/18642/ PHPUnit (sqlsrv) https://ci.moodle.org/view/Testing/job/DEV.01%20-%20Developer-requested%20Behat/63863/ Behat (NonJS - boost and classic) https://ci.moodle.org/view/Testing/job/DEV.01%20-%20Developer-requested%20Behat/63864/ Behat (Firefox - boost) Launching automatic jobs for branch MDL-84793 -main https://ci.moodle.org/view/Testing/job/DEV.02%20-%20Developer-requested%20PHPUnit/18643/ PHPUnit (sqlsrv) https://ci.moodle.org/view/Testing/job/DEV.01%20-%20Developer-requested%20Behat/63865/ Behat (NonJS - boost and classic) https://ci.moodle.org/view/Testing/job/DEV.01%20-%20Developer-requested%20Behat/63866/ Behat (Firefox - boost) https://ci.moodle.org/view/Testing/job/DEV.01%20-%20Developer-requested%20Behat/63867/ Behat (Firefox - classic) https://ci.moodle.org/view/Testing/job/DEV.01%20-%20Developer-requested%20Behat/63868/ App tests (stable app version) Built on: Tue Mar 18 10:26:02 UTC 2025

      While developing complex features that have a lot of setup to get to them, it is sometimes useful to be able to set up the server with a Behat test (either an existing test, or a half-written one that will eventually become a test for your feature). Then you can develop using your web browser pointing at the Behat instance.

      I do this fairly often. In this case I'm slightly abusing the Behat installation (wwwroot/prefix/dataroot) as just a second Moodle installation, with Behat used just for the original setup.

      One drawback is that you cannot use CLI commands. For example, while making theme changes it's useful to be able to run:

      php admin/cli/purge_caches.php --theme
      

      Similar for JS, for example:

      grunt amd --root=local/mymodule/amd && php admin/cli/purge_caches.php --js
      

      These commands will work on the 'real' Moodle instance, not the Behat one, so I have to use the GUI instead.

      This enhancement allows an environment variable to be used to indicate that the CLI command should run on the Behat instance (equivalent to how the web requests do this if the wwwroot matches the Behat wwwroot).

      BEHAT_CLI=1 php admin/cli/purge_caches.php --theme
      

      grunt amd --root=local/mymodule/amd && BEHAT_CLI=1 php admin/cli/purge_caches.php --js
      

        1. MDL-84793.png
          297 kB
          Ron Carl Alfon Yu

            quen Sam Marshall
            quen Sam Marshall
            Neill Magill Neill Magill
            Simey Lameze Simey Lameze
            Ron Carl Alfon Yu Ron Carl Alfon Yu
            Votes:
            2 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - 0 minutes
                0m
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 2 hours, 58 minutes
                2h 58m

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