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_module\Entity\OpignoModule;
use Drupal\user\UserInterface;
class OpignoLinkConditionTest extends LearningPathBrowserTestBase {
protected function setUp() {
parent::setUp();
\Drupal::service('module_installer')
->install([
'opigno_messaging',
'opigno_learning_path_test',
'search',
]);
\Drupal::service('theme_handler')
->install([
'platon',
]);
\Drupal::service('theme_handler')
->setDefault('platon');
}
public function testLinkCondition() {
$training = $this
->createGroup([
'field_learning_path_visibility' => 'public',
]);
$training
->addMember($this->groupCreator);
$parent_module = $this
->createOpignoModule();
$child_module_1 = $this
->createOpignoModule();
$child_module_2 = $this
->createOpignoModule();
$this
->addModuleToTraining($training, $parent_module);
$this
->addModuleToTraining($training, $child_module_1);
$this
->addModuleToTraining($training, $child_module_2);
$content_parrent_module = OpignoGroupManagedContent::loadByProperties([
'group_content_type_id' => 'ContentTypeModule',
'entity_id' => $parent_module
->id(),
]);
$content_parrent_module = reset($content_parrent_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_parrent_module
->id(), $content_child_module_1
->id(), 50);
$link_1
->save();
$link_2 = OpignoGroupManagedLink::createWithValues($content_child_module_2
->getGroupId(), $content_parrent_module
->id(), $content_child_module_2
->id(), 0);
$link_2
->save();
$this
->drupalGet('/group/' . $training
->id() . '/learning-path/start');
$this
->assertSession()
->titleEquals($parent_module
->getName() . ' | Drupal');
$this
->finishCurrentModuleAttempt($this, $training, $parent_module, $this->groupCreator, 0);
$this
->drupalGet('/group/' . $training
->id() . '/learning-path/nextstep/' . $content_parrent_module
->id());
$this
->assertSession()
->titleEquals($child_module_2
->getName() . ' | Drupal');
$this
->finishCurrentModuleAttempt($this, $training, $child_module_2, $this->groupCreator, 0);
$this
->finishCurrentModuleAttempt($this, $training, $parent_module, $this->groupCreator, 60);
$this
->drupalGet('/group/' . $training
->id() . '/learning-path/nextstep/' . $content_parrent_module
->id());
$this
->assertSession()
->titleEquals($child_module_1
->getName() . ' | Drupal');
$this
->finishCurrentModuleAttempt($this, $training, $child_module_1, $this->groupCreator, 100);
}
private function finishCurrentModuleAttempt($stack, $training, OpignoModule $module, UserInterface $user, $score) {
$current_attempt = $module
->getModuleActiveAttempt($user);
$current_attempt
->setModule($module);
$current_attempt
->setScore($score);
$current_attempt
->setMaxScore(70);
$current_attempt
->setEvaluated(1);
$current_attempt
->setFinished(time());
$current_attempt
->save();
$stack
->resetAll();
$step = opigno_learning_path_get_module_step($training
->id(), $user
->id(), $module);
$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());
}
}