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;
22
use context_system;
23
use moodle_url;
1441 ariadna 24
use core_reportbuilder\output\report_action;
1 efrain 25
use core_reportbuilder\system_report_available;
26
use core_reportbuilder\system_report_factory;
27
 
28
/**
29
 * Unit tests for system report exporter
30
 *
31
 * @package     core_reportbuilder
32
 * @covers      \core_reportbuilder\external\system_report_exporter
33
 * @copyright   2021 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class system_report_exporter_test extends advanced_testcase {
1 efrain 37
 
38
    /**
39
     * Load test fixture
40
     */
41
    public static function setUpBeforeClass(): void {
42
        global $CFG;
43
 
44
        require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php");
1441 ariadna 45
        parent::setUpBeforeClass();
1 efrain 46
    }
47
 
48
    /**
49
     * Data provider for {@see test_export}
50
     *
51
     * @return array[]
52
     */
1441 ariadna 53
    public static function export_provider(): array {
1 efrain 54
        return [
1441 ariadna 55
            'With filters' => [true],
56
            'Without filters' => [false],
1 efrain 57
        ];
58
    }
59
 
60
    /**
61
     * Text execute method
62
     *
63
     * @param bool $withfilters
64
     *
65
     * @dataProvider export_provider
66
     */
67
    public function test_export(bool $withfilters): void {
68
        global $PAGE;
69
 
70
        $this->resetAfterTest();
71
 
72
        // Prevent debug warnings from flexible_table.
73
        $PAGE->set_url(new moodle_url('/'));
74
 
1441 ariadna 75
        $instance = system_report_factory::create(system_report_available::class, context_system::instance(), '', '', 0,
76
            ['withfilters' => $withfilters]);
77
        $instance->set_report_action(new report_action('Add', []));
78
        $instance->set_report_info_container('Hello');
79
        $instance->add_attributes(['data-foo' => 'bar', 'data-another' => '1']);
1 efrain 80
 
1441 ariadna 81
        $exporter = new system_report_exporter($instance->get_report_persistent(), [
82
            'source' => $instance,
83
            'parameters' => json_encode($instance->get_parameters()),
1 efrain 84
        ]);
85
 
86
        $data = $exporter->export($PAGE->get_renderer('core_reportbuilder'));
87
        $this->assertNotEmpty($data->table);
1441 ariadna 88
        $this->assertNotEmpty($data->button);
89
        $this->assertEquals('Hello', $data->infocontainer);
1 efrain 90
 
91
        if ($withfilters) {
92
            $this->assertEquals('{"withfilters":true}', $data->parameters);
93
            $this->assertTrue($data->filterspresent);
94
            $this->assertNotEmpty($data->filtersform);
95
        } else {
96
            $this->assertEquals('{"withfilters":false}', $data->parameters);
97
            $this->assertFalse($data->filterspresent);
98
            $this->assertEmpty($data->filtersform);
99
        }
100
 
101
        $this->assertEquals([
102
            ['name' => 'data-foo', 'value' => 'bar'],
103
            ['name' => 'data-another', 'value' => '1']
104
        ], $data->attributes);
105
    }
106
}