Differences Between: [Versions 310 and 311] [Versions 39 and 311]
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; 18 19 /** 20 * Unit tests for core\notification. 21 * 22 * @package core 23 * @category test 24 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk> 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 26 */ 27 class notification_test extends \advanced_testcase { 28 29 /** 30 * Setup required for all notification tests. 31 * 32 * This includes emptying the list of notifications on the session, resetting any session which exists, and setting 33 * up a new \moodle_page object. 34 */ 35 public function setUp(): void { 36 global $PAGE, $SESSION; 37 38 parent::setUp(); 39 $PAGE = new \moodle_page(); 40 \core\session\manager::init_empty_session(); 41 $SESSION->notifications = []; 42 } 43 44 /** 45 * Tear down required for all notification tests. 46 * 47 * This includes emptying the list of notifications on the session, resetting any session which exists, and setting 48 * up a new \moodle_page object. 49 */ 50 public function tearDown(): void { 51 global $PAGE, $SESSION; 52 53 $PAGE = null; 54 \core\session\manager::init_empty_session(); 55 $SESSION->notifications = []; 56 parent::tearDown(); 57 } 58 59 /** 60 * Test the way in which notifications are added to the session in different stages of the page load. 61 */ 62 public function test_add_during_output_stages() { 63 global $PAGE, $SESSION; 64 65 \core\notification::add('Example before header', \core\notification::INFO); 66 $this->assertCount(1, $SESSION->notifications); 67 68 $PAGE->set_state(\moodle_page::STATE_PRINTING_HEADER); 69 \core\notification::add('Example during header', \core\notification::INFO); 70 $this->assertCount(2, $SESSION->notifications); 71 72 $PAGE->set_state(\moodle_page::STATE_IN_BODY); 73 \core\notification::add('Example in body', \core\notification::INFO); 74 $this->expectOutputRegex('/Example in body/'); 75 $this->assertCount(2, $SESSION->notifications); 76 77 $PAGE->set_state(\moodle_page::STATE_DONE); 78 \core\notification::add('Example after page', \core\notification::INFO); 79 $this->assertCount(2, $SESSION->notifications); 80 $this->expectOutputRegex('/Example after page/'); 81 82 \core\session\manager::write_close(); 83 \core\notification::add('Example after write_close', \core\notification::INFO); 84 $this->assertCount(2, $SESSION->notifications); 85 $this->expectOutputRegex('/Example after write_close/'); 86 87 // Simulate shutdown handler which calls fetch. 88 $this->assertCount(2, \core\notification::fetch()); 89 } 90 91 /** 92 * Test fetching of notifications from the session. 93 */ 94 public function test_fetch() { 95 // Initially there won't be any notifications. 96 $this->assertCount(0, \core\notification::fetch()); 97 98 // Adding a notification should make one available to fetch. 99 \core\notification::success('Notification created'); 100 $this->assertCount(1, \core\notification::fetch()); 101 $this->assertCount(0, \core\notification::fetch()); 102 } 103 104 /** 105 * Test that session notifications are persisted across session clears. 106 */ 107 public function test_session_persistance() { 108 global $PAGE, $SESSION; 109 110 // Initially there won't be any notifications. 111 $this->assertCount(0, $SESSION->notifications); 112 113 // Adding a notification should make one available to fetch. 114 \core\notification::success('Notification created'); 115 $this->assertCount(1, $SESSION->notifications); 116 117 // Re-creating the session will not empty the notification bag. 118 \core\session\manager::init_empty_session(); 119 $this->assertCount(1, $SESSION->notifications); 120 } 121 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body