| 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;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | use advanced_testcase;
 | 
        
           |  |  | 22 | use core_reportbuilder_generator;
 | 
        
           |  |  | 23 | use core_tag_tag;
 | 
        
           |  |  | 24 | use core_user\reportbuilder\datasource\users;
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | global $CFG;
 | 
        
           |  |  | 29 | require_once("{$CFG->dirroot}/reportbuilder/lib.php");
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | /**
 | 
        
           |  |  | 32 |  * Unit tests for the component callbacks
 | 
        
           |  |  | 33 |  *
 | 
        
           |  |  | 34 |  * @package     core_reportbuilder
 | 
        
           |  |  | 35 |  * @copyright   2023 Paul Holden <paulh@moodle.com>
 | 
        
           |  |  | 36 |  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 37 |  */
 | 
        
           | 1441 | ariadna | 38 | final class lib_test extends advanced_testcase {
 | 
        
           | 1 | efrain | 39 |   | 
        
           |  |  | 40 |     /**
 | 
        
           |  |  | 41 |      * Test getting tagged reports
 | 
        
           |  |  | 42 |      *
 | 
        
           |  |  | 43 |      * @covers ::core_reportbuilder_get_tagged_reports
 | 
        
           |  |  | 44 |      */
 | 
        
           |  |  | 45 |     public function test_core_reportbuilder_get_tagged_reports(): void {
 | 
        
           |  |  | 46 |         $this->resetAfterTest();
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |         /** @var core_reportbuilder_generator $generator */
 | 
        
           |  |  | 49 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |         // Create three tagged reports.
 | 
        
           |  |  | 52 |         $reportone = $generator->create_report(['name' => 'Report 1', 'source' => users::class, 'tags' => ['cat']]);
 | 
        
           |  |  | 53 |         $reporttwo = $generator->create_report(['name' => 'Report 2', 'source' => users::class, 'tags' => ['dog']]);
 | 
        
           |  |  | 54 |         $reportthree = $generator->create_report(['name' => 'Report 3', 'source' => users::class, 'tags' => ['cat']]);
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         // Add all users audience to report one and two.
 | 
        
           |  |  | 57 |         $generator->create_audience(['reportid' => $reportone->get('id'), 'configdata' => []]);
 | 
        
           |  |  | 58 |         $generator->create_audience(['reportid' => $reporttwo->get('id'), 'configdata' => []]);
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |         $tag = core_tag_tag::get_by_name(0, 'cat');
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |         // Current user can only access report one with "cat" tag.
 | 
        
           |  |  | 63 |         $user = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 64 |         $this->setUser($user);
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |         $tagindex = core_reportbuilder_get_tagged_reports($tag);
 | 
        
           |  |  | 67 |         $this->assertStringContainsString($reportone->get_formatted_name(), $tagindex->content);
 | 
        
           |  |  | 68 |         $this->assertStringNotContainsString($reporttwo->get_formatted_name(), $tagindex->content);
 | 
        
           |  |  | 69 |         $this->assertStringNotContainsString($reportthree->get_formatted_name(), $tagindex->content);
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |         // Admin can access both reports with "cat" tag.
 | 
        
           |  |  | 72 |         $this->setAdminUser();
 | 
        
           |  |  | 73 |         $tagindex = core_reportbuilder_get_tagged_reports($tag);
 | 
        
           |  |  | 74 |         $this->assertStringContainsString($reportone->get_formatted_name(), $tagindex->content);
 | 
        
           |  |  | 75 |         $this->assertStringNotContainsString($reporttwo->get_formatted_name(), $tagindex->content);
 | 
        
           |  |  | 76 |         $this->assertStringContainsString($reportthree->get_formatted_name(), $tagindex->content);
 | 
        
           |  |  | 77 |     }
 | 
        
           |  |  | 78 | }
 |