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
namespace core_courseformat;
18
 
19
/**
20
 * Course format actions class tests.
21
 *
22
 * @package    core_courseformat
23
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 25
 * @coversDefaultClass \core_courseformat\formatactions
1 efrain 26
 */
1441 ariadna 27
final class formatactions_test extends \advanced_testcase {
1 efrain 28
 
29
    /**
30
     * Setup to ensure that fixtures are loaded.
31
     */
32
    public static function setUpBeforeClass(): void {
33
        global $CFG;
34
        require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
35
        require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_courseactions.php');
36
        require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_sectionactions.php');
37
        require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_cmactions.php');
1441 ariadna 38
        parent::setUpBeforeClass();
1 efrain 39
    }
40
 
41
    /**
42
     * Test for get_instance static method.
43
     * @dataProvider provider_classname_action
44
     * @covers ::instance
45
     * @param string $format
46
     * @param array $classnames
47
     */
48
    public function test_instance(string $format, array $classnames): void {
49
        $this->resetAfterTest();
50
        $course = $this->getDataGenerator()->create_course(['format' => $format]);
51
 
52
        $instance1 = formatactions::instance($course);
53
        $this->assertInstanceOf('\core_courseformat\formatactions', $instance1);
54
 
55
        $instance2 = formatactions::instance($course->id);
56
        $this->assertInstanceOf('\core_courseformat\formatactions', $instance2);
57
 
58
        // Validate the method is caching the result.
59
        $this->assertEquals($instance1, $instance2);
60
 
61
        // Validate public attribute classes.
62
        $this->assertInstanceOf($classnames['course'], $instance1->course);
63
        $this->assertInstanceOf($classnames['section'], $instance1->section);
64
        $this->assertInstanceOf($classnames['cm'], $instance1->cm);
65
    }
66
 
67
    /**
68
     * Test that the course action instance is created correctly.
69
     * @dataProvider provider_classname_action
70
     * @covers ::course
71
     * @param string $format
72
     * @param array $classnames
73
     */
74
    public function test_course_action_instance(string $format, array $classnames): void {
75
        $this->resetAfterTest();
76
        $course = $this->getDataGenerator()->create_course(['format' => $format]);
77
 
78
        $instance1 = formatactions::course($course);
79
        $this->assertInstanceOf($classnames['course'], $instance1);
80
 
81
        $instance2 = formatactions::course($course->id);
82
        $this->assertInstanceOf($classnames['course'], $instance2);
83
    }
84
 
85
    /**
86
     * Test that the section action instance is created correctly.
87
     * @dataProvider provider_classname_action
88
     * @covers ::section
89
     *
90
     * @param string $format
91
     * @param array $classnames
92
     */
93
    public function test_static_sectionactions_instance(string $format, array $classnames): void {
94
        $this->resetAfterTest();
95
        $course = $this->getDataGenerator()->create_course(['format' => $format]);
96
 
97
        $instance1 = formatactions::section($course);
98
        $this->assertInstanceOf($classnames['section'], $instance1);
99
 
100
        $instance2 = formatactions::section($course->id);
101
        $this->assertInstanceOf($classnames['section'], $instance2);
102
    }
103
 
104
    /**
105
     * Test that the cm action instance is created correctly.
106
     * @dataProvider provider_classname_action
107
     * @covers ::cm
108
     *
109
     * @param string $format
110
     * @param array $classnames
111
     */
112
    public function test_static_cmactions_instance(string $format, array $classnames): void {
113
        $this->resetAfterTest();
114
        $course = $this->getDataGenerator()->create_course(['format' => $format]);
115
 
116
        $instance1 = formatactions::cm($course);
117
        $this->assertInstanceOf($classnames['cm'], $instance1);
118
 
119
        $instance2 = formatactions::cm($course->id);
120
        $this->assertInstanceOf($classnames['cm'], $instance2);
121
    }
122
 
123
    /**
124
     * Data provider for format class names scenarios.
125
     * @return array
126
     */
127
    public static function provider_classname_action(): array {
128
        return [
129
            'Topics format' => [
130
                'format' => 'topics',
131
                'classnames' => [
132
                    'course' => '\core_courseformat\local\courseactions',
133
                    'section' => '\core_courseformat\local\sectionactions',
134
                    'cm' => '\core_courseformat\local\cmactions',
135
                ],
136
            ],
137
            'The unit test fixture format' => [
138
                'format' => 'theunittest',
139
                'classnames' => [
140
                    'course' => '\format_theunittest\courseformat\courseactions',
141
                    'section' => '\format_theunittest\courseformat\sectionactions',
142
                    'cm' => '\format_theunittest\courseformat\cmactions',
143
                ],
144
            ],
145
        ];
146
    }
147
}