diff --git a/lib/behat/behat_base.php b/lib/behat/behat_base.php new file mode 100644 index 00000000000..2e8a31eb359 --- /dev/null +++ b/lib/behat/behat_base.php @@ -0,0 +1,51 @@ +. + +/** + * Base class of all steps definitions. + * + * This script is only called from Behat as part of it's integration + * in Moodle. + * + * @package core + * @category test + * @copyright 2012 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +/** + * Steps definitions base class. + * + * To extend by the steps definitions of the different Moodle components. + * + * It can not contain steps definitions to avoid duplicates, only utility + * methods shared between steps. + * + * @package core + * @category test + * @copyright 2012 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_base extends Behat\MinkExtension\Context\RawMinkContext { + + /** + * The timeout for each Behat step (load page, wait for an element to load...). + */ + const TIMEOUT = 6; + +} diff --git a/lib/sessionlib.php b/lib/sessionlib.php index 80525173786..49288dca5ac 100644 --- a/lib/sessionlib.php +++ b/lib/sessionlib.php @@ -1128,7 +1128,7 @@ function session_set_user($user) { } sesskey(); // init session key - if (PHPUNIT_TEST) { + if (PHPUNIT_TEST || defined('BEHAT_RUNNING')) { // phpunit tests use reversed reference global $USER; $USER = $_SESSION['USER']; diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php new file mode 100644 index 00000000000..c92e13847f3 --- /dev/null +++ b/lib/tests/behat/behat_hooks.php @@ -0,0 +1,173 @@ +. + +/** + * Behat hooks steps definitions. + * + * This methods are used by Behat CLI command. + * + * @package core + * @category test + * @copyright 2012 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +require_once(__DIR__ . '/../../behat/behat_base.php'); + +use Behat\Behat\Event\SuiteEvent as SuiteEvent; +use Behat\Behat\Event\ScenarioEvent as ScenarioEvent; +use Behat\Behat\Event\StepEvent as StepEvent; + +/** + * Hooks to the behat process. + * + * Behat accepts hooks after and before each + * suite, feature, scenario and step. + * + * They can not call other steps as part of their process + * like regular steps definitions does. + * + * Throws generic Exception because they are captured by Behat. + * + * @package core + * @category test + * @copyright 2012 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_hooks extends behat_base { + + /** + * @var string The last visited URL. + */ + private $lasturl = null; + + /** + * 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; + + // To work with behat_dataroot and behat_prefix instead of the regular environment. + define('BEHAT_RUNNING', 1); + define('CLI_SCRIPT', 1); + + 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'); + + // 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'); + } + + // Avoid parallel tests execution, it continues when the previous lock is released. + test_lock::acquire('behat'); + } + + /** + * Resets the test environment. + * + * @throws coding_exception If here we are not using the test database it should be because of a coding error + * @BeforeScenario + */ + public function before_scenario($event) { + global $DB, $SESSION, $CFG; + + // As many checks as we can. + if (!defined('BEHAT_RUNNING') || + php_sapi_name() != 'cli' || + !behat_util::is_test_mode_enabled() || + !behat_util::is_test_site() || + !isset($CFG->originaldataroot)) { + throw new coding_exception('Behat only can modify the test database and the test dataroot!'); + } + + behat_util::reset_database(); + behat_util::reset_dataroot(); + + // Assing valid data to admin user (some generator-related code needs a valid user). + $user = $DB->get_record('user', array('username' => 'admin')); + session_set_user($user); + + // Avoid some notices / warnings. + $SESSION = new stdClass(); + } + + /** + * Ensures selenium is running. + * + * Is only executed in scenarios which requires Javascript to run, + * it returns a direct error message about what's going on. + * + * @throws Exception + * @BeforeScenario @javascript + */ + public function before_scenario_javascript($event) { + + // Just trying if server responds. + try { + $this->getSession()->executeScript('// empty comment'); + } catch (Exception $e) { + $moreinfo = 'More info in ' . behat_command::DOCS_URL . '#Running_tests'; + $msg = 'Selenium server is not running, you need to start it to run tests that involves Javascript. ' . $moreinfo; + throw new Exception($msg); + } + } + + /** + * Checks that all DOM is ready. + * + * Executed only when running against a real browser. + * + * @AfterStep @javascript + */ + public function after_step_javascript($event) { + + // If it doesn't have definition or it fails there is no need to check it. + if ($event->getResult() != StepEvent::PASSED || + !$event->hasDefinition()) { + return; + } + + // Wait until the page is ready if we are in a new URL. + $currenturl = $this->getSession()->getCurrentUrl(); + if (is_null($this->lasturl) || $currenturl !== $this->lasturl) { + $this->lasturl = $currenturl; + $this->getSession()->wait(self::TIMEOUT, '(document.readyState === "complete")'); + } + } + +}