[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * This abstract class isn't supposed to be run as tests
   5   * See login_*.php files
   6   */
   7  abstract class Login_Base extends PHPUnit\Framework\TestCase {
   8  
   9      protected $backup_request;
  10  
  11      protected function setUp(): void {
  12          $this->backup_request = $_REQUEST;
  13          $_REQUEST['nonce'] = yourls_create_nonce('admin_login');
  14      }
  15  
  16      protected function tearDown(): void {
  17          $_REQUEST = $this->backup_request;
  18          yourls_remove_all_actions('pre_yourls_die');
  19      }
  20  
  21      /**
  22       * Check that user, as submitted by REQUEST (see phpunit XML config file), is valid
  23       *
  24       * @since 0.1
  25       */
  26  	public function test_login() {
  27          $pre_login    = yourls_did_action( 'pre_login' );
  28          $login        = yourls_did_action( 'login' );
  29          $login_failed = yourls_did_action( 'login_failed' );
  30  
  31          $this->assertTrue( yourls_is_valid_user() );
  32  
  33          $this->assertEquals( $pre_login + 1, yourls_did_action( 'pre_login' ) );
  34          $this->assertEquals( $login + 1, yourls_did_action( 'login' ) );
  35          $this->assertEquals( $login_failed, yourls_did_action( 'login_failed' ) );
  36      }
  37  
  38      /**
  39       * Check that auth is shuntable
  40       *
  41       * @since 0.1
  42       */
  43      public function test_login_shunt() {
  44          yourls_add_filter( 'shunt_is_valid_user', 'yourls_return_empty_array' );
  45          $this->assertSame( array(), yourls_is_valid_user() );
  46          yourls_remove_filter( 'shunt_is_valid_user', 'yourls_return_empty_array' );
  47      }
  48  
  49      /**
  50       * Check that auth returns false with no credential
  51       *
  52       * @since 0.1
  53       */
  54      public function test_login_with_no_credential() {
  55          $_REQUEST = array();
  56          $login        = yourls_did_action( 'login' );
  57          $login_failed = yourls_did_action( 'login_failed' );
  58  
  59          $this->assertNotTrue( yourls_is_valid_user() );
  60  
  61          $this->assertEquals( $login, yourls_did_action( 'login' ) );
  62          $this->assertEquals( $login_failed + 1, yourls_did_action( 'login_failed' ) );
  63      }
  64  
  65      /**
  66       * Check that auth returns false with empty credential
  67       *
  68       * @since 0.1
  69       */
  70      public function test_login_with_empty_credential() {
  71          $_REQUEST = array( 'username' => '', 'password' => '' );
  72          $login        = yourls_did_action( 'login' );
  73          $login_failed = yourls_did_action( 'login_failed' );
  74  
  75          $this->assertNotTrue( yourls_is_valid_user() );
  76  
  77          $this->assertEquals( $login, yourls_did_action( 'login' ) );
  78          $this->assertEquals( $login_failed + 1, yourls_did_action( 'login_failed' ) );
  79      }
  80  
  81      /**
  82       * Check that auth returns false with incorrect credentials
  83       *
  84       * @since 0.1
  85       */
  86      public function test_login_with_random_credentials() {
  87          $_REQUEST = array( 'username' => rand_str(), 'password' => rand_str() );
  88          $login        = yourls_did_action( 'login' );
  89          $login_failed = yourls_did_action( 'login_failed' );
  90  
  91          // with "normal" logins, we simulate the login forms and the presence of a nonce
  92          if (get_class($this) == 'Auth_Login_Normal_Tests') {
  93              $this->expectException(Exception::class);
  94              $this->expectExceptionMessage('I have died');
  95              // intercept yourls_die() before it actually dies
  96              yourls_add_action( 'pre_yourls_die', function() { throw new Exception( 'I have died' ); } );
  97          }
  98  
  99          $this->assertNotTrue( yourls_is_valid_user() );
 100      }
 101  
 102  }


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