<commit_before><?php

/* $Id$ */

if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
}

// test handler function
function sample_function_handler($eventdata) {
    static $called = 0;
    static $ignorefail = false;

    if ($eventdata == 'status') {
        return $called;

    } else if ($eventdata == 'reset') {
        $called = 0;
        $ignorefail = false;
        return;

    } else if ($eventdata == 'fail') {
        if ($ignorefail) {
            $called++;
            return true;
        } else {
            return false;
        }

    } else if ($eventdata == 'ignorefail') {
        $ignorefail = true;
        return;

    } else if ($eventdata == 'ok') {
        $called++;
        return true;
    }

    print_error('invalideventdata', '', '', $eventdata);
}

// test handler class with static method
class sample_handler_class {
    function static_method($eventdata) {
        static $called = 0;
        static $ignorefail = false;

        if ($eventdata == 'status') {
            return $called;

        } else if ($eventdata == 'reset') {
            $called = 0;
            $ignorefail = false;
            return;

        } else if ($eventdata == 'fail') {
            if ($ignorefail) {
                $called++;
                return true;
            } else {
                return false;
            }

        } else if ($eventdata == 'ignorefail') {
            $ignorefail = true;
            return;

        } else if ($eventdata == 'ok') {
            $called++;
            return true;
        }

        print_error('invalideventdata', '', '', $eventdata);
    }
}

class eventslib_test extends FakeDBUnitTestCase {
    private $realdb;
    /**
     * Create temporary entries in the database for these tests.
     * These tests have to work no matter the data currently in the database
     * (meaning they should run on a brand new site). This means several items of
     * data have to be artificially inseminated (:-) in the DB.
     */
    function setUp() {
        // Set global category settings to -1 (not force)
        global $CFG, $DB;

        if (is_null($DB)) {
            $this->realdb = $DB;
            $DB = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
            $DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix);
        }

        events_uninstall('unittest');
        sample_function_handler('reset');
        sample_handler_class::static_method('reset');
        events_update_definition('unittest');
    }

    /**
     * Delete temporary entries from the database
     */
    function tearDown() {
       events_uninstall('unittest');
    }

    /**
     * Tests the installation of event handlers from file
     */
    function test__events_update_definition__install() {
        global $CFG, $DB;

        $dbcount = $DB->count_records('events_handlers', array('handlermodule'=>'unittest'));
        $handlers = array();
        require($CFG->libdir.'/simpletest/fixtures/events.php');
        $filecount = count($handlers);
        $this->assertEqual($dbcount, $filecount, 'Equal number of handlers in file and db: %s');
    }

    /**
     * Tests the uninstallation of event handlers from file
     */
    function test__events_update_definition__uninstall() {<commit_msg>MDL-16483 - partial unbreaking of eventlsib test<commit_after><?php

/* $Id$ */

if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
}

// test handler function
function sample_function_handler($eventdata) {
    static $called = 0;
    static $ignorefail = false;

    if ($eventdata == 'status') {
        return $called;

    } else if ($eventdata == 'reset') {
        $called = 0;
        $ignorefail = false;
        return;

    } else if ($eventdata == 'fail') {
        if ($ignorefail) {
            $called++;
            return true;
        } else {
            return false;
        }

    } else if ($eventdata == 'ignorefail') {
        $ignorefail = true;
        return;

    } else if ($eventdata == 'ok') {
        $called++;
        return true;
    }

    print_error('invalideventdata', '', '', $eventdata);
}

// test handler class with static method
class sample_handler_class {
    function static_method($eventdata) {
        static $called = 0;
        static $ignorefail = false;

        if ($eventdata == 'status') {
            return $called;

        } else if ($eventdata == 'reset') {
            $called = 0;
            $ignorefail = false;
            return;

        } else if ($eventdata == 'fail') {
            if ($ignorefail) {
                $called++;
                return true;
            } else {
                return false;
            }

        } else if ($eventdata == 'ignorefail') {
            $ignorefail = true;
            return;

        } else if ($eventdata == 'ok') {
            $called++;
            return true;
        }

        print_error('invalideventdata', '', '', $eventdata);
    }
}

class eventslib_test extends UnitTestCase {
    /**
     * Create temporary entries in the database for these tests.
     * These tests have to work no matter the data currently in the database
     * (meaning they should run on a brand new site). This means several items of
     * data have to be artificially inseminated (:-) in the DB.
     */
    function setUp() {
        // Set global category settings to -1 (not force)
        events_uninstall('unittest');
        sample_function_handler('reset');
        sample_handler_class::static_method('reset');
        events_update_definition('unittest');
    }

    /**
     * Delete temporary entries from the database
     */
    function tearDown() {
       events_uninstall('unittest');
    }

    /**
     * Tests the installation of event handlers from file
     */
    function test__events_update_definition__install() {
        global $CFG, $DB;

        $dbcount = $DB->count_records('events_handlers', array('handlermodule'=>'unittest'));
        $handlers = array();
        require($CFG->libdir.'/simpletest/fixtures/events.php');
        $filecount = count($handlers);
        $this->assertEqual($dbcount, $filecount, 'Equal number of handlers in file and db: %s');
    }

    /**
     * Tests the uninstallation of event handlers from file
     */
    function test__events_update_definition__uninstall() {
{'fraction_non_alphanumeric': 0.1256127450980392, 'fraction_numerical': 0.002297794117647059, 'mean_word_length': 2.0666979802724286, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1188707', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1780, 'n_words': 510}