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   * PHPUnit data generator testcase
  21   *
  22   * @package    mod_forum
  23   * @category   phpunit
  24   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class generator_test extends \advanced_testcase {
  28  
  29      public function setUp(): void {
  30          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  31          // tests using these functions.
  32          \mod_forum\subscriptions::reset_forum_cache();
  33      }
  34  
  35      public function tearDown(): void {
  36          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  37          // tests using these functions.
  38          \mod_forum\subscriptions::reset_forum_cache();
  39      }
  40  
  41      public function test_generator() {
  42          global $DB;
  43  
  44          $this->resetAfterTest(true);
  45  
  46          $this->assertEquals(0, $DB->count_records('forum'));
  47  
  48          $course = $this->getDataGenerator()->create_course();
  49  
  50          /** @var mod_forum_generator $generator */
  51          $generator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
  52          $this->assertInstanceOf('mod_forum_generator', $generator);
  53          $this->assertEquals('forum', $generator->get_modulename());
  54  
  55          $generator->create_instance(array('course'=>$course->id));
  56          $generator->create_instance(array('course'=>$course->id));
  57          $forum = $generator->create_instance(array('course'=>$course->id));
  58          $this->assertEquals(3, $DB->count_records('forum'));
  59  
  60          $cm = get_coursemodule_from_instance('forum', $forum->id);
  61          $this->assertEquals($forum->id, $cm->instance);
  62          $this->assertEquals('forum', $cm->modname);
  63          $this->assertEquals($course->id, $cm->course);
  64  
  65          $context = \context_module::instance($cm->id);
  66          $this->assertEquals($forum->cmid, $context->instanceid);
  67  
  68          // test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
  69          $forum = $generator->create_instance(array('course'=>$course->id, 'assessed'=>1, 'scale'=>100));
  70          $gitem = $DB->get_record('grade_items', array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule'=>'forum', 'iteminstance'=>$forum->id));
  71          $this->assertNotEmpty($gitem);
  72          $this->assertEquals(100, $gitem->grademax);
  73          $this->assertEquals(0, $gitem->grademin);
  74          $this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
  75      }
  76  
  77      /**
  78       * Test create_discussion.
  79       */
  80      public function test_create_discussion() {
  81          global $DB;
  82  
  83          $this->resetAfterTest(true);
  84  
  85          // User that will create the forum.
  86          $user = self::getDataGenerator()->create_user();
  87  
  88          // Create course to add the forum to.
  89          $course = self::getDataGenerator()->create_course();
  90  
  91          // The forum.
  92          $record = new \stdClass();
  93          $record->course = $course->id;
  94          $forum = self::getDataGenerator()->create_module('forum', $record);
  95  
  96          // Add a few discussions.
  97          $record = array();
  98          $record['course'] = $course->id;
  99          $record['forum'] = $forum->id;
 100          $record['userid'] = $user->id;
 101          $record['pinned'] = FORUM_DISCUSSION_PINNED; // Pin one discussion.
 102          self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 103          $record['pinned'] = FORUM_DISCUSSION_UNPINNED; // No pin for others.
 104          self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 105          self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 106  
 107          // Check the discussions were correctly created.
 108          $this->assertEquals(3, $DB->count_records_select('forum_discussions', 'forum = :forum',
 109              array('forum' => $forum->id)));
 110  
 111          $record['tags'] = array('Cats', 'mice');
 112          $record = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 113          $this->assertEquals(array('Cats', 'mice'),
 114              array_values(\core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->firstpost)));
 115      }
 116  
 117      /**
 118       * Test create_post.
 119       */
 120      public function test_create_post() {
 121          global $DB;
 122  
 123          $this->resetAfterTest(true);
 124  
 125          // Create a bunch of users
 126          $user1 = self::getDataGenerator()->create_user();
 127          $user2 = self::getDataGenerator()->create_user();
 128          $user3 = self::getDataGenerator()->create_user();
 129          $user4 = self::getDataGenerator()->create_user();
 130  
 131          // Create course to add the forum.
 132          $course = self::getDataGenerator()->create_course();
 133  
 134          // The forum.
 135          $record = new \stdClass();
 136          $record->course = $course->id;
 137          $forum = self::getDataGenerator()->create_module('forum', $record);
 138  
 139          // Add a discussion.
 140          $record->forum = $forum->id;
 141          $record->userid = $user1->id;
 142          $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 143  
 144          // Add a bunch of replies, changing the userid.
 145          $record = new \stdClass();
 146          $record->discussion = $discussion->id;
 147          $record->userid = $user2->id;
 148          self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
 149          $record->userid = $user3->id;
 150          self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
 151          $record->userid = $user4->id;
 152          self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
 153  
 154          // Check the posts were correctly created, remember, when creating a discussion a post
 155          // is generated as well, so we should have 4 posts, not 3.
 156          $this->assertEquals(4, $DB->count_records_select('forum_posts', 'discussion = :discussion',
 157              array('discussion' => $discussion->id)));
 158  
 159          $record->tags = array('Cats', 'mice');
 160          $record = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
 161          $this->assertEquals(array('Cats', 'mice'),
 162              array_values(\core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $record->id)));
 163      }
 164  
 165      public function test_create_content() {
 166          global $DB;
 167  
 168          $this->resetAfterTest(true);
 169  
 170          // Create a bunch of users
 171          $user1 = self::getDataGenerator()->create_user();
 172          $user2 = self::getDataGenerator()->create_user();
 173          $user3 = self::getDataGenerator()->create_user();
 174          $user4 = self::getDataGenerator()->create_user();
 175  
 176          $this->setAdminUser();
 177  
 178          // Create course and forum.
 179          $course = self::getDataGenerator()->create_course();
 180          $forum = self::getDataGenerator()->create_module('forum', array('course' => $course));
 181  
 182          $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
 183          // This should create discussion.
 184          $post1 = $generator->create_content($forum);
 185          // This should create posts in the discussion.
 186          $post2 = $generator->create_content($forum, array('parent' => $post1->id));
 187          $post3 = $generator->create_content($forum, array('discussion' => $post1->discussion));
 188          // This should create posts answering another post.
 189          $post4 = $generator->create_content($forum, array('parent' => $post2->id));
 190          // This should create post with tags.
 191          $post5 = $generator->create_content($forum, array('parent' => $post2->id, 'tags' => array('Cats', 'mice')));
 192  
 193          $discussionrecords = $DB->get_records('forum_discussions', array('forum' => $forum->id));
 194          $postrecords = $DB->get_records('forum_posts');
 195          $postrecords2 = $DB->get_records('forum_posts', array('discussion' => $post1->discussion));
 196          $this->assertEquals(1, count($discussionrecords));
 197          $this->assertEquals(5, count($postrecords));
 198          $this->assertEquals(5, count($postrecords2));
 199          $this->assertEquals($post1->id, $discussionrecords[$post1->discussion]->firstpost);
 200          $this->assertEquals($post1->id, $postrecords[$post2->id]->parent);
 201          $this->assertEquals($post1->id, $postrecords[$post3->id]->parent);
 202          $this->assertEquals($post2->id, $postrecords[$post4->id]->parent);
 203  
 204          $this->assertEquals(array('Cats', 'mice'),
 205              array_values(\core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $post5->id)));
 206      }
 207  }