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
declare(strict_types=1);
18
 
19
namespace core_reportbuilder\external;
20
 
21
use advanced_testcase;
1441 ariadna 22
use core_customfield_generator;
1 efrain 23
use core_reportbuilder_generator;
24
use core_user\reportbuilder\datasource\users;
25
 
26
/**
27
 * Unit tests for custom report details exporter
28
 *
29
 * @package     core_reportbuilder
30
 * @covers      \core_reportbuilder\external\custom_report_details_exporter
31
 * @copyright   2022 Paul Holden <paulh@moodle.com>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
1441 ariadna 34
final class custom_report_details_exporter_test extends advanced_testcase {
1 efrain 35
 
36
    /**
37
     * Test exported data structure
38
     */
39
    public function test_export(): void {
40
        global $PAGE;
41
 
42
        $this->resetAfterTest();
1441 ariadna 43
        $this->setAdminUser();
1 efrain 44
 
1441 ariadna 45
        /** @var core_customfield_generator $generator */
46
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
47
        $category = $generator->create_category(['component' => 'core_reportbuilder', 'area' => 'report']);
48
        $generator->create_field([
49
            'categoryid' => $category->get('id'),
50
            'name' => 'My field',
51
            'shortname' => 'myfield',
52
            'type' => 'number',
53
        ]);
1 efrain 54
 
55
        /** @var core_reportbuilder_generator $generator */
56
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
1441 ariadna 57
        $report = $generator->create_report([
58
            'name' => 'My report',
59
            'source' => users::class,
60
            'tags' => ['cat', 'dog'],
61
            'customfield_myfield' => 42,
62
        ]);
1 efrain 63
 
64
        $exporter = new custom_report_details_exporter($report);
65
        $export = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
66
 
67
        // The exporter outputs the persistent details, plus two other properties.
68
        $this->assertEquals($report->get('name'), $export->name);
69
        $this->assertEquals($report->get('source'), $export->source);
70
 
71
        // Source name should be the name of the source.
1441 ariadna 72
        $this->assertObjectHasProperty('sourcename', $export);
1 efrain 73
        $this->assertEquals(users::get_name(), $export->sourcename);
74
 
1441 ariadna 75
        // We use the tag exporter for report tags.
76
        $this->assertObjectHasProperty('tags', $export);
77
        $this->assertEquals(['cat', 'dog'], array_column($export->tags, 'name'));
78
 
79
        // We use the custom field exporter for report custom fields.
80
        $this->assertObjectHasProperty('customfields', $export);
81
        $this->assertEquals(['42'], array_column($export->customfields->data, 'value'));
82
 
1 efrain 83
        // We use the user exporter for the modifier of the report.
84
        $this->assertObjectHasProperty('modifiedby', $export);
1441 ariadna 85
        $this->assertEquals('Admin User', $export->modifiedby->fullname);
1 efrain 86
    }
87
}