[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/tests/plugins/ -> filters.php (source)

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


Generated: Wed Sep 28 05:10:02 2022 Cross-referenced by PHPXref 0.7.1