You are here

public function OpignoModuleScoreTest::testModuleScoreDisplay in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x tests/src/Functional/OpignoModuleScoreTest.php \Drupal\Tests\opigno_learning_path\Functional\OpignoModuleScoreTest::testModuleScoreDisplay()

Test Opigno Module score on different pages (achievements, statistics etc).

Depends on keep_results option (all, newest, best).

File

tests/src/Functional/OpignoModuleScoreTest.php, line 36

Class

OpignoModuleScoreTest
Tests Opigno module score display.

Namespace

Drupal\Tests\opigno_learning_path\Functional

Code

public function testModuleScoreDisplay() {

  // Create training.
  $training = $this
    ->createGroup([
    'field_learning_path_visibility' => 'public',
  ]);

  // Add member to a training.
  $training
    ->addMember($this->groupCreator);

  // Create module and add to a training.
  $module = $this
    ->createOpignoModule([
    'keep_results' => $this
      ->getKeepResultsOption('best'),
  ]);
  $training = $this
    ->addModuleToTraining($training, $module);

  // Create finished Module attempt with score 100%.

  /* @see \Drupal\opigno_module\Entity\UserModuleStatus */
  $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');

  // Create another finished Module attempt with worse score 50%.

  /* @see \Drupal\opigno_module\Entity\UserModuleStatus */
  $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');

  // Change keep score option to show only newest (last) score.
  $module
    ->set('keep_results', $this
    ->getKeepResultsOption('newest'));
  $module
    ->save();

  // Create another finished Module attempt with worse score 50%
  // because statistics only updates
  // when user finish module after keep_results option was changed.

  /* @see \Drupal\opigno_module\Entity\UserModuleStatus */
  $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');
}