Merge branch 'MDL-53166' of https://github.com/eugeneventer/moodle-fixes
[moodle.git] / mod / forum / tests / output_email_test.php
CommitLineData
a6fc9171
AN
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 module forums tests
19 *
20 * @package mod_forum
21 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Tests for the forum output/email class.
29 *
30 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33class mod_forum_output_email_testcase extends advanced_testcase {
34 /**
35 * Data provider for the postdate function tests.
36 */
37 public function postdate_provider() {
38 return array(
39 'Timed discussions disabled, timestart unset' => array(
40 'globalconfig' => array(
41 'forum_enabletimedposts' => 0,
42 ),
43 'forumconfig' => array(
44 ),
45 'postconfig' => array(
46 'modified' => 1000,
47 ),
48 'discussionconfig' => array(
49 ),
50 'expectation' => 1000,
51 ),
52 'Timed discussions disabled, timestart set and newer' => array(
53 'globalconfig' => array(
54 'forum_enabletimedposts' => 0,
55 ),
56 'forumconfig' => array(
57 ),
58 'postconfig' => array(
59 'modified' => 1000,
60 ),
61 'discussionconfig' => array(
62 'timestart' => 2000,
63 ),
64 'expectation' => 1000,
65 ),
66 'Timed discussions disabled, timestart set but older' => array(
67 'globalconfig' => array(
68 'forum_enabletimedposts' => 0,
69 ),
70 'forumconfig' => array(
71 ),
72 'postconfig' => array(
73 'modified' => 1000,
74 ),
75 'discussionconfig' => array(
76 'timestart' => 500,
77 ),
78 'expectation' => 1000,
79 ),
80 'Timed discussions enabled, timestart unset' => array(
81 'globalconfig' => array(
82 'forum_enabletimedposts' => 1,
83 ),
84 'forumconfig' => array(
85 ),
86 'postconfig' => array(
87 'modified' => 1000,
88 ),
89 'discussionconfig' => array(
90 ),
91 'expectation' => 1000,
92 ),
93 'Timed discussions enabled, timestart set and newer' => array(
94 'globalconfig' => array(
95 'forum_enabletimedposts' => 1,
96 ),
97 'forumconfig' => array(
98 ),
99 'postconfig' => array(
100 'modified' => 1000,
101 ),
102 'discussionconfig' => array(
103 'timestart' => 2000,
104 ),
105 'expectation' => 2000,
106 ),
107 'Timed discussions enabled, timestart set but older' => array(
108 'globalconfig' => array(
109 'forum_enabletimedposts' => 1,
110 ),
111 'forumconfig' => array(
112 ),
113 'postconfig' => array(
114 'modified' => 1000,
115 ),
116 'discussionconfig' => array(
117 'timestart' => 500,
118 ),
119 'expectation' => 1000,
120 ),
121 );
122 }
123
124 /**
125 * Test for the forum email renderable postdate.
126 *
127 * @dataProvider postdate_provider
128 *
129 * @param array $globalconfig The configuration to set on $CFG
130 * @param array $forumconfig The configuration for this forum
131 * @param array $postconfig The configuration for this post
132 * @param array $discussionconfig The configuration for this discussion
133 * @param string $expectation The expected date
134 */
135 public function test_postdate($globalconfig, $forumconfig, $postconfig, $discussionconfig, $expectation) {
136 global $CFG, $DB;
137 $this->resetAfterTest(true);
138
139 // Apply the global configuration.
140 foreach ($globalconfig as $key => $value) {
141 $CFG->$key = $value;
142 }
143
144 // Create the fixture.
145 $user = $this->getDataGenerator()->create_user();
146 $course = $this->getDataGenerator()->create_course();
147 $forum = $this->getDataGenerator()->create_module('forum', (object) array('course' => $course->id));
148 $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
149
150 $this->getDataGenerator()->enrol_user($user->id, $course->id);
151
152 // Create a new discussion.
153 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion(
154 (object) array_merge($discussionconfig, array(
155 'course' => $course->id,
156 'forum' => $forum->id,
157 'userid' => $user->id,
158 )));
159
160 // Apply the discussion configuration.
161 // Some settings are ignored by the generator and must be set manually.
162 $discussion = $DB->get_record('forum_discussions', array('id' => $discussion->id));
163 foreach ($discussionconfig as $key => $value) {
164 $discussion->$key = $value;
165 }
166 $DB->update_record('forum_discussions', $discussion);
167
168 // Apply the post configuration.
169 // Some settings are ignored by the generator and must be set manually.
170 $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
171 foreach ($postconfig as $key => $value) {
172 $post->$key = $value;
173 }
174 $DB->update_record('forum_posts', $post);
175
176 // Create the renderable.
177 $renderable = new mod_forum\output\forum_post_email(
178 $course,
179 $cm,
180 $forum,
181 $discussion,
182 $post,
183 $user,
184 $user,
185 true
186 );
187
188 // Check the postdate matches our expectations.
189 $this->assertEquals(userdate($expectation, "", \core_date::get_user_timezone($user)), $renderable->get_postdate());
190 }
191}