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 core_customfield\external;
20
 
21
use advanced_testcase;
22
use core_customfield_generator;
23
 
24
/**
25
 * Unit tests for custom field data exporter
26
 *
27
 * @package     core_customfield
28
 * @covers      \core_customfield\external\field_data_exporter
29
 * @copyright   2025 Paul Holden <paulh@moodle.com>
30
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
final class field_data_exporter_test extends advanced_testcase {
33
 
34
    /**
35
     * Test exported data/structure
36
     */
37
    public function test_export(): void {
38
        global $PAGE;
39
 
40
        $this->resetAfterTest();
41
        $this->setAdminUser();
42
 
43
        /** @var core_customfield_generator $generator */
44
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
45
        $category = $generator->create_category();
46
        $generator->create_field([
47
            'categoryid' => $category->get('id'),
48
            'name' => 'My field',
49
            'shortname' => 'myfield',
50
            'type' => 'number',
51
        ]);
52
 
53
        $courseone = $this->getDataGenerator()->create_course(['customfield_myfield' => 42]);
54
        $coursetwo = $this->getDataGenerator()->create_course();
55
 
56
        $output = $PAGE->get_renderer('core_customfield');
57
 
58
        // Course one has our custom field populated.
59
        $courseoneexport = (new field_data_exporter(null, [
60
            'component' => 'core_course',
61
            'area' => 'course',
62
            'instanceid' => (int) $courseone->id,
63
        ]))->export($output);
64
 
65
        $this->assertEquals([
66
            [
67
                'value' => 42,
68
                'type' => 'number',
69
                'shortname' => 'myfield',
70
                'name' => 'My field',
71
                'hasvalue' => true,
72
                'instanceid' => $courseone->id,
73
            ],
74
        ], array_values($courseoneexport->data));
75
 
76
        // Course two does not have our custom field populated.
77
        $coursetwoexport = (new field_data_exporter(null, [
78
            'component' => 'core_course',
79
            'area' => 'course',
80
            'instanceid' => (int) $coursetwo->id,
81
        ]))->export($output);
82
 
83
        $this->assertEquals([
84
            [
85
                'value' => null,
86
                'type' => 'number',
87
                'shortname' => 'myfield',
88
                'name' => 'My field',
89
                'hasvalue' => false,
90
                'instanceid' => $coursetwo->id,
91
            ],
92
        ], array_values($coursetwoexport->data));
93
    }
94
}