QuizTestBase.php in Quiz 8.6
File
tests/src/Functional/QuizTestBase.php
View source
<?php
namespace Drupal\Tests\quiz\Functional;
use Drupal\Core\Config\Development\ConfigSchemaChecker;
use Drupal\quiz\Entity\Quiz;
use Drupal\quiz\Entity\QuizQuestion;
use Drupal\Tests\BrowserTestBase;
use function quiz_get_feedback_options;
abstract class QuizTestBase extends BrowserTestBase {
protected $strictConfigSchema = FALSE;
protected $defaultTheme = 'stark';
public static $modules = array(
'quiz',
);
public function setUp($admin_permissions = array(), $user_permissions = array()) {
parent::setUp();
$admin_permissions[] = 'administer quiz configuration';
$admin_permissions[] = 'administer quiz_question';
$admin_permissions[] = 'administer quiz_result_answer';
$admin_permissions[] = 'administer quiz_result';
$admin_permissions[] = 'administer quiz';
if ($this->profile == 'standard') {
$admin_permissions[] = 'use text format basic_html';
$admin_permissions[] = 'use text format restricted_html';
$admin_permissions[] = 'use text format full_html';
$user_permissions[] = 'use text format basic_html';
$user_permissions[] = 'use text format restricted_html';
}
$user_permissions[] = 'access quiz';
$user_permissions[] = 'view any quiz';
$user_permissions[] = 'view own quiz_result';
$this->admin = $this
->drupalCreateUser(array_unique($admin_permissions));
$this->user = $this
->drupalCreateUser(array_unique($user_permissions));
}
public function linkQuestionToQuiz(QuizQuestion $quiz_question, Quiz $quiz = NULL) {
static $weight = 0;
if (!$quiz) {
$quiz = $this
->createQuiz();
}
$quiz
->addQuestion($quiz_question)
->set('weight', $weight)
->save();
$weight++;
return $quiz;
}
public function createQuiz($settings = array()) {
$settings += array(
'title' => 'Quiz',
'body' => 'Quiz description',
'type' => 'quiz',
'result_type' => 'quiz_result',
'review_options' => array(
'end' => array_combine(array_keys(quiz_get_feedback_options()), array_keys(quiz_get_feedback_options())),
),
);
$quiz = Quiz::create($settings);
$quiz
->save();
return $quiz;
}
public function createQuestion($settings = array()) {
$question = QuizQuestion::create($settings);
$question
->save();
return $question;
}
}