From e56c6ac2b551d00e70ebe7e13b469982ef6d6a54 Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Fri, 6 Sep 2024 15:44:18 +0300 Subject: [PATCH] Add Mink test for email authentication. --- .../Mink/EmailAuthenticationTest.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 module/VuFind/tests/integration-tests/src/VuFindTest/Mink/EmailAuthenticationTest.php diff --git a/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/EmailAuthenticationTest.php b/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/EmailAuthenticationTest.php new file mode 100644 index 00000000000..55c8a1da36f --- /dev/null +++ b/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/EmailAuthenticationTest.php @@ -0,0 +1,130 @@ + + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Page + */ + +namespace VuFindTest\Mink; + +/** + * Email authentication test class. + * + * Class must be final due to use of "new static()" by LiveDatabaseTrait. + * + * @category VuFind + * @package Tests + * @author Demian Katz + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License + * @link https://vufind.org Main Page + */ +final class EmailAuthenticationTest extends \VuFindTest\Integration\MinkTestCase +{ + use \VuFindTest\Feature\EmailTrait; + use \VuFindTest\Feature\LiveDatabaseTrait; + + /** + * Standard setup method. + * + * @return void + */ + public static function setUpBeforeClass(): void + { + static::failIfDataExists(); + } + + /** + * Test the email authentication process. + * + * @return void + */ + public function testEmailAuthentication(): void + { + // Set up configs, session and message logging: + $this->changeConfigs( + [ + 'config' => [ + 'Authentication' => [ + 'method' => 'ILS', + ], + 'Catalog' => [ + 'driver' => 'Demo' + ], + 'Mail' => [ + 'testOnly' => true, + 'message_log' => $this->getEmailLogPath(), + 'default_from' => 'noreply@vufind.org', + ], + ], + 'Demo' => [ + 'Catalog' => [ + 'loginMethod' => 'email', + ], + ], + ] + ); + + $this->resetEmailLog(); + $session = $this->getMinkSession(); + $session->visit($this->getVuFindUrl()); + $page = $session->getPage(); + + // Request login: + $this->clickCss($page, '#loginOptions a'); + $this->findCssAndSetValue($page, '.modal-body [name="username"]', 'catuser@vufind.org'); + $this->clickCss($page, '.modal-body .btn.btn-primary'); + $this->assertEquals( + 'We have sent a login link to your email address. It may take a few moments for the link to arrive.' + . " If you don't receive the link shortly, please check also your spam filter.", + $this->findCssAndGetText($page, '.alert-success') + ); + + // Extract the link from the provided message: + $email = file_get_contents($this->getEmailLogPath()); + $this->assertStringContainsString('From: noreply@vufind.org', $email); + $this->assertStringContainsString('To: catuser@vufind.org', $email); + preg_match('/Link to login: <(http.*)>/', $email, $matches); + $loginLink = $matches[1]; + + // Follow the verification link: + $session->visit($loginLink); + + // Log out (we can't log out unless we successfully logged in): + $this->clickCss($page, '.logoutOptions a.logout'); + + // Clean up the email log: + $this->resetEmailLog(); + } + + /** + * Standard teardown method. + * + * @return void + */ + public static function tearDownAfterClass(): void + { + static::removeUsers(['catuser@vufind.org']); + } +}