[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

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

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


Generated: Mon Mar 31 05:10:02 2025 Cross-referenced by PHPXref 0.7.1