[ Index ] |
PHP Cross Reference of YOURLS |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Filter related tests 5 * 6 * @since 0.1 7 */ 8 #[\PHPUnit\Framework\Attributes\Group('plugins')] 9 class FiltersTest extends PHPUnit\Framework\TestCase { 10 11 /** 12 * this var will allow to share "$this" across multiple tests here 13 */ 14 public static $instance; 15 16 /** 17 * Check adding a filter with a simple function name 18 * 19 * Syntax tested: yourls_add_filter( $hook, 'func_name' ); 20 * 21 * @since 0.1 22 */ 23 public function test_add_filter_funcname() { 24 // Random function name 25 $hook = rand_str(); 26 $this->assertFalse( yourls_has_filter( $hook ) ); 27 yourls_add_filter( $hook, rand_str() ); 28 $this->assertTrue( yourls_has_filter( $hook ) ); 29 30 // Specific function name to test with yourls_apply_filter 31 $hook = rand_str(); 32 $this->assertFalse( yourls_has_filter( $hook ) ); 33 yourls_add_filter( $hook, 'change_variable' ); 34 $this->assertTrue( yourls_has_filter( $hook ) ); 35 36 return $hook; 37 } 38 39 /** 40 * Check applying a filter hooked with a simple function name 41 * 42 * @since 0.1 43 */ 44 #[\PHPUnit\Framework\Attributes\Depends('test_add_filter_funcname')] 45 public function test_apply_filter_funcname( $hook ) { 46 $var = rand_str(); 47 48 $filtered = yourls_apply_filter( $hook, $var ); 49 $this->assertNotSame( $var, $filtered ); 50 } 51 52 53 /** 54 * Check removing a filter hooked with a simple function name 55 * 56 * @since 0.1 57 */ 58 public function test_remove_filter_funcname() { 59 $hook = rand_str(); 60 $function = rand_str(); 61 62 $this->assertFalse( yourls_has_filter( $hook ) ); 63 yourls_add_filter( $hook, $function ); 64 $this->assertTrue( yourls_has_filter( $hook ) ); 65 66 $removed = yourls_remove_filter( $hook, $function ); 67 $this->assertTrue( $removed ); 68 $this->assertFalse( yourls_has_filter( $hook ) ); 69 } 70 71 /** 72 * Check that default priority is 10 73 * 74 * @since 0.1 75 */ 76 public function test_default_priority() { 77 $hook = rand_str(); 78 global $yourls_filters; 79 80 $this->assertArrayNotHasKey( $hook, $yourls_filters ); 81 yourls_add_filter( $hook, rand_str() ); 82 $this->assertArrayHasKey( 10, $yourls_filters[$hook] ); 83 } 84 85 /** 86 * Check removing a filter with non default priority 87 * 88 * @since 0.1 89 */ 90 public function test_remove_filter_priority() { 91 $hook = rand_str(); 92 $function = rand_str(); 93 // Random priority but not 10 94 do { 95 $priority = rand( 1,100 ); 96 } while ( $priority == 10 ); 97 98 $this->assertFalse( yourls_has_filter( $hook ) ); 99 yourls_add_filter( $hook, $function, $priority ); 100 $this->assertTrue( yourls_has_filter( $hook ) ); 101 102 $removed = yourls_remove_filter( $hook, $function ); 103 $this->assertFalse( $removed ); 104 105 $removed = yourls_remove_filter( $hook, $function, $priority ); 106 $this->assertTrue( $removed ); 107 $this->assertFalse( yourls_has_filter( $hook ) ); 108 } 109 110 111 /** 112 * Check adding a filter with an anonymous function using create_function() 113 * 114 * Syntax tested: yourls_add_filter( $hook, create_function() ); 115 * 116 * @since 0.1 117 */ 118 public function test_add_filter_create_function() { 119 $hook = rand_str(); 120 $this->assertFalse( yourls_has_filter( $hook ) ); 121 yourls_add_filter( $hook, function() { 122 return rand_str(); 123 } ); 124 $this->assertTrue( yourls_has_filter( $hook ) ); 125 126 return $hook; 127 } 128 129 /** 130 * Check applying a filter hooked with an anonymous function using create_function() 131 * 132 * @since 0.1 133 */ 134 #[\PHPUnit\Framework\Attributes\Depends('test_add_filter_create_function')] 135 public function test_apply_filter_create_function( $hook ) { 136 $var = rand_str(); 137 138 $filtered = yourls_apply_filter( $hook, $var ); 139 $this->assertNotSame( $var, $filtered ); 140 } 141 142 143 /** 144 * Check adding a filter with function within class 145 * 146 * Syntax tested: yourls_add_filter( $hook, 'Class::Function' ); 147 * 148 * @since 0.1 149 */ 150 public function test_add_filter_within_class() { 151 $hook = rand_str(); 152 $this->assertFalse( yourls_has_filter( $hook ) ); 153 yourls_add_filter( $hook, 'Change_Variable::change_it' ); 154 $this->assertTrue( yourls_has_filter( $hook ) ); 155 156 return $hook; 157 } 158 159 /** 160 * Check applying a filter hooked with function within class 161 * 162 * @since 0.1 163 */ 164 #[\PHPUnit\Framework\Attributes\Depends('test_add_filter_within_class')] 165 public function test_apply_filter_within_class( $hook ) { 166 $var = rand_str(); 167 168 $filtered = yourls_apply_filter( $hook, $var ); 169 $this->assertNotSame( $var, $filtered ); 170 } 171 172 /** 173 * Check removing a filter hooked with function within class 174 * 175 * @since 0.1 176 */ 177 #[\PHPUnit\Framework\Attributes\Depends('test_add_filter_within_class')] 178 public function test_remove_filter_within_class( $hook ) { 179 $removed = yourls_remove_filter( $hook, 'Change_Variable::change_it' ); 180 $this->assertTrue( $removed ); 181 $this->assertFalse( yourls_has_filter( $hook ) ); 182 } 183 184 185 /** 186 * Check adding filter with function within class using an array 187 * 188 * Syntax tested: yourls_add_filter( $hook, array( 'Class', 'Function' ) ); 189 * 190 * @since 0.1 191 */ 192 public function test_add_filter_within_class_array() { 193 $hook = rand_str(); 194 $this->assertFalse( yourls_has_filter( $hook ) ); 195 yourls_add_filter( $hook, array( 'Change_Variable', 'change_it' ) ); 196 $this->assertTrue( yourls_has_filter( $hook ) ); 197 198 return $hook; 199 } 200 201 /** 202 * Check applying a filter hooked with function within class 203 * 204 * @since 0.1 205 */ 206 #[\PHPUnit\Framework\Attributes\Depends('test_add_filter_within_class_array')] 207 public function test_apply_filter_within_class_array( $hook ) { 208 $var = rand_str(); 209 $filtered = yourls_apply_filter( $hook, $var ); 210 $this->assertNotSame( $var, $filtered ); 211 } 212 213 /** 214 * Check removing a filter hooked with function within class using an array 215 * 216 * @since 0.1 217 */ 218 #[\PHPUnit\Framework\Attributes\Depends('test_add_filter_within_class_array')] 219 public function test_remove_filter_within_class_array( $hook ) { 220 $removed = yourls_remove_filter( $hook, array( 'Change_Variable', 'change_it' ) ); 221 $this->assertTrue( $removed ); 222 $this->assertFalse( yourls_has_filter( $hook ) ); 223 } 224 225 226 /** 227 * Check adding a filter with function within class instance 228 * 229 * Syntax tested: yourls_add_filter( $hook, array( $class, 'function' ) ); 230 * 231 * @since 0.1 232 */ 233 public function test_add_filter_within_class_instance() { 234 /* Note : in the unit tests context, we cannot rely on "$this" keeping the same 235 * between tests, whereas it totally works in a "normal" class context 236 * For this reason, using yourls_add_filter($hook, array($this, 'some_func')) in one test and 237 * yourls_remove_filter($hook,array($this,'some_func')) in another test doesn't work. 238 * To circumvent this, we're storing $this in $instance. 239 */ 240 self::$instance = $this; 241 $hook = rand_str(); 242 $this->assertFalse( yourls_has_filter( $hook ) ); 243 yourls_add_filter( $hook, array( self::$instance, 'change_variable' ) ); 244 $this->assertTrue( yourls_has_filter( $hook ) ); 245 246 return $hook; 247 } 248 249 /** 250 * Check applying a filter hooked with function within class instance 251 * 252 * @since 0.1 253 */ 254 #[\PHPUnit\Framework\Attributes\Depends('test_add_filter_within_class_instance')] 255 public function test_apply_filter_within_class_instance( $hook ) { 256 $var = rand_str(); 257 $filtered = yourls_apply_filter( $hook, $var ); 258 $this->assertNotSame( $var, $filtered ); 259 } 260 261 /** 262 * Check removing a filter hooked with function within class 263 * 264 * @since 0.1 265 */ 266 #[\PHPUnit\Framework\Attributes\Depends('test_add_filter_within_class_instance')] 267 public function test_remove_filter_within_class_instance( $hook ) { 268 $this->assertTrue( yourls_has_filter( $hook ) ); 269 $removed = yourls_remove_filter( $hook, array( self::$instance, 'change_variable' ) ); 270 $this->assertTrue( $removed ); 271 $this->assertFalse( yourls_has_filter( $hook ) ); 272 } 273 274 /** 275 * Check that hooking to 'Class::Method' or array( 'Class', 'Method') is the same 276 * 277 * @since 0.1 278 */ 279 public function test_add_filter_class_and_array() { 280 $hook = rand_str(); 281 282 $this->assertFalse( yourls_has_filter( $hook ) ); 283 284 yourls_add_filter( $hook, array( 'Class', 'Method' ) ); 285 $this->assertSame( 10, yourls_has_filter( $hook, array( 'Class', 'Method' ) ) ); 286 $this->assertSame( 10, yourls_has_filter( $hook, 'Class::Method' ) ); 287 } 288 289 290 /** 291 * Check adding a filter with anonymous function using closure 292 * 293 * Syntax tested: yourls_add_filter( $hook, function(){ // do stuff } ); 294 * 295 * @since 0.1 296 */ 297 public function test_add_filter_closure() { 298 $hook = rand_str(); 299 $this->assertFalse( yourls_has_filter( $hook ) ); 300 yourls_add_filter( $hook, function() { return rand_str(); } ); 301 $this->assertTrue( yourls_has_filter( $hook ) ); 302 303 return $hook; 304 } 305 306 /** 307 * Check applying a filter hooked with anonymous function using closure 308 * 309 * @since 0.1 310 */ 311 #[\PHPUnit\Framework\Attributes\Depends('test_add_filter_closure')] 312 public function test_apply_filter_closure( $hook ) { 313 $var = rand_str(); 314 315 $filtered = yourls_apply_filter( $hook, $var ); 316 $this->assertNotSame( $var, $filtered ); 317 } 318 319 /** 320 * Check applying multiple filters to one hook 321 * 322 * @since 0.1 323 */ 324 public function test_multiple_filter() { 325 $hook = rand_str(); 326 $var = rand_str(); 327 328 yourls_add_filter( $hook, function( $in ) { return $in . "1"; } ); 329 yourls_add_filter( $hook, function( $in ) { return $in . "2"; } ); 330 331 $filtered = yourls_apply_filter( $hook, $var ); 332 $this->assertSame( $var . "1" . "2", $filtered ); 333 } 334 335 /** 336 * Check applying multiple filters with priorities to one hook 337 * 338 * @since 0.1 339 */ 340 public function test_multiple_filter_with_priority() { 341 $hook = rand_str(); 342 $var = rand_str(); 343 344 yourls_add_filter( $hook, function( $in ) { return $in . "1"; }, 10 ); 345 yourls_add_filter( $hook, function( $in ) { return $in . "2"; }, 9 ); 346 347 $filtered = yourls_apply_filter( $hook, $var ); 348 $this->assertSame( $var . "2" . "1", $filtered ); 349 350 $hook = rand_str(); 351 $var = rand_str(); 352 353 yourls_add_filter( $hook, function( $in ) { return $in . "1"; }, 10 ); 354 yourls_add_filter( $hook, function( $in ) { return $in . "2"; }, 11 ); 355 356 $filtered = yourls_apply_filter( $hook, $var ); 357 $this->assertSame( $var . "1" . "2", $filtered ); 358 } 359 360 /** 361 * Check return values of yourls_has_filter() 362 * 363 * @since 0.1 364 */ 365 public function test_has_filter_return_values() { 366 $hook = rand_str(); 367 368 yourls_add_filter( $hook, 'some_function' ); 369 yourls_add_filter( $hook, 'some_other_function', 1337 ); 370 371 $this->assertTrue( yourls_has_filter( $hook ) ); 372 $this->assertSame( 10, yourls_has_filter( $hook, 'some_function' ) ); 373 $this->assertSame( 1337, yourls_has_filter( $hook, 'some_other_function' ) ); 374 $this->assertFalse( yourls_has_filter( $hook, 'nope_not_this_function' ) ); 375 } 376 377 /** 378 * Check that yourls_get_filters() returns expected values 379 */ 380 public function test_get_filters() { 381 $hook = rand_str(); 382 383 yourls_add_filter( $hook, 'some_function' ); 384 yourls_add_filter( $hook, 'some_other_function', 1337 ); 385 386 $filters = yourls_get_filters( $hook ); 387 $this->assertArrayHasKey('some_function', $filters[10]); 388 $this->assertArrayHasKey('some_other_function', $filters[1337]); 389 390 $this->assertSame( [], yourls_get_filters( rand_str() ) ); 391 } 392 393 /** 394 * Check that applied function must exist 395 * 396 * @since 0.1 397 */ 398 public function test_function_must_exist_if_applied() { 399 if (PHP_VERSION_ID >= 80000) { 400 $this->expectException(TypeError::class); 401 } else { 402 $this->expectException(PHPUnit\Framework\Error\Error::class); 403 } 404 $this->expectExceptionMessageMatches('/call_user_func_array\(\).* a valid callback, function (\'|")[0-9a-z]+(\'|") not found or invalid function name/'); 405 406 $hook = rand_str(); 407 yourls_add_filter( $hook, rand_str() ); 408 // this will trigger an error, converted to an exception by PHPUnit 409 $test = yourls_apply_filter( $hook, rand_str() ); 410 } 411 412 /** 413 * Check filters accept multiple and defined number of arguments 414 * 415 * @since 0.1 416 */ 417 public function test_filter_specified_arguments() { 418 // Ask for 2 arguments and provide 2 419 $hook = rand_str(); 420 yourls_add_filter( $hook, function( $var1 = '', $var2 = '' ) { return "$var1 $var2"; }, 10, 2 ); 421 $test = yourls_apply_filter( $hook, 'hello', 'world' ); 422 $this->assertSame( 'hello world', $test ); 423 424 // Ask for 1 argument and provide 2 425 $hook = rand_str(); 426 yourls_add_filter( $hook, function( $var1 = '', $var2 = '' ) { return "$var1 $var2"; }, 10, 1 ); 427 $test = yourls_apply_filter( $hook, 'hello', 'world' ); 428 $this->assertSame( 'hello ', $test ); 429 430 // Ask for 2 arguments and provide 1 431 $hook = rand_str(); 432 yourls_add_filter( $hook, function( $var1 = '', $var2 = '' ) { return "$var1 $var2"; }, 10, 2 ); 433 $test = yourls_apply_filter( $hook, 'hello' ); 434 $this->assertSame( 'hello ', $test ); 435 } 436 437 /** 438 * Make sure yourls_apply_filter accepts an arbitrary number of elements if unspecified 439 * 440 * @since 0.1 441 */ 442 public function test_filter_arbitrary_arguments() { 443 $hook = rand_str(); 444 $var1 = rand_str(); 445 $var2 = rand_str(); 446 $var3 = rand_str(); 447 448 yourls_add_filter( $hook, function( $var1 = '', $var2 = '', $var3 = '' ) { return $var1 . $var2 . $var3; } ); 449 450 $filtered = yourls_apply_filter( $hook, $var1 ); 451 $this->assertSame( $var1, $filtered ); 452 453 $filtered = yourls_apply_filter( $hook, $var1, $var2 ); 454 $this->assertSame( $var1 . $var2, $filtered ); 455 456 $filtered = yourls_apply_filter( $hook, $var1, $var2, $var3 ); 457 $this->assertSame( $var1 . $var2 . $var3, $filtered ); 458 } 459 460 /** 461 * Check applying multiple filters and count executions 462 * 463 * @since 0.1 464 */ 465 public function test_multiple_filter_and_count() { 466 $hook = rand_str(); 467 468 $times = mt_rand( 5, 15 ); 469 for ( $i = 1; $i <= $times; $i++ ) { 470 // This will register every time a different closure function 471 yourls_add_filter( $hook, function() { global $counter; ++$counter; return rand_str(); } ); 472 } 473 474 global $counter; 475 $counter = 0; 476 $filtered = yourls_apply_filter( $hook, rand_str() ); 477 $this->assertSame( $times, $counter ); 478 } 479 480 /** 481 * Dummy function -- just modifies the value of a var 482 */ 483 public function change_variable( $var ) { 484 $var = rand_str(); 485 return $var; 486 } 487 488 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Apr 26 05:10:07 2025 | Cross-referenced by PHPXref 0.7.1 |