View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal;
use Drupal\quiz\Entity\QuizQuestion;
class QuizRevisioningTest extends QuizTestBase {
protected static $modules = [
'quiz_truefalse',
];
public function testQuizRevisioning() {
$config = Drupal::configFactory()
->getEditable('quiz.settings');
$config
->set('revisioning', TRUE)
->save();
$this
->drupalLogin($this->admin);
$question = $this
->createQuestion([
'title' => 'Revision 1',
'body' => 'Revision 1',
'type' => 'truefalse',
'truefalse_correct' => 1,
'feedback' => 'Question feedback for Revision 1',
]);
$quiz_node = $this
->linkQuestionToQuiz($question);
$quiz_node
->set('allow_resume', 1)
->save();
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertText("Revision 1");
$this
->drupalLogin($this->admin);
$this
->drupalGet("quiz-question/{$question->id()}/edit");
$this
->assertText('Warning: This question has attempts.');
$this
->drupalPostForm(NULL, [], t('Save'));
$this
->assertText('Create new revision field is required.');
$this
->drupalPostForm(NULL, [
'title[0][value]' => 'Revision 2',
'body[0][value]' => 'Revision 2',
'truefalse_correct' => 0,
'feedback[0][value]' => 'Question feedback for Revision 2',
'revision' => TRUE,
], t('Save'));
Drupal::entityTypeManager()
->getStorage('quiz_question')
->resetCache();
$question = QuizQuestion::load($question
->id());
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertText("Revision 1");
$this
->drupalPostForm(NULL, [
"question[{$question->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('You got 1 of 1 possible points.');
$this
->assertText('Question feedback for Revision 1');
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertText("Revision 1");
$this
->drupalLogin($this->admin);
$this
->drupalGet("quiz/{$quiz_node->id()}/questions");
$this
->assertText('This quiz has been answered.');
$this
->clickLink('create a new revision');
$this
->assertText('Warning: This quiz has attempts.');
$this
->drupalPostForm(NULL, [
'revision' => TRUE,
], t('Save'));
$this
->assertNoText('This quiz has been answered.');
$this
->drupalPostForm(NULL, [
"question_list[{$question->getRevisionId()}][question_vid]" => TRUE,
], t('Submit'));
$this
->drupalLogin($this->user);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertText("Revision 1");
$this
->drupalPostForm(NULL, [
"question[{$question->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('You got 1 of 1 possible points.');
$this
->assertText('Question feedback for Revision 1');
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertText("Revision 2");
$this
->drupalPostForm(NULL, [
"question[{$question->id()}][answer]" => 1,
], t('Finish'));
$this
->assertText('You got 0 of 1 possible points.');
$this
->assertText('Question feedback for Revision 2');
$mega_admin = $this
->createUser([
'administer quiz',
'administer quiz_question',
'override quiz revisioning',
]);
$this
->drupalLogin($mega_admin);
$this
->drupalGet("quiz/{$quiz_node->id()}/questions");
}
public function testQuizNoRevisioning() {
$this
->drupalLogin($this->admin);
$question_node = $this
->createQuestion([
'title' => 'Revision 1',
'body' => 'Revision 1',
'type' => 'truefalse',
'truefalse_correct' => 1,
'feedback' => 'Question feedback for Revision 1',
]);
$quiz_node = $this
->linkQuestionToQuiz($question_node);
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->assertText("Revision 1");
$this
->drupalPostForm(NULL, [
"question[{$question_node->id()}][answer]" => 1,
], t('Finish'));
$this
->drupalGet("quiz/{$quiz_node->id()}/edit");
$this
->assertText('You must delete all attempts on this quiz before editing.');
$this
->assertSession()
->elementAttributeExists('css', '#edit-submit', 'disabled');
$this
->drupalGet("quiz-question/{$question_node->id()}/edit");
$this
->assertText('You must delete all attempts on this question before editing.');
$this
->assertSession()
->elementAttributeExists('css', '#edit-submit', 'disabled');
$mega_admin = $this
->createUser([
'administer quiz',
'administer quiz_question',
'override quiz revisioning',
]);
$this
->drupalLogin($mega_admin);
$this
->drupalGet("quiz/{$quiz_node->id()}/edit");
$this
->assertText('You should delete all attempts on this quiz before editing.');
$this
->drupalPostForm(NULL, [], t('Save'));
$this
->drupalGet("quiz-question/{$question_node->id()}/edit");
$this
->assertText('You should delete all attempts on this question before editing.');
$this
->drupalPostForm(NULL, [], t('Save'));
}
}