| 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 | namespace core_my\external;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use externallib_advanced_testcase;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 | global $CFG;
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | require_once($CFG->dirroot . '/webservice/tests/helpers.php');
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | /**
 | 
        
           |  |  | 28 |  * Test Class for external function core_my_view_page.
 | 
        
           |  |  | 29 |  *
 | 
        
           |  |  | 30 |  * @package   core_my
 | 
        
           |  |  | 31 |  * @category  external
 | 
        
           |  |  | 32 |  * @copyright 2023 Rodrigo Mady <rodrigo.mady@moodle.com>
 | 
        
           |  |  | 33 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 34 |  * @since     Moodle 4.3
 | 
        
           |  |  | 35 |  * @covers \core_my\external\view_page
 | 
        
           |  |  | 36 |  */
 | 
        
           | 1441 | ariadna | 37 | final class view_page_test extends externallib_advanced_testcase {
 | 
        
           | 1 | efrain | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      * Set up for every test.
 | 
        
           |  |  | 41 |      */
 | 
        
           |  |  | 42 |     public function setUp(): void {
 | 
        
           | 1441 | ariadna | 43 |         parent::setUp();
 | 
        
           | 1 | efrain | 44 |         $this->resetAfterTest();
 | 
        
           |  |  | 45 |     }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |     /**
 | 
        
           |  |  | 48 |      * Helper.
 | 
        
           |  |  | 49 |      *
 | 
        
           |  |  | 50 |      * @param string $page
 | 
        
           |  |  | 51 |      * @return array
 | 
        
           |  |  | 52 |      */
 | 
        
           |  |  | 53 |     protected function view_page(string $page): array {
 | 
        
           |  |  | 54 |         $result = view_page::execute($page);
 | 
        
           |  |  | 55 |         return \core_external\external_api::clean_returnvalue(view_page::execute_returns(), $result);
 | 
        
           |  |  | 56 |     }
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 |     /**
 | 
        
           |  |  | 59 |      * Test for webservice my view page.
 | 
        
           |  |  | 60 |      */
 | 
        
           |  |  | 61 |     public function test_view_page(): void {
 | 
        
           |  |  | 62 |         $user = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 63 |         $this->setUser($user);
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |         // Trigger and capture the event.
 | 
        
           |  |  | 66 |         $sink = $this->redirectEvents();
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |         // Request to trigger the view event in my.
 | 
        
           |  |  | 69 |         $result = $this->view_page('my');
 | 
        
           |  |  | 70 |         $this->assertTrue($result['status']);
 | 
        
           |  |  | 71 |         $this->assertEmpty($result['warnings']);
 | 
        
           |  |  | 72 |   | 
        
           |  |  | 73 |         // Request to trigger the view event in dashboard.
 | 
        
           |  |  | 74 |         $result = $this->view_page('dashboard');
 | 
        
           |  |  | 75 |         $this->assertTrue($result['status']);
 | 
        
           |  |  | 76 |         $this->assertEmpty($result['warnings']);
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         // Wrong page to trigger the event.
 | 
        
           |  |  | 79 |         $result = $this->view_page('test');
 | 
        
           |  |  | 80 |         $this->assertFalse($result['status']);
 | 
        
           |  |  | 81 |         $this->assertNotEmpty($result['warnings']);
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |         $events = $sink->get_events();
 | 
        
           |  |  | 84 |         // Check if the log still with two rows.
 | 
        
           |  |  | 85 |         $this->assertCount(2, $events);
 | 
        
           |  |  | 86 |         $this->assertInstanceOf('\core\event\mycourses_viewed', $events[0]);
 | 
        
           |  |  | 87 |         $this->assertInstanceOf('\core\event\dashboard_viewed', $events[1]);
 | 
        
           |  |  | 88 |         $sink->close();
 | 
        
           |  |  | 89 |     }
 | 
        
           |  |  | 90 | }
 |