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 - 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
namespace tool_courserating;
18
 
19
use tool_courserating\local\models\flag;
20
use tool_courserating\local\models\rating;
21
use tool_courserating\local\models\summary;
22
 
23
/**
24
 * Tests for permission class
25
 *
26
 * @package     tool_courserating
27
 * @covers      \tool_courserating\permission
28
 * @copyright   2022 Marina Glancy <marina.glancy@gmail.com>
29
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
final class observer_test extends \advanced_testcase {
32
 
33
    /**
34
     * Set up
35
     */
36
    public function setUp(): void {
37
        $this->resetAfterTest();
38
        set_config(\tool_courserating\constants::SETTING_RATINGMODE,
39
            \tool_courserating\constants::RATEBY_ANYTIME, 'tool_courserating');
40
    }
41
 
42
    /**
43
     * Generator
44
     *
45
     * @return \tool_courserating_generator
46
     */
47
    protected function get_generator(): \tool_courserating_generator {
48
        /** @var \tool_courserating_generator $generator */
49
        $generator = self::getDataGenerator()->get_plugin_generator('tool_courserating');
50
        return $generator;
51
    }
52
 
53
    public function test_course_updated(): void {
54
        $course = $this->getDataGenerator()->create_course();
55
 
56
        $this->get_generator()->set_config(constants::SETTING_PERCOURSE, 1);
57
        $this->get_generator()->set_config(constants::SETTING_RATINGMODE, constants::RATEBY_NOONE);
58
 
59
        $this->assertEquals(constants::RATEBY_NOONE, summary::get_for_course($course->id)->get('ratingmode'));
60
        update_course((object)['id' => $course->id, 'customfield_'.constants::CFIELD_RATINGMODE => constants::RATEBY_ANYTIME]);
61
        $this->assertEquals(constants::RATEBY_ANYTIME, summary::get_for_course($course->id)->get('ratingmode'));
62
    }
63
 
64
    public function test_course_deleted(): void {
65
        global $DB;
66
        $course = $this->getDataGenerator()->create_course();
67
        $user1 = $this->getDataGenerator()->create_user();
68
        $user2 = $this->getDataGenerator()->create_user();
69
 
70
        $this->setUser($user1);
71
        $rating = api::set_rating($course->id, (object)['rating' => 5]);
72
 
73
        $this->setUser($user2);
74
        api::flag_review($rating->get('id'));
75
 
76
        delete_course($course->id, false);
77
        $this->assertEmpty($DB->get_records(summary::TABLE, ['courseid' => $course->id]));
78
        $this->assertEmpty($DB->get_records(rating::TABLE, []));
79
        $this->assertEmpty($DB->get_records(flag::TABLE, []));
80
    }
81
 
82
    public function test_course_created(): void {
83
        global $DB;
84
        $course = $this->getDataGenerator()->create_course();
85
        $this->assertNotEmpty($DB->get_records(summary::TABLE, ['courseid' => $course->id]));
86
    }
87
}