MDL-65032 mod_forum: Behat test for locking
[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,
f5b4320e
P
59 false,
60 0
47d38303
RW
61 );
62 $firstpost = new post_entity(
63 4,
64 1,
65 0,
66 1,
67 time(),
68 time(),
69 true,
70 'post subject',
71 'post message',
72 1,
73 true,
74 false,
75 0,
76 false,
bc4c7337 77 false,
47d38303
RW
78 false
79 );
80 $notfirstpost = new post_entity(
81 1,
82 1,
83 0,
84 1,
85 time(),
86 time(),
87 true,
88 'post subject',
89 'post message',
90 1,
91 true,
92 false,
93 0,
94 false,
bc4c7337 95 false,
47d38303
RW
96 false
97 );
98
99 $this->assertEquals(1, $discussion->get_id());
100 $this->assertEquals(2, $discussion->get_course_id());
101 $this->assertEquals(3, $discussion->get_forum_id());
102 $this->assertEquals('test discussion', $discussion->get_name());
103 $this->assertEquals(4, $discussion->get_first_post_id());
104 $this->assertEquals(5, $discussion->get_user_id());
105 $this->assertEquals(6, $discussion->get_group_id());
106 $this->assertEquals(false, $discussion->is_assessed());
107 $this->assertEquals($time, $discussion->get_time_modified());
108 $this->assertEquals($time, $discussion->get_user_modified());
109 $this->assertEquals(0, $discussion->get_time_start());
110 $this->assertEquals(0, $discussion->get_time_end());
111 $this->assertEquals(false, $discussion->is_pinned());
112 $this->assertEquals(true, $discussion->is_first_post($firstpost));
113 $this->assertEquals(false, $discussion->is_first_post($notfirstpost));
114 $this->assertEquals(true, $discussion->has_started());
115 $this->assertEquals(true, $discussion->has_group());
116 }
814db56f
P
117
118 /**
119 * Test the display period settings for discussions.
120 * This covers each individual date function as well as the combination of the 2.
121 *
122 * @dataProvider test_diplay_period_options_provider
123 * @param string $testdescription A basic description of the base assertions.
3eed2af1
JP
124 * @param int $startoffset Start time offset with current time in seconds.
125 * @param int $endoffset End time offset with current time in seconds.
814db56f
P
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 */
3eed2af1
JP
130 public function test_display_period_settings($testdescription, $startoffset, $endoffset,
131 $timestartresult, $timeendresult, $isvisible) {
814db56f
P
132 global $CFG;
133 $this->resetAfterTest();
134
3eed2af1
JP
135 $basetime = time();
136 $starttime = $startoffset != 0 ? $basetime + $startoffset : 0;
137 $endtime = $endoffset != 0 ? $basetime + $endoffset : 0;
814db56f
P
138 $discussion = new discussion_entity(
139 1,
140 2,
141 3,
142 'test discussion',
143 4,
144 5,
145 6,
146 false,
147 $basetime,
148 $basetime,
3eed2af1
JP
149 $starttime,
150 $endtime,
814db56f
P
151 false
152 );
814db56f
P
153 $CFG->forum_enabletimedposts = true;
154
155 $this->assertEquals($timestartresult, $discussion->has_started(), $testdescription);
156 $this->assertEquals($timeendresult, $discussion->has_ended(), $testdescription);
157 $this->assertEquals($isvisible, $discussion->is_timed_discussion_visible(), $testdescription);
814db56f
P
158 }
159
160 /**
161 * Data provider for test_display_period_settings().
162 *
3eed2af1 163 * @return array start/end time offsets and the expected results.
814db56f
P
164 */
165 public function test_diplay_period_options_provider() {
814db56f 166 return array(
3eed2af1
JP
167 ["No dates set", 0, 0, true, false, true],
168 ["Only started date in the future", 100, 0, false, false, false],
169 ["Only started date in the past", -100, 0, true, false, true],
170 ["Only end date in the future", 0, 100, true, false, true],
171 ["Only end date in the past", 0, -100, true, true, false],
172 ["Start date in the past, end date in the future", -100, 100, true, false, true],
173 ["Both dates in the past", -100, -50, true, true, false],
174 ["Both dates in the future", 100, 150, false, false, false],
814db56f
P
175 );
176 }
3eed2af1 177}