| 1 | 
           efrain | 
           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 forum module trait with additional generator helpers.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    mod_forum
  | 
        
        
            | 
            | 
           21 | 
            * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           trait mod_forum_tests_generator_trait {
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
               /**
  | 
        
        
            | 
            | 
           30 | 
                * Helper to create the required number of users in the specified course.
  | 
        
        
            | 
            | 
           31 | 
                * Users are enrolled as students by default.
  | 
        
        
            | 
            | 
           32 | 
                *
  | 
        
        
            | 
            | 
           33 | 
                * @param   stdClass $course The course object
  | 
        
        
            | 
            | 
           34 | 
                * @param   integer $count The number of users to create
  | 
        
        
            | 
            | 
           35 | 
                * @param   string  $role The role to assign users as
  | 
        
        
            | 
            | 
           36 | 
                * @return  array The users created
  | 
        
        
            | 
            | 
           37 | 
                */
  | 
        
        
            | 
            | 
           38 | 
               protected function helper_create_users($course, $count, $role = null) {
  | 
        
        
            | 
            | 
           39 | 
                   $users = array();
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
                   for ($i = 0; $i < $count; $i++) {
  | 
        
        
            | 
            | 
           42 | 
                       $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           43 | 
                       $this->getDataGenerator()->enrol_user($user->id, $course->id, $role);
  | 
        
        
            | 
            | 
           44 | 
                       $users[] = $user;
  | 
        
        
            | 
            | 
           45 | 
                   }
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
                   return $users;
  | 
        
        
            | 
            | 
           48 | 
               }
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
               /**
  | 
        
        
            | 
            | 
           51 | 
                * Create a new discussion and post within the specified forum, as the
  | 
        
        
            | 
            | 
           52 | 
                * specified author.
  | 
        
        
            | 
            | 
           53 | 
                *
  | 
        
        
            | 
            | 
           54 | 
                * @param stdClass $forum The forum to post in
  | 
        
        
            | 
            | 
           55 | 
                * @param stdClass $author The author to post as
  | 
        
        
            | 
            | 
           56 | 
                * @param array $fields any other fields in discussion (name, message, messageformat, ...)
  | 
        
        
            | 
            | 
           57 | 
                * @return array An array containing the discussion object, and the post object
  | 
        
        
            | 
            | 
           58 | 
                */
  | 
        
        
            | 
            | 
           59 | 
               protected function helper_post_to_forum($forum, $author, $fields = array()) {
  | 
        
        
            | 
            | 
           60 | 
                   global $DB;
  | 
        
        
            | 
            | 
           61 | 
                   $generator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   // Create a discussion in the forum, and then add a post to that discussion.
  | 
        
        
            | 
            | 
           64 | 
                   $record = (object)$fields;
  | 
        
        
            | 
            | 
           65 | 
                   $record->course = $forum->course;
  | 
        
        
            | 
            | 
           66 | 
                   $record->userid = $author->id;
  | 
        
        
            | 
            | 
           67 | 
                   $record->forum = $forum->id;
  | 
        
        
            | 
            | 
           68 | 
                   $discussion = $generator->create_discussion($record);
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
                   // Retrieve the post which was created by create_discussion.
  | 
        
        
            | 
            | 
           71 | 
                   $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
                   return [$discussion, $post];
  | 
        
        
            | 
            | 
           74 | 
               }
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
               /**
  | 
        
        
            | 
            | 
           77 | 
                * Update the post time for the specified post by $factor.
  | 
        
        
            | 
            | 
           78 | 
                *
  | 
        
        
            | 
            | 
           79 | 
                * @param stdClass $post The post to update
  | 
        
        
            | 
            | 
           80 | 
                * @param int $factor The amount to update by
  | 
        
        
            | 
            | 
           81 | 
                */
  | 
        
        
            | 
            | 
           82 | 
               protected function helper_update_post_time($post, $factor) {
  | 
        
        
            | 
            | 
           83 | 
                   global $DB;
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
                   // Update the post to have a created in the past.
  | 
        
        
            | 
            | 
           86 | 
                   $DB->set_field('forum_posts', 'created', $post->created + $factor, array('id' => $post->id));
  | 
        
        
            | 
            | 
           87 | 
               }
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
               /**
  | 
        
        
            | 
            | 
           90 | 
                * Update the subscription time for the specified user/discussion by $factor.
  | 
        
        
            | 
            | 
           91 | 
                *
  | 
        
        
            | 
            | 
           92 | 
                * @param stdClass $user The user to update
  | 
        
        
            | 
            | 
           93 | 
                * @param stdClass $discussion The discussion to update for this user
  | 
        
        
            | 
            | 
           94 | 
                * @param int $factor The amount to update by
  | 
        
        
            | 
            | 
           95 | 
                */
  | 
        
        
            | 
            | 
           96 | 
               protected function helper_update_subscription_time($user, $discussion, $factor) {
  | 
        
        
            | 
            | 
           97 | 
                   global $DB;
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   $sub = $DB->get_record('forum_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id));
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
                   // Update the subscription to have a preference in the past.
  | 
        
        
            | 
            | 
           102 | 
                   $DB->set_field('forum_discussion_subs', 'preference', $sub->preference + $factor, array('id' => $sub->id));
  | 
        
        
            | 
            | 
           103 | 
               }
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
               /**
  | 
        
        
            | 
            | 
           106 | 
                * Create a new post within an existing discussion, as the specified author.
  | 
        
        
            | 
            | 
           107 | 
                *
  | 
        
        
            | 
            | 
           108 | 
                * @param stdClass $forum The forum to post in
  | 
        
        
            | 
            | 
           109 | 
                * @param stdClass $discussion The discussion to post in
  | 
        
        
            | 
            | 
           110 | 
                * @param stdClass $author The author to post as
  | 
        
        
            | 
            | 
           111 | 
                * @param array $options Additional options to pass to `create_post`
  | 
        
        
            | 
            | 
           112 | 
                * @return stdClass The forum post
  | 
        
        
            | 
            | 
           113 | 
                */
  | 
        
        
            | 
            | 
           114 | 
               protected function helper_post_to_discussion($forum, $discussion, $author, array $options = []) {
  | 
        
        
            | 
            | 
           115 | 
                   global $DB;
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
                   $generator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
                   // Add a post to the discussion.
  | 
        
        
            | 
            | 
           120 | 
                   $strre = get_string('re', 'forum');
  | 
        
        
            | 
            | 
           121 | 
                   $record = array_merge([
  | 
        
        
            | 
            | 
           122 | 
                       'course' => $forum->course,
  | 
        
        
            | 
            | 
           123 | 
                       'subject' => "{$strre} {$discussion->subject}",
  | 
        
        
            | 
            | 
           124 | 
                       'userid' => $author->id,
  | 
        
        
            | 
            | 
           125 | 
                       'forum' => $forum->id,
  | 
        
        
            | 
            | 
           126 | 
                       'discussion' => $discussion->id,
  | 
        
        
            | 
            | 
           127 | 
                       'mailnow' => 1,
  | 
        
        
            | 
            | 
           128 | 
                   ], $options);
  | 
        
        
            | 
            | 
           129 | 
              | 
        
        
            | 
            | 
           130 | 
                   $post = $generator->create_post((object) $record);
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
                   return $post;
  | 
        
        
            | 
            | 
           133 | 
               }
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
            | 
            | 
           135 | 
               /**
  | 
        
        
            | 
            | 
           136 | 
                * Create a new post within an existing discussion, as the specified author.
  | 
        
        
            | 
            | 
           137 | 
                *
  | 
        
        
            | 
            | 
           138 | 
                * @param stdClass $parent The post being replied to
  | 
        
        
            | 
            | 
           139 | 
                * @param stdClass $author The author to post as
  | 
        
        
            | 
            | 
           140 | 
                * @param array $options Additional options to pass to `create_post`
  | 
        
        
            | 
            | 
           141 | 
                * @return stdClass The forum post
  | 
        
        
            | 
            | 
           142 | 
                */
  | 
        
        
            | 
            | 
           143 | 
               protected function helper_reply_to_post($parent, $author, array $options = []) {
  | 
        
        
            | 
            | 
           144 | 
                   global $DB;
  | 
        
        
            | 
            | 
           145 | 
              | 
        
        
            | 
            | 
           146 | 
                   $generator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
  | 
        
        
            | 
            | 
           147 | 
              | 
        
        
            | 
            | 
           148 | 
                   // Add a post to the discussion.
  | 
        
        
            | 
            | 
           149 | 
                   $strre = get_string('re', 'forum');
  | 
        
        
            | 
            | 
           150 | 
                   $record = (object) array_merge([
  | 
        
        
            | 
            | 
           151 | 
                       'discussion' => $parent->discussion,
  | 
        
        
            | 
            | 
           152 | 
                       'parent' => $parent->id,
  | 
        
        
            | 
            | 
           153 | 
                       'userid' => $author->id,
  | 
        
        
            | 
            | 
           154 | 
                       'mailnow' => 1,
  | 
        
        
            | 
            | 
           155 | 
                       'subject' => $strre . ' ' . $parent->subject,
  | 
        
        
            | 
            | 
           156 | 
                   ], $options);
  | 
        
        
            | 
            | 
           157 | 
              | 
        
        
            | 
            | 
           158 | 
                   $post = $generator->create_post($record);
  | 
        
        
            | 
            | 
           159 | 
              | 
        
        
            | 
            | 
           160 | 
                   return $post;
  | 
        
        
            | 
            | 
           161 | 
               }
  | 
        
        
            | 
            | 
           162 | 
              | 
        
        
            | 
            | 
           163 | 
               /**
  | 
        
        
            | 
            | 
           164 | 
                * Gets the role id from it's shortname.
  | 
        
        
            | 
            | 
           165 | 
                *
  | 
        
        
            | 
            | 
           166 | 
                * @param   string $roleshortname
  | 
        
        
            | 
            | 
           167 | 
                * @return  int
  | 
        
        
            | 
            | 
           168 | 
                */
  | 
        
        
            | 
            | 
           169 | 
               protected function get_role_id($roleshortname) {
  | 
        
        
            | 
            | 
           170 | 
                   global $DB;
  | 
        
        
            | 
            | 
           171 | 
              | 
        
        
            | 
            | 
           172 | 
                   return $DB->get_field('role', 'id', ['shortname' => $roleshortname]);
  | 
        
        
            | 
            | 
           173 | 
               }
  | 
        
        
            | 
            | 
           174 | 
           }
  |