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\columns\sort;
20
 
21
use core_reportbuilder_generator;
22
use core_external\external_api;
23
use externallib_advanced_testcase;
1441 ariadna 24
use core_reportbuilder\exception\report_access_exception;
1 efrain 25
use core_reportbuilder\local\models\column;
26
use core_user\reportbuilder\datasource\users;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
global $CFG;
31
require_once("{$CFG->dirroot}/webservice/tests/helpers.php");
32
 
33
/**
34
 * Unit tests of external class for re-ordering report column sorting
35
 *
36
 * @package     core_reportbuilder
37
 * @covers      \core_reportbuilder\external\columns\sort\reorder
38
 * @copyright   2021 Paul Holden <paulh@moodle.com>
39
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
1441 ariadna 41
final class reorder_test extends externallib_advanced_testcase {
1 efrain 42
 
43
    /**
44
     * Text execute method
45
     */
46
    public function test_execute(): void {
47
        $this->resetAfterTest();
48
        $this->setAdminUser();
49
 
50
        /** @var core_reportbuilder_generator $generator */
51
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
52
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => false]);
53
 
54
        // Add four columns.
55
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
56
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
57
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:country']);
58
        $columncity = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:city']);
59
 
60
        // Move the city column sort order to second position.
61
        $result = reorder::execute($report->get('id'), $columncity->get('id'), 2);
62
        $result = external_api::clean_returnvalue(reorder::execute_returns(), $result);
63
 
64
        $this->assertTrue($result['hassortablecolumns']);
65
        $this->assertCount(4, $result['sortablecolumns']);
1441 ariadna 66
 
67
        $sortablecolumn = $result['sortablecolumns'][1];
1 efrain 68
        $this->assertEquals('City/town', $sortablecolumn['title']);
69
        $this->assertEquals(SORT_ASC, $sortablecolumn['sortdirection']);
70
        $this->assertEquals(0, $sortablecolumn['sortenabled']);
71
        $this->assertEquals(2, $sortablecolumn['sortorder']);
1441 ariadna 72
        $this->assertArrayHasKey('sorticon', $sortablecolumn);
1 efrain 73
 
74
        // Assert report column sort order.
75
        $columns = column::get_records(['reportid' => $report->get('id')], 'sortorder');
76
 
77
        $columnidentifiers = array_map(static function(column $column): string {
78
            return $column->get('uniqueidentifier');
79
        }, $columns);
80
 
81
        $this->assertEquals([
82
            'user:fullname',
83
            'user:city',
84
            'user:email',
85
            'user:country',
86
        ], $columnidentifiers);
87
    }
88
 
89
    /**
90
     * Test execute method for a user without permission to edit reports
91
     */
92
    public function test_execute_access_exception(): void {
93
        $this->resetAfterTest();
94
 
95
        /** @var core_reportbuilder_generator $generator */
96
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
97
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
98
        $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
99
 
100
        $user = $this->getDataGenerator()->create_user();
101
        $this->setUser($user);
102
 
103
        $this->expectException(report_access_exception::class);
104
        $this->expectExceptionMessage('You cannot edit this report');
105
        reorder::execute($report->get('id'), $column->get('id'), 2);
106
    }
107
}