diff --git a/mod/lti/tests/behat/restricttocategory.feature b/mod/lti/tests/behat/restricttocategory.feature index b9ae7c4ab70..a006548d499 100644 --- a/mod/lti/tests/behat/restricttocategory.feature +++ b/mod/lti/tests/behat/restricttocategory.feature @@ -1,4 +1,4 @@ -@mod @mod_lti @javascript +@mod @mod_lti Feature: Make an LTI only available to specific course categories In order to restrict which courses a tool can be used in As an administrator @@ -24,48 +24,34 @@ Feature: Make an LTI only available to specific course categories | teacher1 | C1 | editingteacher | | teacher1 | C2 | editingteacher | | teacher1 | C3 | editingteacher | - And I log in as "admin" - 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 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" + And the following "mod_lti > tool types" exist: + | name | description | baseurl | coursevisible | state | lti_coursecategories | + | Teaching Tool 1 | Tool 1 description | /mod/lti/tests/fixtures/tool_provider.php | 1 | 1 | catb | + | Teaching Tool 2 | Tool 2 description | /mod/lti/tests/fixtures/tool_provider.php | 2 | 1 | catca | Scenario: Tool is set to "Show as preconfigured tool when adding an external tool" on parent category - Given I log in as "teacher1" - And I am on "Course 2" course homepage with editing mode on - And I add a "External tool" to section "1" - When I click on "Preconfigured tool" "select" - Then I should see "Teaching Tool 1" + Given I am on the "Course 2" course page logged in as teacher1 + When I navigate to "LTI External tools" in current page administration + Then I should see "Teaching Tool 1" in the "reportbuilder-table" "table" + And I should not see "Teaching Tool 2" in the "reportbuilder-table" "table" + @javascript Scenario: Tool is set to "Show in activity chooser and as preconfigured tool" on child category Given I log in as "teacher1" When I am on "Course 3" course homepage with editing mode on And I open the activity chooser 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" When I am on "Course 1" course homepage with editing mode on 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 Given I log in as "admin" And I navigate to "Plugins > Activity modules > External tool > Manage tools" in site administration diff --git a/mod/lti/tests/locallib_test.php b/mod/lti/tests/locallib_test.php index 8853c4be68b..48a9f58fa22 100644 --- a/mod/lti/tests/locallib_test.php +++ b/mod/lti/tests/locallib_test.php @@ -631,36 +631,33 @@ class locallib_test extends mod_lti_testcase { public function test_lti_get_tools_by_domain() { $this->resetAfterTest(); - $this->setAdminUser(); + /** @var \mod_lti_generator $ltigenerator */ + $ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti'); // Create a tool type with good domain. - $type = new \stdClass(); - $data = new \stdClass(); - $data->lti_contentitem = true; - $type->state = LTI_TOOL_STATE_CONFIGURED; - $type->name = "Test tool 1"; - $type->description = "Good example description"; - $type->tooldomain = 'example.com'; - $type->baseurl = 'https://example.com/i/am/?where=here'; - $type->course = SITEID; - $typeid = lti_add_type($type, $data); + $ltigenerator->create_tool_types([ + 'name' => 'Test tool 1', + 'description' => 'Good example description', + 'tooldomain' => 'example.com', + 'baseurl' => 'https://example.com/i/am/?where=here', + 'state' => LTI_TOOL_STATE_CONFIGURED + ]); // Create a tool type with bad domain. - $type = new \stdClass(); - $data = new \stdClass(); - $data->lti_contentitem = true; - $type->state = LTI_TOOL_STATE_CONFIGURED; - $type->name = "Test tool 2"; - $type->description = "Bad example description"; - $type->tooldomain = 'badexample.com'; - $type->baseurl = 'https://badexample.com/i/am/?where=here'; - $type->course = SITEID; - $typeid = lti_add_type($type, $data); + $ltigenerator->create_tool_types([ + 'name' => 'Test tool 2', + 'description' => 'Bad example description', + 'tooldomain' => 'badexample.com', + 'baseurl' => 'https://badexample.com/i/am/?where=here', + 'state' => LTI_TOOL_STATE_CONFIGURED + ]); - $records = lti_get_tools_by_domain('example.com', LTI_TOOL_STATE_CONFIGURED, null); - foreach ($records as $record) { - $this->assertEquals('example.com', $record->tooldomain); - } + $records = lti_get_tools_by_domain('example.com', LTI_TOOL_STATE_CONFIGURED); + $this->assertCount(1, $records); + $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() { $this->resetAfterTest(); - $this->setAdminUser(); - $coursecat1 = $this->getDataGenerator()->create_category(); $coursecat2 = $this->getDataGenerator()->create_category(); $course1 = $this->getDataGenerator()->create_course(['category' => $coursecat1->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. - $type = new \stdClass(); - $data = new \stdClass(); - $data->lti_contentitem = true; - $type->state = LTI_TOOL_STATE_CONFIGURED; - $type->name = "Test tool 1"; - $type->description = "Good example description"; - $type->tooldomain = 'exampleone.com'; - $type->baseurl = 'https://exampleone.com/i/am/?where=here'; - $type->course = $course1->id; - $typeid = lti_add_type($type, $data); - $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/1', + 'state' => LTI_TOOL_STATE_CONFIGURED, + 'lti_coursecategories' => $coursecat1->id + ]); + + // Create another tool type using the same domain, restricted to category2. + $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. - $type = new \stdClass(); - $data = new \stdClass(); - $data->lti_contentitem = true; - $type->state = LTI_TOOL_STATE_CONFIGURED; - $type->name = "Test tool 2"; - $type->description = "Good example description"; - $type->tooldomain = 'exampletwo.com'; - $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); + $ltigenerator->create_tool_types([ + 'name' => 'Test tool 2', + 'description' => 'Good example description', + 'tooldomain' => 'exampletwo.com', + 'baseurl' => 'https://exampletwo.com/tool/3', + 'state' => LTI_TOOL_STATE_CONFIGURED, + 'lti_coursecategories' => $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); - foreach ($records as $record) { - $this->assertEquals('exampleone.com', $record->tooldomain); - } + $this->assertCount(1, $records); + $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); } /**