MDL-63185 mod_quiz: replace user session handling to use set_user()

Part of MDL-62610
This commit is contained in:
Simey Lameze 2018-09-26 09:08:17 +08:00
parent a190910595
commit df48d3cc71
2 changed files with 25 additions and 29 deletions

View File

@ -40,8 +40,11 @@ Feature: Basic use of the Responses report
Then I should see "Attempts: 0"
And I should see "Nothing to display"
And I set the field "Attempts from" to "enrolled users who have not attempted the quiz"
And I press "Show report"
And "Student One" row "State" column of "responses" table should contain "-"
And I log out
@javascript
Scenario: Report works when there are attempts
Given I log in as "student1"
And user "student1" has started an attempt at quiz "Quiz 1"
And user "student1" has submitted answers in their attempt at quiz "Quiz 1":
| slot | response |
@ -53,6 +56,11 @@ Feature: Basic use of the Responses report
| slot | response |
| 1 | 3.14 |
And user "student1" has finished an attempt at quiz "Quiz 1"
And I log out
When I log in as "teacher"
And I am on "Course 1" course homepage
And I follow "Quiz 1"
And I navigate to "Results > Responses" in current page administration
Then I should see "Attempts: 1"
And I should see "Student One"
And I should not see "Student Two"

View File

@ -595,7 +595,7 @@ class behat_mod_quiz extends behat_question_base {
* @Given /^user "([^"]*)" has attempted "([^"]*)" with responses:$/
*/
public function user_has_attempted_with_responses($username, $quizname, TableNode $attemptinfo) {
global $DB, $USER;
global $DB;
/** @var mod_quiz_generator $quizgenerator */
$quizgenerator = behat_util::get_data_generator()->get_plugin_generator('mod_quiz');
@ -627,15 +627,14 @@ class behat_mod_quiz extends behat_question_base {
}
}
$saveduser = $USER; // TODO there is probably a better way to do this. If not, there should be.
$USER = $user;
$this->set_user($user);
$attempt = $quizgenerator->create_attempt($quizid, $user->id,
$forcedrandomquestions, $forcedvariants);
$quizgenerator->submit_responses($attempt->id, $responses, true);
$USER = $saveduser;
$this->set_user();
}
/**
@ -650,19 +649,16 @@ class behat_mod_quiz extends behat_question_base {
* @Given /^user "([^"]*)" has started an attempt at quiz "([^"]*)"$/
*/
public function user_has_started_an_attempt_at_quiz($username, $quizname) {
global $DB, $USER;
global $DB;
/** @var mod_quiz_generator $quizgenerator */
$quizgenerator = behat_util::get_data_generator()->get_plugin_generator('mod_quiz');
$quizid = $DB->get_field('quiz', 'id', ['name' => $quizname], MUST_EXIST);
$user = $DB->get_record('user', ['username' => $username], '*', MUST_EXIST);
$saveduser = $USER; // TODO there is probably a better way to do this. If not, there should be.
$USER = $user;
$this->set_user($user);
$quizgenerator->create_attempt($quizid, $user->id);
$USER = $saveduser;
$this->set_user();
}
/**
@ -671,10 +667,11 @@ class behat_mod_quiz extends behat_question_base {
* @param string $quizname the name of the quiz the user will attempt.
* @param string $username the username of the user that will attempt.
* @param TableNode $attemptinfo information about the questions to add, as above.
* @throws \Behat\Mink\Exception\ExpectationException
* @Given /^user "([^"]*)" has submitted answers in their attempt at quiz "([^"]*)":$/
*/
public function user_has_submitted_answers_in_their_attempt_at_quiz($username, $quizname, TableNode $attemptinfo) {
global $DB, $USER;
global $DB;
/** @var mod_quiz_generator $quizgenerator */
$quizgenerator = behat_util::get_data_generator()->get_plugin_generator('mod_quiz');
@ -705,45 +702,36 @@ class behat_mod_quiz extends behat_question_base {
}
}
$saveduser = $USER; // TODO there is probably a better way to do this. If not, there should be.
$USER = $user;
$this->set_user($user);
foreach (quiz_get_user_attempts($quizid, $user->id, 'unfinished', true) as $attemptid => $attemptobj) {
$quizgenerator->submit_responses($attemptid, $responses, false);
break;
}
$USER = $saveduser;
$saveduser = $USER; // TODO there is probably a better way to do this. If not, there should be.
$USER = $user;
$USER = $saveduser;
$this->set_user();
}
/**
* Finish a quiz attempt.
* Finish an existing quiz attempt.
*
* @param string $quizname the name of the quiz the user will attempt.
* @param string $username the username of the user that will attempt.
* @Given /^user "([^"]*)" has finished an attempt at quiz "([^"]*)"$/
*/
public function user_has_finished_an_attempt_at_quiz($username, $quizname) {
global $DB, $USER;
/** @var mod_quiz_generator $quizgenerator */
$quizgenerator = behat_util::get_data_generator()->get_plugin_generator('mod_quiz');
global $DB;
$quizid = $DB->get_field('quiz', 'id', ['name' => $quizname], MUST_EXIST);
$user = $DB->get_record('user', ['username' => $username], '*', MUST_EXIST);
$saveduser = $USER; // TODO there is probably a better way to do this. If not, there should be.
$USER = $user;
$this->set_user($user);
foreach (quiz_get_user_attempts($quizid, $user->id, 'unfinished', true) as $attemptid => $attemptobj) {
$attemptobj = quiz_attempt::create($attemptid);
$attemptobj->process_finish(time(), true);
break;
}
$USER = $saveduser;
$this->set_user();
}
}