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 core_contentbank\external;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
  23  require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
  24  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  25  
  26  use core_external\external_api;
  27  
  28  /**
  29   * Content bank's copy content external function tests.
  30   *
  31   * @package    core_contentbank
  32   * @copyright  2023 Daniel Neis Araujo
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   * @coversDefaultClass \core_contentbank\external\copy_content
  35   */
  36  class copy_content_test extends \externallib_advanced_testcase {
  37  
  38      /**
  39       * Test the behaviour of copy_content() for users with permission.
  40       *
  41       * @covers ::execute
  42       */
  43      public function test_copy_content_with_permission() {
  44          global $CFG, $DB;
  45          $this->resetAfterTest();
  46  
  47          // Create users.
  48          $roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
  49          $teacher = $this->getDataGenerator()->create_user();
  50  
  51          $this->getDataGenerator()->role_assign($roleid, $teacher->id);
  52          $this->setUser($teacher);
  53  
  54          // Add some content to the content bank as teacher.
  55          $filename = 'filltheblanks.h5p';
  56          $filepath = $CFG->dirroot . '/h5p/tests/fixtures/' . $filename;
  57          $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
  58          $contents = $generator->generate_contentbank_data('contenttype_h5p', 1, $teacher->id, null, true, $filepath);
  59          $content = array_shift($contents);
  60  
  61          $oldname = $content->get_name();
  62  
  63          $newname = 'New name';
  64  
  65          // Call the WS and check the content is copied as expected.
  66          $result = copy_content::execute($content->get_id(), $newname);
  67          $result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
  68          $this->assertNotEmpty($result['id']);
  69          $record = $DB->get_record('contentbank_content', ['id' => $result['id']]);
  70          $this->assertEquals($newname, $record->name);
  71  
  72          $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
  73          $this->assertEquals($oldname, $record->name);
  74  
  75          // Call the WS using an unexisting contentid and check an error is thrown.
  76          $this->expectException(\invalid_response_exception::class);
  77          $result = copy_content::execute_returns($content->get_id() + 1, $oldname);
  78          $result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
  79          $this->assertNotEmpty($result['warnings']);
  80      }
  81  
  82      /**
  83       * Test the behaviour of copy_content() for users with and without permission.
  84       *
  85       * @covers ::execute
  86       */
  87      public function test_copy_content() {
  88          global $CFG, $DB;
  89          $this->resetAfterTest();
  90  
  91          // Create users.
  92          $course = $this->getDataGenerator()->create_course();
  93          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  94          $teacher2 = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  95          $manager = $this->getDataGenerator()->create_and_enrol($course, 'manager');
  96          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  97  
  98          // Add some content to the content bank as teacher.
  99          $coursecontext = \context_course::instance($course->id);
 100          $filename = 'filltheblanks.h5p';
 101          $filepath = $CFG->dirroot . '/h5p/tests/fixtures/' . $filename;
 102          $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
 103          $contents = $generator->generate_contentbank_data('contenttype_h5p', 1, $teacher->id, $coursecontext, true, $filepath);
 104          $content = array_shift($contents);
 105  
 106          $oldname = $content->get_name();
 107          $newname = 'New name';
 108  
 109          // Call the WS and check the teacher can copy his/her own content.
 110          $this->setUser($teacher);
 111          $result = copy_content::execute($content->get_id(), $newname);
 112          $result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
 113          $this->assertEmpty($result['warnings']);
 114          $record = $DB->get_record('contentbank_content', ['id' => $result['id']]);
 115          $this->assertEquals($newname, $record->name);
 116  
 117          // Call the WS and check the content has not been copied by the student.
 118          $this->setUser($student);
 119          $result = copy_content::execute($content->get_id(), $newname);
 120          $result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
 121          $this->assertNotEmpty($result['warnings']);
 122          $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
 123          $this->assertEquals($oldname, $record->name);
 124          $this->assertNotEquals($newname, $record->name);
 125  
 126          // Call the WS an check the content with empty name is not copied by the teacher.
 127          $this->setUser($teacher);
 128          $result = copy_content::execute($content->get_id(), '');
 129          $result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
 130          $this->assertNotEmpty($result['warnings']);
 131  
 132          // Call the WS and check a teacher cannot copy content from another teacher by default.
 133          $this->setUser($teacher2);
 134          $result = copy_content::execute($content->get_id(), 'New name 2');
 135          $result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
 136          $this->assertNotEmpty($result['warnings']);
 137  
 138          // Call the WS and check a manager can copy content from a teacher by default.
 139          $this->setUser($manager);
 140          $result = copy_content::execute($content->get_id(), $newname);
 141          $result = external_api::clean_returnvalue(copy_content::execute_returns(), $result);
 142          $this->assertEmpty($result['warnings']);
 143          $record = $DB->get_record('contentbank_content', ['id' => $result['id']]);
 144          $this->assertEquals($newname, $record->name);
 145      }
 146  }