Skip to content

Commit

Permalink
iss1357 - Add tests. Fix mustache.
Browse files Browse the repository at this point in the history
  • Loading branch information
EJMFarrow committed Jan 17, 2025
1 parent 9c00c34 commit ebf0387
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
4 changes: 2 additions & 2 deletions templates/questionlibrary.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@
</div>
<div class="col-sm-6 col-lg-5">
<div class="btn-group" role="group" style="float:right">
<button class="btn btn-primary library-import-link""
<button class="btn btn-primary library-import-link"
type="button" data-filepath="{{path}}" disabled>
{{#str}} stack_library_import, qtype_stack {{/str}}
</button>
<button class="btn btn-primary library-import-link-folder""
<button class="btn btn-primary library-import-link-folder"
type="button" data-filepath="{{path}}" disabled>
{{#str}} stack_library_import_folder, qtype_stack {{/str}}
</button>
Expand Down
20 changes: 20 additions & 0 deletions tests/behat/library.feature
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ Feature: Test STACK library
And I click on "Return to question bank" "link"
Then I should see "CR-Diff-02-linearity-1.b"

@javascript @current
Scenario: Import a question folder starting from question bank.
When I am on the "Course 1" "core_question > course question bank" page logged in as "teacher"
And I click on "Create a new question" "button"
And I set the field "item_qtype_stack" to "1"
And I press "submitbutton"
And I click on "STACK question library" "link"
Then I should see "Test questions"
And I should not see "Question variables"
And I click on "Calculus-Refresher" "button"
And I click on "CR_Diff_02" "button"
And I click on "CR-Diff-02-linearity-1-b.xml" "button"
And I should see "Differentiate \[{@p@}\] with respect to {@v@}. [[input:ans1]]"
And I click on "Import folder" "button"
And I click on "Return to question bank" "link"
Then I should see "CR-Diff-02-linearity-1.a"
And I should see "CR-Diff-02-linearity-1.b"
And I should see "CR-Diff-02-linearity-2.a"
And I should see "CR-Diff-02-linearity-5.b"

@javascript
Scenario: Import a question starting from quiz in Moodle < 4.3.
Given the site is running Moodle version 4.2 or lower
Expand Down
46 changes: 42 additions & 4 deletions tests/library_import_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function setUp(): void {
/**
* Test the library_import function when capabilities are present.
*/
public function x_test_capabilities(): void {
public function test_capabilities(): void {
global $DB;
// Set the required capabilities - webservice access and export rights on course.
$context = context_course::instance($this->course->id);
Expand All @@ -95,7 +95,7 @@ public function x_test_capabilities(): void {
/**
* Test the library_import function fails when not logged in.
*/
public function x_test_not_logged_in(): void {
public function test_not_logged_in(): void {
global $DB;
$this->setUser();
$this->expectException(require_login_exception::class);
Expand All @@ -107,7 +107,7 @@ public function x_test_not_logged_in(): void {
/**
* Test the library_import function fails when no capability to add questions assigned.
*/
public function x_test_no_access(): void {
public function test_no_access(): void {
global $DB;
$context = context_course::instance($this->course->id);
$teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'teacher']);
Expand All @@ -121,7 +121,7 @@ public function x_test_no_access(): void {
/**
* Test the library_import function fails when user has no access to supplied context.
*/
public function x_test_export_capability(): void {
public function test_export_capability(): void {
$this->expectException(require_login_exception::class);
$this->expectExceptionMessage('Not enrolled');
library_import::import_execute($this->qcategory->id, $this->filepath, false);
Expand Down Expand Up @@ -161,4 +161,42 @@ public function test_library_import(): void {
$dbquestion = $DB->get_record('question', ['name' => 'CR-Diff-01-basic-1.e'], '*', MUST_EXIST);
$this->assertEquals($dbquestion->id, $returnvalue[0]['questionid']);
}

/**
* Test output of library_import function for an entire folder.
*/
public function test_library_import_folder(): void {
global $DB;
// Set the required capabilities - webservice access and export rights on course.
$context = context_course::instance($this->course->id);
$managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
role_assign($managerroleid, $this->user->id, $context->id);
$sink = $this->redirectEvents();

$returnvalue = library_import::import_execute($this->qcategory->id, $this->filepath, true);

// We need to execute the return values cleaning process to simulate
// the web service server.
$returnvalue = external_api::clean_returnvalue(
library_import::import_execute_returns(),
$returnvalue
);

$this->assertEquals(18, count($returnvalue));
$this->assertEquals(true, $returnvalue[0]['success']);
$this->assertEquals('CR-Diff-01-basic-1.b', $returnvalue[0]['questionname']);
$this->assertEquals('CR-Diff-01-basic-1-b.xml', $returnvalue[0]['filename']);
$this->assertEquals(true, $returnvalue[0]['isstack']);

$events = $sink->get_events();
$this->assertEquals(count($events), 19);
$this->assertInstanceOf('\core\event\question_created', $events['0']);
$this->assertInstanceOf('\core\event\question_created', $events['17']);
$this->assertInstanceOf('\core\event\questions_imported', $events['18']);

$dbquestion = $DB->get_record('question', ['name' => 'CR-Diff-01-basic-1.b'], '*', MUST_EXIST);
$this->assertEquals($dbquestion->id, $returnvalue[0]['questionid']);
$dbquestions = $DB->get_records('question', ['qtype' => 'stack'], '', 'id');
$this->assertEquals(18, count($dbquestions));
}
}

0 comments on commit ebf0387

Please sign in to comment.