LoginLogoutTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. use PHPUnit\Framework\Attributes\DataProvider;
  3. use PHPUnit\Framework\Attributes\Group;
  4. /**
  5. * Login and logout against a live install using admin and regular-user accounts.
  6. */
  7. #[Group('integration')]
  8. class LoginLogoutTest extends BxDolIntegrationTestCase
  9. {
  10. static public function providerForAccounts()
  11. {
  12. return [
  13. 'admin' => ['admin'],
  14. 'user' => ['user'],
  15. ];
  16. }
  17. #[DataProvider('providerForAccounts')]
  18. public function testValidCredentialsAreAccepted(string $sKind): void
  19. {
  20. $this->bxAccount($sKind);
  21. $this->assertSame('', bx_check_password($this->bxTestEmail($sKind), $this->bxTestPassword($sKind), BX_DOL_ROLE_MEMBER));
  22. }
  23. #[DataProvider('providerForAccounts')]
  24. public function testInvalidPasswordIsRejected(string $sKind): void
  25. {
  26. $this->bxAccount($sKind);
  27. $sError = bx_check_password($this->bxTestEmail($sKind), 'not-the-password', BX_DOL_ROLE_MEMBER);
  28. $this->assertNotSame('', $sError);
  29. $this->assertFalse(isLogged());
  30. $this->assertSame(0, (int)getLoggedId());
  31. }
  32. #[DataProvider('providerForAccounts')]
  33. public function testLoginEstablishesSession(string $sKind): void
  34. {
  35. $this->assertFalse(isLogged());
  36. $oAccount = $this->bxAccount($sKind);
  37. $aInfo = $this->bxLoginAs($sKind);
  38. $this->assertSame((int)$oAccount->id(), (int)$aInfo['id']);
  39. $this->assertSame($this->bxTestEmail($sKind), $aInfo['email']);
  40. $this->assertTrue(isLogged());
  41. $this->assertTrue(isMember());
  42. $this->assertSame((int)$oAccount->id(), (int)getLoggedId());
  43. $this->assertSame((int)$oAccount->id(), (int)BxDolSession::getInstance()->getUserId());
  44. if ($sKind === 'admin')
  45. $this->assertTrue(isAdmin());
  46. else
  47. $this->assertFalse(isAdmin());
  48. }
  49. #[DataProvider('providerForAccounts')]
  50. public function testLogoutClearsSession(string $sKind): void
  51. {
  52. $this->bxLoginAs($sKind);
  53. $this->assertTrue(isLogged());
  54. bx_logout();
  55. $this->bxBecomeGuest();
  56. $this->assertFalse(isLogged());
  57. $this->assertFalse(isAdmin());
  58. $this->assertFalse(isMember());
  59. $this->assertSame(0, (int)getLoggedId());
  60. $this->assertSame(0, (int)BxDolSession::getInstance()->getUserId());
  61. }
  62. #[DataProvider('providerForAccounts')]
  63. public function testLoginFormAcceptsCredentials(string $sKind): void
  64. {
  65. $this->bxAccount($sKind);
  66. $oForm = $this->bxSubmitLoginForm($this->bxTestEmail($sKind), $this->bxTestPassword($sKind));
  67. $this->assertTrue($oForm->isSubmitted());
  68. $this->assertTrue($oForm->isSubmittedAndValid(), $oForm->getLoginError() ?: 'login form should accept ' . $sKind . ' credentials');
  69. $oAccount = BxDolAccount::getInstance(trim($oForm->getCleanValue('ID')));
  70. $this->assertInstanceOf(BxDolAccount::class, $oAccount);
  71. $aInfo = bx_login($oAccount->id(), $oForm->getRememberMe());
  72. $this->assertIsArray($aInfo);
  73. $this->bxSyncSessionCookie();
  74. $this->bxSetLoggedFlags((int)$aInfo['role']);
  75. $this->assertTrue(isLogged());
  76. $this->assertSame((int)$oAccount->id(), (int)getLoggedId());
  77. if ($sKind === 'admin')
  78. $this->assertTrue(isAdmin());
  79. else
  80. $this->assertFalse(isAdmin());
  81. }
  82. #[DataProvider('providerForAccounts')]
  83. public function testLoginFormRejectsWrongPassword(string $sKind): void
  84. {
  85. $this->bxAccount($sKind);
  86. $oForm = $this->bxSubmitLoginForm($this->bxTestEmail($sKind), 'not-the-password');
  87. $this->assertTrue($oForm->isSubmitted());
  88. $this->assertFalse($oForm->isSubmittedAndValid());
  89. $this->assertNotSame('', $oForm->getLoginError());
  90. $this->assertFalse(isLogged());
  91. }
  92. #[DataProvider('providerForAccounts')]
  93. public function testServiceLogoutEndsSession(string $sKind): void
  94. {
  95. $this->bxLoginAs($sKind);
  96. $this->assertTrue(isLogged());
  97. $this->assertTrue(bx_srv('system', 'logout', [false], 'TemplServiceContent'));
  98. $this->bxBecomeGuest();
  99. $this->assertFalse(isLogged());
  100. $this->assertSame(0, (int)getLoggedId());
  101. }
  102. }