Skip to content

Commit

Permalink
Merge branch 'MDL-37656_master' of git://github.com/dmonllao/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Jan 29, 2013
2 parents e0f7550 + f5ceb6c commit 10b8d16
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 1 deletion.
51 changes: 51 additions & 0 deletions lib/behat/behat_base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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/>.

/**
* 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;

}
2 changes: 1 addition & 1 deletion lib/sessionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
173 changes: 173 additions & 0 deletions lib/tests/behat/behat_hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?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 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")');
}
}

}

0 comments on commit 10b8d16

Please sign in to comment.