<commit_before><?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Behat commands
 *
 * @package    tool_behat
 * @copyright  2012 <NAME>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once($CFG->libdir . '/filestorage/file_exceptions.php');
require_once($CFG->libdir . '/phpunit/bootstraplib.php');
require_once($CFG->libdir . '/phpunit/classes/tests_finder.php');

/**
 * Behat commands manager
 *
 * CLI + web execution
 *
 * @package    tool_behat
 * @copyright  2012 <NAME>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class tool_behat {

    private static $behat_tests_path = '/tests/behat';

    /**
     * Displays basic info about acceptance tests
     */
    public static function info() {

        $html = self::get_header();
        $html .= self::get_info();

        if (!self::is_test_environment_running()) {
            $html .= self::get_steps_definitions_form();
        }

        $html .= self::get_footer();

        echo $html;
    }

    /**
     * Lists the available steps definitions
     * @param string $filter Keyword to filter the list of steps definitions availables
     */
    public static function stepsdefinitions($filter = false) {
        global $CFG;

        self::check_behat_setup();

        self::update_config_file();

        // Priority to the one specified as argument.
        if (!$filter) {
            $filter = optional_param('filter', false, PARAM_ALPHANUMEXT);
        }

        if ($filter) {
            $filteroption = ' -d ' . $filter;
        } else {
            $filteroption = ' -di';
        }

        $color = '';
        if (CLI_SCRIPT) {
            $color = '--ansi ';
        }

        $currentcwd = getcwd();
        chdir($CFG->behatpath);
        exec('bin/behat ' . $color . ' --config="' . self::get_behat_config_filepath() . '" ' . $filteroption, $steps, $code);
        chdir($currentcwd);

        // Outputing steps.

        $content = '';
        if ($steps) {
            foreach ($steps as $line) {

                // Skipping the step definition context.
                if (strpos($line, '#') == 0) {
                    if (CLI_SCRIPT) {
                        $content .= $line . PHP_EOL;
                    } else {
                        $content .= htmlentities($line) . '<br/>';
                    }

                }
            }
        }

        if ($content === '') {
            $content = get_string('nostepsdefinitions', 'tool_behat');
        }

        if (!CLI_SCRIPT) {
            $html = self::get_header();
            $html .= self::get_steps_definitions_form($filter);
            $html .= html_writer::tag('div', $content, array('id' => 'steps-definitions'));
            $html .= self::get_footer();
            echo $html;
        } else {
            echo $content;
        }
    }

    /**
     * Switches from and to the regular environment to the testing environment
     * @param string $testenvironment enable|disable
     */
    public static function switchenvironment($testenvironment = false) {
        global $CFG;

        // Priority to the one specified as argument.
        if (!$testenvironment) {
            $testenvironment = optional_param('testenvironment', 'enable', PARAM_ALPHA);
        }

        if ($testenvironment == 'enable') {
            self::enable_test_environment();
        } else if ($testenvironment == 'disable') {
            self::disable_test_environment();
        }
    }

    /**
     * Runs the acceptance tests
     * @param string $tags Restricts the executed tests to the ones that matches the tags
     * @param string $extra Extra CLI behat options
     */
    public static function runtests($tags = false, $extra = false) {
        global $CFG;

        self::check_behat_setup();

        self::update_config_file();

        @set_time_limit(0);

        // Priority to the one specified as argument.
        if (!$tags) {
            $tags = optional_param('tags', false, PARAM_ALPHANUMEXT);
        }

        $tagsoption = '';
        if ($tags) {
            $tagsoption = ' --tags ' . $tags;
        }

        if (!$extra) {
            $extra = '';
        }

        // Switching database and dataroot to test environment.
        self::enable_test_environment();
        $currentcwd = getcwd();

        chdir($CFG->behatpath);
        ob_start();
        passthru('bin/behat --ansi --config="' . self::get_behat_config_filepath() .'" ' . $tagsoption . ' ' .$extra, $code);
        $output = ob_get_contents();
        ob_end_clean();

        // Switching back to regular environment.
        self::disable_test_environment();
        chdir($currentcwd);

        // Output.
        echo self::get_header();
        echo $output;

        // Dirty hack to avoid behat bad HTML structure when test execution throws an exception and there are skipped steps.
        if (strstr($output, 'class="skipped"') != false) {
            echo '</ol></div></div></div></body></html>';
        }

        echo self::get_footer();
    }

    /**
     * Updates the config file
     * @throws file_exception
     */
    private static function update_config_file() {
        global $CFG;

        $behatpath = rtrim($CFG->behatpath, '/');

        // Basic behat dependencies.
        $contents = 'default:
  paths:
    features: ' . $behatpath . '/features
    bootstrap: ' . $behatpath . '/features/bootstrap
  extensions:
    Behat\MinkExtension\Extension:
      base_url: ' . $CFG->wwwroot . '
      goutte: ~
      selenium2: ~
    Sanpi\Behatch\Extension:
      contexts:
        browser: ~
        system: ~
        json: ~
        table: ~
        rest: ~
    ' . $behatpath . '/vendor/moodlehq/behat-extension/init.php:
';

        // Gets all the components with features.
        $components = tests_finder::get_components_with_tests('features');
        if ($components) {
            $featurespaths = array('');
            foreach ($components as $componentname => $path) {
                $path = self::clean_path($path) . self::$behat_tests_path;
                if (empty($featurespaths[$path]) && file_exists($path)) {
                    $featurespaths[$path] = $path;<commit_msg>MDL-37046 behat: Removing temporarly the rest context of Behatch until upstream fixes the problem<commit_after><?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Behat commands
 *
 * @package    tool_behat
 * @copyright  2012 <NAME>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once($CFG->libdir . '/filestorage/file_exceptions.php');
require_once($CFG->libdir . '/phpunit/bootstraplib.php');
require_once($CFG->libdir . '/phpunit/classes/tests_finder.php');

/**
 * Behat commands manager
 *
 * CLI + web execution
 *
 * @package    tool_behat
 * @copyright  2012 <NAME>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class tool_behat {

    private static $behat_tests_path = '/tests/behat';

    /**
     * Displays basic info about acceptance tests
     */
    public static function info() {

        $html = self::get_header();
        $html .= self::get_info();

        if (!self::is_test_environment_running()) {
            $html .= self::get_steps_definitions_form();
        }

        $html .= self::get_footer();

        echo $html;
    }

    /**
     * Lists the available steps definitions
     * @param string $filter Keyword to filter the list of steps definitions availables
     */
    public static function stepsdefinitions($filter = false) {
        global $CFG;

        self::check_behat_setup();

        self::update_config_file();

        // Priority to the one specified as argument.
        if (!$filter) {
            $filter = optional_param('filter', false, PARAM_ALPHANUMEXT);
        }

        if ($filter) {
            $filteroption = ' -d ' . $filter;
        } else {
            $filteroption = ' -di';
        }

        $color = '';
        if (CLI_SCRIPT) {
            $color = '--ansi ';
        }

        $currentcwd = getcwd();
        chdir($CFG->behatpath);
        exec('bin/behat ' . $color . ' --config="' . self::get_behat_config_filepath() . '" ' . $filteroption, $steps, $code);
        chdir($currentcwd);

        // Outputing steps.

        $content = '';
        if ($steps) {
            foreach ($steps as $line) {

                // Skipping the step definition context.
                if (strpos($line, '#') == 0) {
                    if (CLI_SCRIPT) {
                        $content .= $line . PHP_EOL;
                    } else {
                        $content .= htmlentities($line) . '<br/>';
                    }

                }
            }
        }

        if ($content === '') {
            $content = get_string('nostepsdefinitions', 'tool_behat');
        }

        if (!CLI_SCRIPT) {
            $html = self::get_header();
            $html .= self::get_steps_definitions_form($filter);
            $html .= html_writer::tag('div', $content, array('id' => 'steps-definitions'));
            $html .= self::get_footer();
            echo $html;
        } else {
            echo $content;
        }
    }

    /**
     * Switches from and to the regular environment to the testing environment
     * @param string $testenvironment enable|disable
     */
    public static function switchenvironment($testenvironment = false) {
        global $CFG;

        // Priority to the one specified as argument.
        if (!$testenvironment) {
            $testenvironment = optional_param('testenvironment', 'enable', PARAM_ALPHA);
        }

        if ($testenvironment == 'enable') {
            self::enable_test_environment();
        } else if ($testenvironment == 'disable') {
            self::disable_test_environment();
        }
    }

    /**
     * Runs the acceptance tests
     * @param string $tags Restricts the executed tests to the ones that matches the tags
     * @param string $extra Extra CLI behat options
     */
    public static function runtests($tags = false, $extra = false) {
        global $CFG;

        self::check_behat_setup();

        self::update_config_file();

        @set_time_limit(0);

        // Priority to the one specified as argument.
        if (!$tags) {
            $tags = optional_param('tags', false, PARAM_ALPHANUMEXT);
        }

        $tagsoption = '';
        if ($tags) {
            $tagsoption = ' --tags ' . $tags;
        }

        if (!$extra) {
            $extra = '';
        }

        // Switching database and dataroot to test environment.
        self::enable_test_environment();
        $currentcwd = getcwd();

        chdir($CFG->behatpath);
        ob_start();
        passthru('bin/behat --ansi --config="' . self::get_behat_config_filepath() .'" ' . $tagsoption . ' ' .$extra, $code);
        $output = ob_get_contents();
        ob_end_clean();

        // Switching back to regular environment.
        self::disable_test_environment();
        chdir($currentcwd);

        // Output.
        echo self::get_header();
        echo $output;

        // Dirty hack to avoid behat bad HTML structure when test execution throws an exception and there are skipped steps.
        if (strstr($output, 'class="skipped"') != false) {
            echo '</ol></div></div></div></body></html>';
        }

        echo self::get_footer();
    }

    /**
     * Updates the config file
     * @throws file_exception
     */
    private static function update_config_file() {
        global $CFG;

        $behatpath = rtrim($CFG->behatpath, '/');

        // Basic behat dependencies.
        $contents = 'default:
  paths:
    features: ' . $behatpath . '/features
    bootstrap: ' . $behatpath . '/features/bootstrap
  extensions:
    Behat\MinkExtension\Extension:
      base_url: ' . $CFG->wwwroot . '
      goutte: ~
      selenium2: ~
    Sanpi\Behatch\Extension:
      contexts:
        browser: ~
        system: ~
        json: ~
        table: ~
    ' . $behatpath . '/vendor/moodlehq/behat-extension/init.php:
';

        // Gets all the components with features.
        $components = tests_finder::get_components_with_tests('features');
        if ($components) {
            $featurespaths = array('');
            foreach ($components as $componentname => $path) {
                $path = self::clean_path($path) . self::$behat_tests_path;
                if (empty($featurespaths[$path]) && file_exists($path)) {
                    $featurespaths[$path] = $path;
{'fraction_non_alphanumeric': 0.13078034682080925, 'fraction_numerical': 0.002384393063583815, 'mean_word_length': 2.001734981565821, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6247982', 'n_tokens_mistral': 4022, 'n_tokens_neox': 3859, 'n_words': 1035}