View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal;
use Drupal\quiz\Entity\QuizResult;
use Drupal\Tests\Traits\Core\CronRunTrait;
use const QUIZ_KEEP_ALL;
use const QUIZ_KEEP_BEST;
use const QUIZ_KEEP_LATEST;
use function count;
class QuizResultPruningTest extends QuizTestBase {
use CronRunTrait;
public static $modules = array(
'quiz_truefalse',
);
public function testResultPruning() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz(array(
'keep_results' => QUIZ_KEEP_ALL,
));
$question1 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question1, $quiz_node);
$question2 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question2, $quiz_node);
$question3 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question3, $quiz_node);
$question4 = $this
->createQuestion(array(
'type' => 'truefalse',
'truefalse_correct' => 1,
));
$this
->linkQuestionToQuiz($question4, $quiz_node);
$this
->drupalLogin($this->user);
for ($i = 1; $i <= 2; $i++) {
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question2->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question3->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question4->id()}][answer]" => 1,
), t('Finish'));
}
$results = QuizResult::loadMultiple();
$this
->assertEqual(count($results), 2, 'Found 2 quiz results.');
$quiz_node->keep_results = QUIZ_KEEP_LATEST;
$quiz_node
->save();
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question2->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question3->id()}][answer]" => 0,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question4->id()}][answer]" => 0,
), t('Finish'));
$results = Drupal::entityTypeManager()
->getStorage('quiz_result')
->loadByProperties([
'is_invalid' => 1,
]);
$this
->assertEqual(count($results), 2, 'Found 2 invalid quiz results');
$results = Drupal::entityTypeManager()
->getStorage('quiz_result')
->loadByProperties([
'is_invalid' => 0,
]);
$this
->assertEqual(count($results), 1, 'Found only one quiz result');
$quiz_result = reset($results);
$this
->assertEqual($quiz_result
->get('score')->value, 50, 'Quiz result was 50%');
$quiz_node->keep_results = QUIZ_KEEP_BEST;
$quiz_node
->save();
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question2->id()}][answer]" => 0,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question3->id()}][answer]" => 0,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question4->id()}][answer]" => 0,
), t('Finish'));
$results = Drupal::entityTypeManager()
->getStorage('quiz_result')
->loadByProperties([
'is_invalid' => 1,
]);
$this
->assertEqual(count($results), 3, 'Found 3 invalid quiz results');
$results = Drupal::entityTypeManager()
->getStorage('quiz_result')
->loadByProperties([
'is_invalid' => 0,
]);
$this
->assertTrue(count($results) == 1, 'Found only one quiz result');
$quiz_result = reset($results);
$this
->assertEqual($quiz_result
->get('score')->value, 50, 'Quiz score was 50%');
$this
->drupalGet("quiz/{$quiz_node->id()}/take");
$this
->drupalPostForm(NULL, array(
"question[{$question1->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question2->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question3->id()}][answer]" => 1,
), t('Next'));
$this
->drupalPostForm(NULL, array(
"question[{$question4->id()}][answer]" => 0,
), t('Finish'));
$results = Drupal::entityTypeManager()
->getStorage('quiz_result')
->loadByProperties([
'is_invalid' => 1,
]);
$this
->assertEqual(count($results), 4, 'Found 4 invalid quiz results');
$results = Drupal::entityTypeManager()
->getStorage('quiz_result')
->loadByProperties([
'is_invalid' => 0,
]);
$this
->assertEqual(count($results), 1, 'Found only one quiz result');
$quiz_result = reset($results);
$this
->assertEqual($quiz_result
->get('score')->value, 75, 'Quiz score was 75%');
$config = \Drupal::service('config.factory')
->getEditable('quiz.settings');
$config
->set('remove_invalid_quiz_record', 86400)
->save();
$this
->cronRun();
$results = Drupal::entityTypeManager()
->getStorage('quiz_result')
->loadByProperties([
'is_invalid' => 1,
]);
$this
->assertEqual(count($results), 4, 'Found 4 invalid quiz results');
$results = Drupal::entityTypeManager()
->getStorage('quiz_result')
->loadByProperties([
'is_invalid' => 0,
]);
$this
->assertEqual(count($results), 1, 'Found only one quiz result');
$quiz_result = reset($results);
$this
->assertEqual($quiz_result
->get('score')->value, 75, 'Quiz score was 75%');
$config
->set('remove_invalid_quiz_record', -86400)
->save();
$this
->cronRun();
$results = QuizResult::loadMultiple();
$this
->assertEqual(count($results), 1, 'Found only one quiz result');
$quiz_result = reset($results);
$this
->assertEqual($quiz_result
->get('score')->value, 75, 'Quiz score was 75%');
$this
->assertEqual($quiz_result
->get('is_invalid')->value, 0, 'Quiz score was valid');
}
}