Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Unit tests for blog
19
 *
20
 * @package    core_blog
21
 * @category   phpunit
22
 * @copyright  2009 Nicolas Connault
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace core_blog;
26
 
27
use blog_listing;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
global $CFG;
32
require_once($CFG->dirroot . '/blog/locallib.php');
33
require_once($CFG->dirroot . '/blog/lib.php');
34
 
35
/**
36
 * Test functions that rely on the DB tables
37
 */
38
class lib_test extends \advanced_testcase {
39
 
40
    private $courseid;
41
    private $cmid;
42
    private $groupid;
43
    private $userid;
44
    private $tagid;
45
    private $postid;
46
 
47
    protected function setUp(): void {
48
        global $DB;
49
        parent::setUp();
50
 
51
        $this->resetAfterTest();
52
 
53
        // Create default course.
54
        $course = $this->getDataGenerator()->create_course(array('category' => 1, 'shortname' => 'ANON'));
55
        $this->assertNotEmpty($course);
56
        $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
57
        $this->assertNotEmpty($page);
58
 
59
        // Create default group.
60
        $group = new \stdClass();
61
        $group->courseid = $course->id;
62
        $group->name = 'ANON';
63
        $group->id = $DB->insert_record('groups', $group);
64
 
65
        // Create default user.
66
        $user = $this->getDataGenerator()->create_user(array(
67
                'username' => 'testuser',
68
                'firstname' => 'Jimmy',
69
                'lastname' => 'Kinnon'
70
        ));
71
 
72
        // Create default tag.
73
        $tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id,
74
            'rawname' => 'Testtagname', 'isstandard' => 1));
75
 
76
        // Create default post.
77
        $post = new \stdClass();
78
        $post->userid = $user->id;
79
        $post->groupid = $group->id;
80
        $post->content = 'test post content text';
81
        $post->module = 'blog';
82
        $post->id = $DB->insert_record('post', $post);
83
 
84
        // Grab important ids.
85
        $this->courseid = $course->id;
86
        $this->cmid = $page->cmid;
87
        $this->groupid  = $group->id;
88
        $this->userid  = $user->id;
89
        $this->tagid  = $tag->id;
90
        $this->postid = $post->id;
91
    }
92
 
93
 
11 efrain 94
    public function test_overrides(): void {
1 efrain 95
        global $SITE;
96
 
97
        // Try all the filters at once: Only the entry filter is active.
98
        $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
99
            'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->tagid, 'entry' => $this->postid);
100
        $bloglisting = new blog_listing($filters);
101
        $this->assertFalse(array_key_exists('site', $bloglisting->filters));
102
        $this->assertFalse(array_key_exists('course', $bloglisting->filters));
103
        $this->assertFalse(array_key_exists('module', $bloglisting->filters));
104
        $this->assertFalse(array_key_exists('group', $bloglisting->filters));
105
        $this->assertFalse(array_key_exists('user', $bloglisting->filters));
106
        $this->assertFalse(array_key_exists('tag', $bloglisting->filters));
107
        $this->assertTrue(array_key_exists('entry', $bloglisting->filters));
108
 
109
        // Again, but without the entry filter: This time, the tag, user and module filters are active.
110
        $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
111
            'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->postid);
112
        $bloglisting = new blog_listing($filters);
113
        $this->assertFalse(array_key_exists('site', $bloglisting->filters));
114
        $this->assertFalse(array_key_exists('course', $bloglisting->filters));
115
        $this->assertFalse(array_key_exists('group', $bloglisting->filters));
116
        $this->assertTrue(array_key_exists('module', $bloglisting->filters));
117
        $this->assertTrue(array_key_exists('user', $bloglisting->filters));
118
        $this->assertTrue(array_key_exists('tag', $bloglisting->filters));
119
 
120
        // We should get the same result by removing the 3 inactive filters: site, course and group.
121
        $filters = array('module' => $this->cmid, 'user' => $this->userid, 'tag' => $this->tagid);
122
        $bloglisting = new blog_listing($filters);
123
        $this->assertFalse(array_key_exists('site', $bloglisting->filters));
124
        $this->assertFalse(array_key_exists('course', $bloglisting->filters));
125
        $this->assertFalse(array_key_exists('group', $bloglisting->filters));
126
        $this->assertTrue(array_key_exists('module', $bloglisting->filters));
127
        $this->assertTrue(array_key_exists('user', $bloglisting->filters));
128
        $this->assertTrue(array_key_exists('tag', $bloglisting->filters));
129
 
130
    }
131
 
132
    // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php.
133
    // Some cases are omitted due to the optional_param variables used.
134
 
11 efrain 135
    public function test_blog_get_headers_case_1(): void {
1 efrain 136
        global $CFG, $PAGE, $OUTPUT;
137
        $blogheaders = blog_get_headers();
138
        $this->assertEquals($blogheaders['heading'], get_string('siteblogheading', 'blog'));
139
    }
140
 
11 efrain 141
    public function test_blog_get_headers_case_6(): void {
1 efrain 142
        global $CFG, $PAGE, $OUTPUT;
143
        $blogheaders = blog_get_headers($this->courseid, null, $this->userid);
144
        $this->assertNotEquals($blogheaders['heading'], '');
145
    }
146
 
11 efrain 147
    public function test_blog_get_headers_case_7(): void {
1 efrain 148
        global $CFG, $PAGE, $OUTPUT;
149
        $blogheaders = blog_get_headers(null, $this->groupid);
150
        $this->assertNotEquals($blogheaders['heading'], '');
151
    }
152
 
11 efrain 153
    public function test_blog_get_headers_case_10(): void {
1 efrain 154
        global $CFG, $PAGE, $OUTPUT;
155
        $blogheaders = blog_get_headers($this->courseid);
156
        $this->assertNotEquals($blogheaders['heading'], '');
157
    }
158
 
159
    /**
160
     * Tests the core_blog_myprofile_navigation() function.
161
     */
11 efrain 162
    public function test_core_blog_myprofile_navigation(): void {
1 efrain 163
        global $USER;
164
 
165
        // Set up the test.
166
        $tree = new \core_user\output\myprofile\tree();
167
        $this->setAdminUser();
168
        $iscurrentuser = true;
169
        $course = null;
170
 
171
        // Enable blogs.
172
        set_config('enableblogs', true);
173
 
174
        // Check the node tree is correct.
175
        core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
176
        $reflector = new \ReflectionObject($tree);
177
        $nodes = $reflector->getProperty('nodes');
178
        $this->assertArrayHasKey('blogs', $nodes->getValue($tree));
179
    }
180
 
181
    /**
182
     * Tests the core_blog_myprofile_navigation() function as a guest.
183
     */
11 efrain 184
    public function test_core_blog_myprofile_navigation_as_guest(): void {
1 efrain 185
        global $USER;
186
 
187
        // Set up the test.
188
        $tree = new \core_user\output\myprofile\tree();
189
        $iscurrentuser = false;
190
        $course = null;
191
 
192
        // Set user as guest.
193
        $this->setGuestUser();
194
 
195
        // Check the node tree is correct.
196
        core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
197
        $reflector = new \ReflectionObject($tree);
198
        $nodes = $reflector->getProperty('nodes');
199
        $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
200
    }
201
 
202
    /**
203
     * Tests the core_blog_myprofile_navigation() function when blogs are disabled.
204
     */
11 efrain 205
    public function test_core_blog_myprofile_navigation_blogs_disabled(): void {
1 efrain 206
        global $USER;
207
 
208
        // Set up the test.
209
        $tree = new \core_user\output\myprofile\tree();
210
        $this->setAdminUser();
211
        $iscurrentuser = false;
212
        $course = null;
213
 
214
        // Disable blogs.
215
        set_config('enableblogs', false);
216
 
217
        // Check the node tree is correct.
218
        core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
219
        $reflector = new \ReflectionObject($tree);
220
        $nodes = $reflector->getProperty('nodes');
221
        $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
222
    }
223
 
11 efrain 224
    public function test_blog_get_listing_course(): void {
1 efrain 225
        $this->setAdminUser();
226
        $coursecontext = \context_course::instance($this->courseid);
227
        $anothercourse = $this->getDataGenerator()->create_course();
228
 
229
        // Add blog associations with a course.
230
        $blog = new \blog_entry($this->postid);
231
        $blog->add_association($coursecontext->id);
232
 
233
        // There is one entry associated with a course.
234
        $bloglisting = new blog_listing(array('course' => $this->courseid));
235
        $this->assertCount(1, $bloglisting->get_entries());
236
 
237
        // There is no entry associated with a wrong course.
238
        $bloglisting = new blog_listing(array('course' => $anothercourse->id));
239
        $this->assertCount(0, $bloglisting->get_entries());
240
 
241
        // There is no entry associated with a module.
242
        $bloglisting = new blog_listing(array('module' => $this->cmid));
243
        $this->assertCount(0, $bloglisting->get_entries());
244
 
245
        // There is one entry associated with a site (id is ignored).
246
        $bloglisting = new blog_listing(array('site' => 12345));
247
        $this->assertCount(1, $bloglisting->get_entries());
248
 
249
        // There is one entry associated with course context.
250
        $bloglisting = new blog_listing(array('context' => $coursecontext->id));
251
        $this->assertCount(1, $bloglisting->get_entries());
252
    }
253
 
11 efrain 254
    public function test_blog_get_listing_module(): void {
1 efrain 255
        $this->setAdminUser();
256
        $coursecontext = \context_course::instance($this->courseid);
257
        $contextmodule = \context_module::instance($this->cmid);
258
        $anothermodule = $this->getDataGenerator()->create_module('page', array('course' => $this->courseid));
259
 
260
        // Add blog associations with a course.
261
        $blog = new \blog_entry($this->postid);
262
        $blog->add_association($contextmodule->id);
263
 
264
        // There is no entry associated with a course.
265
        $bloglisting = new blog_listing(array('course' => $this->courseid));
266
        $this->assertCount(0, $bloglisting->get_entries());
267
 
268
        // There is one entry associated with a module.
269
        $bloglisting = new blog_listing(array('module' => $this->cmid));
270
        $this->assertCount(1, $bloglisting->get_entries());
271
 
272
        // There is no entry associated with a wrong module.
273
        $bloglisting = new blog_listing(array('module' => $anothermodule->cmid));
274
        $this->assertCount(0, $bloglisting->get_entries());
275
 
276
        // There is one entry associated with a site (id is ignored).
277
        $bloglisting = new blog_listing(array('site' => 12345));
278
        $this->assertCount(1, $bloglisting->get_entries());
279
 
280
        // There is one entry associated with course context (module is a subcontext of a course).
281
        $bloglisting = new blog_listing(array('context' => $coursecontext->id));
282
        $this->assertCount(1, $bloglisting->get_entries());
283
    }
284
}
285