Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace mod_lti\external;
  18  
  19  use core_external\external_api;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  
  25  require_once($CFG->dirroot . '/mod/lti/tests/mod_lti_testcase.php');
  26  
  27  /**
  28   * PHPUnit tests for toggle_showinactivitychooser external function.
  29   *
  30   * @package    mod_lti
  31   * @copyright  2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   * @coversDefaultClass \mod_lti\external\toggle_showinactivitychooser
  34   */
  35  class toggle_showinactivitychooser_test extends \mod_lti_testcase {
  36  
  37      /**
  38       * Test toggle_showinactivitychooser for course tool.
  39       * @covers ::execute
  40       */
  41      public function test_toggle_showinactivitychooser_course_tool() {
  42          global $DB;
  43          $this->resetAfterTest();
  44  
  45          $course = $this->getDataGenerator()->create_course();
  46          $editingteacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  47          $this->setUser($editingteacher);
  48  
  49          $typeid = lti_add_type(
  50              (object) [
  51                  'state' => LTI_TOOL_STATE_CONFIGURED,
  52                  'course' => $course->id,
  53                  'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER
  54              ],
  55              (object) [
  56                  'lti_typename' => "My course tool",
  57                  'lti_toolurl' => 'http://example.com',
  58                  'lti_ltiversion' => 'LTI-1p0',
  59                  'lti_coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER
  60              ]
  61          );
  62          $result = toggle_showinactivitychooser::execute($typeid, $course->id, false);
  63          $result = external_api::clean_returnvalue(toggle_showinactivitychooser::execute_returns(), $result);
  64          $this->assertTrue($result);
  65  
  66          $sql = "SELECT lt.coursevisible coursevisible
  67                    FROM {lti_types} lt
  68                   WHERE lt.id = ?";
  69          $actual = $DB->get_record_sql($sql, [$typeid]);
  70          $this->assertEquals(LTI_COURSEVISIBLE_PRECONFIGURED, $actual->coursevisible);
  71  
  72          $result = toggle_showinactivitychooser::execute($typeid, $course->id, true);
  73          $result = external_api::clean_returnvalue(toggle_showinactivitychooser::execute_returns(), $result);
  74          $this->assertTrue($result);
  75          $actual = $DB->get_record_sql($sql, [$typeid]);
  76          $this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible);
  77      }
  78  
  79      /**
  80       * Test toggle_showinactivitychooser for site tool.
  81       * @covers ::execute
  82       */
  83      public function test_toggle_showinactivitychooser_site_tool() {
  84          global $DB;
  85  
  86          $this->resetAfterTest();
  87  
  88          $coursecat1 = $this->getDataGenerator()->create_category();
  89          $coursecat2 = $this->getDataGenerator()->create_category();
  90          $course = $this->getDataGenerator()->create_course(['category' => $coursecat1->id]);
  91  
  92          $editingteacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  93          $this->setUser($editingteacher);
  94  
  95          $type = $this->generate_tool_type(123); // Creates a site tool.
  96  
  97          $result = toggle_showinactivitychooser::execute($type->id, $course->id, false);
  98          $result = external_api::clean_returnvalue(toggle_showinactivitychooser::execute_returns(), $result);
  99          $this->assertTrue($result);
 100  
 101          $sql = "SELECT lt.coursevisible coursevisible1, lc.coursevisible AS coursevisible2
 102                    FROM {lti_types} lt
 103               LEFT JOIN {lti_coursevisible} lc ON lt.id = lc.typeid
 104                   WHERE lt.id = ?
 105                     AND lc.courseid = ?";
 106          $actual = $DB->get_record_sql($sql, [$type->id, $course->id]);
 107          $this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible1);
 108          $this->assertEquals(LTI_COURSEVISIBLE_PRECONFIGURED, $actual->coursevisible2);
 109  
 110          $result = toggle_showinactivitychooser::execute($type->id, $course->id, true);
 111          $result = external_api::clean_returnvalue(toggle_showinactivitychooser::execute_returns(), $result);
 112          $this->assertTrue($result);
 113  
 114          $actual = $DB->get_record_sql($sql, [$type->id, $course->id]);
 115          $this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible1);
 116          $this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible2);
 117  
 118          $ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
 119          $ltigenerator->create_tool_types([
 120              'name' => 'site tool preconfigured and activity chooser, restricted to category 1',
 121              'baseurl' => 'http://example.com/tool/1',
 122              'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER,
 123              'state' => LTI_TOOL_STATE_CONFIGURED,
 124              'lti_coursecategories' => $coursecat1->id
 125          ]);
 126          $tool = $DB->get_record('lti_types', ['name' => 'site tool preconfigured and activity chooser, restricted to category 1']);
 127          $result = toggle_showinactivitychooser::execute($tool->id, $course->id, false);
 128          $result = external_api::clean_returnvalue(toggle_showinactivitychooser::execute_returns(), $result);
 129          $this->assertTrue($result);
 130  
 131          $actual = $DB->get_record_sql($sql, [$tool->id, $course->id]);
 132          $this->assertEquals(LTI_COURSEVISIBLE_ACTIVITYCHOOSER, $actual->coursevisible1);
 133          $this->assertEquals(LTI_COURSEVISIBLE_PRECONFIGURED, $actual->coursevisible2);
 134  
 135          $ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
 136          $ltigenerator->create_tool_types([
 137              'name' => 'site tool preconfigured and activity chooser, restricted to category 2',
 138              'baseurl' => 'http://example.com/tool/1',
 139              'coursevisible' => LTI_COURSEVISIBLE_ACTIVITYCHOOSER,
 140              'state' => LTI_TOOL_STATE_CONFIGURED,
 141              'lti_coursecategories' => $coursecat2->id
 142          ]);
 143          $tool = $DB->get_record('lti_types', ['name' => 'site tool preconfigured and activity chooser, restricted to category 2']);
 144          $this->expectException('moodle_exception');
 145          $this->expectExceptionMessage('You are not allowed to change this setting for this tool.');
 146          toggle_showinactivitychooser::execute($tool->id, $course->id, true);
 147  
 148          $ltigenerator = $this->getDataGenerator()->get_plugin_generator('mod_lti');
 149          $ltigenerator->create_tool_types([
 150              'name' => 'site tool dont show',
 151              'baseurl' => 'http://example.com/tool/1',
 152              'coursevisible' => LTI_COURSEVISIBLE_NO,
 153              'state' => LTI_TOOL_STATE_CONFIGURED,
 154          ]);
 155          $tool = $DB->get_record('lti_types', ['name' => 'site tool dont show']);
 156          $result = toggle_showinactivitychooser::execute($tool->id, $course->id, false);
 157          $result = external_api::clean_returnvalue(toggle_showinactivitychooser::execute_returns(), $result);
 158          $this->assertFalse($result);
 159  
 160      }
 161  
 162  }