Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16
 
17
namespace tool_courserating\output;
18
 
19
use tool_courserating\api;
20
 
21
/**
22
 * Tests for report for 3.11
23
 *
24
 * TODO remove when the minimum supported version is Moodle 4.0
25
 *
26
 * @package     tool_courserating
27
 * @copyright   2022 Marina Glancy
28
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 *
30
 * @covers \tool_courserating\output\report311
31
 */
32
final class report311_test extends \advanced_testcase {
33
    /** @var \stdClass */
34
    protected $course;
35
 
36
    /**
37
     * setUp
38
     */
39
    protected function setUp(): void {
40
        $this->resetAfterTest();
41
        set_config(\tool_courserating\constants::SETTING_RATINGMODE,
42
            \tool_courserating\constants::RATEBY_ANYTIME, 'tool_courserating');
43
    }
44
 
45
    /**
46
     * Set up for test
47
     */
48
    protected function set_up_for_test() {
49
        $course = $this->getDataGenerator()->create_course();
50
        $this->getDataGenerator()->create_course();
51
        $user1 = $this->getDataGenerator()->create_user(['firstname' => 'User1']);
52
        $user2 = $this->getDataGenerator()->create_user(['firstname' => 'User2']);
53
        /** @var \tool_courserating_generator $generator */
54
        $generator = self::getDataGenerator()->get_plugin_generator('tool_courserating');
55
        $rating1 = $generator->create_rating($user1->id, $course->id, 3, 'hello <b>unclosed tag');
56
        sleep(1); // Make sure timestamp is different on the ratings.
57
        $rating2 = $generator->create_rating($user2->id, $course->id, 2);
58
 
59
        $this->setUser($user2);
60
        api::flag_review($rating1->get('id'));
61
        $this->setAdminUser();
62
 
63
        $this->course = $course;
64
    }
65
 
66
    /**
67
     * Test for report content
68
     *
69
     * @return void
70
     */
71
    public function test_content(): void {
72
        $this->set_up_for_test();
73
 
74
        $report = new report311(new \moodle_url('/admin/tool/courserating/index.php', ['id' => $this->course->id]));
75
        $report->setup();
76
        $report->query_db(50, false);
77
 
78
        // Analyse raw results.
79
        $content = $report->rawdata;
80
        $this->assertCount(2, $content);
81
 
82
        $this->assertEquals([2, 3], array_column($content, 'rating'));
83
        $this->assertEquals(['User2', 'User1'], array_column($content, 'firstname'));
84
        $this->assertEquals(['', 'hello <b>unclosed tag'], array_column($content, 'review'));
85
        $this->assertEquals([0, 1], array_column($content, 'flags'));
86
 
87
        // Analyse formatted output.
88
        ob_start();
89
        $report->build_table();
90
        $report->close_recordset();
91
        $report->finish_output();
92
        $output = ob_get_contents();
93
        ob_end_clean();
94
 
95
        $this->assertEquals(false, strpos($output, 'hello <b>unclosed tag'));
96
        $this->assertNotEmpty(strpos($output, '<div class="text_to_html">hello unclosed tag</div>'));
97
    }
98
}