MDL-69240 tool_moodlenet: Clean MoodleNet profile field
[moodle.git] / mod / lti / tests / service_exception_handler_test.php
blob50e257f7ee0996372f11e37231f8843b4057c7cc
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.
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/>.
17 /**
18 * Tests Exception handler for LTI services
20 * @package mod_lti
21 * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 use mod_lti\service_exception_handler;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Tests Exception handler for LTI services
32 * @package mod_lti
33 * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class mod_lti_service_exception_handler_testcase extends advanced_testcase {
37 /**
38 * Testing service error handling.
40 public function test_handle() {
41 $handler = new service_exception_handler(false);
42 $handler->set_message_id('123');
43 $handler->set_message_type('testRequest');
44 $handler->handle(new Exception('Error happened'));
46 $this->expectOutputRegex('/imsx_codeMajor>failure/');
47 $this->expectOutputRegex('/imsx_description>Error happened/');
48 $this->expectOutputRegex('/imsx_messageRefIdentifier>123/');
49 $this->expectOutputRegex('/imsx_operationRefIdentifier>testRequest/');
50 $this->expectOutputRegex('/imsx_POXBody><testResponse/');
53 /**
54 * Testing service error handling when message ID and type are not known yet.
56 public function test_handle_early_error() {
57 $handler = new service_exception_handler(false);
58 $handler->handle(new Exception('Error happened'));
60 $this->expectOutputRegex('/imsx_codeMajor>failure/');
61 $this->expectOutputRegex('/imsx_description>Error happened/');
62 $this->expectOutputRegex('/imsx_messageRefIdentifier\/>/');
63 $this->expectOutputRegex('/imsx_operationRefIdentifier>unknownRequest/');
64 $this->expectOutputRegex('/imsx_POXBody><unknownResponse/');
67 /**
68 * Testing that a log file is generated when logging is turned on.
70 public function test_handle_log() {
71 global $CFG;
73 $this->resetAfterTest();
75 $handler = new service_exception_handler(true);
77 ob_start();
78 $handler->handle(new Exception('Error happened'));
79 ob_end_clean();
81 $this->assertTrue(is_dir($CFG->dataroot.'/temp/mod_lti'));
82 $files = glob($CFG->dataroot.'/temp/mod_lti/mod*');
83 $this->assertEquals(1, count($files));