2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for core\notification.
22 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Unit tests for core\notification.
34 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_notification_testcase
extends advanced_testcase
{
40 * Setup required for all notification tests.
42 * This includes emptying the list of notifications on the session, resetting any session which exists, and setting
43 * up a new moodle_page object.
45 public function setUp() {
46 global $PAGE, $SESSION;
49 $PAGE = new moodle_page();
50 \core\session\manager
::init_empty_session();
51 $SESSION->notifications
= [];
55 * Tear down required for all notification tests.
57 * This includes emptying the list of notifications on the session, resetting any session which exists, and setting
58 * up a new moodle_page object.
60 public function tearDown() {
61 global $PAGE, $SESSION;
64 \core\session\manager
::init_empty_session();
65 $SESSION->notifications
= [];
70 * Test the way in which notifications are added to the session in different stages of the page load.
72 public function test_add_during_output_stages() {
73 global $PAGE, $SESSION;
75 \core\notification
::add('Example before header', \core\notification
::INFO
);
76 $this->assertCount(1, $SESSION->notifications
);
78 $PAGE->set_state(\moodle_page
::STATE_PRINTING_HEADER
);
79 \core\notification
::add('Example during header', \core\notification
::INFO
);
80 $this->assertCount(2, $SESSION->notifications
);
82 $PAGE->set_state(\moodle_page
::STATE_IN_BODY
);
83 \core\notification
::add('Example in body', \core\notification
::INFO
);
84 $this->expectOutputRegex('/Example in body/');
85 $this->assertCount(2, $SESSION->notifications
);
87 $PAGE->set_state(\moodle_page
::STATE_DONE
);
88 \core\notification
::add('Example after page', \core\notification
::INFO
);
89 $this->assertCount(3, $SESSION->notifications
);
93 * Test fetching of notifications from the session.
95 public function test_fetch() {
96 // Initially there won't be any notifications.
97 $this->assertCount(0, \core\notification
::fetch());
99 // Adding a notification should make one available to fetch.
100 \core\notification
::success('Notification created');
101 $this->assertCount(1, \core\notification
::fetch());
102 $this->assertCount(0, \core\notification
::fetch());
106 * Test that session notifications are persisted across session clears.
108 public function test_session_persistance() {
109 global $PAGE, $SESSION;
111 // Initially there won't be any notifications.
112 $this->assertCount(0, $SESSION->notifications
);
114 // Adding a notification should make one available to fetch.
115 \core\notification
::success('Notification created');
116 $this->assertCount(1, $SESSION->notifications
);
118 // Re-creating the session will not empty the notification bag.
119 \core\session\manager
::init_empty_session();
120 $this->assertCount(1, $SESSION->notifications
);