Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 17... Línea 17...
17
declare(strict_types=1);
17
declare(strict_types=1);
Línea 18... Línea 18...
18
 
18
 
Línea 19... Línea 19...
19
namespace core_reportbuilder\local\helpers;
19
namespace core_reportbuilder\local\helpers;
-
 
20
 
20
 
21
use advanced_testcase;
21
use advanced_testcase;
-
 
22
use core_reportbuilder_generator;
22
use core\context\system;
23
use invalid_parameter_exception;
23
use core_reportbuilder_generator;
24
use core_reportbuilder\datasource;
24
use core_reportbuilder\{datasource, system_report_factory};
25
use core_reportbuilder\local\models\column;
25
use core_reportbuilder\local\models\{audience, column, filter, schedule};
26
use core_reportbuilder\local\models\filter;
26
use core_reportbuilder\local\systemreports\report_access_list;
-
 
27
use core_tag_tag;
Línea 27... Línea 28...
27
use core_tag_tag;
28
use core_user\reportbuilder\datasource\users;
28
use core_user\reportbuilder\datasource\users;
29
use invalid_parameter_exception;
29
 
30
 
30
/**
31
/**
31
 * Unit tests for the report helper class
32
 * Unit tests for the report helper class
32
 *
33
 *
33
 * @package     core_reportbuilder
34
 * @package     core_reportbuilder
34
 * @covers      \core_reportbuilder\local\helpers\report
35
 * @covers      \core_reportbuilder\local\helpers\report
35
 * @copyright   2021 Paul Holden <paulh@moodle.com>
36
 * @copyright   2021 Paul Holden <paulh@moodle.com>
Línea 36... Línea 37...
36
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
 */
38
class report_test extends advanced_testcase {
39
final class report_test extends advanced_testcase {
39
 
40
 
Línea 50... Línea 51...
50
            'tags' => ['cat', 'dog'],
51
            'tags' => ['cat', 'dog'],
51
        ]);
52
        ]);
Línea 52... Línea 53...
52
 
53
 
53
        $this->assertEquals('My report with tags', $report->get('name'));
54
        $this->assertEquals('My report with tags', $report->get('name'));
54
        $this->assertEquals(datasource::TYPE_CUSTOM_REPORT, $report->get('type'));
55
        $this->assertEquals(datasource::TYPE_CUSTOM_REPORT, $report->get('type'));
-
 
56
        $this->assertEqualsCanonicalizing(
55
        $this->assertEqualsCanonicalizing(['cat', 'dog'],
57
            ['cat', 'dog'],
-
 
58
            array_values(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report->get('id'))),
Línea 56... Línea 59...
56
            core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report->get('id')));
59
        );
57
 
60
 
58
        $report = report::create_report((object) [
61
        $report = report::create_report((object) [
59
            'name' => 'My report without tags',
62
            'name' => 'My report without tags',
Línea 95... Línea 98...
95
            'tags' => ['cat', 'dog'],
98
            'tags' => ['cat', 'dog'],
96
        ]);
99
        ]);
Línea 97... Línea 100...
97
 
100
 
98
        $this->assertEquals('My renamed report adding tags', $reportupdated->get('name'));
101
        $this->assertEquals('My renamed report adding tags', $reportupdated->get('name'));
99
        $this->assertTrue($reportupdated->get('uniquerows'));
102
        $this->assertTrue($reportupdated->get('uniquerows'));
-
 
103
        $this->assertEqualsCanonicalizing(
100
        $this->assertEqualsCanonicalizing(['cat', 'dog'],
104
            ['cat', 'dog'],
-
 
105
            array_values(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $reportupdated->get('id'))),
-
 
106
        );
-
 
107
    }
-
 
108
 
-
 
109
    /**
-
 
110
     * Test duplicate report
-
 
111
     */
-
 
112
    public function test_duplicate_report(): void {
-
 
113
        $this->resetAfterTest();
-
 
114
        $this->setAdminUser();
-
 
115
 
-
 
116
        /** @var core_reportbuilder_generator $generator */
-
 
117
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
-
 
118
 
-
 
119
        // Create custom report containing single column, condition and filter..
-
 
120
        $report = $generator->create_report(['name' => 'Report 1', 'source' => users::class, 'default' => 0]);
-
 
121
        $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
-
 
122
        $condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
-
 
123
        $filter = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:email']);
-
 
124
 
-
 
125
        // Add an audience and schedule.
-
 
126
        $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
-
 
127
        $schedule = $generator->create_schedule([
-
 
128
            'reportid' => $report->get('id'),
-
 
129
            'name' => 'My schedule',
-
 
130
            'audiences' => json_encode([
-
 
131
                $audience->get_persistent()->get('id'),
-
 
132
            ]),
-
 
133
        ]);
-
 
134
 
-
 
135
        // Duplicate report with audiences and schedules.
-
 
136
        $newreport = report::duplicate_report($report, 'Report 1 copy', true, true);
-
 
137
        $this->assertNotEquals($report->get('id'), $newreport->get('id'));
-
 
138
        $this->assertEquals('Report 1 copy', $newreport->get('name'));
-
 
139
        $this->assertEquals(users::class, $newreport->get('source'));
-
 
140
 
-
 
141
        // Assert new report columns.
-
 
142
        $newcolumns = column::get_records(['reportid' => $newreport->get('id')]);
-
 
143
        $this->assertCount(1, $newcolumns);
-
 
144
        [$newcolumn] = $newcolumns;
-
 
145
        $this->assertNotEquals($column->get('id'), $newcolumn->get('id'));
-
 
146
        $this->assertEquals('user:lastname', $newcolumn->get('uniqueidentifier'));
-
 
147
 
-
 
148
        // Assert new report conditions.
-
 
149
        $newconditions = filter::get_condition_records($newreport->get('id'));
-
 
150
        $this->assertCount(1, $newconditions);
-
 
151
        [$newcondition] = $newconditions;
-
 
152
        $this->assertNotEquals($condition->get('id'), $newcondition->get('id'));
-
 
153
        $this->assertEquals('user:firstname', $newcondition->get('uniqueidentifier'));
-
 
154
 
-
 
155
        // Assert new report filters.
-
 
156
        $newfilters = filter::get_filter_records($newreport->get('id'));
-
 
157
        $this->assertCount(1, $newfilters);
-
 
158
        [$newfilter] = $newfilters;
-
 
159
        $this->assertNotEquals($filter->get('id'), $newfilter->get('id'));
-
 
160
        $this->assertEquals('user:email', $newfilter->get('uniqueidentifier'));
-
 
161
 
-
 
162
        // Assert new report audiences.
-
 
163
        $newaudiences = audience::get_records(['reportid' => $newreport->get('id')]);
-
 
164
        $this->assertCount(1, $newaudiences);
-
 
165
        [$newaudience] = $newaudiences;
-
 
166
        $this->assertNotEquals($audience->get_persistent()->get('id'), $newaudience->get('id'));
-
 
167
 
-
 
168
         // Assert new report schedules.
-
 
169
        $newschedules = schedule::get_records(['reportid' => $newreport->get('id')]);
-
 
170
        $this->assertCount(1, $newschedules);
-
 
171
        [$newschedule] = $newschedules;
-
 
172
        $this->assertNotEquals($schedule->get('id'), $newschedule->get('id'));
-
 
173
        $this->assertEquals([
-
 
174
            $newaudience->get('id'),
-
 
175
        ], (array) json_decode($newschedule->get('audiences')));
-
 
176
 
-
 
177
        // Duplicate report without schedules.
-
 
178
        $newreporttwo = report::duplicate_report($report, 'Report 1 copy #2', true, false);
-
 
179
        $this->assertEquals(1, column::count_records(['reportid' => $newreporttwo->get('id')]));
-
 
180
        $this->assertEquals(1, filter::count_records(['reportid' => $newreporttwo->get('id'), 'iscondition' => 1]));
-
 
181
        $this->assertEquals(1, filter::count_records(['reportid' => $newreporttwo->get('id'), 'iscondition' => 0]));
-
 
182
        $this->assertEquals(1, audience::count_records(['reportid' => $newreporttwo->get('id')]));
-
 
183
        $this->assertEquals(0, schedule::count_records(['reportid' => $newreporttwo->get('id')]));
-
 
184
 
-
 
185
        // Duplicate report without audiences or schedules.
-
 
186
        $newreportthree = report::duplicate_report($report, 'Report 1 copy #3', false, false);
-
 
187
        $this->assertEquals(1, column::count_records(['reportid' => $newreportthree->get('id')]));
-
 
188
        $this->assertEquals(1, filter::count_records(['reportid' => $newreportthree->get('id'), 'iscondition' => 1]));
-
 
189
        $this->assertEquals(1, filter::count_records(['reportid' => $newreportthree->get('id'), 'iscondition' => 0]));
-
 
190
        $this->assertEquals(0, audience::count_records(['reportid' => $newreportthree->get('id')]));
101
            core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $reportupdated->get('id')));
191
        $this->assertEquals(0, schedule::count_records(['reportid' => $newreportthree->get('id')]));
Línea 102... Línea 192...
102
    }
192
    }
103
 
193
 
104
    /**
194
    /**
Línea 741... Línea 831...
741
 
831
 
742
        $this->expectException(invalid_parameter_exception::class);
832
        $this->expectException(invalid_parameter_exception::class);
743
        $this->expectExceptionMessage('Invalid filter');
833
        $this->expectExceptionMessage('Invalid filter');
744
        report::reorder_report_filter($report->get('id'), 42, 1);
834
        report::reorder_report_filter($report->get('id'), 42, 1);
-
 
835
    }
-
 
836
 
-
 
837
    /**
-
 
838
     * Test getting row count for a custom report
-
 
839
     */
-
 
840
    public function test_get_report_row_count_custom_report(): void {
-
 
841
        $this->resetAfterTest();
-
 
842
 
-
 
843
        $this->getDataGenerator()->create_user();
-
 
844
 
-
 
845
        /** @var core_reportbuilder_generator $generator */
-
 
846
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
-
 
847
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
-
 
848
 
-
 
849
        // There are two users, the admin plus the user we just created.
-
 
850
        $this->assertEquals(2, report::get_report_row_count($report->get('id')));
-
 
851
    }
-
 
852
 
-
 
853
    /**
-
 
854
     * Test getting row count for a system report
-
 
855
     */
-
 
856
    public function test_get_report_row_count_system_report(): void {
-
 
857
        $this->resetAfterTest();
-
 
858
        $this->setAdminUser();
-
 
859
 
-
 
860
        $this->getDataGenerator()->create_user();
-
 
861
 
-
 
862
        /** @var core_reportbuilder_generator $generator */
-
 
863
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
-
 
864
 
-
 
865
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
-
 
866
        $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
-
 
867
 
-
 
868
        $reportaccesslist = system_report_factory::create(
-
 
869
            report_access_list::class,
-
 
870
            system::instance(),
-
 
871
            parameters: ['id' => $report->get('id')],
-
 
872
        )->get_report_persistent();
-
 
873
 
-
 
874
        // There are two users, the admin plus the user we just created.
-
 
875
        $this->assertEquals(
-
 
876
            2,
-
 
877
            report::get_report_row_count($reportaccesslist->get('id'), ['id' => $report->get('id')],
-
 
878
        ));
745
    }
879
    }