510ff1929af891fdd61174b9b0b0e4cc92b6751a
[moodle.git] / mod / forum / tests / vaults_discussion_list_test.php
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/>.
17 /**
18  * The discussion_list vault 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  */
25 defined('MOODLE_INTERNAL') || die();
27 require_once(__DIR__ . '/generator_trait.php');
29 /**
30  * The discussion_list vault tests.
31  *
32  * @package    mod_forum
33  * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class mod_forum_vaults_discussion_list_testcase extends advanced_testcase {
37     // Make use of the test generator trait.
38     use mod_forum_tests_generator_trait;
40     /** @var \mod_forum\local\vaults\discussion_list */
41     private $vault;
43     /**
44      * Set up function for tests.
45      */
46     public function setUp() {
47         $vaultfactory = \mod_forum\local\container::get_vault_factory();
48         $this->vault = $vaultfactory->get_discussions_in_forum_vault();
49     }
51     /**
52      * Test get_from_id.
53      */
54     public function test_get_from_id() {
55         $this->resetAfterTest();
57         $datagenerator = $this->getDataGenerator();
58         $vault = $this->vault;
59         $user = $datagenerator->create_user();
60         $course = $datagenerator->create_course();
61         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
62         [$discussion, $post] = $this->helper_post_to_forum($forum, $user);
64         $discussionlist = $vault->get_from_id($discussion->id);
66         $this->assertEquals($discussion->id, $discussionlist->get_discussion()->get_id());
67         $this->assertEquals($post->id, $discussionlist->get_first_post()->get_id());
68         $this->assertEquals($user->id, $discussionlist->get_first_post_author()->get_id());
69         $this->assertEquals($user->id, $discussionlist->get_latest_post_author()->get_id());
70     }
72     /**
73      * Test get_from_forum_id.
74      */
75     public function test_get_from_forum_id() {
76         $this->resetAfterTest();
78         $datagenerator = $this->getDataGenerator();
79         $vault = $this->vault;
80         $user = $datagenerator->create_user();
81         self::setUser($user);
82         $course = $datagenerator->create_course();
83         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
85         $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, $user->id, null, 0, 0, $user));
87         $now = time();
88         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
89         [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
90         [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
92         $summaries = array_values($vault->get_from_forum_id($forum->id, false, null, null, 0, 0, $user));
93         $this->assertCount(2, $summaries);
94         $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
95         $this->assertEquals($discussion1->id, $summaries[1]->get_discussion()->get_id());
97         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null, 0, 0, $user));
98         $this->assertCount(3, $summaries);
99         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
100         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
101         $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
103         $summaries = array_values($vault->get_from_forum_id($forum->id, false, $user->id, null, 0, 0, $user));
104         $this->assertCount(3, $summaries);
105         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
106         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
107         $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
109         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null, 1, 0, $user));
110         $this->assertCount(1, $summaries);
111         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
113         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null, 1, 1, $user));
114         $this->assertCount(1, $summaries);
115         $this->assertEquals($discussion2->id, $summaries[0]->get_discussion()->get_id());
117         $summaries = array_values($vault->get_from_forum_id($forum->id, true, null, null, 1, 2, $user));
118         $this->assertCount(1, $summaries);
119         $this->assertEquals($discussion1->id, $summaries[0]->get_discussion()->get_id());
120     }
122     /**
123      * Test get_from_forum_id_and_group_id.
124      */
125     public function test_get_from_forum_id_and_group_id() {
126         $this->resetAfterTest();
128         $datagenerator = $this->getDataGenerator();
129         $vault = $this->vault;
130         $user = $datagenerator->create_user();
131         self::setUser($user);
132         $course = $datagenerator->create_course();
133         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
135         $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, true, null, 0, 0, $user));
137         $now = time();
138         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
139         [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
140         [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
141         [$groupdiscussion1, $post4] = $this->helper_post_to_forum(
142             $forum,
143             $user,
144             ['timestart' => $now - 8, 'timemodified' => 4, 'groupid' => 1]
145         );
146         [$groupdiscussion2, $post5] = $this->helper_post_to_forum(
147             $forum,
148             $user,
149             ['timestart' => $now - 7, 'timemodified' => 5, 'groupid' => 2]
150         );
151         [$hiddengroupdiscussion, $post6] = $this->helper_post_to_forum(
152             $forum,
153             $user,
154             ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
155         );
157         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true, null, null, 0, 0, $user));
158         $this->assertCount(6, $summaries);
159         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
160         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
161         $this->assertEquals($groupdiscussion2->id, $summaries[2]->get_discussion()->get_id());
162         $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
163         $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
164         $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
166         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false, $user->id, null, 0, 0, $user));
167         $this->assertCount(6, $summaries);
168         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
169         $this->assertEquals($hiddendiscussion->id, $summaries[1]->get_discussion()->get_id());
170         $this->assertEquals($groupdiscussion2->id, $summaries[2]->get_discussion()->get_id());
171         $this->assertEquals($groupdiscussion1->id, $summaries[3]->get_discussion()->get_id());
172         $this->assertEquals($discussion2->id, $summaries[4]->get_discussion()->get_id());
173         $this->assertEquals($discussion1->id, $summaries[5]->get_discussion()->get_id());
175         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true, null, null, 1, 0, $user));
176         $this->assertCount(1, $summaries);
177         $this->assertEquals($hiddengroupdiscussion->id, $summaries[0]->get_discussion()->get_id());
179         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true, null, null, 1, 1, $user));
180         $this->assertCount(1, $summaries);
181         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
183         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], true, null, null, 1, 2, $user));
184         $this->assertCount(1, $summaries);
185         $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
187         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [1, 2, 3], false, null, null, 0, 0, $user));
188         $this->assertCount(4, $summaries);
189         $this->assertEquals($groupdiscussion2->id, $summaries[0]->get_discussion()->get_id());
190         $this->assertEquals($groupdiscussion1->id, $summaries[1]->get_discussion()->get_id());
191         $this->assertEquals($discussion2->id, $summaries[2]->get_discussion()->get_id());
192         $this->assertEquals($discussion1->id, $summaries[3]->get_discussion()->get_id());
194         $summaries = array_values($vault->get_from_forum_id_and_group_id($forum->id, [], true, null, null, 0, 0, $user));
195         $this->assertCount(3, $summaries);
196         $this->assertEquals($hiddendiscussion->id, $summaries[0]->get_discussion()->get_id());
197         $this->assertEquals($discussion2->id, $summaries[1]->get_discussion()->get_id());
198         $this->assertEquals($discussion1->id, $summaries[2]->get_discussion()->get_id());
199     }
202     /**
203      * Test get_total_discussion_count_from_forum_id.
204      */
205     public function test_get_total_discussion_count_from_forum_id() {
206         $this->resetAfterTest();
208         $datagenerator = $this->getDataGenerator();
209         $vault = $this->vault;
210         $user = $datagenerator->create_user();
211         $course = $datagenerator->create_course();
212         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
214         $this->assertEquals(0, $vault->get_total_discussion_count_from_forum_id($forum->id, true, null));
216         $now = time();
217         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
218         [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
219         [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
221         $this->assertEquals(2, $vault->get_total_discussion_count_from_forum_id($forum->id, false, null));
222         $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, true, null));
223         $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id($forum->id, false, $user->id));
224     }
227     /**
228      * Test get_total_discussion_count_from_forum_id_and_group_id.
229      */
230     public function test_get_total_discussion_count_from_forum_id_and_group_id() {
231         $this->resetAfterTest();
233         $datagenerator = $this->getDataGenerator();
234         $vault = $this->vault;
235         $user = $datagenerator->create_user();
236         self::setUser($user);
237         $course = $datagenerator->create_course();
238         $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
240         $this->assertEquals([], $vault->get_from_forum_id($forum->id, true, true, null, 0, 0, $user));
242         $now = time();
243         [$discussion1, $post1] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 10, 'timemodified' => 1]);
244         [$discussion2, $post2] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now - 9, 'timemodified' => 2]);
245         [$hiddendiscussion, $post3] = $this->helper_post_to_forum($forum, $user, ['timestart' => $now + 10, 'timemodified' => 3]);
246         [$groupdiscussion1, $post4] = $this->helper_post_to_forum(
247             $forum,
248             $user,
249             ['timestart' => $now - 8, 'timemodified' => 4, 'groupid' => 1]
250         );
251         [$groupdiscussion2, $post5] = $this->helper_post_to_forum(
252             $forum,
253             $user,
254             ['timestart' => $now - 7, 'timemodified' => 5, 'groupid' => 2]
255         );
256         [$hiddengroupdiscussion, $post6] = $this->helper_post_to_forum(
257             $forum,
258             $user,
259             ['timestart' => $now + 11, 'timemodified' => 6, 'groupid' => 3]
260         );
262         $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3], true, null));
263         $this->assertEquals(6, $vault->get_total_discussion_count_from_forum_id_and_group_id(
264             $forum->id,
265             [1, 2, 3],
266             false,
267             $user->id
268         ));
269         $this->assertEquals(4, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [1, 2, 3], false, null));
270         $this->assertEquals(3, $vault->get_total_discussion_count_from_forum_id_and_group_id($forum->id, [], true, null));
271     }