Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * The portfolio forum tests.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2018 onwards Totara Learning Solutions LTD {@link http://www.totaralms.com/}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   * @author     Brendan Cox <brendan.cox@totaralearning.com>
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Class mod_forum_portfolio_caller_testcase
  30   *
  31   * Tests behaviour of the forum_portfolio_caller class.
  32   */
  33  class mod_forum_portfolio_caller_testcase extends advanced_testcase {
  34  
  35      /**
  36       * Ensure that a file will be loaded in an instance of the caller when supplied valid and
  37       * accessible post and attachment file ids.
  38       */
  39      public function test_file_in_user_post_is_loaded() {
  40          global $CFG;
  41          require_once($CFG->dirroot . '/mod/forum/locallib.php');
  42          $this->resetAfterTest(true);
  43  
  44          $user = $this->getDataGenerator()->create_user();
  45          $course = $this->getDataGenerator()->create_course();
  46          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
  47          $context = context_module::instance($forum->cmid);
  48  
  49          /* @var mod_forum_generator $forumgenerator */
  50          $forumgenerator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
  51          $discussion = $forumgenerator->create_discussion(
  52              array(
  53                  'course' => $course->id,
  54                  'forum' => $forum->id,
  55                  'userid' => $user->id,
  56                  'attachment' => 1
  57              )
  58          );
  59  
  60          $fs = get_file_storage();
  61          $dummy = (object) array(
  62              'contextid' => $context->id,
  63              'component' => 'mod_forum',
  64              'filearea' => 'attachment',
  65              'itemid' => $discussion->firstpost,
  66              'filepath' => '/',
  67              'filename' => 'myassignmnent.pdf'
  68          );
  69          $firstpostfile = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
  70  
  71          $caller = new forum_portfolio_caller(array(
  72              'postid' => $discussion->firstpost,
  73              'attachment' => $firstpostfile->get_id()
  74          ));
  75  
  76          $caller->load_data();
  77          $this->assertEquals($caller->get_sha1_file(), $firstpostfile->get_contenthash());
  78      }
  79  
  80      /**
  81       * Ensure that files will not be loaded if the supplied attachment id is for a file that is not attached to
  82       * the supplied post id.
  83       */
  84      public function test_file_not_in_user_post_not_loaded() {
  85          global $CFG;
  86          require_once($CFG->dirroot . '/mod/forum/locallib.php');
  87          $this->resetAfterTest(true);
  88  
  89          $user = $this->getDataGenerator()->create_user();
  90          $course = $this->getDataGenerator()->create_course();
  91          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
  92          $context = context_module::instance($forum->cmid);
  93  
  94          /* @var mod_forum_generator $forumgenerator */
  95          $forumgenerator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
  96          $discussion = $forumgenerator->create_discussion(
  97              array(
  98                  'course' => $course->id,
  99                  'forum' => $forum->id,
 100                  'userid' => $user->id,
 101                  'attachment' => 1
 102              )
 103          );
 104  
 105          $fs = get_file_storage();
 106          $dummyone = (object) array(
 107              'contextid' => $context->id,
 108              'component' => 'mod_forum',
 109              'filearea' => 'attachment',
 110              'itemid' => $discussion->firstpost,
 111              'filepath' => '/',
 112              'filename' => 'myassignmnent.pdf'
 113          );
 114          $firstpostfile = $fs->create_file_from_string($dummyone, 'Content of ' . $dummyone->filename);
 115  
 116          // Create a second post and add a file there.
 117          $secondpost = $forumgenerator->create_post(
 118              array(
 119                  'discussion' => $discussion->id,
 120                  'userid' => $user->id,
 121                  'attachment' => 1
 122              )
 123          );
 124          $dummytwo = (object) array(
 125              'contextid' => $context->id,
 126              'component' => 'mod_forum',
 127              'filearea' => 'attachment',
 128              'itemid' => $secondpost->id,
 129              'filepath' => '/',
 130              'filename' => 'myotherthing.pdf'
 131          );
 132          $secondpostfile = $fs->create_file_from_string($dummytwo, 'Content of ' . $dummytwo->filename);
 133  
 134          $caller = new forum_portfolio_caller(array(
 135              'postid' => $discussion->firstpost,
 136              'attachment' => $secondpostfile->get_id()
 137          ));
 138  
 139          $this->expectExceptionMessage('Sorry, the requested file could not be found');
 140          $caller->load_data();
 141      }
 142  }