Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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  declare(strict_types = 1);
  18  
  19  namespace mod_forum\h5p;
  20  
  21  use stdClass;
  22  
  23  /**
  24   * Test class covering the H5P canedit class.
  25   *
  26   * @package    mod_forum
  27   * @copyright  2021 Sara Arjona <sara@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   * @coversDefaultClass \mod_forum\h5p\canedit
  30   */
  31  class canedit_test extends \advanced_testcase {
  32  
  33      /**
  34       * Test the behaviour of can_edit_content().
  35       *
  36       * @covers ::can_edit_content
  37       * @dataProvider can_edit_content_provider
  38       *
  39       * @param string $currentuser User who will call the method.
  40       * @param string $fileauthor Author of the file to check.
  41       * @param string $filecomponent Component of the file to check.
  42       * @param bool $expected Expected result after calling the can_edit_content method.
  43       * @param string $filearea Area of the file to check.
  44       *
  45       * @return void
  46       */
  47      public function test_can_edit_content(string $currentuser, string $fileauthor, string $filecomponent, bool $expected,
  48              $filearea = 'unittest'): void {
  49          global $USER, $DB;
  50  
  51          $this->setRunTestInSeparateProcess(true);
  52          $this->resetAfterTest();
  53  
  54          // Create course.
  55          $course = $this->getDataGenerator()->create_course();
  56          $context = \context_course::instance($course->id);
  57  
  58          // Create some users.
  59          $this->setAdminUser();
  60          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  61          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  62          $users = [
  63              'admin' => $USER,
  64              'teacher' => $teacher,
  65              'student' => $student,
  66          ];
  67  
  68          // Set current user.
  69          if ($currentuser !== 'admin') {
  70              $this->setUser($users[$currentuser]);
  71          }
  72  
  73          $itemid = rand();
  74          if ($filearea === 'post') {
  75              // Create a forum and add a discussion.
  76              $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]);
  77  
  78              $record = new stdClass();
  79              $record->course = $course->id;
  80              $record->userid = $users[$fileauthor]->id;
  81              $record->forum = $forum->id;
  82              $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
  83              $post = $DB->get_record('forum_posts', ['discussion' => $discussion->id]);
  84              $itemid = $post->id;
  85          }
  86  
  87          // Create the file.
  88          $filename = 'greeting-card.h5p';
  89          $path = __DIR__ . '/../../../../h5p/tests/fixtures/' . $filename;
  90          if ($filecomponent === 'contentbank') {
  91              $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
  92              $contents = $generator->generate_contentbank_data(
  93                  'contenttype_h5p',
  94                  1,
  95                  (int)$users[$fileauthor]->id,
  96                  $context,
  97                  true,
  98                  $path
  99              );
 100              $content = array_shift($contents);
 101              $file = $content->get_file();
 102          } else {
 103              $filerecord = [
 104                  'contextid' => $context->id,
 105                  'component' => $filecomponent,
 106                  'filearea'  => $filearea,
 107                  'itemid'    => $itemid,
 108                  'filepath'  => '/',
 109                  'filename'  => basename($path),
 110                  'userid'    => $users[$fileauthor]->id,
 111              ];
 112              $fs = get_file_storage();
 113              $file = $fs->create_file_from_pathname($filerecord, $path);
 114          }
 115  
 116          // Check if the currentuser can edit the file.
 117          $result = canedit::can_edit_content($file);
 118          $this->assertEquals($expected, $result);
 119      }
 120  
 121      /**
 122       * Data provider for test_can_edit_content().
 123       *
 124       * @return array
 125       */
 126      public function can_edit_content_provider(): array {
 127          return [
 128              // Component = mod_forum.
 129              'mod_forum: Admin user is author' => [
 130                  'currentuser' => 'admin',
 131                  'fileauthor' => 'admin',
 132                  'filecomponent' => 'mod_forum',
 133                  'expected' => true,
 134              ],
 135              'mod_forum: Admin user, teacher is author' => [
 136                  'currentuser' => 'admin',
 137                  'fileauthor' => 'teacher',
 138                  'filecomponent' => 'mod_forum',
 139                  'expected' => true,
 140              ],
 141              'mod_forum: Teacher user, admin is author' => [
 142                  'currentuser' => 'teacher',
 143                  'fileauthor' => 'admin',
 144                  'filecomponent' => 'mod_forum',
 145                  'expected' => true,
 146              ],
 147              'mod_forum: Student user, teacher is author' => [
 148                  'currentuser' => 'student',
 149                  'fileauthor' => 'teacher',
 150                  'filecomponent' => 'mod_forum',
 151                  'expected' => false,
 152              ],
 153              'mod_forum/post: Admin user is author' => [
 154                  'currentuser' => 'admin',
 155                  'fileauthor' => 'admin',
 156                  'filecomponent' => 'mod_forum',
 157                  'expected' => true,
 158                  'filearea' => 'post',
 159              ],
 160              'mod_forum/post: Teacher user, admin is author' => [
 161                  'currentuser' => 'teacher',
 162                  'fileauthor' => 'admin',
 163                  'filecomponent' => 'mod_forum',
 164                  'expected' => true,
 165                  'filearea' => 'post',
 166              ],
 167              'mod_forum/post: Student user, teacher is author' => [
 168                  'currentuser' => 'student',
 169                  'fileauthor' => 'teacher',
 170                  'filecomponent' => 'mod_forum',
 171                  'expected' => false,
 172                  'filearea' => 'post',
 173              ],
 174  
 175              // Component <> mod_forum.
 176              'mod_page: Admin user is author' => [
 177                  'currentuser' => 'admin',
 178                  'fileauthor' => 'admin',
 179                  'filecomponent' => 'mod_page',
 180                  'expected' => false,
 181              ],
 182  
 183              // Unexisting components.
 184              'Unexisting component' => [
 185                  'currentuser' => 'admin',
 186                  'fileauthor' => 'admin',
 187                  'filecomponent' => 'unexisting_component',
 188                  'expected' => false,
 189              ],
 190              'Unexisting module activity' => [
 191                  'currentuser' => 'admin',
 192                  'fileauthor' => 'admin',
 193                  'filecomponent' => 'mod_unexisting',
 194                  'expected' => false,
 195              ],
 196              'Unexisting block' => [
 197                  'currentuser' => 'admin',
 198                  'fileauthor' => 'admin',
 199                  'filecomponent' => 'block_unexisting',
 200                  'expected' => false,
 201              ],
 202          ];
 203      }
 204  }