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
+ defined ('MOODLE_INTERNAL ' ) || die ();
18
+ require_once (__DIR__ . "/../../../lib/cronlib.php " );
19
+
20
+ class mod_moodleoverflow_search_testcase extends advanced_testcase {
21
+
22
+ public function test_for_no_content () {
23
+ $ this ->resetAfterTest ();
24
+ global $ CFG ;
25
+ $ CFG ->enableglobalsearch = 1 ;
26
+ $ searchmanager = \core_search \manager::instance ();
27
+ $ searchmanager ->index (true );
28
+ $ search = \core_search \manager::instance ();
29
+ $ results = $ search ->search ((object )['q ' => "" ]);
30
+ // Will find the site itself, so 1 result is ok.
31
+ $ this ->assertEquals (1 , count ($ results ));
32
+ }
33
+
34
+ public function test_discussion_discussion () {
35
+ global $ CFG ;
36
+ $ this ->resetAfterTest ();
37
+ $ CFG ->enableglobalsearch = 1 ;
38
+
39
+ $ course = $ this ->getDataGenerator ()->create_course ();
40
+ $ moodleoverflow = $ this ->getDataGenerator ()->create_module ("moodleoverflow " , array ("course " => $ course ));
41
+ $ moodleoverflowgen = $ this ->getDataGenerator ()->get_plugin_generator ('mod_moodleoverflow ' );
42
+ $ user = $ this ->getDataGenerator ()->create_user ();
43
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
44
+ $ this ->setUser ($ user );
45
+
46
+ [$ discussion , $ post1 ] = $ moodleoverflowgen ->post_to_forum ($ moodleoverflow , $ user );
47
+ $ post2 = $ moodleoverflowgen ->post_to_discussion ($ moodleoverflow , $ discussion , $ user );
48
+
49
+ $ searchmanager = \core_search \manager::instance ();
50
+ $ searchmanager ->index (true );
51
+ $ search = \core_search \manager::instance ();
52
+ $ results = $ search ->search ((object )['q ' => $ discussion ->name ]);
53
+ $ this ->assertEquals (2 , count ($ results ));
54
+ $ this ->assertEquals ($ post1 ->id , $ results [0 ]->get ('itemid ' ));
55
+ $ this ->assertEquals ($ post2 ->id , $ results [1 ]->get ('itemid ' ));
56
+ }
57
+
58
+ public function test_discussion_post () {
59
+ global $ CFG ;
60
+ $ this ->resetAfterTest ();
61
+ $ CFG ->enableglobalsearch = 1 ;
62
+
63
+ $ course = $ this ->getDataGenerator ()->create_course ();
64
+ $ moodleoverflow = $ this ->getDataGenerator ()->create_module ("moodleoverflow " , array ("course " => $ course ));
65
+ $ moodleoverflowgen = $ this ->getDataGenerator ()->get_plugin_generator ('mod_moodleoverflow ' );
66
+ $ user = $ this ->getDataGenerator ()->create_user ();
67
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
68
+ $ this ->setUser ($ user );
69
+
70
+ [$ discussion , $ post ] = $ moodleoverflowgen ->post_to_forum ($ moodleoverflow , $ user );
71
+
72
+ $ searchmanager = \core_search \manager::instance ();
73
+ $ searchmanager ->index (true );
74
+ $ search = \core_search \manager::instance ();
75
+ $ results = $ search ->search ((object )['q ' => $ post ->message ]);
76
+ $ this ->assertEquals (1 , count ($ results ));
77
+ $ this ->assertEquals ($ post ->id , $ results [0 ]->get ('itemid ' ));
78
+ }
79
+
80
+ public function test_deleted_discussion () {
81
+ global $ CFG ;
82
+ $ this ->resetAfterTest ();
83
+ $ CFG ->enableglobalsearch = 1 ;
84
+
85
+ $ course = $ this ->getDataGenerator ()->create_course ();
86
+ $ moodleoverflow = $ this ->getDataGenerator ()->create_module ("moodleoverflow " , array ("course " => $ course ));
87
+ $ moodleoverflowgen = $ this ->getDataGenerator ()->get_plugin_generator ('mod_moodleoverflow ' );
88
+ $ user = $ this ->getDataGenerator ()->create_user ();
89
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
90
+ $ this ->setUser ($ user );
91
+
92
+ [$ discussion , $ post ] = $ moodleoverflowgen ->post_to_forum ($ moodleoverflow , $ user );
93
+
94
+ $ cm = get_coursemodule_from_instance ('moodleoverflow ' , $ moodleoverflow ->id , $ course ->id );
95
+ moodleoverflow_delete_discussion ($ discussion , $ course , $ cm , $ moodleoverflow );
96
+
97
+ $ searchmanager = \core_search \manager::instance ();
98
+ $ searchmanager ->index (true );
99
+ $ search = \core_search \manager::instance ();
100
+ $ results = $ search ->search ((object )['q ' => $ post ->message ]);
101
+ $ this ->assertEquals (0 , count ($ results ));
102
+
103
+ $ accessmanager = new \mod_moodleoverflow \search \moodleoverflowposts ();
104
+ $ access = $ accessmanager ->check_access ($ post ->id );
105
+ $ this ->assertEquals (2 , $ access );
106
+ }
107
+
108
+ public function test_deleted_post () {
109
+ global $ CFG ;
110
+ $ this ->resetAfterTest ();
111
+ $ CFG ->enableglobalsearch = 1 ;
112
+
113
+ $ course = $ this ->getDataGenerator ()->create_course ();
114
+ $ moodleoverflow = $ this ->getDataGenerator ()->create_module ("moodleoverflow " , array ("course " => $ course ));
115
+ $ moodleoverflowgen = $ this ->getDataGenerator ()->get_plugin_generator ('mod_moodleoverflow ' );
116
+ $ user = $ this ->getDataGenerator ()->create_user ();
117
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
118
+ $ this ->setUser ($ user );
119
+
120
+ [$ discussion , $ post ] = $ moodleoverflowgen ->post_to_forum ($ moodleoverflow , $ user );
121
+ $ post2 = $ moodleoverflowgen ->post_to_discussion ($ moodleoverflow , $ discussion , $ user );
122
+
123
+ $ cm = get_coursemodule_from_instance ('moodleoverflow ' , $ moodleoverflow ->id , $ course ->id );
124
+ moodleoverflow_delete_post ($ post2 , true , $ course , $ cm , $ moodleoverflow );
125
+
126
+ $ searchmanager = \core_search \manager::instance ();
127
+ $ searchmanager ->index (true );
128
+ $ search = \core_search \manager::instance ();
129
+ $ results = $ search ->search ((object )['q ' => $ post2 ->message ]);
130
+ $ this ->assertEquals (0 , count ($ results ));
131
+
132
+ $ accessmanager = new \mod_moodleoverflow \search \moodleoverflowposts ();
133
+ $ access = $ accessmanager ->check_access ($ post2 ->id );
134
+ $ this ->assertEquals (2 , $ access );
135
+ }
136
+
137
+ public function test_forbidden_discussion () {
138
+ global $ CFG ;
139
+ $ this ->resetAfterTest ();
140
+ $ CFG ->enableglobalsearch = 1 ;
141
+
142
+ $ course = $ this ->getDataGenerator ()->create_course ();
143
+ $ moodleoverflow = $ this ->getDataGenerator ()->create_module ("moodleoverflow " , array ("course " => $ course ));
144
+ $ moodleoverflowgen = $ this ->getDataGenerator ()->get_plugin_generator ('mod_moodleoverflow ' );
145
+ $ user = $ this ->getDataGenerator ()->create_user ();
146
+ $ user2 = $ this ->getDataGenerator ()->create_user ();
147
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
148
+ $ this ->setUser ($ user2 );
149
+
150
+ [$ discussion , $ post ] = $ moodleoverflowgen ->post_to_forum ($ moodleoverflow , $ user );
151
+
152
+ $ searchmanager = \core_search \manager::instance ();
153
+ $ searchmanager ->index (true );
154
+ $ search = \core_search \manager::instance ();
155
+ $ results = $ search ->search ((object )['q ' => $ discussion ->name ]);
156
+ $ this ->assertEquals (0 , count ($ results ));
157
+ }
158
+
159
+ public function test_forbidden_post () {
160
+ global $ CFG ;
161
+ $ this ->resetAfterTest ();
162
+ $ CFG ->enableglobalsearch = 1 ;
163
+
164
+ $ course = $ this ->getDataGenerator ()->create_course ();
165
+ $ moodleoverflow = $ this ->getDataGenerator ()->create_module ("moodleoverflow " , array ("course " => $ course ));
166
+ $ moodleoverflowgen = $ this ->getDataGenerator ()->get_plugin_generator ('mod_moodleoverflow ' );
167
+ $ user = $ this ->getDataGenerator ()->create_user ();
168
+ $ user2 = $ this ->getDataGenerator ()->create_user ();
169
+ $ this ->getDataGenerator ()->enrol_user ($ user ->id , $ course ->id );
170
+ $ this ->setUser ($ user2 );
171
+
172
+ [$ discussion , $ post ] = $ moodleoverflowgen ->post_to_forum ($ moodleoverflow , $ user );
173
+
174
+ $ searchmanager = \core_search \manager::instance ();
175
+ $ searchmanager ->index (true );
176
+ $ search = \core_search \manager::instance ();
177
+ $ results = $ search ->search ((object )['q ' => $ post ->message ]);
178
+ $ this ->assertEquals (0 , count ($ results ));
179
+
180
+ $ accessmanager = new \mod_moodleoverflow \search \moodleoverflowposts ();
181
+ $ access = $ accessmanager ->check_access ($ post ->id );
182
+ $ this ->assertEquals (0 , $ access );
183
+ }
184
+
185
+ }
0 commit comments