Differences Between: [Versions 401 and 402]
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 24 /** 25 * Unit tests for custom report audience cards exporter 26 * 27 * @package core_reportbuilder 28 * @covers \core_reportbuilder\external\custom_report_audience_cards_exporter 29 * @copyright 2022 Paul Holden <paulh@moodle.com> 30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 31 */ 32 class custom_report_audience_cards_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 $exporter = new custom_report_audience_cards_exporter(null); 44 $export = $exporter->export($PAGE->get_renderer('core_reportbuilder')); 45 46 $this->assertNotEmpty($export->menucards); 47 48 // Test only the site audiences, so tests are unaffected by audiences within components. 49 $menucardsite = array_filter($export->menucards, static function(array $menucard): bool { 50 return $menucard['name'] === get_string('site'); 51 }); 52 53 $this->assertCount(1, $menucardsite); 54 $menucardsite = reset($menucardsite); 55 56 $this->assertNotEmpty($menucardsite['key']); 57 $this->assertGreaterThanOrEqual(4, count($menucardsite['items'])); 58 59 // Test the structure of the first menu card item. 60 $menucarditem = reset($menucardsite['items']); 61 $this->assertEquals([ 62 'name' => 'All users', 63 'identifier' => \core_reportbuilder\reportbuilder\audience\allusers::class, 64 'title' => 'Add audience \'All users\'', 65 'action' => 'add-audience', 66 'disabled' => false, 67 ], $menucarditem); 68 } 69 70 /** 71 * Test exported data when user cannot add some audience types 72 */ 73 public function test_export_audience_user_can_add(): void { 74 global $DB, $PAGE; 75 76 $this->resetAfterTest(); 77 78 $user = $this->getDataGenerator()->create_user(); 79 $this->setUser($user); 80 81 // This capability controls access to the all/manual users audiences. 82 $userrole = $DB->get_field('role', 'id', ['shortname' => 'user']); 83 assign_capability('moodle/user:viewalldetails', CAP_ALLOW, $userrole, context_system::instance()); 84 85 $exporter = new custom_report_audience_cards_exporter(null); 86 $export = $exporter->export($PAGE->get_renderer('core_reportbuilder')); 87 88 $this->assertCount(1, $export->menucards); 89 $this->assertEquals('Site', $export->menucards[0]['name']); 90 $this->assertEquals([ 91 'All users', 92 'Manually added users', 93 ], array_column($export->menucards[0]['items'], 'name')); 94 } 95 96 /** 97 * Test exported data when user can add an audience type, but it isn't available 98 */ 99 public function test_export_audience_is_available(): void { 100 global $DB, $PAGE; 101 102 $this->resetAfterTest(); 103 104 $user = $this->getDataGenerator()->create_user(); 105 $this->setUser($user); 106 107 $userrole = $DB->get_field('role', 'id', ['shortname' => 'user']); 108 assign_capability('moodle/cohort:view', CAP_ALLOW, $userrole, context_system::instance()); 109 110 $exporter = new custom_report_audience_cards_exporter(null); 111 $export = $exporter->export($PAGE->get_renderer('core_reportbuilder')); 112 113 $this->assertCount(1, $export->menucards); 114 $this->assertEquals('Site', $export->menucards[0]['name']); 115 116 // Cohort audience should be present, but disabled. 117 $this->assertCount(1, $export->menucards[0]['items']); 118 $this->assertEquals('Member of cohort', $export->menucards[0]['items'][0]['name']); 119 $this->assertTrue($export->menucards[0]['items'][0]['disabled']); 120 } 121 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body