mirror of
https://github.com/moodle/moodle.git
synced 2025-08-11 03:46:42 +02:00
Merge branch 'MDL-70909-311' of git://github.com/ferranrecio/moodle into MOODLE_311_STABLE
This commit is contained in:
commit
66131d9307
13 changed files with 555 additions and 68 deletions
|
@ -25,10 +25,11 @@
|
|||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once(__DIR__ . '/../../behat/behat_base.php');
|
||||
|
||||
use Behat\Gherkin\Node\TableNode as TableNode;
|
||||
use Behat\Behat\Tester\Exception\PendingException as PendingException;
|
||||
|
||||
|
||||
/**
|
||||
* Class to quickly create Behat test data using component data generators.
|
||||
*
|
||||
|
@ -480,53 +481,7 @@ abstract class behat_generator_base {
|
|||
* @return context
|
||||
*/
|
||||
protected function get_context($levelname, $contextref) {
|
||||
global $DB;
|
||||
|
||||
// Getting context levels and names (we will be using the English ones as it is the test site language).
|
||||
$contextlevels = context_helper::get_all_levels();
|
||||
$contextnames = array();
|
||||
foreach ($contextlevels as $level => $classname) {
|
||||
$contextnames[context_helper::get_level_name($level)] = $level;
|
||||
}
|
||||
|
||||
if (empty($contextnames[$levelname])) {
|
||||
throw new Exception('The specified "' . $levelname . '" context level does not exist');
|
||||
}
|
||||
$contextlevel = $contextnames[$levelname];
|
||||
|
||||
// Return it, we don't need to look for other internal ids.
|
||||
if ($contextlevel == CONTEXT_SYSTEM) {
|
||||
return context_system::instance();
|
||||
}
|
||||
|
||||
switch ($contextlevel) {
|
||||
|
||||
case CONTEXT_USER:
|
||||
$instanceid = $DB->get_field('user', 'id', array('username' => $contextref));
|
||||
break;
|
||||
|
||||
case CONTEXT_COURSECAT:
|
||||
$instanceid = $DB->get_field('course_categories', 'id', array('idnumber' => $contextref));
|
||||
break;
|
||||
|
||||
case CONTEXT_COURSE:
|
||||
$instanceid = $DB->get_field('course', 'id', array('shortname' => $contextref));
|
||||
break;
|
||||
|
||||
case CONTEXT_MODULE:
|
||||
$instanceid = $DB->get_field('course_modules', 'id', array('idnumber' => $contextref));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
$contextclass = $contextlevels[$contextlevel];
|
||||
if (!$context = $contextclass::instance($instanceid, IGNORE_MISSING)) {
|
||||
throw new Exception('The specified "' . $contextref . '" context reference does not exist');
|
||||
}
|
||||
|
||||
return $context;
|
||||
return behat_base::get_context($levelname, $contextref);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1068,6 +1068,69 @@ EOF;
|
|||
|
||||
\core\session\manager::set_user($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the internal moodle context id from the context reference.
|
||||
*
|
||||
* The context reference changes depending on the context
|
||||
* level, it can be the system, a user, a category, a course or
|
||||
* a module.
|
||||
*
|
||||
* @throws Exception
|
||||
* @param string $levelname The context level string introduced by the test writer
|
||||
* @param string $contextref The context reference introduced by the test writer
|
||||
* @return context
|
||||
*/
|
||||
public static function get_context(string $levelname, string $contextref): context {
|
||||
global $DB;
|
||||
|
||||
// Getting context levels and names (we will be using the English ones as it is the test site language).
|
||||
$contextlevels = context_helper::get_all_levels();
|
||||
$contextnames = array();
|
||||
foreach ($contextlevels as $level => $classname) {
|
||||
$contextnames[context_helper::get_level_name($level)] = $level;
|
||||
}
|
||||
|
||||
if (empty($contextnames[$levelname])) {
|
||||
throw new Exception('The specified "' . $levelname . '" context level does not exist');
|
||||
}
|
||||
$contextlevel = $contextnames[$levelname];
|
||||
|
||||
// Return it, we don't need to look for other internal ids.
|
||||
if ($contextlevel == CONTEXT_SYSTEM) {
|
||||
return context_system::instance();
|
||||
}
|
||||
|
||||
switch ($contextlevel) {
|
||||
|
||||
case CONTEXT_USER:
|
||||
$instanceid = $DB->get_field('user', 'id', array('username' => $contextref));
|
||||
break;
|
||||
|
||||
case CONTEXT_COURSECAT:
|
||||
$instanceid = $DB->get_field('course_categories', 'id', array('idnumber' => $contextref));
|
||||
break;
|
||||
|
||||
case CONTEXT_COURSE:
|
||||
$instanceid = $DB->get_field('course', 'id', array('shortname' => $contextref));
|
||||
break;
|
||||
|
||||
case CONTEXT_MODULE:
|
||||
$instanceid = $DB->get_field('course_modules', 'id', array('idnumber' => $contextref));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
$contextclass = $contextlevels[$contextlevel];
|
||||
if (!$context = $contextclass::instance($instanceid, IGNORE_MISSING)) {
|
||||
throw new Exception('The specified "' . $contextref . '" context reference does not exist');
|
||||
}
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger click on node via javascript instead of actually clicking on it via pointer.
|
||||
*
|
||||
|
|
|
@ -1383,6 +1383,10 @@ function is_enrolled(context $context, $user = null, $withcapability = '', $only
|
|||
* several times (e.g. as manual enrolment, and as self enrolment). You may
|
||||
* need to use a SELECT DISTINCT in your query (see get_enrolled_sql for example).
|
||||
*
|
||||
* In case is guaranteed some of the joins never match any rows, the resulting
|
||||
* join_sql->cannotmatchanyrows will be true. This happens when the capability
|
||||
* is prohibited.
|
||||
*
|
||||
* @param context $context
|
||||
* @param string $prefix optional, a prefix to the user id column
|
||||
* @param string|array $capability optional, may include a capability name, or array of names.
|
||||
|
@ -1392,24 +1396,27 @@ function is_enrolled(context $context, $user = null, $withcapability = '', $only
|
|||
* @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
|
||||
* @param bool $onlysuspended inverse of onlyactive, consider only suspended enrolments
|
||||
* @param int $enrolid The enrolment ID. If not 0, only users enrolled using this enrolment method will be returned.
|
||||
* @return \core\dml\sql_join Contains joins, wheres, params
|
||||
* @return \core\dml\sql_join Contains joins, wheres, params and cannotmatchanyrows
|
||||
*/
|
||||
function get_enrolled_with_capabilities_join(context $context, $prefix = '', $capability = '', $group = 0,
|
||||
$onlyactive = false, $onlysuspended = false, $enrolid = 0) {
|
||||
$uid = $prefix . 'u.id';
|
||||
$joins = array();
|
||||
$wheres = array();
|
||||
$cannotmatchanyrows = false;
|
||||
|
||||
$enrolledjoin = get_enrolled_join($context, $uid, $onlyactive, $onlysuspended, $enrolid);
|
||||
$joins[] = $enrolledjoin->joins;
|
||||
$wheres[] = $enrolledjoin->wheres;
|
||||
$params = $enrolledjoin->params;
|
||||
$cannotmatchanyrows = $cannotmatchanyrows || $enrolledjoin->cannotmatchanyrows;
|
||||
|
||||
if (!empty($capability)) {
|
||||
$capjoin = get_with_capability_join($context, $capability, $uid);
|
||||
$joins[] = $capjoin->joins;
|
||||
$wheres[] = $capjoin->wheres;
|
||||
$params = array_merge($params, $capjoin->params);
|
||||
$cannotmatchanyrows = $cannotmatchanyrows || $capjoin->cannotmatchanyrows;
|
||||
}
|
||||
|
||||
if ($group) {
|
||||
|
@ -1419,13 +1426,14 @@ function get_enrolled_with_capabilities_join(context $context, $prefix = '', $ca
|
|||
if (!empty($groupjoin->wheres)) {
|
||||
$wheres[] = $groupjoin->wheres;
|
||||
}
|
||||
$cannotmatchanyrows = $cannotmatchanyrows || $groupjoin->cannotmatchanyrows;
|
||||
}
|
||||
|
||||
$joins = implode("\n", $joins);
|
||||
$wheres[] = "{$prefix}u.deleted = 0";
|
||||
$wheres = implode(" AND ", $wheres);
|
||||
|
||||
return new \core\dml\sql_join($joins, $wheres, $params);
|
||||
return new \core\dml\sql_join($joins, $wheres, $params, $cannotmatchanyrows);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -249,4 +249,44 @@ class behat_permissions extends behat_base {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark context as frozen.
|
||||
*
|
||||
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" is context frozen$/
|
||||
* @throws ExpectationException if the context cannot be frozen or found
|
||||
* @param string $element Element we look on
|
||||
* @param string $selector The type of where we look (activity, course)
|
||||
*/
|
||||
public function the_context_is_context_frozen(string $element, string $selector) {
|
||||
|
||||
// Enable context freeze if it is not done yet.
|
||||
set_config('contextlocking', 1);
|
||||
|
||||
// Find context.
|
||||
$context = self::get_context($selector, $element);
|
||||
|
||||
// Freeze context.
|
||||
$context->set_locked(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmark context as frozen.
|
||||
*
|
||||
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" is not context frozen$/
|
||||
* @throws ExpectationException if the context cannot be frozen or found
|
||||
* @param string $element Element we look on
|
||||
* @param string $selector The type of where we look (activity, course)
|
||||
*/
|
||||
public function the_context_is_not_context_frozen(string $element, string $selector) {
|
||||
|
||||
// Enable context freeze if it is not done yet.
|
||||
set_config('contextlocking', 1);
|
||||
|
||||
// Find context.
|
||||
$context = self::get_context($selector, $element);
|
||||
|
||||
// Freeze context.
|
||||
$context->set_locked(false);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue