[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Login tests - via Cookies
   4   *
   5   * @group auth
   6   * @group login
   7   * @group cookies
   8   * @since 0.1
   9   */
  10  class Auth_Login_Cookie_Tests extends PHPUnit\Framework\TestCase {
  11  
  12      protected $cookie;
  13      protected $request;
  14      protected $backup_yourls_actions;
  15  
  16      protected function setUp(): void {
  17          $this->cookie = $_COOKIE;
  18          $this->request = $_REQUEST;
  19          global $yourls_actions;
  20          $this->backup_yourls_actions = $yourls_actions;
  21      }
  22  
  23      protected function tearDown(): void {
  24          $_COOKIE = $this->cookie;
  25          $_REQUEST = $this->request;
  26          global $yourls_actions;
  27          $yourls_actions = $this->backup_yourls_actions;
  28      }
  29  
  30      public static function setUpBeforeClass(): void {
  31          yourls_add_filter( 'is_API', 'yourls_return_false' );
  32      }
  33  
  34      public static function tearDownAfterClass(): void {
  35          yourls_remove_filter( 'is_API', 'yourls_return_false' );
  36      }
  37  
  38      /**
  39       * Check for valid cookie name
  40       */
  41  	public function test_cookie_name() {
  42          $this->assertTrue( is_string(yourls_cookie_name()) );
  43      }
  44  
  45      /**
  46       * Check for valid cookie value
  47       */
  48  	public function test_cookie_value() {
  49          $this->assertTrue( is_string(yourls_cookie_value(rand_str())) );
  50      }
  51  
  52      /**
  53       * Check for valid cookie life
  54       */
  55  	public function test_cookie_life() {
  56          $this->assertTrue( is_int(yourls_get_cookie_life()) );
  57      }
  58  
  59      /**
  60       * Test login with valid cookie - also check that cookie is set
  61       */
  62  	public function test_login_valid_cookie() {
  63          global $yourls_user_passwords;
  64          $random_user = array_rand($yourls_user_passwords);
  65          $_COOKIE[yourls_cookie_name()] = yourls_cookie_value( $random_user );
  66          unset($_REQUEST);
  67  
  68          $this->assertSame( 0, yourls_did_action('pre_setcookie') );
  69          $this->assertTrue(yourls_check_auth_cookie());
  70          $this->assertTrue(yourls_is_valid_user());
  71          $this->assertSame( 1, yourls_did_action('pre_setcookie') );
  72      }
  73  
  74      /**
  75       * Test login with invalid cookie - also check that no cookie is set
  76       */
  77  	public function test_login_invalid_cookie() {
  78          $_COOKIE[yourls_cookie_name()] = yourls_cookie_value( rand_str() );
  79          unset($_REQUEST);
  80  
  81          $this->assertSame( 0, yourls_did_action('pre_setcookie') );
  82          $this->assertFalse(yourls_check_auth_cookie());
  83          $this->assertNotTrue(yourls_is_valid_user());
  84          $this->assertSame( 0, yourls_did_action('pre_setcookie') );
  85      }
  86  
  87  }


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