View source
<?php
namespace Drupal\Tests\opigno_learning_path\Functional;
use Drupal\opigno_module\Entity\UserModuleStatus;
class OpignoModuleScoreTest extends LearningPathBrowserTestBase {
protected function setUp() {
parent::setUp();
\Drupal::service('module_installer')
->install([
'opigno_statistics',
'opigno_messaging',
'opigno_learning_path_test',
'search',
]);
\Drupal::service('theme_handler')
->install([
'platon',
]);
\Drupal::service('theme_handler')
->setDefault('platon');
}
public function testModuleScoreDisplay() {
$training = $this
->createGroup([
'field_learning_path_visibility' => 'public',
]);
$training
->addMember($this->groupCreator);
$module = $this
->createOpignoModule([
'keep_results' => $this
->getKeepResultsOption('best'),
]);
$training = $this
->addModuleToTraining($training, $module);
$this
->createAndFinishAttempt($training, $module, $this->groupCreator, 100);
$this
->drupalGet('/group/' . $training
->id());
$this
->assertSession()
->pageTextContains('100%');
$this
->drupalGet('/achievements', [
'query' => [
'preload-progress' => 'true',
],
]);
$content = $this
->getSession()
->getPage()
->find('css', '.lp_step_summary_score');
$this
->assertEquals('100%', $content
->getText(), 'Best score displays on achievements');
$this
->drupalGet('/statistics/training/' . $training
->id());
$content = $this
->getSession()
->getPage()
->find('css', '.training-content-list');
$this
->assertEquals(1, substr_count($content
->getText(), '100%'), 'Best score displays on statistics');
$this
->createAndFinishAttempt($training, $module, $this->groupCreator, 50);
$this
->drupalGet('/group/' . $training
->id());
$this
->assertSession()
->pageTextContains('100%');
$this
->drupalGet('/achievements', [
'query' => [
'preload-progress' => 'true',
],
]);
$content = $this
->getSession()
->getPage()
->find('css', '.lp_step_summary_score', 'Best score still displays on achievements when user get a worse result');
$this
->assertEquals('100%', $content
->getText());
$this
->drupalGet('/statistics/training/' . $training
->id());
$content = $this
->getSession()
->getPage()
->find('css', '.training-content-list');
$this
->assertEquals(1, substr_count($content
->getText(), '100%'), 'Best score still displays on statistics when user get a worse result');
$module
->set('keep_results', $this
->getKeepResultsOption('newest'));
$module
->save();
$this
->createAndFinishAttempt($training, $module, $this->groupCreator, 50, TRUE);
$this
->drupalGet('/group/' . $training
->id());
$this
->assertSession()
->pageTextContains('50%');
$this
->drupalGet('/achievements', [
'query' => [
'preload-progress' => 'true',
],
]);
$content = $this
->getSession()
->getPage()
->find('css', '.lp_step_summary_score');
$this
->assertEquals('50%', $content
->getText(), 'Newest score displays on achievements');
$this
->drupalGet('/statistics/training/' . $training
->id());
$content = $this
->getSession()
->getPage()
->find('css', '.training-content-list');
$this
->assertEquals(1, substr_count($content
->getText(), '50%'), 'Newest score displays on statistics');
}
private function getKeepResultsOption($name) {
$options = [
'best' => 0,
'newest' => 1,
'all' => 2,
];
return in_array($name, $options) ? $options[$name] : NULL;
}
private function createAndFinishAttempt($training, $module, $user, $score, $newest = FALSE) {
$attempt = UserModuleStatus::create([]);
$attempt
->setModule($module);
$attempt
->setScore($score);
$attempt
->setEvaluated(1);
$attempt
->setFinished(time());
$attempt
->save();
drupal_static_reset();
$step = opigno_learning_path_get_module_step($training
->id(), $user
->id(), $module);
if ($newest) {
$step['best score'] = $step['current attempt score'] = $score;
}
opigno_learning_path_save_step_achievements($training
->id(), $user
->id(), $step);
opigno_learning_path_save_achievements($training
->id(), $user
->id());
return $attempt;
}
}