/**
  * Gives access to moodle codebase, ensures all is ready and sets up the test lock.
  *
  * Includes config.php to use moodle codebase with $CFG->behat_*
  * instead of $CFG->prefix and $CFG->dataroot, called once per suite.
  *
  * @static
  * @throws Exception
  * @BeforeSuite
  */
 public static function before_suite($event)
 {
     global $CFG;
     // Defined only when the behat CLI command is running, the moodle init setup process will
     // read this value and switch to $CFG->behat_dataroot and $CFG->behat_prefix instead of
     // the normal site.
     define('BEHAT_TEST', 1);
     define('CLI_SCRIPT', 1);
     // With BEHAT_TEST we will be using $CFG->behat_* instead of $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
     require_once __DIR__ . '/../../../config.php';
     // Now that we are MOODLE_INTERNAL.
     require_once __DIR__ . '/../../behat/classes/behat_command.php';
     require_once __DIR__ . '/../../behat/classes/behat_selectors.php';
     require_once __DIR__ . '/../../behat/classes/behat_context_helper.php';
     require_once __DIR__ . '/../../behat/classes/util.php';
     require_once __DIR__ . '/../../testing/classes/test_lock.php';
     require_once __DIR__ . '/../../testing/classes/nasty_strings.php';
     // Avoids vendor/bin/behat to be executed directly without test environment enabled
     // to prevent undesired db & dataroot modifications, this is also checked
     // before each scenario (accidental user deletes) in the BeforeScenario hook.
     if (!behat_util::is_test_mode_enabled()) {
         throw new Exception('Behat only can run if test mode is enabled. More info in ' . behat_command::DOCS_URL . '#Running_tests');
     }
     if (!behat_util::is_server_running()) {
         throw new Exception($CFG->behat_wwwroot . ' is not available, ensure you specified correct url and that the server is set up and started.' . ' More info in ' . behat_command::DOCS_URL . '#Running_tests');
     }
     // Prevents using outdated data, upgrade script would start and tests would fail.
     if (!behat_util::is_test_data_updated()) {
         $commandpath = 'php admin/tool/behat/cli/init.php';
         throw new Exception("Your behat test site is outdated, please run\n\n    " . $commandpath . "\n\nfrom your moodle dirroot to drop and install the behat test site again.");
     }
     // Avoid parallel tests execution, it continues when the previous lock is released.
     test_lock::acquire('behat');
     // Store the browser reset time if reset after N seconds is specified in config.php.
     if (!empty($CFG->behat_restart_browser_after)) {
         // Store the initial browser session opening.
         self::$lastbrowsersessionstart = time();
     }
     if (!empty($CFG->behat_faildump_path) && !is_writable($CFG->behat_faildump_path)) {
         throw new Exception('You set $CFG->behat_faildump_path to a non-writable directory');
     }
 }
Esempio n. 2
0
    /**
     * Gives access to moodle codebase, ensures all is ready and sets up the test lock.
     *
     * Includes config.php to use moodle codebase with $CFG->behat_*
     * instead of $CFG->prefix and $CFG->dataroot, called once per suite.
     *
     * @static
     * @throws Exception
     * @BeforeSuite
     */
    public static function before_suite($event) {
        global $CFG;

        // Defined only when the behat CLI command is running, the moodle init setup process will
        // read this value and switch to $CFG->behat_dataroot and $CFG->behat_prefix instead of
        // the normal site.
        define('BEHAT_TEST', 1);

        define('CLI_SCRIPT', 1);

        // With BEHAT_TEST we will be using $CFG->behat_* instead of $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
        require_once(__DIR__ . '/../../../config.php');

        // Now that we are MOODLE_INTERNAL.
        require_once(__DIR__ . '/../../behat/classes/behat_command.php');
        require_once(__DIR__ . '/../../behat/classes/util.php');
        require_once(__DIR__ . '/../../testing/classes/test_lock.php');
        require_once(__DIR__ . '/../../testing/classes/nasty_strings.php');

        // Avoids vendor/bin/behat to be executed directly without test environment enabled
        // to prevent undesired db & dataroot modifications, this is also checked
        // before each scenario (accidental user deletes) in the BeforeScenario hook.

        if (!behat_util::is_test_mode_enabled()) {
            throw new Exception('Behat only can run if test mode is enabled. More info in ' . behat_command::DOCS_URL . '#Running_tests');
        }

        if (!behat_util::is_server_running()) {
            throw new Exception($CFG->behat_wwwroot . ' is not available, ensure you started your PHP built-in server. More info in ' . behat_command::DOCS_URL . '#Running_tests');
        }

        // Prevents using outdated data, upgrade script would start and tests would fail.
        if (!behat_util::is_test_data_updated()) {
            $commandpath = 'php admin/tool/behat/cli/init.php';
            throw new Exception('Your behat test site is outdated, please run ' . $commandpath . ' from your moodle dirroot to drop and install the behat test site again.');
        }
        // Avoid parallel tests execution, it continues when the previous lock is released.
        test_lock::acquire('behat');
    }