MDL-78597 mod_lti: fix those tests not using lti generators

The following were added in MDL-69489 and had a few issues, which are
fixed in this patch:
Unit tests:
- were not using the existing generator.
- were making assertions inside a foreach, which doesn't test anything
if the result set returned is empty (error prone).
- were calling setAdminUser() unnecessarily.
Behat tests:
- the restricttocategory.feature file used manual steps to setup site
tools.
This commit is contained in:
Jake Dallimore 2023-08-29 11:45:36 +08:00
parent c497672778
commit a23abb0538
No known key found for this signature in database
2 changed files with 84 additions and 82 deletions

View file

@ -1,4 +1,4 @@
@mod @mod_lti @javascript @mod @mod_lti
Feature: Make an LTI only available to specific course categories Feature: Make an LTI only available to specific course categories
In order to restrict which courses a tool can be used in In order to restrict which courses a tool can be used in
As an administrator As an administrator
@ -24,48 +24,34 @@ Feature: Make an LTI only available to specific course categories
| teacher1 | C1 | editingteacher | | teacher1 | C1 | editingteacher |
| teacher1 | C2 | editingteacher | | teacher1 | C2 | editingteacher |
| teacher1 | C3 | editingteacher | | teacher1 | C3 | editingteacher |
And I log in as "admin" And the following "mod_lti > tool types" exist:
And I navigate to "Plugins > Activity modules > External tool > Manage tools" in site administration | name | description | baseurl | coursevisible | state | lti_coursecategories |
And I follow "Manage preconfigured tools" | Teaching Tool 1 | Tool 1 description | /mod/lti/tests/fixtures/tool_provider.php | 1 | 1 | catb |
And I follow "Add preconfigured tool" | Teaching Tool 2 | Tool 2 description | /mod/lti/tests/fixtures/tool_provider.php | 2 | 1 | catca |
And I expand all fieldsets
And I set the following fields to these values:
| Tool name | Teaching Tool 1 |
| Tool configuration usage | Show as preconfigured tool when adding an external tool |
| catb | 1 |
And I set the field "Tool URL" to local url "/mod/lti/tests/fixtures/tool_provider.php"
And I press "Save changes"
And I navigate to "Plugins > Activity modules > External tool > Manage tools" in site administration
And I follow "Manage preconfigured tools"
And I follow "Add preconfigured tool"
And I expand all fieldsets
And I click on "cata" "link"
And I set the following fields to these values:
| Tool name | Teaching Tool 2 |
| Tool configuration usage | Show in activity chooser and as a preconfigured tool |
| catca | 1 |
And I set the field "Tool URL" to local url "/mod/lti/tests/fixtures/tool_provider.php"
And I press "Save changes"
Scenario: Tool is set to "Show as preconfigured tool when adding an external tool" on parent category Scenario: Tool is set to "Show as preconfigured tool when adding an external tool" on parent category
Given I log in as "teacher1" Given I am on the "Course 2" course page logged in as teacher1
And I am on "Course 2" course homepage with editing mode on When I navigate to "LTI External tools" in current page administration
And I add a "External tool" to section "1" Then I should see "Teaching Tool 1" in the "reportbuilder-table" "table"
When I click on "Preconfigured tool" "select" And I should not see "Teaching Tool 2" in the "reportbuilder-table" "table"
Then I should see "Teaching Tool 1"
@javascript
Scenario: Tool is set to "Show in activity chooser and as preconfigured tool" on child category Scenario: Tool is set to "Show in activity chooser and as preconfigured tool" on child category
Given I log in as "teacher1" Given I log in as "teacher1"
When I am on "Course 3" course homepage with editing mode on When I am on "Course 3" course homepage with editing mode on
And I open the activity chooser And I open the activity chooser
Then I should see "Teaching Tool 2" in the "Add an activity or resource" "dialogue" Then I should see "Teaching Tool 2" in the "Add an activity or resource" "dialogue"
And I should not see "Teaching Tool 1" in the "Add an activity or resource" "dialogue"
Scenario: Tool restrict access @javascript
Scenario: View a course in a category in which no tools are available
Given I log in as "teacher1" Given I log in as "teacher1"
When I am on "Course 1" course homepage with editing mode on When I am on "Course 1" course homepage with editing mode on
And I open the activity chooser And I open the activity chooser
Then I should not see "Teaching Tool 2" in the "Add an activity or resource" "dialogue" Then I should not see "Teaching Tool 1" in the "Add an activity or resource" "dialogue"
And I should not see "Teaching Tool 2" in the "Add an activity or resource" "dialogue"
@javascript
Scenario: Editing and saving selected parent / child categories Scenario: Editing and saving selected parent / child categories
Given I log in as "admin" Given I log in as "admin"
And I navigate to "Plugins > Activity modules > External tool > Manage tools" in site administration And I navigate to "Plugins > Activity modules > External tool > Manage tools" in site administration

View file

@ -631,36 +631,33 @@ class locallib_test extends mod_lti_testcase {
public function test_lti_get_tools_by_domain() { public function test_lti_get_tools_by_domain() {
$this->resetAfterTest(); $this->resetAfterTest();
$this->setAdminUser(); /** @var \mod_lti_generator $ltigenerator */
$ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
// Create a tool type with good domain. // Create a tool type with good domain.
$type = new \stdClass(); $ltigenerator->create_tool_types([
$data = new \stdClass(); 'name' => 'Test tool 1',
$data->lti_contentitem = true; 'description' => 'Good example description',
$type->state = LTI_TOOL_STATE_CONFIGURED; 'tooldomain' => 'example.com',
$type->name = "Test tool 1"; 'baseurl' => 'https://example.com/i/am/?where=here',
$type->description = "Good example description"; 'state' => LTI_TOOL_STATE_CONFIGURED
$type->tooldomain = 'example.com'; ]);
$type->baseurl = 'https://example.com/i/am/?where=here';
$type->course = SITEID;
$typeid = lti_add_type($type, $data);
// Create a tool type with bad domain. // Create a tool type with bad domain.
$type = new \stdClass(); $ltigenerator->create_tool_types([
$data = new \stdClass(); 'name' => 'Test tool 2',
$data->lti_contentitem = true; 'description' => 'Bad example description',
$type->state = LTI_TOOL_STATE_CONFIGURED; 'tooldomain' => 'badexample.com',
$type->name = "Test tool 2"; 'baseurl' => 'https://badexample.com/i/am/?where=here',
$type->description = "Bad example description"; 'state' => LTI_TOOL_STATE_CONFIGURED
$type->tooldomain = 'badexample.com'; ]);
$type->baseurl = 'https://badexample.com/i/am/?where=here';
$type->course = SITEID;
$typeid = lti_add_type($type, $data);
$records = lti_get_tools_by_domain('example.com', LTI_TOOL_STATE_CONFIGURED, null); $records = lti_get_tools_by_domain('example.com', LTI_TOOL_STATE_CONFIGURED);
foreach ($records as $record) { $this->assertCount(1, $records);
$this->assertEquals('example.com', $record->tooldomain); $this->assertEmpty(array_diff(
} ['https://example.com/i/am/?where=here'],
array_column($records, 'baseurl')
));
} }
/** /**
@ -671,45 +668,64 @@ class locallib_test extends mod_lti_testcase {
public function test_lti_get_tools_by_domain_restrict_types_category() { public function test_lti_get_tools_by_domain_restrict_types_category() {
$this->resetAfterTest(); $this->resetAfterTest();
$this->setAdminUser();
$coursecat1 = $this->getDataGenerator()->create_category(); $coursecat1 = $this->getDataGenerator()->create_category();
$coursecat2 = $this->getDataGenerator()->create_category(); $coursecat2 = $this->getDataGenerator()->create_category();
$course1 = $this->getDataGenerator()->create_course(['category' => $coursecat1->id]); $course1 = $this->getDataGenerator()->create_course(['category' => $coursecat1->id]);
$course2 = $this->getDataGenerator()->create_course(['category' => $coursecat2->id]); $course2 = $this->getDataGenerator()->create_course(['category' => $coursecat2->id]);
/** @var \mod_lti_generator $ltigenerator */
$ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
// Create a tool type with domain restricting to a category1. // Create a tool type with domain restricting to a category1.
$type = new \stdClass(); $ltigenerator->create_tool_types([
$data = new \stdClass(); 'name' => 'Test tool 1',
$data->lti_contentitem = true; 'description' => 'Good example description',
$type->state = LTI_TOOL_STATE_CONFIGURED; 'tooldomain' => 'exampleone.com',
$type->name = "Test tool 1"; 'baseurl' => 'https://exampleone.com/tool/1',
$type->description = "Good example description"; 'state' => LTI_TOOL_STATE_CONFIGURED,
$type->tooldomain = 'exampleone.com'; 'lti_coursecategories' => $coursecat1->id
$type->baseurl = 'https://exampleone.com/i/am/?where=here'; ]);
$type->course = $course1->id;
$typeid = lti_add_type($type, $data); // Create another tool type using the same domain, restricted to category2.
$typecategoryid = lti_type_add_categories($typeid, $coursecat1->id); $ltigenerator->create_tool_types([
'name' => 'Test tool 1',
'description' => 'Good example description',
'tooldomain' => 'exampleone.com',
'baseurl' => 'https://exampleone.com/tool/2',
'state' => LTI_TOOL_STATE_CONFIGURED,
'lti_coursecategories' => $coursecat2->id
]);
// Create a tool type with domain restricting to a category2. // Create a tool type with domain restricting to a category2.
$type = new \stdClass(); $ltigenerator->create_tool_types([
$data = new \stdClass(); 'name' => 'Test tool 2',
$data->lti_contentitem = true; 'description' => 'Good example description',
$type->state = LTI_TOOL_STATE_CONFIGURED; 'tooldomain' => 'exampletwo.com',
$type->name = "Test tool 2"; 'baseurl' => 'https://exampletwo.com/tool/3',
$type->description = "Good example description"; 'state' => LTI_TOOL_STATE_CONFIGURED,
$type->tooldomain = 'exampletwo.com'; 'lti_coursecategories' => $coursecat2->id
$type->baseurl = 'https://exampletwo.com/i/am/?where=here'; ]);
$type->course = $course2->id;
$typeid = lti_add_type($type, $data);
$typecategoryid = lti_type_add_categories($typeid, $coursecat2->id);
// Get tool types for domain 'exampleone' in course 1 and verify only the one result under course category 1 is included.
$records = lti_get_tools_by_domain('exampleone.com', LTI_TOOL_STATE_CONFIGURED, $course1->id); $records = lti_get_tools_by_domain('exampleone.com', LTI_TOOL_STATE_CONFIGURED, $course1->id);
foreach ($records as $record) { $this->assertCount(1, $records);
$this->assertEquals('exampleone.com', $record->tooldomain); $this->assertEmpty(array_diff(
} ['https://exampleone.com/tool/1'],
array_column($records, 'baseurl')
));
// Get tool types for domain 'exampleone' in course 2 and verify only the one result under course category 2 is included.
$records = lti_get_tools_by_domain('exampleone.com', LTI_TOOL_STATE_CONFIGURED, $course2->id);
$this->assertCount(1, $records);
$this->assertEmpty(array_diff(
['https://exampleone.com/tool/2'],
array_column($records, 'baseurl')
));
// Get tool types for domain 'exampletwo' in course 1 and verify that no results are found.
$records = lti_get_tools_by_domain('exampletwo.com', LTI_TOOL_STATE_CONFIGURED, $course1->id);
$this->assertCount(0, $records);
} }
/** /**