[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/tests/auth/ -> logout.php (source)

   1  <?php
   2  
   3  /**
   4   * Logout function
   5   *
   6   * @group auth
   7   */
   8  class Logout_Func_Tests extends PHPUnit\Framework\TestCase {
   9  
  10      protected $backup_get;
  11      protected $backup_request;
  12      private static $user;
  13  
  14      protected function setUp(): void {
  15          $this->backup_get     = $_GET;
  16          $this->backup_request = $_REQUEST;
  17          self::$user = false;
  18      }
  19  
  20      protected function tearDown(): void {
  21          $_GET = $this->backup_get;
  22          $_REQUEST = $this->backup_request;
  23      }
  24  
  25      public static function setUpBeforeClass(): void {
  26          yourls_add_action( 'pre_setcookie', function ($in) {
  27              self::$user = $in[0]; // $in[0] is the user ID passed to yourls_setcookie()
  28          } );
  29      }
  30  
  31      public static function tearDownAfterClass(): void {
  32          yourls_remove_all_actions('pre_setcookie');
  33      }
  34  
  35      /**
  36       * Check logout procedure - phase 1 - we're logging in
  37       */
  38      public function test_logout_user_is_logged_in() {
  39          $_REQUEST['nonce'] = yourls_create_nonce('admin_login');
  40          $valid = yourls_is_valid_user();
  41          $this->assertTrue($valid);
  42          $this->assertSame(self::$user, 'yourls');
  43      }
  44  
  45      /**
  46       * Check logout procedure - phase 2 - we're logging out and checking that cookie was reset
  47       * @depends test_logout_user_is_logged_in
  48       */
  49      public function test_logout_user_logs_out() {
  50          $_GET['action'] = 'logout';
  51          $_REQUEST['nonce'] = yourls_create_nonce('admin_logout', 'logout');
  52          $invalid = yourls_is_valid_user();
  53          $this->assertNotTrue( $invalid );
  54          $this->assertSame(self::$user, '');
  55      }
  56  
  57      /**
  58       * Check logout procedure - phase 3 - check we can log in again
  59       * @depends test_logout_user_logs_out
  60       */
  61      public function test_logout_user_is_logged_in_back() {
  62          $_REQUEST['nonce'] = yourls_create_nonce('admin_login');
  63          $valid = yourls_is_valid_user();
  64          $this->assertTrue( $valid );
  65          $this->assertSame(self::$user, 'yourls');
  66      }
  67  
  68  }


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