BxDolAccountTestCase.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $iProfileId = (new BxTemplAccountForms())->onAccountCreated($iAccountId, $oForm->isSetPendingApproval());
  71. $this->assertGreaterThan(0, (int)$iProfileId);
  72. $oAccount = BxDolAccount::getInstance($iAccountId, true);
  73. $this->assertInstanceOf(BxDolAccount::class, $oAccount);
  74. $aInfo = $oAccount->getInfo();
  75. $this->bxSyncSessionCookie();
  76. $this->bxSetLoggedFlags((int)$aInfo['role']);
  77. return [
  78. 'id' => (int)$iAccountId,
  79. 'profile_id' => (int)$iProfileId,
  80. 'email' => $sEmail,
  81. 'name' => $sName,
  82. 'password' => $sPassword,
  83. 'account' => $oAccount,
  84. ];
  85. }
  86. protected function bxSubmitEmailForm(int $iAccountId, string $sEmail, string $sPassword): string
  87. {
  88. $this->bxAccountForm('sys_account_settings_email');
  89. $_POST = [
  90. 'email' => $sEmail,
  91. 'password_current' => $sPassword,
  92. 'receive_updates' => '1',
  93. 'receive_news' => '1',
  94. 'do_submit' => '1',
  95. ];
  96. return (new BxTemplAccountForms())->editAccountEmailSettingsForm($iAccountId);
  97. }
  98. protected function bxSubmitInfoForm(int $iAccountId, string $sName): string
  99. {
  100. $this->bxAccountForm('sys_account_settings_info');
  101. $_POST = [
  102. 'name' => $sName,
  103. 'do_submit' => '1',
  104. ];
  105. return (new BxTemplAccountForms())->editAccountInfoForm($iAccountId);
  106. }
  107. protected function bxSubmitDeleteForm(BxDolAccount $oAccount, string $sPassword, string $sConfirm = '1'): BxTemplFormAccount
  108. {
  109. $oForm = $this->bxAccountForm('sys_account_settings_del_account');
  110. $_POST = [
  111. 'delete_content' => '0',
  112. 'delete_confirm' => $sConfirm,
  113. 'password_current' => $sPassword,
  114. 'do_submit' => '1',
  115. ];
  116. $oForm->initChecker($oAccount->getInfo());
  117. return $oForm;
  118. }
  119. protected function bxAccountIdByEmail(string $sEmail): int
  120. {
  121. return (int)BxDolAccountQuery::getInstance()->getIdByEmail($sEmail);
  122. }
  123. protected function bxAccountIdByName(string $sName): int
  124. {
  125. return (int)BxDolDb::getInstance()->getOne('SELECT `id` FROM `sys_accounts` WHERE `name` = :name', [
  126. 'name' => $sName,
  127. ]);
  128. }
  129. protected function bxAssertAccountsTableIntact(): void
  130. {
  131. $this->assertTrue(BxDolDb::getInstance()->isTableExists('sys_accounts'));
  132. $this->assertGreaterThan(0, $this->bxAccountIdByEmail($this->bxTestEmail('admin')));
  133. }
  134. protected function bxAssertFormRejected(BxDolForm $oForm, string $sField = ''): void
  135. {
  136. $this->assertTrue($oForm->isSubmitted());
  137. $this->assertFalse($oForm->isSubmittedAndValid(), $oForm->getFormErrors() ?: 'form should reject the posted values');
  138. if ($sField !== '')
  139. $this->assertNotEmpty($oForm->aInputs[$sField]['error'] ?? '', $sField . ' should have a checker error');
  140. }
  141. protected function bxSetParamCache(string $sKey, $mixedValue): void
  142. {
  143. $oProp = new ReflectionProperty(BxDolDb::class, '_aParams');
  144. $aParams = $oProp->getValue();
  145. $aParams[$sKey] = $mixedValue;
  146. $oProp->setValue(null, $aParams);
  147. }
  148. protected function bxInstallTestAlertHandlers(): void
  149. {
  150. new BxDolAlerts('system', 'phpunit_init', 0, 0);
  151. $oProp = new ReflectionProperty(BxDolAlerts::class, '_aCacheData');
  152. $this->_aAlertsCacheBackup = $oProp->getValue();
  153. $aCache = $this->_aAlertsCacheBackup;
  154. foreach ($aCache['handlers'] as $mixedId => $aHandler) {
  155. if (in_array($aHandler['class'] ?? '', ['BxSMTPAlertsResponse', 'BxMapShowAlertsResponse'], true))
  156. $aCache['handlers'][$mixedId]['active'] = 0;
  157. }
  158. $sId = 'phpunit_skip_mail';
  159. $aCache['handlers'][$sId] = [
  160. 'name' => $sId,
  161. 'class' => BxTestSkipMailAlertsResponse::class,
  162. 'file' => 'tests/integration/account/BxDolAccountTestCase.php',
  163. 'service_call' => '',
  164. 'active' => 1,
  165. ];
  166. $aCache['alerts']['system']['check_send_mail'][] = $sId;
  167. $oProp->setValue(null, $aCache);
  168. }
  169. protected function bxRestoreTestAlertHandlers(): void
  170. {
  171. if ($this->_aAlertsCacheBackup === null)
  172. return;
  173. $oProp = new ReflectionProperty(BxDolAlerts::class, '_aCacheData');
  174. $oProp->setValue(null, $this->_aAlertsCacheBackup);
  175. $this->_aAlertsCacheBackup = null;
  176. }
  177. protected function bxCleanupCreatedAccount(): void
  178. {
  179. $aEmails = $this->_aCreatedEmails;
  180. if ($this->_iCreatedAccountId)
  181. $aEmails[] = $this->_iCreatedAccountId;
  182. foreach (array_unique($aEmails) as $mixedId) {
  183. $oAccount = BxDolAccount::getInstance($mixedId, true);
  184. if (!$oAccount)
  185. continue;
  186. $sJob = 'account_delete_' . $oAccount->id();
  187. $oJobs = BxDolBackgroundJobs::getInstance();
  188. if ($oJobs->exists($sJob))
  189. $oJobs->delete($sJob);
  190. $oAccount->delete(false, false);
  191. }
  192. $this->_iCreatedAccountId = 0;
  193. $this->_aCreatedEmails = [];
  194. }
  195. }