See Release Notes
Long Term Support Release
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 declare(strict_types=1); 18 19 namespace core_reportbuilder\local\helpers; 20 21 use advanced_testcase; 22 23 /** 24 * Unit tests for the user filter helper 25 * 26 * @package core_reportbuilder 27 * @covers \core_reportbuilder\local\helpers\user_filter_manager 28 * @copyright 2021 Paul Holden <paulh@moodle.com> 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class user_filter_manager_test extends advanced_testcase { 32 33 /** 34 * Helper method to return all user preferences for filters - based on the current storage backend using the same 35 * 36 * @return array 37 */ 38 private function get_filter_preferences(): array { 39 return array_filter(get_user_preferences(), static function(string $key): bool { 40 return strpos($key, 'reportbuilder-report-') === 0; 41 }, ARRAY_FILTER_USE_KEY); 42 } 43 44 /** 45 * Data provider for {@see test_get} 46 * 47 * @return array 48 */ 49 public function get_provider(): array { 50 return [ 51 'Small value' => ['foo'], 52 'Large value' => [str_repeat('A', 4000)], 53 'Empty value' => [''], 54 ]; 55 } 56 57 /** 58 * Test getting filter values 59 * 60 * @param string $value 61 * 62 * @dataProvider get_provider 63 */ 64 public function test_get(string $value): void { 65 $this->resetAfterTest(); 66 67 $values = [ 68 'entity:filter_name' => $value, 69 ]; 70 user_filter_manager::set(5, $values); 71 72 // Make sure we get the same value back. 73 $this->assertEquals($values, user_filter_manager::get(5)); 74 } 75 76 /** 77 * Test getting filter values that once spanned multiple chunks 78 */ 79 public function test_get_large_to_small(): void { 80 $this->resetAfterTest(); 81 82 // Set a large initial filter value. 83 user_filter_manager::set(5, [ 84 'longvalue' => str_repeat('ABCD', 1000), 85 ]); 86 87 // Sanity check, there should be 4 (because 4000 characters plus some JSON encoding requires that many chunks). 88 $preferences = $this->get_filter_preferences(); 89 $this->assertCount(4, $preferences); 90 91 $values = [ 92 'longvalue' => 'ABCD', 93 ]; 94 user_filter_manager::set(5, $values); 95 96 // Make sure we get the same value back. 97 $this->assertEquals($values, user_filter_manager::get(5)); 98 99 // Everything should now fit in a single filter preference. 100 $preferences = $this->get_filter_preferences(); 101 $this->assertCount(1, $preferences); 102 } 103 104 /** 105 * Test getting filter values that haven't been set 106 */ 107 public function test_get_empty(): void { 108 $this->assertEquals([], user_filter_manager::get(5)); 109 } 110 111 /** 112 * Data provider for {@see test_reset_all} 113 * 114 * @return array 115 */ 116 public function reset_all_provider(): array { 117 return [ 118 'Small value' => ['foo'], 119 'Large value' => [str_repeat('A', 4000)], 120 'Empty value' => [''], 121 ]; 122 } 123 124 /** 125 * Test resetting all filter values 126 * 127 * @param string $value 128 * 129 * @dataProvider reset_all_provider 130 */ 131 public function test_reset_all(string $value): void { 132 $this->resetAfterTest(); 133 134 user_filter_manager::set(5, [ 135 'entity:filter_name' => $value 136 ]); 137 138 $reset = user_filter_manager::reset_all(5); 139 $this->assertTrue($reset); 140 141 // We should get an empty array back. 142 $this->assertEquals([], user_filter_manager::get(5)); 143 144 // All filter preferences should be removed. 145 $this->assertEmpty($this->get_filter_preferences()); 146 } 147 148 /** 149 * Test resetting single filter values 150 */ 151 public function test_reset_single(): void { 152 $this->resetAfterTest(); 153 154 user_filter_manager::set(5, [ 155 'entity:filter_name' => 'foo', 156 'entity:filter_value' => 'bar', 157 'entity:other_name' => 'baz', 158 'entity:other_value' => 'bax', 159 ]); 160 161 $reset = user_filter_manager::reset_single(5, 'entity:other'); 162 $this->assertTrue($reset); 163 164 $this->assertEquals([ 165 'entity:filter_name' => 'foo', 166 'entity:filter_value' => 'bar', 167 ], user_filter_manager::get(5)); 168 } 169 170 /** 171 * Test merging filter values 172 */ 173 public function test_merge(): void { 174 $this->resetAfterTest(); 175 176 $values = [ 177 'entity:filter_name' => 'foo', 178 'entity:filter_value' => 'bar', 179 'entity:filter2_name' => 'tree', 180 'entity:filter2_value' => 'house', 181 ]; 182 183 // Make sure we get the same value back. 184 user_filter_manager::set(5, $values); 185 $this->assertEqualsCanonicalizing($values, user_filter_manager::get(5)); 186 187 user_filter_manager::merge(5, [ 188 'entity:filter_name' => 'twotimesfoo', 189 'entity:filter_value' => 'twotimesbar', 190 ]); 191 192 // Make sure that both values have been changed and the other values have not been modified. 193 $expected = [ 194 'entity:filter_name' => 'twotimesfoo', 195 'entity:filter_value' => 'twotimesbar', 196 'entity:filter2_name' => 'tree', 197 'entity:filter2_value' => 'house', 198 ]; 199 $this->assertEqualsCanonicalizing($expected, user_filter_manager::get(5)); 200 } 201 202 /** 203 * Test to get all filters from a given user 204 */ 205 public function test_get_all_for_user(): void { 206 $this->resetAfterTest(); 207 $user = $this->getDataGenerator()->create_user(); 208 $this->setUser($user); 209 210 $filtervalues1 = [ 211 'entity:filter_name' => 'foo', 212 'entity:filter_value' => 'bar', 213 'entity:other_name' => 'baz', 214 'entity:other_value' => 'bax', 215 ]; 216 user_filter_manager::set(5, $filtervalues1); 217 218 $filtervalues2 = [ 219 'entity:filter_name' => 'blue', 220 'entity:filter_value' => 'red', 221 ]; 222 user_filter_manager::set(9, $filtervalues2); 223 224 $this->setAdminUser(); 225 $values = user_filter_manager::get_all_for_user((int)$user->id); 226 $this->assertEqualsCanonicalizing([$filtervalues1, $filtervalues2], [reset($values), end($values)]); 227 228 // Check for a user with no filters. 229 $user2 = $this->getDataGenerator()->create_user(); 230 $values = user_filter_manager::get_all_for_user((int)$user2->id); 231 $this->assertEmpty($values); 232 } 233 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body