session.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Test WP_Session_Tokens and WP_User_Meta_Session_Tokens, in wp-includes/session.php
  4. *
  5. * @group user
  6. * @group session
  7. */
  8. class Tests_User_Session extends WP_UnitTestCase {
  9. function setUp() {
  10. parent::setUp();
  11. remove_all_filters( 'session_token_manager' );
  12. $user_id = self::factory()->user->create();
  13. $this->manager = WP_Session_Tokens::get_instance( $user_id );
  14. $this->assertInstanceOf( 'WP_Session_Tokens', $this->manager );
  15. $this->assertInstanceOf( 'WP_User_Meta_Session_Tokens', $this->manager );
  16. }
  17. function test_verify_and_destroy_token() {
  18. $expiration = time() + DAY_IN_SECONDS;
  19. $token = $this->manager->create( $expiration );
  20. $this->assertFalse( $this->manager->verify( 'foo' ) );
  21. $this->assertTrue( $this->manager->verify( $token ) );
  22. $this->manager->destroy( $token );
  23. $this->assertFalse( $this->manager->verify( $token ) );
  24. }
  25. function test_destroy_other_tokens() {
  26. $expiration = time() + DAY_IN_SECONDS;
  27. $token_1 = $this->manager->create( $expiration );
  28. $token_2 = $this->manager->create( $expiration );
  29. $token_3 = $this->manager->create( $expiration );
  30. $this->assertTrue( $this->manager->verify( $token_1 ) );
  31. $this->assertTrue( $this->manager->verify( $token_2 ) );
  32. $this->assertTrue( $this->manager->verify( $token_3 ) );
  33. $this->manager->destroy_others( $token_2 );
  34. $this->assertFalse( $this->manager->verify( $token_1 ) );
  35. $this->assertTrue( $this->manager->verify( $token_2 ) );
  36. $this->assertFalse( $this->manager->verify( $token_3 ) );
  37. }
  38. function test_destroy_all_tokens() {
  39. $expiration = time() + DAY_IN_SECONDS;
  40. $token_1 = $this->manager->create( $expiration );
  41. $token_2 = $this->manager->create( $expiration );
  42. $this->assertTrue( $this->manager->verify( $token_1 ) );
  43. $this->assertTrue( $this->manager->verify( $token_2 ) );
  44. $this->manager->destroy_all();
  45. $this->assertFalse( $this->manager->verify( $token_1 ) );
  46. $this->assertFalse( $this->manager->verify( $token_2 ) );
  47. }
  48. }