View source
<?php
namespace Drupal\Tests\opigno_learning_path\Functional;
use Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent;
use Drupal\opigno_group_manager\Entity\OpignoGroupManagedLink;
use Drupal\opigno_learning_path\Controller\LearningPathStepsController;
use Drupal\opigno_module\Entity\UserModuleStatus;
class TrainingCompleteTest extends LearningPathBrowserTestBase {
public function testTrainingComplete() {
$training = $this
->createGroup([
'field_learning_path_visibility' => 'public',
'field_learning_path_published' => TRUE,
]);
$training
->addMember($this->groupCreator);
$mandatory_module = $this
->createOpignoModule();
$child_module_1 = $this
->createOpignoModule();
$child_module_2 = $this
->createOpignoModule();
$this
->addModuleToTraining($training, $mandatory_module);
$this
->addModuleToTraining($training, $child_module_1, 0);
$this
->addModuleToTraining($training, $child_module_2, 0);
$content_mandatory_module = OpignoGroupManagedContent::loadByProperties([
'group_content_type_id' => 'ContentTypeModule',
'entity_id' => $mandatory_module
->id(),
]);
$content_mandatory_module = reset($content_mandatory_module);
$content_child_module_1 = OpignoGroupManagedContent::loadByProperties([
'group_content_type_id' => 'ContentTypeModule',
'entity_id' => $child_module_1
->id(),
]);
$content_child_module_1 = reset($content_child_module_1);
$content_child_module_2 = OpignoGroupManagedContent::loadByProperties([
'group_content_type_id' => 'ContentTypeModule',
'entity_id' => $child_module_2
->id(),
]);
$content_child_module_2 = reset($content_child_module_2);
$link_1 = OpignoGroupManagedLink::createWithValues($content_child_module_1
->getGroupId(), $content_mandatory_module
->id(), $content_child_module_1
->id(), 50);
$link_1
->save();
$link_2 = OpignoGroupManagedLink::createWithValues($content_child_module_2
->getGroupId(), $content_mandatory_module
->id(), $content_child_module_2
->id(), 0);
$link_2
->save();
$this
->createAnswersAndAttempt($training, $mandatory_module, $this->groupCreator, 100);
$this
->createAnswersAndAttempt($training, $child_module_1, $this->groupCreator, 100);
$is_complated = opigno_learning_path_completed_on($training
->id(), $this->groupCreator
->id());
$is_complated = $is_complated > 0;
$this
->assertTrue($is_complated, 'The training is completed.');
$this
->drupalGet('/achievements', [
'query' => [
'preload-progress' => 'true',
],
]);
$content = $this
->getSession()
->getPage()
->find('css', '.lp_summary_step_state_passed');
$this
->assertNotEmpty($content, 'Training status is "Passed" on achievements page');
}
private function createAnswersAndAttempt($training, $module, $user, $score) {
$attempt = UserModuleStatus::create([]);
$attempt
->setModule($module);
$attempt
->setScore($score);
$attempt
->setOwnerId($user
->id());
$attempt
->setEvaluated(1);
$attempt
->setFinished(time());
$attempt
->save();
$activities_ids = array_keys($module
->getModuleActivities());
if (count($activities_ids) > 0) {
$activities_storage = \Drupal::entityTypeManager()
->getStorage('opigno_activity');
$activities = $activities_storage
->loadMultiple($activities_ids);
foreach ($activities as $activity) {
$this
->createAnswer($activity, $module, $attempt, $user
->id(), $score);
}
}
drupal_static_reset();
$step = opigno_learning_path_get_module_step($training
->id(), $user
->id(), $module);
$step['completed on'] = time();
$step['passed'] = TRUE;
opigno_learning_path_save_step_achievements($training
->id(), $user
->id(), $step);
opigno_learning_path_save_achievements($training
->id(), $user
->id());
return $attempt;
}
}