[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Action related tests
   5   *
   6   * @group plugins
   7   * @since 0.1
   8   */
   9  
  10  class Plugin_Actions_Tests extends PHPUnit\Framework\TestCase {
  11  
  12      /**
  13       * Check adding an action with a simple function name
  14       *
  15       * Syntax tested: yourls_add_action( $hook, 'func_name' );
  16       *
  17       * @since 0.1
  18       */
  19  	public function test_add_action_funcname() {
  20          // Random function name
  21          $hook = rand_str();
  22          $this->assertFalse( yourls_has_action( $hook ) );
  23          yourls_add_action( $hook, rand_str() );
  24          $this->assertTrue( yourls_has_action( $hook ) );
  25  
  26          // Specific function name to test with yourls_do_action
  27          $hook = rand_str();
  28          $this->assertFalse( yourls_has_action( $hook ) );
  29          yourls_add_action( $hook, 'change_one_global' );
  30          $this->assertTrue( yourls_has_action( $hook ) );
  31  
  32          return $hook;
  33      }
  34  
  35      /**
  36       * Remove an action
  37       *
  38       * @since 0.1
  39       */
  40      public function test_remove_action() {
  41          $hook = rand_str();
  42          $action = rand_str();
  43  
  44          $this->assertFalse( yourls_has_action( $hook ) );
  45          yourls_add_action( $hook, $action );
  46          $this->assertTrue( yourls_has_action( $hook ) );
  47          $this->assertSame( 10, yourls_has_action( $hook, $action ) );
  48  
  49          yourls_remove_action( $hook, $action );
  50          $this->assertFalse( yourls_has_action( $hook ) );
  51      }
  52  
  53      /**
  54       * Add several actions on the same hook
  55       *
  56       * @since 0.1
  57       */
  58      public function test_add_several_actions_default_priority() {
  59          $hook = rand_str();
  60  
  61          $times = mt_rand( 5, 15 );
  62          for ( $i = 1; $i <= $times; $i++ ) {
  63              yourls_add_action( $hook, rand_str() );
  64          }
  65  
  66          $this->assertTrue( yourls_has_action( $hook ) );
  67          global $yourls_filters;
  68          $this->assertSame( $times, count( $yourls_filters[ $hook ][10] ) );
  69      }
  70  
  71      /**
  72       * Add several actions on the same hook with different priorities
  73       *
  74       * @since 0.1
  75       */
  76      public function test_add_several_actions_random_priorities() {
  77          $hook = rand_str();
  78  
  79          $times = mt_rand( 5, 15 );
  80          for ( $i = 1; $i <= $times; $i++ ) {
  81              yourls_add_action( $hook, rand_str(), mt_rand( 1, 10 ) );
  82          }
  83  
  84          $this->assertTrue( yourls_has_action( $hook ) );
  85  
  86          global $yourls_filters;
  87          $total = 0;
  88          foreach( $yourls_filters[ $hook ] as $prio => $action ) {
  89              $total += count( $yourls_filters[ $hook ][ $prio ] );
  90          }
  91  
  92          $this->assertSame( $times, $total );
  93      }
  94  
  95      /**
  96       * Remove all actions on a hook
  97       *
  98       * @since 0.1
  99       */
 100      public function test_remove_all_actions() {
 101          $hook = rand_str();
 102  
 103          $times = mt_rand( 5, 15 );
 104          for ( $i = 1; $i <= $times; $i++ ) {
 105              yourls_add_action( $hook, rand_str() );
 106          }
 107  
 108          $this->assertTrue( yourls_has_action( $hook ) );
 109          yourls_remove_all_actions( $hook );
 110          $this->assertFalse( yourls_has_action( $hook ) );
 111      }
 112  
 113      /**
 114       * Remove all actions with random priorities on a hook
 115       *
 116       * @since 0.1
 117       */
 118      public function test_remove_all_actions_random_prio() {
 119          $hook = rand_str();
 120  
 121          $times = mt_rand( 5, 15 );
 122          for ( $i = 1; $i <= $times; $i++ ) {
 123              yourls_add_action( $hook, rand_str(), mt_rand( 1, 10 ) );
 124          }
 125  
 126          $this->assertTrue( yourls_has_action( $hook ) );
 127          yourls_remove_all_actions( $hook );
 128          $this->assertFalse( yourls_has_action( $hook ) );
 129      }
 130  
 131      /**
 132       * Remove all actions with specific priority
 133       *
 134       * @since 0.1
 135       */
 136      public function test_remove_only_actions_with_given_prio() {
 137          $hook = rand_str();
 138          $priorities = array();
 139  
 140          $times = mt_rand( 10, 30 );
 141          for ( $i = 1; $i <= $times; $i++ ) {
 142              $prio = mt_rand( 1, 100 );
 143              $priorities[] = $prio;
 144              yourls_add_action( $hook, rand_str(), $prio );
 145          }
 146          $this->assertTrue( yourls_has_action( $hook ) );
 147  
 148          global $yourls_filters;
 149  
 150          // Pick a random number of randomly picked priorities (but not all of them)
 151          $priorities = array_unique( $priorities );
 152          $random_priorities = (array) array_rand( $priorities, mt_rand( 1, count( $priorities ) - 1 ) );
 153  
 154          // Count how many we're supposed to remove
 155          $removed = 0;
 156          foreach( $yourls_filters[ $hook ] as $prio => $action ) {
 157              if( in_array( $prio, $random_priorities ) )
 158                  $removed += count( $yourls_filters[ $hook ][ $prio ] );
 159          }
 160  
 161          // Remove the randomly picked priorities
 162          foreach( $random_priorities as $random_priority ) {
 163              yourls_remove_all_actions( $hook, $random_priority );
 164          }
 165  
 166          $this->assertTrue( yourls_has_action( $hook ) );
 167  
 168          // Count how many are left
 169          $remaining = 0;
 170          foreach( $yourls_filters[ $hook ] as $prio => $action ) {
 171              $remaining += count( $yourls_filters[ $hook ][ $prio ] );
 172          }
 173          $this->assertSame( $remaining, $times - $removed );
 174      }
 175  
 176      /**
 177       * Check 'doing' an action hooked with a simple function name
 178       *
 179       * @depends test_add_action_funcname
 180       * @since 0.1
 181       */
 182  	public function test_do_action_funcname( $hook ) {
 183          $var_name  = rand_str();
 184          $var_value = rand_str();
 185          $GLOBALS['test_var']  = $var_name;
 186          $GLOBALS[ $var_name ] = $var_value;
 187  
 188          $this->assertSame( $var_value, $GLOBALS[ $var_name ] );
 189          $this->assertSame( 0, yourls_did_action( $hook ) );
 190          yourls_do_action( $hook );
 191          $this->assertSame( 1, yourls_did_action( $hook ) );
 192          $this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
 193  
 194          return $hook;
 195      }
 196  
 197      /**
 198       * Check we keep correct track of the number of time an action is done
 199       *
 200       * @since 0.1
 201       */
 202  	public function test_do_action_several_times_and_count() {
 203          $hook = rand_str();
 204          $this->assertSame( 0, yourls_did_action( $hook ) );
 205  
 206          $times = mt_rand( 5, 15 );
 207          for ( $i = 1; $i <= $times; $i++ ) {
 208              yourls_do_action( $hook );
 209          }
 210  
 211          $this->assertSame( $times, yourls_did_action( $hook ) );
 212      }
 213  
 214  
 215      /**
 216       * Check adding an action with an anonymous function using create_function()
 217       *
 218       * Syntax tested: yourls_add_action( $hook, create_function() );
 219       *
 220       * @since 0.1
 221       */
 222  	public function test_add_action_create_function() {
 223          $hook = rand_str();
 224          $this->assertFalse( yourls_has_action( $hook ) );
 225          yourls_add_action( $hook, function() {
 226              $var_name = $GLOBALS["test_var"]; $GLOBALS[ $var_name ] = rand_str();
 227          } );
 228          $this->assertTrue( yourls_has_action( $hook ) );
 229  
 230          return $hook;
 231      }
 232  
 233      /**
 234       * Check 'doing' an action hooked with an anonymous function using create_function()
 235       *
 236       * @depends test_add_action_create_function
 237       * @since 0.1
 238       */
 239  	public function test_do_action_create_function( $hook ) {
 240          $var_name  = rand_str();
 241          $var_value = rand_str();
 242          $GLOBALS['test_var']  = $var_name;
 243          $GLOBALS[ $var_name ] = $var_value;
 244  
 245          $this->assertSame( $var_value, $GLOBALS[ $var_name ] );
 246          $this->assertSame( 0, yourls_did_action( $hook ) );
 247          yourls_do_action( $hook );
 248          $this->assertSame( 1, yourls_did_action( $hook ) );
 249          $this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
 250  
 251          return $hook;
 252      }
 253  
 254  
 255      /**
 256       * Check adding an action with function within class
 257       *
 258       * Syntax tested: yourls_add_action( $hook, 'Class::Function' );
 259       *
 260       * @since 0.1
 261       */
 262  	public function test_add_action_within_class() {
 263          $hook = rand_str();
 264          $this->assertFalse( yourls_has_action( $hook ) );
 265          yourls_add_action( $hook, 'Change_One_Global::change_it' );
 266          $this->assertTrue( yourls_has_action( $hook ) );
 267  
 268          return $hook;
 269      }
 270  
 271      /**
 272       * Check 'doing' an action hooked with function within class
 273       *
 274       * @depends test_add_action_within_class
 275       * @since 0.1
 276       */
 277  	public function test_do_action_within_class( $hook ) {
 278          $var_name  = rand_str();
 279          $var_value = rand_str();
 280          $GLOBALS['test_var']  = $var_name;
 281          $GLOBALS[ $var_name ] = $var_value;
 282  
 283          $this->assertSame( $var_value, $GLOBALS[ $var_name ] );
 284          $this->assertSame( 0, yourls_did_action( $hook ) );
 285          yourls_do_action( $hook );
 286          $this->assertSame( 1, yourls_did_action( $hook ) );
 287          $this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
 288  
 289          return $hook;
 290      }
 291  
 292  
 293      /**
 294       * Check adding an action with function within class using an array
 295       *
 296       * Syntax tested: yourls_add_action( $hook, array( 'Class', 'Function' ) );
 297       *
 298       * @since 0.1
 299       */
 300  	public function test_add_action_within_class_array() {
 301          $hook = rand_str();
 302          $this->assertFalse( yourls_has_action( $hook ) );
 303          yourls_add_action( $hook, array( 'Change_One_Global', 'change_it' ) );
 304          $this->assertTrue( yourls_has_action( $hook ) );
 305  
 306          return $hook;
 307      }
 308  
 309      /**
 310       * Check 'doing' an action hooked with function within class
 311       *
 312       * @depends test_add_action_within_class_array
 313       * @since 0.1
 314       */
 315  	public function test_do_action_within_class_array( $hook ) {
 316          $var_name  = rand_str();
 317          $var_value = rand_str();
 318          $GLOBALS['test_var']  = $var_name;
 319          $GLOBALS[ $var_name ] = $var_value;
 320  
 321          $this->assertSame( $var_value, $GLOBALS[ $var_name ] );
 322          $this->assertSame( 0, yourls_did_action( $hook ) );
 323          yourls_do_action( $hook );
 324          $this->assertSame( 1, yourls_did_action( $hook ) );
 325          $this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
 326  
 327          return $hook;
 328      }
 329  
 330  
 331      /**
 332       * Check adding an action with function within class instance
 333       *
 334       * Syntax tested: yourls_add_action( $hook, array( $class, 'function' ) );
 335       *
 336       * @since 0.1
 337       */
 338  	public function test_add_action_within_class_instance() {
 339          $hook = rand_str();
 340          $this->assertFalse( yourls_has_action( $hook ) );
 341          yourls_add_action( $hook, array( $this, 'change_one_global' ) );
 342          $this->assertTrue( yourls_has_action( $hook ) );
 343  
 344          return $hook;
 345      }
 346  
 347      /**
 348       * Check 'doing' an action hooked with function within class instance
 349       *
 350       * @depends test_add_action_within_class_instance
 351       * @since 0.1
 352       */
 353  	public function test_do_action_within_class_instance( $hook ) {
 354          $var_name  = rand_str();
 355          $var_value = rand_str();
 356          $GLOBALS['test_var']  = $var_name;
 357          $GLOBALS[ $var_name ] = $var_value;
 358  
 359          $this->assertSame( $var_value, $GLOBALS[ $var_name ] );
 360          $this->assertSame( 0, yourls_did_action( $hook ) );
 361          yourls_do_action( $hook );
 362          $this->assertSame( 1, yourls_did_action( $hook ) );
 363          $this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
 364  
 365          return $hook;
 366      }
 367  
 368  
 369      /**
 370       * Check that hooking to 'Class::Method' or array( 'Class', 'Method') is the same
 371       *
 372       * @since 0.1
 373       */
 374  	public function test_add_action_class_and_array() {
 375          $hook = rand_str();
 376  
 377          $this->assertFalse( yourls_has_action( $hook ) );
 378  
 379          yourls_add_action( $hook, array( 'Class', 'Method' ) );
 380          $this->assertSame( 10, yourls_has_action( $hook, array( 'Class', 'Method' ) ) );
 381          $this->assertSame( 10, yourls_has_action( $hook, 'Class::Method' ) );
 382      }
 383  
 384  
 385      /**
 386       * Check adding an action with anonymous function using closure
 387       *
 388       * Syntax tested: yourls_add_action( $hook, function(){ // do stuff } );
 389       *
 390       * @since 0.1
 391       */
 392  	public function test_add_action_closure() {
 393          $hook = rand_str();
 394          $this->assertFalse( yourls_has_action( $hook ) );
 395          yourls_add_action( $hook, function() { $var_name = $GLOBALS['test_var']; $GLOBALS[ $var_name ] = rand_str(); } );
 396          $this->assertTrue( yourls_has_action( $hook ) );
 397  
 398          return $hook;
 399      }
 400  
 401      /**
 402       * Check 'doing' an action hooked with anonymous function using closure
 403       *
 404       * @depends test_add_action_closure
 405       * @since 0.1
 406       */
 407  	public function test_do_action_closure( $hook ) {
 408          $var_name  = rand_str();
 409          $var_value = rand_str();
 410          $GLOBALS['test_var']  = $var_name;
 411          $GLOBALS[ $var_name ] = $var_value;
 412  
 413          $this->assertSame( $var_value, $GLOBALS[ $var_name ] );
 414          $this->assertSame( 0, yourls_did_action( $hook ) );
 415          yourls_do_action( $hook );
 416          $this->assertSame( 1, yourls_did_action( $hook ) );
 417          $this->assertNotSame( $var_value, $GLOBALS[ $var_name ] );
 418  
 419          return $hook;
 420      }
 421  
 422      /**
 423       * Check that applied function must exist
 424       *
 425       * @since 0.1
 426       */
 427      public function test_function_must_exist_if_applied() {
 428          if (PHP_VERSION_ID >= 80000) {
 429              $this->expectException(TypeError::class);
 430          } else {
 431              $this->expectException(PHPUnit\Framework\Error\Error::class);
 432          }
 433          $this->expectExceptionMessageMatches('/call_user_func_array\(\).* a valid callback, function (\'|")[0-9a-z]+(\'|") not found or invalid function name/');
 434  
 435          $hook = rand_str();
 436          yourls_add_action( $hook, rand_str() );
 437          // this will trigger an error, converted to an exception by PHPUnit
 438          yourls_do_action( $hook );
 439      }
 440  
 441      /**
 442       * Test yourls_do_action() with multiple params
 443       *
 444       * Note : this test will expectedly fail when YOURLS actions work the same as filters, see issue #1203
 445       *
 446       * @since 0.1
 447       */
 448      public function test_do_action_no_params() {
 449          $hook = rand_str();
 450          yourls_add_action( $hook, array( $this, 'accept_multiple_params' ) );
 451  
 452          $this->expectOutputString( "array (0 => '',)" );
 453          yourls_do_action( $hook );
 454      }
 455  
 456      /**
 457       * Test yourls_do_action() with multiple params
 458       *
 459       * Note : this test will expectedly fail when YOURLS actions work the same as filters, see issue #1203
 460       *
 461       * @since 0.1
 462       */
 463      public function test_do_action_1_params() {
 464          $hook = rand_str();
 465          yourls_add_action( $hook, array( $this, 'accept_multiple_params' ) );
 466  
 467          $this->expectOutputString( "array (0 => 'hello',)" );
 468          yourls_do_action( $hook, 'hello' );
 469      }
 470  
 471      /**
 472       * Test yourls_do_action() with multiple params
 473       *
 474       * Note : this test will expectedly fail when YOURLS actions work the same as filters, see issue #1203
 475       *
 476       * @since 0.1
 477       */
 478      public function test_do_action_2_params() {
 479          $hook = rand_str();
 480          yourls_add_action( $hook, array( $this, 'accept_multiple_params' ) );
 481  
 482          $this->expectOutputString( "array (0 => 'hello',1 => 'world',)" );
 483          yourls_do_action( $hook, 'hello', 'world' );
 484      }
 485  
 486      /**
 487       * Check return values of yourls_has_action()
 488       */
 489      public function test_has_action_return_values() {
 490          $hook = rand_str();
 491  
 492          yourls_add_action( $hook, 'some_function' );
 493          yourls_add_action( $hook, 'some_other_function', 1337 );
 494  
 495          $this->assertTrue( yourls_has_action( $hook ) );
 496          $this->assertSame( 10, yourls_has_action( $hook, 'some_function' ) );
 497          $this->assertSame( 1337, yourls_has_action( $hook, 'some_other_function' ) );
 498          $this->assertFalse( yourls_has_action( $hook, 'nope_not_this_function' ) );
 499      }
 500  
 501      /**
 502       * Check that yourls_get_actions() returns expected values
 503       */
 504      public function test_get_actions() {
 505          $hook = rand_str();
 506  
 507          yourls_add_action( $hook, 'some_function' );
 508          yourls_add_action( $hook, 'some_other_function', 1337 );
 509  
 510          $actions = yourls_get_actions( $hook );
 511          $this->assertTrue(isset($actions[10]['some_function']));
 512          $this->assertTrue(isset($actions[1337]['some_other_function']));
 513  
 514          $this->assertSame( [], yourls_get_actions( rand_str() ) );
 515      }
 516  
 517      /**
 518       * Dummy function -- just modifies the value of a global var
 519       */
 520      public function change_one_global() {
 521          $var_name = $GLOBALS['test_var'];
 522          $GLOBALS[ $var_name ] = rand_str();
 523      }
 524  
 525      /**
 526       * Dummy function -- echo in one line arguments passed
 527       */
 528      public function accept_multiple_params( $args ) {
 529          echo preg_replace( '/\s{2,}|\t|\n|\r/', '', var_export( $args, true ) );;
 530      }
 531  
 532  }


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