Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 customfield_number\local\numberproviders\nofactivities;
20
 
21
use advanced_testcase;
22
use customfield_number\local\numberproviders\nofactivities;
23
use customfield_number\provider_base;
24
 
25
/**
26
 * Tests for the number of activities
27
 *
28
 * @package    customfield_number
29
 * @covers     \customfield_number\local\numberproviders\nofactivities
30
 * @copyright  2024 Ilya Tregubov <ilya.tregubov@proton.me>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
final class nofactivities_test extends advanced_testcase {
34
 
35
    /**
36
     * Test that we can automatically calculate number of activities in courses.
37
     */
38
    public function test_recalculate(): void {
39
        global $DB;
40
 
41
        $this->resetAfterTest();
42
        $this->setAdminUser();
43
 
44
        $course1 = $this->getDataGenerator()->create_course();
45
        $course2 = $this->getDataGenerator()->create_course();
46
 
47
        // Add some activities to the courses.
48
        $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
49
        $assign1 = $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
50
        $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
51
        $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 0]);
52
 
53
        $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
54
        $quizgenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
55
        $quizgenerator->create_instance(['course' => $course1->id, 'visible' => 0]);
56
        $quizgenerator->create_instance(['course' => $course1->id, 'visible' => 0]);
57
 
58
        $forumgenerator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
59
        $forumgenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
60
        $forumgenerator->create_instance(['course' => $course1->id, 'visible' => 0]);
61
        $forumgenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
62
 
63
        $assigngenerator->create_instance(['course' => $course2->id, 'visible' => 1]);
64
        $assigngenerator->create_instance(['course' => $course2->id, 'visible' => 1]);
65
        $assigngenerator->create_instance(['course' => $course2->id, 'visible' => 1]);
66
 
67
        /** @var \core_customfield_generator $generator */
68
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
69
 
70
        // Create a category and field.
71
        $category = $generator->create_category();
72
        $field = $generator->create_field([
73
            'categoryid' => $category->get('id'),
74
            'type' => 'number',
75
            'configdata' => [
76
                'fieldtype' => 'customfield_number\local\numberproviders\nofactivities',
77
                "activitytypes" => ["assign", "forum"],
78
            ],
79
        ]);
80
 
81
        // Test if the provider has been added correctly.
82
        $providers = provider_base::get_all_providers($field);
83
        $this->assertNotEmpty($providers);
84
        $this->assertInstanceOf(nofactivities::class, $providers[0]);
85
 
86
        // Calculate only in course1.
87
        $providers[0]->recalculate((int)$course1->id);
88
        $course1customfield = $DB->get_field('customfield_data', 'decvalue', ['instanceid' => $course1->id]);
89
        $course2customfield = $DB->get_field('customfield_data', 'decvalue', ['instanceid' => $course2->id]);
90
 
91
        $this->assertEquals(4.0000, $course1customfield);
92
        $this->assertEquals(false, $course2customfield);
93
 
94
        // Calculate in all courses.
95
        $providers[0]->recalculate();
96
        $course1customfield = $DB->get_field('customfield_data', 'decvalue', ['instanceid' => $course1->id]);
97
        $course2customfield = $DB->get_field('customfield_data', 'decvalue', ['instanceid' => $course2->id]);
98
 
99
        $this->assertEquals(4.0000, $course1customfield);
100
        $this->assertEquals(3.0000, $course2customfield);
101
 
102
        // Delete some assign module.
103
        $cm = get_coursemodule_from_instance('assign', $assign1->id);
104
        course_delete_module($cm->id);
105
        $providers[0]->recalculate((int)$course1->id);
106
        $course1customfield = $DB->get_field('customfield_data', 'decvalue', ['instanceid' => $course1->id]);
107
        // Module is marked as deleted.
108
        $this->assertEquals(3.0000, $course1customfield);
109
 
110
        // Now, run the course module deletion adhoc task.
111
        \phpunit_util::run_all_adhoc_tasks();
112
        $providers[0]->recalculate((int)$course1->id);
113
        $course1customfield = $DB->get_field('customfield_data', 'decvalue', ['instanceid' => $course1->id]);
114
        $this->assertEquals(3.0000, $course1customfield);
115
    }
116
 
117
    /**
118
     * Test that the data record is updated/deleted when the value is recalculated
119
     *
120
     * Also test that export_value() is correct
121
     *
122
     * @return void
123
     */
124
    public function test_recalculate_change_value(): void {
125
        global $DB;
126
 
127
        $this->resetAfterTest();
128
        $this->setAdminUser();
129
 
130
        // Create a course with one activity.
131
        $course1 = $this->getDataGenerator()->create_course();
132
        $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
133
        $assign1 = $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
134
 
135
        /** @var \core_customfield_generator $generator */
136
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
137
 
138
        // Create a category and two fields, one with displaywhenzero, another one without.
139
        $category = $generator->create_category();
140
        $field1 = $generator->create_field([
141
            'categoryid' => $category->get('id'),
142
            'type' => 'number',
143
            'configdata' => [
144
                'fieldtype' => nofactivities::class,
145
                'activitytypes' => ['assign'],
146
                'displaywhenzero' => '0',
147
            ],
148
        ]);
149
        $field2 = $generator->create_field([
150
            'categoryid' => $category->get('id'),
151
            'type' => 'number',
152
            'configdata' => [
153
                'fieldtype' => nofactivities::class,
154
                'activitytypes' => ['assign'],
155
                'displaywhenzero' => '',
156
            ],
157
        ]);
158
        $getdata = fn(\customfield_number\field_controller $field): \customfield_number\data_controller =>
159
            \core_customfield\api::get_instance_fields_data([$field->get('id') => $field], (int)$course1->id)[$field->get('id')];
160
 
161
        // Recalculate the value of the field and assert it is set to 1 (one activity in the course).
162
        (new \customfield_number\task\cron())->execute();
163
        $data = $getdata($field1);
164
        $this->assertEquals(1, $data->get('decvalue'));
165
        $this->assertSame('1', $data->export_value());
166
        $data = $getdata($field2);
167
        $this->assertEquals(1, $data->get('decvalue'));
168
        $this->assertSame('1', $data->export_value());
169
 
170
        // Add another module, recalculate and assert the value of the field is set to 2 (two activities in the course).
171
        $assign2 = $assigngenerator->create_instance(['course' => $course1->id, 'visible' => 1]);
172
        (new \customfield_number\task\cron())->execute();
173
        $data = $getdata($field1);
174
        $this->assertEquals(2, $data->get('decvalue'));
175
        $this->assertSame('2', $data->export_value());
176
        $data = $getdata($field2);
177
        $this->assertEquals(2, $data->get('decvalue'));
178
        $this->assertSame('2', $data->export_value());
179
 
180
        // Delete both modules, recalculate.
181
        course_delete_module($assign1->cmid);
182
        course_delete_module($assign2->cmid);
183
        (new \customfield_number\task\cron())->execute();
184
        // Field1 (displaywhenzero='0') has the value zero.
185
        $data = $getdata($field1);
186
        $this->assertNotEmpty($data->get('id'));
187
        $this->assertEquals(0, $data->get('decvalue'));
188
        $this->assertSame('0', $data->export_value());
189
        // Field2 (displaywhenzero='') no longer has a data record and it is not displayed.
190
        $data = $getdata($field2);
191
        $this->assertEmpty($data->get('id'));
192
        $this->assertEquals(null, $data->get('decvalue'));
193
        $this->assertSame(null, $data->export_value());
194
    }
195
}