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
|
|
|
25 |
* @coversDefaultClass \core_courseformat\base
|
|
|
26 |
*/
|
|
|
27 |
class formatactions_test extends \advanced_testcase {
|
|
|
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');
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Test for get_instance static method.
|
|
|
42 |
* @dataProvider provider_classname_action
|
|
|
43 |
* @covers ::instance
|
|
|
44 |
* @param string $format
|
|
|
45 |
* @param array $classnames
|
|
|
46 |
*/
|
|
|
47 |
public function test_instance(string $format, array $classnames): void {
|
|
|
48 |
$this->resetAfterTest();
|
|
|
49 |
$course = $this->getDataGenerator()->create_course(['format' => $format]);
|
|
|
50 |
|
|
|
51 |
$instance1 = formatactions::instance($course);
|
|
|
52 |
$this->assertInstanceOf('\core_courseformat\formatactions', $instance1);
|
|
|
53 |
|
|
|
54 |
$instance2 = formatactions::instance($course->id);
|
|
|
55 |
$this->assertInstanceOf('\core_courseformat\formatactions', $instance2);
|
|
|
56 |
|
|
|
57 |
// Validate the method is caching the result.
|
|
|
58 |
$this->assertEquals($instance1, $instance2);
|
|
|
59 |
|
|
|
60 |
// Validate public attribute classes.
|
|
|
61 |
$this->assertInstanceOf($classnames['course'], $instance1->course);
|
|
|
62 |
$this->assertInstanceOf($classnames['section'], $instance1->section);
|
|
|
63 |
$this->assertInstanceOf($classnames['cm'], $instance1->cm);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Test that the course action instance is created correctly.
|
|
|
68 |
* @dataProvider provider_classname_action
|
|
|
69 |
* @covers ::course
|
|
|
70 |
* @param string $format
|
|
|
71 |
* @param array $classnames
|
|
|
72 |
*/
|
|
|
73 |
public function test_course_action_instance(string $format, array $classnames): void {
|
|
|
74 |
$this->resetAfterTest();
|
|
|
75 |
$course = $this->getDataGenerator()->create_course(['format' => $format]);
|
|
|
76 |
|
|
|
77 |
$instance1 = formatactions::course($course);
|
|
|
78 |
$this->assertInstanceOf($classnames['course'], $instance1);
|
|
|
79 |
|
|
|
80 |
$instance2 = formatactions::course($course->id);
|
|
|
81 |
$this->assertInstanceOf($classnames['course'], $instance2);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Test that the section action instance is created correctly.
|
|
|
86 |
* @dataProvider provider_classname_action
|
|
|
87 |
* @covers ::section
|
|
|
88 |
*
|
|
|
89 |
* @param string $format
|
|
|
90 |
* @param array $classnames
|
|
|
91 |
*/
|
|
|
92 |
public function test_static_sectionactions_instance(string $format, array $classnames): void {
|
|
|
93 |
$this->resetAfterTest();
|
|
|
94 |
$course = $this->getDataGenerator()->create_course(['format' => $format]);
|
|
|
95 |
|
|
|
96 |
$instance1 = formatactions::section($course);
|
|
|
97 |
$this->assertInstanceOf($classnames['section'], $instance1);
|
|
|
98 |
|
|
|
99 |
$instance2 = formatactions::section($course->id);
|
|
|
100 |
$this->assertInstanceOf($classnames['section'], $instance2);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Test that the cm action instance is created correctly.
|
|
|
105 |
* @dataProvider provider_classname_action
|
|
|
106 |
* @covers ::cm
|
|
|
107 |
*
|
|
|
108 |
* @param string $format
|
|
|
109 |
* @param array $classnames
|
|
|
110 |
*/
|
|
|
111 |
public function test_static_cmactions_instance(string $format, array $classnames): void {
|
|
|
112 |
$this->resetAfterTest();
|
|
|
113 |
$course = $this->getDataGenerator()->create_course(['format' => $format]);
|
|
|
114 |
|
|
|
115 |
$instance1 = formatactions::cm($course);
|
|
|
116 |
$this->assertInstanceOf($classnames['cm'], $instance1);
|
|
|
117 |
|
|
|
118 |
$instance2 = formatactions::cm($course->id);
|
|
|
119 |
$this->assertInstanceOf($classnames['cm'], $instance2);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Data provider for format class names scenarios.
|
|
|
124 |
* @return array
|
|
|
125 |
*/
|
|
|
126 |
public static function provider_classname_action(): array {
|
|
|
127 |
return [
|
|
|
128 |
'Topics format' => [
|
|
|
129 |
'format' => 'topics',
|
|
|
130 |
'classnames' => [
|
|
|
131 |
'course' => '\core_courseformat\local\courseactions',
|
|
|
132 |
'section' => '\core_courseformat\local\sectionactions',
|
|
|
133 |
'cm' => '\core_courseformat\local\cmactions',
|
|
|
134 |
],
|
|
|
135 |
],
|
|
|
136 |
'The unit test fixture format' => [
|
|
|
137 |
'format' => 'theunittest',
|
|
|
138 |
'classnames' => [
|
|
|
139 |
'course' => '\format_theunittest\courseformat\courseactions',
|
|
|
140 |
'section' => '\format_theunittest\courseformat\sectionactions',
|
|
|
141 |
'cm' => '\format_theunittest\courseformat\cmactions',
|
|
|
142 |
],
|
|
|
143 |
],
|
|
|
144 |
];
|
|
|
145 |
}
|
|
|
146 |
}
|