CourseObjectQuizTest.php in Course 3.x
File
modules/course_quiz/tests/src/Functional/CourseObjectQuizTest.php
View source
<?php
namespace Drupal\Tests\course_quiz\Functional;
use Drupal\course\Entity\CourseObject;
use Drupal\quiz\Entity\Quiz;
use Drupal\quiz\Entity\QuizQuestion;
use Drupal\Tests\course\Functional\CourseTestBase;
class CourseObjectQuizTest extends CourseTestBase {
protected $strictConfigSchema = FALSE;
protected static $modules = [
'course_quiz',
'quiz_truefalse',
];
function setUp() {
parent::setUp();
$this->quiz_admin = $this
->drupalCreateUser(array(
'access quiz',
'create truefalse quiz_question',
'update truefalse quiz_question',
'create quiz quiz',
'update any quiz quiz',
));
}
function testQuizCourseObject() {
$this
->drupalLogin($this->quiz_admin);
$course = $this
->createCourse();
$co1 = CourseObject::create([
'object_type' => 'quiz',
]);
$co1
->setCourse($course
->id());
$co1
->setOption('passing_grade', 100);
$co1
->save();
$this
->assertTrue($co1
->getInstanceId() > 0, 'Quiz node created on course object save.');
$quiz = Quiz::load($co1
->getInstanceId());
$this
->assertNotEmpty($quiz);
$quiz_question = QuizQuestion::create([
'type' => 'truefalse',
'truefalse_correct' => 1,
]);
$quiz_question
->save();
$quiz
->addQuestion($quiz_question);
$course
->enroll($this->quiz_admin);
$this
->drupalGet($quiz
->toUrl('take'));
$this
->drupalPostForm(NULL, array(
'question[1][answer]' => 0,
), t('Finish'));
\Drupal::entityTypeManager()
->getStorage('course_object_fulfillment')
->resetCache();
$this
->assertFalse($co1
->getFulfillment($this->quiz_admin)
->isComplete(), 'Check that quiz fulfillment is not complete after fail.');
$this
->drupalGet($quiz
->toUrl('take'));
$this
->drupalPostForm(NULL, array(
'question[1][answer]' => 1,
), t('Finish'));
\Drupal::entityTypeManager()
->getStorage('course_object_fulfillment')
->resetCache();
$this
->assertTrue($co1
->getFulfillment($this->quiz_admin)
->isComplete(), 'Check that quiz fulfillment is complete.');
}
}