Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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  /**
  18   * Core content bank external functions tests.
  19   *
  20   * @package    core_contentbank
  21   * @category   external
  22   * @copyright  2020 Sara Arjona <sara@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.9
  25   */
  26  
  27  namespace core_contentbank\external;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
  33  require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
  34  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  35  
  36  use external_api;
  37  
  38  /**
  39   * Core content bank external functions tests.
  40   *
  41   * @package    core_contentbank
  42   * @copyright  2020 Sara Arjona <sara@moodle.com>
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   * @coversDefaultClass \core_contentbank\external\rename_content
  45   */
  46  class rename_content_test extends \externallib_advanced_testcase {
  47  
  48      /**
  49       * Data provider for test_rename_content.
  50       *
  51       * @return  array
  52       */
  53      public function rename_content_provider() {
  54          return [
  55              'Standard name' => ['New name', 'New name', true],
  56              'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017', true],
  57              'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle', true],
  58              'Name with tags' => ['This is <b>bold</b>', 'This is bold', true],
  59              'Long name' => [str_repeat('a', 100), str_repeat('a', 100), true],
  60              'Too long name' => [str_repeat('a', 300), str_repeat('a', 255), true],
  61              'Empty name' => ['', 'Test content ', false],
  62              'Blanks only' => ['  ', 'Test content ', false],
  63              'Zero name' => ['0', '0', true],
  64          ];
  65      }
  66  
  67      /**
  68       * Test the behaviour of rename_content() for users with permission.
  69       *
  70       * @dataProvider    rename_content_provider
  71       * @param   string  $newname    The name to set
  72       * @param   string   $expectedname   The name result
  73       * @param   bool   $expectedresult   The bolean result expected when renaming
  74       *
  75       * @covers ::execute
  76       */
  77      public function test_rename_content_with_permission(string $newname, string $expectedname, bool $expectedresult) {
  78          global $DB;
  79          $this->resetAfterTest();
  80  
  81          // Create users.
  82          $roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
  83          $teacher = $this->getDataGenerator()->create_user();
  84  
  85          $this->getDataGenerator()->role_assign($roleid, $teacher->id);
  86          $this->setUser($teacher);
  87  
  88          // Add some content to the content bank as teacher.
  89          $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
  90          $contents = $generator->generate_contentbank_data('contenttype_testable', 1, $teacher->id);
  91          $content = array_shift($contents);
  92  
  93          $oldname = $content->get_name();
  94  
  95          // Call the WS and check the content is renamed as expected.
  96          $result = rename_content::execute($content->get_id(), $newname);
  97          $result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
  98          $this->assertEquals($expectedresult, $result['result']);
  99          $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
 100          $this->assertEquals($expectedname, $record->name);
 101  
 102          // Call the WS using an unexisting contentid and check an error is thrown.
 103          $this->expectException(\invalid_response_exception::class);
 104          $result = rename_content::execute_returns($content->get_id() + 1, $oldname);
 105          $result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
 106          $this->assertFalse($result['result']);
 107      }
 108  
 109      /**
 110       * Test the behaviour of rename_content() for users with permission.
 111       *
 112       * @covers ::execute
 113       */
 114      public function test_rename_content_without_permission() {
 115          global $DB;
 116          $this->resetAfterTest();
 117  
 118          // Create users.
 119          $course = $this->getDataGenerator()->create_course();
 120          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
 121          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 122  
 123          // Add some content to the content bank as teacher.
 124          $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
 125          $contents = $generator->generate_contentbank_data('contenttype_testable', 1, $teacher->id);
 126          $content = array_shift($contents);
 127  
 128          $oldname = $content->get_name();
 129          $newname = 'New name';
 130  
 131          // Call the WS and check the content has not been renamed by the student.
 132          $this->setUser($student);
 133          $result = rename_content::execute($content->get_id(), $newname);
 134          $result = external_api::clean_returnvalue(rename_content::execute_returns(), $result);
 135          $this->assertFalse($result['result']);
 136          $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
 137          $this->assertEquals($oldname, $record->name);
 138          $this->assertNotEquals($newname, $record->name);
 139      }
 140  }