| 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 | 
            */
  | 
        
        
            | 
            | 
           37 | 
           class view_page_test extends externallib_advanced_testcase {
  | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
               /**
  | 
        
        
            | 
            | 
           40 | 
                * Set up for every test.
  | 
        
        
            | 
            | 
           41 | 
                */
  | 
        
        
            | 
            | 
           42 | 
               public function setUp(): void {
  | 
        
        
            | 
            | 
           43 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           44 | 
               }
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
               /**
  | 
        
        
            | 
            | 
           47 | 
                * Helper.
  | 
        
        
            | 
            | 
           48 | 
                *
  | 
        
        
            | 
            | 
           49 | 
                * @param string $page
  | 
        
        
            | 
            | 
           50 | 
                * @return array
  | 
        
        
            | 
            | 
           51 | 
                */
  | 
        
        
            | 
            | 
           52 | 
               protected function view_page(string $page): array {
  | 
        
        
            | 
            | 
           53 | 
                   $result = view_page::execute($page);
  | 
        
        
            | 
            | 
           54 | 
                   return \core_external\external_api::clean_returnvalue(view_page::execute_returns(), $result);
  | 
        
        
            | 
            | 
           55 | 
               }
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
               /**
  | 
        
        
            | 
            | 
           58 | 
                * Test for webservice my view page.
  | 
        
        
            | 
            | 
           59 | 
                */
  | 
        
        
            | 
            | 
           60 | 
               public function test_view_page(): void {
  | 
        
        
            | 
            | 
           61 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           62 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
                   // Trigger and capture the event.
  | 
        
        
            | 
            | 
           65 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   // Request to trigger the view event in my.
  | 
        
        
            | 
            | 
           68 | 
                   $result = $this->view_page('my');
  | 
        
        
            | 
            | 
           69 | 
                   $this->assertTrue($result['status']);
  | 
        
        
            | 
            | 
           70 | 
                   $this->assertEmpty($result['warnings']);
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
                   // Request to trigger the view event in dashboard.
  | 
        
        
            | 
            | 
           73 | 
                   $result = $this->view_page('dashboard');
  | 
        
        
            | 
            | 
           74 | 
                   $this->assertTrue($result['status']);
  | 
        
        
            | 
            | 
           75 | 
                   $this->assertEmpty($result['warnings']);
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                   // Wrong page to trigger the event.
  | 
        
        
            | 
            | 
           78 | 
                   $result = $this->view_page('test');
  | 
        
        
            | 
            | 
           79 | 
                   $this->assertFalse($result['status']);
  | 
        
        
            | 
            | 
           80 | 
                   $this->assertNotEmpty($result['warnings']);
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           83 | 
                   // Check if the log still with two rows.
  | 
        
        
            | 
            | 
           84 | 
                   $this->assertCount(2, $events);
  | 
        
        
            | 
            | 
           85 | 
                   $this->assertInstanceOf('\core\event\mycourses_viewed', $events[0]);
  | 
        
        
            | 
            | 
           86 | 
                   $this->assertInstanceOf('\core\event\dashboard_viewed', $events[1]);
  | 
        
        
            | 
            | 
           87 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           88 | 
               }
  | 
        
        
            | 
            | 
           89 | 
           }
  |