| 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 mod_lti;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           /**
  | 
        
        
            | 
            | 
           20 | 
            * Tests Exception handler for LTI services
  | 
        
        
            | 
            | 
           21 | 
            *
  | 
        
        
            | 
            | 
           22 | 
            * @package   mod_lti
  | 
        
        
            | 
            | 
           23 | 
            * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
  | 
        
        
            | 
            | 
           24 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           25 | 
            */
  | 
        
        
            | 
            | 
           26 | 
           class service_exception_handler_test extends \advanced_testcase {
  | 
        
        
            | 
            | 
           27 | 
               /**
  | 
        
        
            | 
            | 
           28 | 
                * Testing service error handling.
  | 
        
        
            | 
            | 
           29 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           30 | 
               public function test_handle(): void {
  | 
        
        
           | 1 | 
           efrain | 
           31 | 
                   $handler = new service_exception_handler(false);
  | 
        
        
            | 
            | 
           32 | 
                   $handler->set_message_id('123');
  | 
        
        
            | 
            | 
           33 | 
                   $handler->set_message_type('testRequest');
  | 
        
        
            | 
            | 
           34 | 
                   $handler->handle(new \Exception('Error happened'));
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
                   $this->expectOutputRegex('/imsx_codeMajor>failure/');
  | 
        
        
            | 
            | 
           37 | 
                   $this->expectOutputRegex('/imsx_description>Error happened/');
  | 
        
        
            | 
            | 
           38 | 
                   $this->expectOutputRegex('/imsx_messageRefIdentifier>123/');
  | 
        
        
            | 
            | 
           39 | 
                   $this->expectOutputRegex('/imsx_operationRefIdentifier>testRequest/');
  | 
        
        
            | 
            | 
           40 | 
                   $this->expectOutputRegex('/imsx_POXBody><testResponse/');
  | 
        
        
            | 
            | 
           41 | 
               }
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
               /**
  | 
        
        
            | 
            | 
           44 | 
                * Testing service error handling when message ID and type are not known yet.
  | 
        
        
            | 
            | 
           45 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           46 | 
               public function test_handle_early_error(): void {
  | 
        
        
           | 1 | 
           efrain | 
           47 | 
                   $handler = new service_exception_handler(false);
  | 
        
        
            | 
            | 
           48 | 
                   $handler->handle(new \Exception('Error happened'));
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
                   $this->expectOutputRegex('/imsx_codeMajor>failure/');
  | 
        
        
            | 
            | 
           51 | 
                   $this->expectOutputRegex('/imsx_description>Error happened/');
  | 
        
        
            | 
            | 
           52 | 
                   $this->expectOutputRegex('/imsx_messageRefIdentifier\/>/');
  | 
        
        
            | 
            | 
           53 | 
                   $this->expectOutputRegex('/imsx_operationRefIdentifier>unknownRequest/');
  | 
        
        
            | 
            | 
           54 | 
                   $this->expectOutputRegex('/imsx_POXBody><unknownResponse/');
  | 
        
        
            | 
            | 
           55 | 
               }
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
               /**
  | 
        
        
            | 
            | 
           58 | 
                * Testing that a log file is generated when logging is turned on.
  | 
        
        
            | 
            | 
           59 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           60 | 
               public function test_handle_log(): void {
  | 
        
        
           | 1 | 
           efrain | 
           61 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
                   $handler = new service_exception_handler(true);
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   ob_start();
  | 
        
        
            | 
            | 
           68 | 
                   $handler->handle(new \Exception('Error happened'));
  | 
        
        
            | 
            | 
           69 | 
                   ob_end_clean();
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
                   $this->assertTrue(is_dir($CFG->dataroot.'/temp/mod_lti'));
  | 
        
        
            | 
            | 
           72 | 
                   $files = glob($CFG->dataroot.'/temp/mod_lti/mod*');
  | 
        
        
            | 
            | 
           73 | 
                   $this->assertEquals(1, count($files));
  | 
        
        
            | 
            | 
           74 | 
               }
  | 
        
        
            | 
            | 
           75 | 
           }
  |