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