Merge branch 'MDL-64703-master-fix-1' of https://github.com/ryanwyllie/moodle
[moodle.git] / mod / forum / tests / entities_discussion_test.php
CommitLineData
47d38303
RW
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 discussion entity tests.
19 *
20 * @package mod_forum
21 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27use mod_forum\local\entities\discussion as discussion_entity;
28use mod_forum\local\entities\post as post_entity;
29
30/**
31 * The discussion entity tests.
32 *
33 * @package mod_forum
34 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class mod_forum_entities_discussion_testcase extends advanced_testcase {
38 /**
39 * Test the entity returns expected values.
40 */
41 public function test_entity() {
42 $this->resetAfterTest();
43
44 // In the past to ensure the time started is true.
45 $time = time() + 10;
46 $discussion = new discussion_entity(
47 1,
48 2,
49 3,
50 'test discussion',
51 4,
52 5,
53 6,
54 false,
55 $time,
56 $time,
57 0,
58 0,
59 false
60 );
61 $firstpost = new post_entity(
62 4,
63 1,
64 0,
65 1,
66 time(),
67 time(),
68 true,
69 'post subject',
70 'post message',
71 1,
72 true,
73 false,
74 0,
75 false,
bc4c7337 76 false,
47d38303
RW
77 false
78 );
79 $notfirstpost = new post_entity(
80 1,
81 1,
82 0,
83 1,
84 time(),
85 time(),
86 true,
87 'post subject',
88 'post message',
89 1,
90 true,
91 false,
92 0,
93 false,
bc4c7337 94 false,
47d38303
RW
95 false
96 );
97
98 $this->assertEquals(1, $discussion->get_id());
99 $this->assertEquals(2, $discussion->get_course_id());
100 $this->assertEquals(3, $discussion->get_forum_id());
101 $this->assertEquals('test discussion', $discussion->get_name());
102 $this->assertEquals(4, $discussion->get_first_post_id());
103 $this->assertEquals(5, $discussion->get_user_id());
104 $this->assertEquals(6, $discussion->get_group_id());
105 $this->assertEquals(false, $discussion->is_assessed());
106 $this->assertEquals($time, $discussion->get_time_modified());
107 $this->assertEquals($time, $discussion->get_user_modified());
108 $this->assertEquals(0, $discussion->get_time_start());
109 $this->assertEquals(0, $discussion->get_time_end());
110 $this->assertEquals(false, $discussion->is_pinned());
111 $this->assertEquals(true, $discussion->is_first_post($firstpost));
112 $this->assertEquals(false, $discussion->is_first_post($notfirstpost));
113 $this->assertEquals(true, $discussion->has_started());
114 $this->assertEquals(true, $discussion->has_group());
115 }
814db56f
P
116
117 /**
118 * Test the display period settings for discussions.
119 * This covers each individual date function as well as the combination of the 2.
120 *
121 * @dataProvider test_diplay_period_options_provider
122 * @param string $testdescription A basic description of the base assertions.
123 * @param int $basetime
124 * @param int $timestart
125 * @param int $timeend
126 * @param bool $timestartresult Expected result from the has_started function
127 * @param bool $timeendresult Expected result from the has_ended function
128 * @param bool $isvisible Expected result from the is_timed_discussion_visible function
129 */
130 public function test_display_period_settings($testdescription, $basetime, $timestart, $timeend,
131 $timestartresult, $timeendresult, $isvisible) {
132 global $CFG;
133 $this->resetAfterTest();
134
135 $discussion = new discussion_entity(
136 1,
137 2,
138 3,
139 'test discussion',
140 4,
141 5,
142 6,
143 false,
144 $basetime,
145 $basetime,
146 $timestart,
147 $timeend,
148 false
149 );
150 $originaltimedposts = $CFG->forum_enabletimedposts;
151 $CFG->forum_enabletimedposts = true;
152
153 $this->assertEquals($timestartresult, $discussion->has_started(), $testdescription);
154 $this->assertEquals($timeendresult, $discussion->has_ended(), $testdescription);
155 $this->assertEquals($isvisible, $discussion->is_timed_discussion_visible(), $testdescription);
156
157 $CFG->forum_enabletimedposts = $originaltimedposts;
158 }
159
160 /**
161 * Data provider for test_display_period_settings().
162 *
163 * @return array base,start, endtimes and the expected results.
164 */
165 public function test_diplay_period_options_provider() {
166 $base = time() + 10;
167 return array(
168 ["No dates set set", $base, 0, 0, true, false, true],
169 ["Only started date in the future", $base, $base + 100, 0, false, false, false],
170 ["Only started date in the past", $base, $base - 100, 0, true, false, true],
171 ["Only end date in the future", $base, 0, $base + 100, true, false, true],
172 ["Only end date in the past", $base, 0, $base - 100, true, true, false],
173 ["Start date in the past, end date in the future", $base, $base - 100, $base + 100, true, false, true],
174 ["Both dates in the past", $base, $base - 100, $base - 50, true, true, false],
175 ["Both dates in the future", $base, $base + 100, $base + 150, false, false, false],
176 );
177 }
178}