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
declare(strict_types=1);
18
 
19
namespace core_blog\reportbuilder\datasource;
20
 
21
use core_blog_generator;
22
use core_comment_generator;
1441 ariadna 23
use core\context\{system, user};
1 efrain 24
use core_reportbuilder_generator;
25
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
1441 ariadna 26
use core_reportbuilder\tests\core_reportbuilder_testcase;
1 efrain 27
 
28
/**
29
 * Unit tests for blogs datasource
30
 *
31
 * @package     core_blog
32
 * @covers      \core_blog\reportbuilder\datasource\blogs
33
 * @copyright   2022 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class blogs_test extends core_reportbuilder_testcase {
1 efrain 37
 
38
    /**
39
     * Test default datasource
40
     */
41
    public function test_datasource_default(): void {
42
        $this->resetAfterTest();
43
 
44
        /** @var core_blog_generator $blogsgenerator */
45
        $blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
46
 
47
        // Our first user will create a course blog.
48
        $course = $this->getDataGenerator()->create_course();
49
        $userone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
50
        $courseblog = $blogsgenerator->create_entry(['publishstate' => 'site', 'userid' => $userone->id,
51
            'subject' => 'Course', 'summary' => 'Course summary', 'courseid' => $course->id]);
52
 
53
        // Our second user will create a personal and site blog.
54
        $usertwo = $this->getDataGenerator()->create_user(['firstname' => 'Amy']);
55
        $personalblog = $blogsgenerator->create_entry(['publishstate' => 'draft', 'userid' => $usertwo->id,
56
            'subject' => 'Personal', 'summary' => 'Personal summary']);
57
 
58
        $this->waitForSecond(); // For consistent ordering we need distinct time for second user blogs.
59
        $siteblog = $blogsgenerator->create_entry(['publishstate' => 'public', 'userid' => $usertwo->id,
60
            'subject' => 'Site', 'summary' => 'Site summary']);
61
 
62
        /** @var core_reportbuilder_generator $generator */
63
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
64
        $report = $generator->create_report(['name' => 'Blogs', 'source' => blogs::class, 'default' => 1]);
65
 
66
        $content = $this->get_custom_report_content($report->get('id'));
67
 
68
        // Default columns are user, course, title, time created. Sorted by user and time created.
69
        $this->assertEquals([
70
            [fullname($usertwo), '', $personalblog->subject, userdate($personalblog->created)],
71
            [fullname($usertwo), '', $siteblog->subject, userdate($siteblog->created)],
72
            [fullname($userone), $course->fullname, $courseblog->subject, userdate($courseblog->created)],
73
        ], array_map('array_values', $content));
74
    }
75
 
76
    /**
77
     * Test datasource columns that aren't added by default
78
     */
79
    public function test_datasource_non_default_columns(): void {
80
        global $DB;
81
 
82
        $this->resetAfterTest();
83
 
84
        $user = $this->getDataGenerator()->create_user();
85
        $this->setUser($user);
86
 
87
        /** @var core_blog_generator $blogsgenerator */
88
        $blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
89
        $blog = $blogsgenerator->create_entry(['publishstate' => 'draft', 'userid' => $user->id, 'subject' => 'My blog',
90
            'summary' => 'Horses', 'tags' => ['horse']]);
91
 
92
        // Add an attachment.
93
        $blog->attachment = 1;
94
        get_file_storage()->create_file_from_string([
1441 ariadna 95
            'contextid' => system::instance()->id,
1 efrain 96
            'component' => 'blog',
97
            'filearea' => 'attachment',
98
            'itemid' => $blog->id,
99
            'filepath' => '/',
100
            'filename' => 'hello.txt',
101
        ], 'hello');
102
 
103
        /** @var core_comment_generator $generator */
104
        $generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
105
        $generator->create_comment([
1441 ariadna 106
            'context' => user::instance($user->id),
1 efrain 107
            'component' => 'blog',
108
            'area' => 'format_blog',
109
            'itemid' => $blog->id,
110
            'content' => 'Cool',
111
        ]);
112
 
113
        // Manually update the created/modified date of the blog.
114
        $blog->created = 1654038000;
115
        $blog->lastmodified = $blog->created + HOURSECS;
116
        $DB->update_record('post', $blog);
117
 
118
        /** @var core_reportbuilder_generator $generator */
119
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
120
        $report = $generator->create_report(['name' => 'Blogs', 'source' => blogs::class, 'default' => 0]);
121
 
122
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:titlewithlink']);
123
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:body']);
124
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:attachment']);
125
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:publishstate']);
126
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'blog:timemodified']);
127
 
128
        // Tag entity (course/user presence already checked by default columns).
129
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']);
130
 
131
        // File entity.
132
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'file:size']);
133
 
134
        // Comment entity.
135
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:content']);
136
 
137
        // Commenter entity.
138
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'commenter:fullname']);
139
 
140
        $content = $this->get_custom_report_content($report->get('id'));
141
        $this->assertCount(1, $content);
142
 
143
        [
144
            $link,
145
            $body,
146
            $attachment,
147
            $publishstate,
148
            $timemodified,
149
            $tags,
150
            $filesize,
151
            $comment,
152
            $commenter,
153
        ] = array_values($content[0]);
154
 
155
        $this->assertEquals("<a href=\"https://www.example.com/moodle/blog/index.php?entryid={$blog->id}\">{$blog->subject}</a>",
156
            $link);
157
        $this->assertStringContainsString('Horses', $body);
158
        $this->assertStringContainsString('hello.txt', $attachment);
159
        $this->assertEquals('Draft', $publishstate);
160
        $this->assertEquals(userdate($blog->lastmodified), $timemodified);
161
        $this->assertEquals('horse', $tags);
162
        $this->assertEquals("5\xc2\xa0bytes", $filesize);
163
        $this->assertEquals(format_text('Cool'), $comment);
164
        $this->assertEquals(fullname($user), $commenter);
165
    }
166
 
167
    /**
168
     * Data provider for {@see test_datasource_filters}
169
     *
170
     * @return array[]
171
     */
1441 ariadna 172
    public static function datasource_filters_provider(): array {
1 efrain 173
        return [
174
            'Filter title' => ['subject', 'Cool', 'blog:title', [
175
                'blog:title_operator' => text::CONTAINS,
176
                'blog:title_value' => 'Cool',
177
            ], true],
178
            'Filter title (no match)' => ['subject', 'Cool', 'blog:title', [
179
                'blog:title_operator' => text::CONTAINS,
180
                'blog:title_value' => 'Beans',
181
            ], false],
182
            'Filter body' => ['summary', 'Awesome', 'blog:body', [
183
                'blog:body_operator' => select::EQUAL_TO,
184
                'blog:body_value' => 'Awesome',
185
            ], true],
186
            'Filter body (no match)' => ['summary', 'Awesome', 'blog:body', [
187
                'blog:body_operator' => select::EQUAL_TO,
188
                'blog:body_value' => 'Beans',
189
            ], false],
190
            'Filter attachment' => ['attachment', 1, 'blog:attachment', [
191
                'blog:attachment_operator' => boolean_select::CHECKED,
192
            ], true],
193
            'Filter attachment (no match)' => ['attachment', 1, 'blog:attachment', [
194
                'blog:attachment_operator' => boolean_select::NOT_CHECKED,
195
            ], false],
196
            'Filter publish state' => ['publishstate', 'site', 'blog:publishstate', [
197
                'blog:publishstate_operator' => select::EQUAL_TO,
198
                'blog:publishstate_value' => 'site',
199
            ], true],
200
            'Filter publish state (no match)' => ['publishstate', 'site', 'blog:publishstate', [
201
                'blog:publishstate_operator' => select::EQUAL_TO,
202
                'blog:publishstate_value' => 'draft',
203
            ], false],
204
            'Filter time created' => ['created', 1654038000, 'blog:timecreated', [
205
                'blog:timecreated_operator' => date::DATE_RANGE,
206
                'blog:timecreated_from' => 1622502000,
207
            ], true],
208
            'Filter time created (no match)' => ['created', 1654038000, 'blog:timecreated', [
209
                'blog:timecreated_operator' => date::DATE_RANGE,
210
                'blog:timecreated_to' => 1622502000,
211
            ], false],
212
            'Filter time modified' => ['lastmodified', 1654038000, 'blog:timemodified', [
213
                'blog:timemodified_operator' => date::DATE_RANGE,
214
                'blog:timemodified_from' => 1622502000,
215
            ], true],
216
            'Filter time modified (no match)' => ['lastmodified', 1654038000, 'blog:timemodified', [
217
                'blog:timemodified_operator' => date::DATE_RANGE,
218
                'blog:timemodified_to' => 1622502000,
219
            ], false],
220
        ];
221
    }
222
 
223
    /**
224
     * Test datasource filters
225
     *
226
     * @param string $field
227
     * @param mixed $value
228
     * @param string $filtername
229
     * @param array $filtervalues
230
     * @param bool $expectmatch
231
     *
232
     * @dataProvider datasource_filters_provider
233
     */
234
    public function test_datasource_filters(
235
        string $field,
236
        $value,
237
        string $filtername,
238
        array $filtervalues,
239
        bool $expectmatch
240
    ): void {
241
        global $DB;
242
 
243
        $this->resetAfterTest();
244
 
245
        $user = $this->getDataGenerator()->create_user();
246
 
247
        /** @var core_blog_generator $blogsgenerator */
248
        $blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
249
 
250
        // Create default blog, then manually override one of it's properties to use for filtering.
251
        $blog = $blogsgenerator->create_entry(['userid' => $user->id, 'subject' => 'My blog', 'summary' => 'Horses']);
252
        $DB->set_field('post', $field, $value, ['id' => $blog->id]);
253
 
254
        /** @var core_reportbuilder_generator $generator */
255
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
256
 
257
        // Create report containing single user column, and given filter.
258
        $report = $generator->create_report(['name' => 'Blogs', 'source' => blogs::class, 'default' => 0]);
259
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
260
 
261
        // Add filter, set it's values.
262
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
263
        $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
264
 
265
        if ($expectmatch) {
266
            $this->assertCount(1, $content);
267
            $this->assertEquals(fullname($user), reset($content[0]));
268
        } else {
269
            $this->assertEmpty($content);
270
        }
271
    }
272
 
273
    /**
274
     * Stress test datasource
275
     *
276
     * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
277
     */
278
    public function test_stress_datasource(): void {
279
        if (!PHPUNIT_LONGTEST) {
280
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
281
        }
282
 
283
        $this->resetAfterTest();
284
 
285
        $user = $this->getDataGenerator()->create_user();
286
 
287
        /** @var core_blog_generator $blogsgenerator */
288
        $blogsgenerator = $this->getDataGenerator()->get_plugin_generator('core_blog');
289
        $blogsgenerator->create_entry(['userid' => $user->id, 'subject' => 'My blog', 'summary' => 'Horses']);
290
 
291
        $this->datasource_stress_test_columns(blogs::class);
292
        $this->datasource_stress_test_columns_aggregation(blogs::class);
293
        $this->datasource_stress_test_conditions(blogs::class, 'blog:title');
294
    }
295
}