QuizTestBase.php in Quiz 6.x
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';
protected static $modules = [
'quiz',
'quiz_test',
];
public function setUp($admin_permissions = [], $user_permissions = []) {
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';
$admin_permissions[] = 'update any quiz_result';
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 = []) {
$settings += [
'title' => 'Quiz',
'body' => 'Quiz description',
'type' => 'quiz',
'result_type' => 'quiz_result',
'review_options' => [
'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 = []) {
$question = QuizQuestion::create($settings);
$question
->save();
return $question;
}
}