Differences Between: [Versions 311 and 400]
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\event; 18 19 use context_system; 20 use context_user; 21 22 /** 23 * Unit tests for the dashboard events. 24 * 25 * @package core 26 * @category test 27 * @copyright 2016 Stephen Bourget 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30 class events_test extends \advanced_testcase { 31 32 /** @var user cobject */ 33 protected $user; 34 35 /** 36 * Setup often used objects for the following tests. 37 */ 38 protected function setUp(): void { 39 global $USER; 40 41 $this->resetAfterTest(); 42 43 // The user we are going to test this on. 44 $this->setAdminUser(); 45 $this->user = $USER; 46 } 47 48 /** 49 * Test the dashboard viewed event. 50 * 51 * There is no external API for viewing the dashboard, so the unit test will simply 52 * create and trigger the event and ensure data is returned as expected. 53 */ 54 public function test_dashboard_viewed() { 55 56 $user = $this->user; 57 // Trigger an event: dashboard viewed. 58 $eventparams = array( 59 'context' => $context = \context_user::instance($user->id) 60 ); 61 62 $event = \core\event\dashboard_viewed::create($eventparams); 63 // Trigger and capture the event. 64 $sink = $this->redirectEvents(); 65 $event->trigger(); 66 $events = $sink->get_events(); 67 $event = reset($events); 68 69 // Check that the event data is valid. 70 $this->assertInstanceOf('\core\event\dashboard_viewed', $event); 71 $this->assertEquals($user->id, $event->userid); 72 $this->assertDebuggingNotCalled(); 73 } 74 75 /** 76 * Test the dashboard reset event. 77 * 78 * We will reset the user dashboard to 79 * trigger the event and ensure data is returned as expected. 80 * 81 * @covers ::my_reset_page 82 */ 83 public function test_dashboard_reset() { 84 global $CFG, $DB; 85 require_once($CFG->dirroot . '/my/lib.php'); 86 87 $user = $this->user; 88 $usercontext = context_user::instance($this->user->id); 89 90 // Create at least one dashboard. 91 my_copy_page($this->user->id); 92 $this->assertNotEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PRIVATE, 93 'name' => MY_PAGE_DEFAULT])); 94 $this->assertNotEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id])); 95 96 // Reset the dashboard. 97 $sink = $this->redirectEvents(); 98 my_reset_page($user->id); 99 100 // Assert that the page and all th blocks were deleted. 101 $this->assertEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PRIVATE, 102 'name' => MY_PAGE_DEFAULT])); 103 $this->assertEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id])); 104 105 // Trigger and capture the event. 106 $events = $sink->get_events(); 107 $event = reset($events); 108 $sink->close(); 109 110 // Check that the event data is valid. 111 $this->assertInstanceOf('\core\event\dashboard_reset', $event); 112 $this->assertEquals($user->id, $event->userid); 113 $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']); 114 $this->assertEquals('my-index', $event->other['pagetype']); 115 $this->assertDebuggingNotCalled(); 116 117 // Reset the dashboard with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'. 118 $systempage = $DB->get_record('my_pages', ['userid' => null, 'name' => MY_PAGE_DEFAULT, 'private' => MY_PAGE_PUBLIC]); 119 $this->getDataGenerator()->create_block('online_users', [ 120 'parentcontextid' => context_system::instance()->id, 121 'pagetypepattern' => 'user-profile', 122 'subpagepattern' => $systempage->id, 123 ]); 124 125 my_copy_page($this->user->id, MY_PAGE_PUBLIC, 'user-profile'); 126 $this->assertNotEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PUBLIC, 127 'name' => MY_PAGE_DEFAULT])); 128 $this->assertNotEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id])); 129 130 $sink = $this->redirectEvents(); 131 my_reset_page($user->id, MY_PAGE_PUBLIC, 'user-profile'); 132 $this->assertEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PUBLIC, 133 'name' => MY_PAGE_DEFAULT])); 134 $this->assertEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id])); 135 136 // Trigger and capture the event. 137 $events = $sink->get_events(); 138 $event = reset($events); 139 $sink->close(); 140 141 $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']); 142 $this->assertEquals('user-profile', $event->other['pagetype']); 143 } 144 145 /** 146 * Test the dashboards reset event. 147 * 148 * We will reset all user dashboards to 149 * trigger the event and ensure data is returned as expected. 150 * 151 * @covers ::my_reset_page_for_all_users 152 */ 153 public function test_dashboards_reset() { 154 global $CFG, $USER, $DB; 155 require_once($CFG->dirroot . '/my/lib.php'); 156 157 $usercontext = context_user::instance($this->user->id); 158 159 // Create at least one dashboard. 160 my_copy_page($this->user->id); 161 $this->assertNotEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PRIVATE, 162 'name' => MY_PAGE_DEFAULT])); 163 $this->assertNotEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id])); 164 165 // Reset all dashbaords. 166 $sink = $this->redirectEvents(); 167 my_reset_page_for_all_users(); 168 169 // Assert that the page and all th blocks were deleted. 170 $this->assertEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PRIVATE, 171 'name' => MY_PAGE_DEFAULT])); 172 $this->assertEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id])); 173 174 // Trigger and capture the event. 175 $events = $sink->get_events(); 176 $event = reset($events); 177 $sink->close(); 178 179 // Check that the event data is valid. 180 $this->assertInstanceOf('\core\event\dashboards_reset', $event); 181 $this->assertEquals($USER->id, $event->userid); 182 $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']); 183 $this->assertEquals('my-index', $event->other['pagetype']); 184 $this->assertDebuggingNotCalled(); 185 186 // Reset the dashboards with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'. 187 $systempage = $DB->get_record('my_pages', ['userid' => null, 'name' => MY_PAGE_DEFAULT, 'private' => MY_PAGE_PUBLIC]); 188 $this->getDataGenerator()->create_block('online_users', [ 189 'parentcontextid' => context_system::instance()->id, 190 'pagetypepattern' => 'user-profile', 191 'subpagepattern' => $systempage->id, 192 ]); 193 194 my_copy_page($this->user->id, MY_PAGE_PUBLIC, 'user-profile'); 195 $this->assertNotEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PUBLIC, 196 'name' => MY_PAGE_DEFAULT])); 197 $this->assertNotEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id])); 198 199 $sink = $this->redirectEvents(); 200 my_reset_page_for_all_users(MY_PAGE_PUBLIC, 'user-profile'); 201 $this->assertEmpty($DB->get_records('my_pages', ['userid' => $this->user->id, 'private' => MY_PAGE_PUBLIC, 202 'name' => MY_PAGE_DEFAULT])); 203 $this->assertEmpty($DB->get_records('block_instances', ['parentcontextid' => $usercontext->id])); 204 205 // Trigger and capture the event. 206 $events = $sink->get_events(); 207 $event = reset($events); 208 $sink->close(); 209 210 $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']); 211 $this->assertEquals('user-profile', $event->other['pagetype']); 212 } 213 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body