Skip to content

Commit

Permalink
Merge branch 'MDL-68656-master' of https://github.com/catalyst/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed May 18, 2020
2 parents 2418c12 + 142a023 commit 4c6a974
Show file tree
Hide file tree
Showing 22 changed files with 152 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,29 @@
use quizaccess_seb\access_manager;
use quizaccess_seb\quiz_settings;
use quizaccess_seb\settings_provider;
use quizaccess_seb\tests\phpunit\quizaccess_seb_testcase;

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/base.php');
require_once(__DIR__ . '/test_helper_trait.php');

/**
* PHPUnit tests for the access manager.
*
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizacces_seb_access_manager_testcase extends quizaccess_seb_testcase {
class quizacces_seb_access_manager_testcase extends advanced_testcase {
use quizaccess_seb_test_helper_trait;

/**
* Called before every test.
*/
public function setUp() {
parent::setUp();

$this->resetAfterTest();
$this->setAdminUser();
$this->course = $this->getDataGenerator()->create_course();
}

/**
Expand Down Expand Up @@ -289,7 +292,7 @@ public function test_get_seb_use_type() {
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
$quizsettings = quiz_settings::get_record(['quizid' => $this->quiz->id]);
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG); // Doesn't check basic header.
$xml = file_get_contents(__DIR__ . '/sample_data/unencrypted.seb');
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
$this->create_module_test_file($xml, $this->quiz->cmid);
$quizsettings->save();
$accessmanager = $this->get_access_manager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use quizaccess_seb\tests\phpunit\quizaccess_seb_testcase;

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/base.php');
require_once(__DIR__ . '/test_helper_trait.php');

/**
* PHPUnit tests for backup and restore functionality.
*
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_seb_backup_restore_testcase extends quizaccess_seb_testcase {
class quizaccess_seb_backup_restore_testcase extends advanced_testcase {
use quizaccess_seb_test_helper_trait;


/** @var \quizaccess_seb\template $template A test template. */
protected $template;
Expand All @@ -47,7 +48,11 @@ public function setUp() {
global $USER;

parent::setUp();

$this->resetAfterTest();
$this->setAdminUser();

$this->course = $this->getDataGenerator()->create_course();
$this->template = $this->create_template();
$this->user = $USER;
}
Expand Down Expand Up @@ -202,7 +207,7 @@ public function test_backup_restore_uploaded_config() {

$expected = \quizaccess_seb\quiz_settings::get_record(['quizid' => $this->quiz->id]);
$expected->set('requiresafeexambrowser', \quizaccess_seb\settings_provider::USE_SEB_UPLOAD_CONFIG);
$xml = file_get_contents(__DIR__ . '/sample_data/unencrypted.seb');
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
$this->create_module_test_file($xml, $this->quiz->cmid);
$expected->save();

Expand Down Expand Up @@ -271,7 +276,7 @@ public function test_restore_template_to_a_different_site_when_the_same_name_but
$this->assertEquals(1, quizaccess_seb\quiz_settings::count_records());
$this->assertEquals(1, quizaccess_seb\template::count_records());

$newxml = file_get_contents($CFG->dirroot . '/mod/quiz/accessrule/seb/tests/phpunit/sample_data/simpleunencrypted.seb');
$newxml = file_get_contents($CFG->dirroot . '/mod/quiz/accessrule/seb/tests/fixtures/simpleunencrypted.seb');
$this->template->set('content', $newxml);
$this->template->save();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function test_config_key_hash_generated_with_empty_string() {
* Check that the Config Key hash is not altered if the originatorVersion is present in the XML or not.
*/
public function test_presence_of_originator_version_does_not_effect_hash() {
$xmlwithoriginatorversion = file_get_contents(__DIR__ . '/sample_data/simpleunencrypted.seb');
$xmlwithoutoriginatorversion = file_get_contents(__DIR__ . '/sample_data/simpleunencryptedwithoutoriginator.seb');
$xmlwithoriginatorversion = file_get_contents(__DIR__ . '/fixtures/simpleunencrypted.seb');
$xmlwithoutoriginatorversion = file_get_contents(__DIR__ . '/fixtures/simpleunencryptedwithoutoriginator.seb');
$hashwithorigver = config_key::generate($xmlwithoriginatorversion)->get_hash();
$hashwithoutorigver = config_key::generate($xmlwithoutoriginatorversion)->get_hash();
$this->assertEquals($hashwithorigver, $hashwithoutorigver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,29 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use quizaccess_seb\tests\phpunit\quizaccess_seb_testcase;

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/base.php');
require_once(__DIR__ . '/test_helper_trait.php');

/**
* PHPUnit tests for all plugin events.
*
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_seb_event_testcase extends quizaccess_seb_testcase {
class quizaccess_seb_event_testcase extends advanced_testcase {
use quizaccess_seb_test_helper_trait;

/**
* Called before every test.
*/
public function setUp() {
parent::setUp();

$this->resetAfterTest();
$this->course = $this->getDataGenerator()->create_course();
}

/**
* Test creating the access_prevented event.
Expand Down
2 changes: 1 addition & 1 deletion mod/quiz/accessrule/seb/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function create_template(array $data) {

if (!isset($data['content'])) {
$data['content'] = file_get_contents(
$CFG->dirroot . '/mod/quiz/accessrule/seb/tests/phpunit/sample_data/unencrypted.seb'
$CFG->dirroot . '/mod/quiz/accessrule/seb/tests/fixtures/unencrypted.seb'
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use quizaccess_seb\tests\phpunit\quizaccess_seb_testcase;

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/base.php');
require_once(__DIR__ . '/test_helper_trait.php');

/**
* PHPUnit tests for helper class.
*
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_seb_helper_testcase extends quizaccess_seb_testcase {
class quizaccess_seb_helper_testhelpertrait extends advanced_testcase {
use quizaccess_seb_test_helper_trait;

/**
* Test that we can check valid seb string.
Expand Down Expand Up @@ -80,6 +80,8 @@ public function test_get_seb_file_headers() {
* Test that the course module must exist to get a seb config file content.
*/
public function test_can_not_get_config_content_with_invalid_cmid() {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
Expand All @@ -104,6 +106,8 @@ public function test_can_not_get_config_content_with_invalid_cmid() {
* Test that the user must be enrolled to get seb config content.
*/
public function test_can_not_get_config_content_when_user_not_enrolled_in_course() {
$this->resetAfterTest();

$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$quiz = $this->create_test_quiz($course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);
Expand All @@ -120,7 +124,7 @@ public function test_can_not_get_config_content_when_user_not_enrolled_in_course
* Test that if SEB quiz settings can't be found, a seb config content won't be provided.
*/
public function test_can_not_get_config_content_if_config_not_found_for_cmid() {
global $DB;
$this->resetAfterTest();

$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
Expand All @@ -139,7 +143,7 @@ public function test_can_not_get_config_content_if_config_not_found_for_cmid() {
* That that if config is empty for a quiz, a seb config content won't be provided.
*/
public function test_can_not_get_config_content_if_config_empty() {
global $DB;
$this->resetAfterTest();

$this->setAdminUser();

Expand All @@ -159,6 +163,8 @@ public function test_can_not_get_config_content_if_config_empty() {
* Test config content is provided successfully.
*/
public function test_config_provided() {
$this->resetAfterTest();

$this->setAdminUser();

$course = $this->getDataGenerator()->create_course();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use quizaccess_seb\tests\phpunit\quizaccess_seb_testcase;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
Expand All @@ -33,21 +32,26 @@

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/base.php');
require_once(__DIR__ . '/test_helper_trait.php');

/**
* PHPUnit tests for privacy provider.
*
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_seb_provider_testcase extends quizaccess_seb_testcase {
class quizaccess_seb_provider_testcase extends advanced_testcase {
use quizaccess_seb_test_helper_trait;

/**
* Setup the user, the quiz and ensure that the user is the last user to modify the SEB quiz settings.
*/
public function setup_test_data() {
$this->resetAfterTest();

$this->setAdminUser();

$this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);

$this->user = $this->getDataGenerator()->create_user();
Expand Down Expand Up @@ -79,6 +83,8 @@ public function test_get_contexts_for_userid() {
* That that no module context is found for a user who has not modified any quiz settings.
*/
public function test_get_no_contexts_for_userid() {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();
$contexts = provider::get_contexts_for_userid($user->id);
$contextids = $contexts->get_contextids();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ public function test_export_to_json($xml, $expectedjson) {
* Test that the xml is exported to JSON from a real SEB config file. Expected JSON extracted from SEB logs.
*/
public function test_export_to_json_full_file() {
$xml = file_get_contents(__DIR__ . '/sample_data/unencrypted_mac_001.seb');
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted_mac_001.seb');
$plist = new property_list($xml);
$plist->delete_element('originatorVersion'); // JSON should not contain originatorVersion key.
$generatedjson = $plist->to_json();
$json = trim(file_get_contents(__DIR__ . '/sample_data/JSON_unencrypted_mac_001.txt'));
$json = trim(file_get_contents(__DIR__ . '/fixtures/JSON_unencrypted_mac_001.txt'));
$this->assertEquals($json, $generatedjson);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@

use quizaccess_seb\quiz_settings;
use quizaccess_seb\settings_provider;
use quizaccess_seb\tests\phpunit\quizaccess_seb_testcase;

defined('MOODLE_INTERNAL') || die();

require_once(__DIR__ . '/base.php');
require_once(__DIR__ . '/test_helper_trait.php');

/**
* PHPUnit tests for quiz_settings class.
*
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_seb_quiz_settings_testcase extends quizaccess_seb_testcase {
class quizaccess_seb_quiz_settings_testcase extends advanced_testcase {
use quizaccess_seb_test_helper_trait;

/** @var context_module $context Test context. */
protected $context;
Expand All @@ -50,7 +51,10 @@ class quizaccess_seb_quiz_settings_testcase extends quizaccess_seb_testcase {
public function setUp() {
parent::setUp();

$this->resetAfterTest();

$this->setAdminUser();
$this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->create_module('quiz', [
'course' => $this->course->id,
'seb_requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY,
Expand Down Expand Up @@ -493,7 +497,7 @@ public function test_templateid_set_correctly_when_save_settings() {
$this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_TEMPLATE, $templateid);

// Case for USE_SEB_UPLOAD_CONFIG, ensure template id reverts to 0.
$xml = file_get_contents(__DIR__ . '/sample_data/unencrypted.seb');
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
$this->create_module_test_file($xml, $this->quiz->cmid);
$this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_UPLOAD_CONFIG);
$quizsettings = quiz_settings::get_record(['quizid' => $this->quiz->id]);
Expand Down Expand Up @@ -693,7 +697,7 @@ public function test_generates_config_values_as_null_when_expected() {
$this->assertNull($quizsettings->get_config());

$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
$xml = file_get_contents(__DIR__ . '/sample_data/unencrypted.seb');
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
$this->create_module_test_file($xml, $this->quiz->cmid);
$quizsettings->save();
$quizsettings = quiz_settings::get_record(['quizid' => $this->quiz->id]);
Expand Down
Loading

0 comments on commit 4c6a974

Please sign in to comment.