BxDolAccountTestCase.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. if (!function_exists('MsgBox'))
  3. require_once BX_DIRECTORY_PATH_INC . 'design.inc.php';
  4. /**
  5. * Pretend mail was sent so account create/edit does not block on SMTP.
  6. */
  7. class BxTestSkipMailAlertsResponse extends BxDolAlertsResponse
  8. {
  9. public function response($oAlert)
  10. {
  11. if ($oAlert->sUnit === 'system' && $oAlert->sAction === 'check_send_mail')
  12. $oAlert->aExtras['override_result'] = true;
  13. }
  14. }
  15. /**
  16. * Shared helpers for account create/edit/delete integration tests.
  17. */
  18. abstract class BxDolAccountTestCase extends BxDolIntegrationTestCase
  19. {
  20. protected $_iCreatedAccountId = 0;
  21. protected $_aCreatedEmails = [];
  22. protected $_aAlertsCacheBackup;
  23. protected $_sConfirmationBackup;
  24. protected function setUp(): void
  25. {
  26. parent::setUp();
  27. $this->_sConfirmationBackup = getParam('sys_account_confirmation_type');
  28. $this->bxSetParamCache('sys_account_confirmation_type', BX_ACCOUNT_CONFIRMATION_NONE);
  29. $this->bxInstallTestAlertHandlers();
  30. }
  31. protected function tearDown(): void
  32. {
  33. $this->bxCleanupCreatedAccount();
  34. $this->bxRestoreTestAlertHandlers();
  35. if ($this->_sConfirmationBackup !== null)
  36. $this->bxSetParamCache('sys_account_confirmation_type', $this->_sConfirmationBackup);
  37. parent::tearDown();
  38. }
  39. protected function bxAccountForm(string $sDisplay): BxTemplFormAccount
  40. {
  41. BxDolForm::unSetObjectInstance('sys_account', $sDisplay);
  42. $oForm = BxDolForm::getObjectInstance('sys_account', $sDisplay);
  43. $this->assertInstanceOf(BxTemplFormAccount::class, $oForm);
  44. $oForm->aParams['csrf']['disable'] = true;
  45. return $oForm;
  46. }
  47. protected function bxSubmitCreateForm(string $sName, string $sEmail, string $sPassword): BxTemplFormAccount
  48. {
  49. $oForm = $this->bxAccountForm('sys_account_create');
  50. $_POST = [
  51. 'name' => $sName,
  52. 'email' => $sEmail,
  53. 'password' => $sPassword,
  54. 'receive_news' => '1',
  55. 'do_publish' => '1',
  56. ];
  57. $oForm->initChecker();
  58. return $oForm;
  59. }
  60. protected function bxCreateLifecycleAccount(string $sName = 'Lifecycle User', string $sPassword = 'UnaTest1a'): array
  61. {
  62. $this->bxBecomeGuest();
  63. $sEmail = 'lifecycle-' . bin2hex(random_bytes(4)) . '@example.com';
  64. $this->_aCreatedEmails[] = $sEmail;
  65. $oForm = $this->bxSubmitCreateForm($sName, $sEmail, $sPassword);
  66. $this->assertTrue($oForm->isSubmittedAndValid(), $oForm->getFormErrors() ?: 'create account form should accept the posted values');
  67. $iAccountId = $oForm->insert(['email_confirmed' => 0]);
  68. $this->assertNotFalse($iAccountId);
  69. $this->_iCreatedAccountId = (int)$iAccountId;
  70. BxDolProfileQuery::getInstance()->getInfoById((int)$iAccountId, true);
  71. $iProfileId = (new BxTemplAccountForms())->onAccountCreated($iAccountId, $oForm->isSetPendingApproval());
  72. $this->assertGreaterThan(0, (int)$iProfileId);
  73. $oAccount = BxDolAccount::getInstance($iAccountId, true);
  74. $this->assertInstanceOf(BxDolAccount::class, $oAccount);
  75. $aInfo = $oAccount->getInfo();
  76. $this->bxSyncSessionCookie();
  77. $this->bxSetLoggedFlags((int)$aInfo['role']);
  78. return [
  79. 'id' => (int)$iAccountId,
  80. 'profile_id' => (int)$iProfileId,
  81. 'email' => $sEmail,
  82. 'name' => $sName,
  83. 'password' => $sPassword,
  84. 'account' => $oAccount,
  85. ];
  86. }
  87. protected function bxSubmitEmailForm(int $iAccountId, string $sEmail, string $sPassword): string
  88. {
  89. $this->bxAccountForm('sys_account_settings_email');
  90. $_POST = [
  91. 'email' => $sEmail,
  92. 'password_current' => $sPassword,
  93. 'receive_updates' => '1',
  94. 'receive_news' => '1',
  95. 'do_submit' => '1',
  96. ];
  97. return (new BxTemplAccountForms())->editAccountEmailSettingsForm($iAccountId);
  98. }
  99. protected function bxSubmitInfoForm(int $iAccountId, string $sName): string
  100. {
  101. $this->bxAccountForm('sys_account_settings_info');
  102. $_POST = [
  103. 'name' => $sName,
  104. 'do_submit' => '1',
  105. ];
  106. return (new BxTemplAccountForms())->editAccountInfoForm($iAccountId);
  107. }
  108. protected function bxSubmitDeleteForm(BxDolAccount $oAccount, string $sPassword, string $sConfirm = '1'): BxTemplFormAccount
  109. {
  110. $oForm = $this->bxAccountForm('sys_account_settings_del_account');
  111. $_POST = [
  112. 'delete_content' => '0',
  113. 'delete_confirm' => $sConfirm,
  114. 'password_current' => $sPassword,
  115. 'do_submit' => '1',
  116. ];
  117. $oForm->initChecker($oAccount->getInfo());
  118. return $oForm;
  119. }
  120. protected function bxAccountIdByEmail(string $sEmail): int
  121. {
  122. return (int)BxDolAccountQuery::getInstance()->getIdByEmail($sEmail);
  123. }
  124. protected function bxAccountIdByName(string $sName): int
  125. {
  126. return (int)BxDolDb::getInstance()->getOne('SELECT `id` FROM `sys_accounts` WHERE `name` = :name', [
  127. 'name' => $sName,
  128. ]);
  129. }
  130. protected function bxAssertAccountsTableIntact(): void
  131. {
  132. $this->assertTrue(BxDolDb::getInstance()->isTableExists('sys_accounts'));
  133. $this->assertGreaterThan(0, $this->bxAccountIdByEmail($this->bxTestEmail('admin')));
  134. }
  135. protected function bxAssertFormRejected(BxDolForm $oForm, string $sField = ''): void
  136. {
  137. $this->assertTrue($oForm->isSubmitted());
  138. $this->assertFalse($oForm->isSubmittedAndValid(), $oForm->getFormErrors() ?: 'form should reject the posted values');
  139. if ($sField !== '')
  140. $this->assertNotEmpty($oForm->aInputs[$sField]['error'] ?? '', $sField . ' should have a checker error');
  141. }
  142. protected function bxSetParamCache(string $sKey, $mixedValue): void
  143. {
  144. $oProp = new ReflectionProperty(BxDolDb::class, '_aParams');
  145. $aParams = $oProp->getValue();
  146. $aParams[$sKey] = $mixedValue;
  147. $oProp->setValue(null, $aParams);
  148. }
  149. protected function bxInstallTestAlertHandlers(): void
  150. {
  151. new BxDolAlerts('system', 'phpunit_init', 0, 0);
  152. $oProp = new ReflectionProperty(BxDolAlerts::class, '_aCacheData');
  153. $this->_aAlertsCacheBackup = $oProp->getValue();
  154. $aCache = $this->_aAlertsCacheBackup;
  155. foreach ($aCache['handlers'] as $mixedId => $aHandler) {
  156. if (in_array($aHandler['class'] ?? '', ['BxSMTPAlertsResponse', 'BxMapShowAlertsResponse'], true))
  157. $aCache['handlers'][$mixedId]['active'] = 0;
  158. }
  159. $sId = 'phpunit_skip_mail';
  160. $aCache['handlers'][$sId] = [
  161. 'name' => $sId,
  162. 'class' => BxTestSkipMailAlertsResponse::class,
  163. 'file' => 'tests/integration/account/BxDolAccountTestCase.php',
  164. 'service_call' => '',
  165. 'active' => 1,
  166. ];
  167. $aCache['alerts']['system']['check_send_mail'][] = $sId;
  168. $oProp->setValue(null, $aCache);
  169. }
  170. protected function bxRestoreTestAlertHandlers(): void
  171. {
  172. if ($this->_aAlertsCacheBackup === null)
  173. return;
  174. $oProp = new ReflectionProperty(BxDolAlerts::class, '_aCacheData');
  175. $oProp->setValue(null, $this->_aAlertsCacheBackup);
  176. $this->_aAlertsCacheBackup = null;
  177. }
  178. protected function bxCleanupCreatedAccount(): void
  179. {
  180. $aEmails = $this->_aCreatedEmails;
  181. if ($this->_iCreatedAccountId)
  182. $aEmails[] = $this->_iCreatedAccountId;
  183. foreach (array_unique($aEmails) as $mixedId) {
  184. $oAccount = BxDolAccount::getInstance($mixedId, true);
  185. if (!$oAccount)
  186. continue;
  187. $sJob = 'account_delete_' . $oAccount->id();
  188. $oJobs = BxDolBackgroundJobs::getInstance();
  189. if ($oJobs->exists($sJob))
  190. $oJobs->delete($sJob);
  191. $oAccount->delete(false, false);
  192. }
  193. $this->_iCreatedAccountId = 0;
  194. $this->_aCreatedEmails = [];
  195. }
  196. }