View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal;
use Drupal\quiz\Entity\Quiz;
use Drupal\quiz\Entity\QuizResult;
use Drupal\Tests\Traits\Core\CronRunTrait;
use function count;
class QuizResultPruningTest extends QuizTestBase {
use CronRunTrait;
protected static $modules = [
'quiz_truefalse',
];
public function testResultPruning() {
$this
->drupalLogin($this->admin);
$quiz_node = $this
->createQuiz([
'keep_results' => Quiz::KEEP_ALL,
]);
$question1 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question1, $quiz_node);
$question2 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question2, $quiz_node);
$question3 = $this
->createQuestion([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$this
->linkQuestionToQuiz($question3, $quiz_node);
$question4 = $this
->createQuestion([
'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, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question3->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"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, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question3->id()}][answer]" => 0,
], t('Next'));
$this
->drupalPostForm(NULL, [
"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, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 0,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question3->id()}][answer]" => 0,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question4->id()}][answer]" => 0,
], t('Finish'));
$this
->assertText('Your previous score on this Quiz was equal or better. This result will not be saved.');
$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, [
"question[{$question1->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question2->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"question[{$question3->id()}][answer]" => 1,
], t('Next'));
$this
->drupalPostForm(NULL, [
"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');
}
}