Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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 qbank_comment;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/question/bank/comment/lib.php');
  23  
  24  
  25  /**
  26   * Comment lib unit tests.
  27   *
  28   * @package    qbank_comment
  29   * @copyright  2021 Catalyst IT Australia Pty Ltd
  30   * @author     Matt Porritt <mattp@catalyst-au.net>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class lib_test extends \advanced_testcase {
  34  
  35      /**
  36       * Test the comment validation callback.
  37       */
  38      public function test_qbank_comment_comment_validate() {
  39          $commentparams = new \stdClass();
  40          $commentparams->commentarea = 'question';
  41          $commentparams->component = 'qbank_comment';
  42  
  43          $isvalid = qbank_comment_comment_validate($commentparams);
  44          $this->assertTrue($isvalid);
  45  
  46          $this->expectException('comment_exception');
  47          $commentparams->commentarea = 'core_comment';
  48          $commentparams->component = 'blog_comment';
  49          qbank_comment_comment_validate($commentparams);
  50  
  51      }
  52  
  53      /**
  54       * Test the comment display callback.
  55       */
  56      public function test_qbank_comment_comment_display() {
  57          $comment = new \stdClass();
  58          $comment->text = 'test';
  59          $comments = [$comment];
  60  
  61          $commentparams = new \stdClass();
  62          $commentparams->commentarea = 'question';
  63          $commentparams->component = 'qbank_comment';
  64  
  65          $responses = qbank_comment_comment_display($comments, $commentparams);
  66          $this->assertEquals($comment->text, $responses[0]->text);
  67  
  68          $this->expectException('comment_exception');
  69          $commentparams->commentarea = 'core_comment';
  70          $commentparams->component = 'blog_comment';
  71          qbank_comment_comment_display($comments, $commentparams);
  72  
  73      }
  74  
  75      /**
  76       * Test the comment preview callback.
  77       */
  78      public function test_qbank_comment_preview_display() {
  79          $this->resetAfterTest();
  80          global $PAGE;
  81          $PAGE->set_url('/');
  82  
  83          // Make a test question.
  84          $category = $this->getDataGenerator()->create_category();
  85          $course = $this->getDataGenerator()->create_course(['category' => $category->id]);
  86          $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
  87          $context = \context_coursecat::instance($category->id);
  88          $qcat = $qgen->create_question_category(['contextid' => $context->id]);
  89          $question = $qgen->create_question('shortanswer', null, ['category' => $qcat->id, 'idnumber' => 'q1']);
  90  
  91          $result = qbank_comment_preview_display($question, $course->id);
  92  
  93          // User doesn't have perms so expecting no output.
  94          $this->assertEmpty($result);
  95  
  96          // Expect output.
  97          $this->setAdminUser();
  98          $result = qbank_comment_preview_display($question, $course->id);
  99          $this->assertStringContainsString('comment-action-post', $result);
 100      }
 101  
 102      /**
 103       * Test the comment preview callback.
 104       */
 105      public function test_qbank_comment_output_fragment_question_comment() {
 106          $this->resetAfterTest();
 107          $this->setAdminUser();
 108          global $PAGE;
 109          $PAGE->set_url('/');
 110  
 111          // Make a test question.
 112          $category = $this->getDataGenerator()->create_category();
 113          $course = $this->getDataGenerator()->create_course(['category' => $category->id]);
 114          $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
 115          $context = \context_coursecat::instance($category->id);
 116          $qcat = $qgen->create_question_category(['contextid' => $context->id]);
 117          $question = $qgen->create_question('shortanswer', null, ['category' => $qcat->id, 'idnumber' => 'q1']);
 118          $args = [
 119              'questionid' => $question->id,
 120              'courseid' => $course->id,
 121              ];
 122  
 123          $result = qbank_comment_output_fragment_question_comment($args);
 124  
 125          // Expect output.
 126          $this->assertStringContainsString('comment-action-post', $result);
 127      }
 128  }