Skip to content

Commit

Permalink
Merge branch 'release-10.1' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Nov 18, 2024
2 parents a7f0659 + 4fe586d commit d9cfa58
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
47 changes: 25 additions & 22 deletions module/VuFind/src/VuFind/AjaxHandler/SystemStatus.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* "Keep Alive" AJAX handler
* "System Status" AJAX handler
*
* PHP version 8
*
Expand All @@ -23,6 +23,7 @@
* @category VuFind
* @package AJAX
* @author Demian Katz <demian.katz@villanova.edu>
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
Expand All @@ -36,14 +37,12 @@
use VuFind\Search\Results\PluginManager as ResultsManager;

/**
* "Keep Alive" AJAX handler
*
* This is responsible for keeping the session alive whenever called
* (via JavaScript)
* "System Status" AJAX handler
*
* @category VuFind
* @package AJAX
* @author Demian Katz <demian.katz@villanova.edu>
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development Wiki
*/
Expand Down Expand Up @@ -93,26 +92,30 @@ public function handleRequest(Params $params)
$this->log('info', 'SystemStatus log check', [], true);

// Test search index
try {
$results = $this->resultsManager->get('Solr');
$paramsObj = $results->getParams();
$paramsObj->setQueryIDs(['healthcheck']);
$results->performAndProcessSearch();
} catch (\Exception $e) {
return $this->formatResponse(
'Search index error: ' . $e->getMessage(),
self::STATUS_HTTP_ERROR
);
if ($params->fromPost('index') ?? $params->fromQuery('index', 1)) {
try {
$results = $this->resultsManager->get(DEFAULT_SEARCH_BACKEND);
$paramsObj = $results->getParams();
$paramsObj->setQueryIDs(['healthcheck' . date('His')]);
$results->performAndProcessSearch();
} catch (\Exception $e) {
return $this->formatResponse(
'Search index error: ' . $e->getMessage(),
self::STATUS_HTTP_ERROR
);
}
}

// Test database connection
try {
$this->sessionService->getSessionById('healthcheck', false);
} catch (\Exception $e) {
return $this->formatResponse(
'Database error: ' . $e->getMessage(),
self::STATUS_HTTP_ERROR
);
if ($params->fromPost('database') ?? $params->fromQuery('database', 1)) {
try {
$this->sessionService->getSessionById('healthcheck', false);
} catch (\Exception $e) {
return $this->formatResponse(
'Database error: ' . $e->getMessage(),
self::STATUS_HTTP_ERROR
);
}
}

// This may be called frequently, don't leave sessions dangling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace VuFindTest\AjaxHandler;

use Laminas\Mvc\Controller\Plugin\Params;
use VuFind\AjaxHandler\SystemStatus;

/**
Expand All @@ -54,8 +55,7 @@ public function testHealthCheckFile(): void
$config = new \Laminas\Config\Config(['System' => ['healthCheckFile' => __FILE__]]);
$sessionService = $this->createMock(\VuFind\Db\Service\SessionServiceInterface::class);
$handler = new SystemStatus($sessionManager, $resultsManager, $config, $sessionService);
$params = $this->createMock(\Laminas\Mvc\Controller\Plugin\Params::class);
$response = $handler->handleRequest($params);
$response = $handler->handleRequest($this->getMockRequestParams());
$this->assertEquals(['Health check file exists', 503], $response);
}

Expand All @@ -77,9 +77,11 @@ public function testSolrFailure(): void
$config = new \Laminas\Config\Config([]);
$sessionService = $this->createMock(\VuFind\Db\Service\SessionServiceInterface::class);
$handler = new SystemStatus($sessionManager, $resultsManager, $config, $sessionService);
$params = $this->createMock(\Laminas\Mvc\Controller\Plugin\Params::class);
$response = $handler->handleRequest($params);
$response = $handler->handleRequest($this->getMockRequestParams());
$this->assertEquals(['Search index error: kaboom', 500], $response);
// Disable index check:
$response = $handler->handleRequest($this->getMockRequestParams(['index' => '0']));
$this->assertEquals([''], $response);
}

/**
Expand All @@ -92,18 +94,20 @@ public function testDatabaseFailure(): void
$sessionManager = $this->createMock(\Laminas\Session\SessionManager::class);
$resultsManager = $this->createMock(\VuFind\Search\Results\PluginManager::class);
$results = $this->createMock(\VuFind\Search\Solr\Results::class);
$results->expects($this->once())->method('performAndProcessSearch');
$resultsManager->expects($this->once())->method('get')->with($this->equalTo('Solr'))->willReturn($results);
$results->expects($this->exactly(2))->method('performAndProcessSearch');
$resultsManager->expects($this->exactly(2))->method('get')->with($this->equalTo('Solr'))->willReturn($results);
$params = $this->createMock(\VuFind\Search\Solr\Params::class);
$results->expects($this->once())->method('getParams')->willReturn($params);
$results->expects($this->exactly(2))->method('getParams')->willReturn($params);
$config = new \Laminas\Config\Config([]);
$sessionService = $this->createMock(\VuFind\Db\Service\SessionServiceInterface::class);
$e = new \Exception('kaboom');
$sessionService->expects($this->once())->method('getSessionById')->willThrowException($e);
$handler = new SystemStatus($sessionManager, $resultsManager, $config, $sessionService);
$params = $this->createMock(\Laminas\Mvc\Controller\Plugin\Params::class);
$response = $handler->handleRequest($params);
$response = $handler->handleRequest($this->getMockRequestParams());
$this->assertEquals(['Database error: kaboom', 500], $response);
// Disable database check:
$response = $handler->handleRequest($this->getMockRequestParams(['database' => '0']));
$this->assertEquals([''], $response);
}

/**
Expand All @@ -125,8 +129,27 @@ public function testSuccessfulResponse(): void
$sessionService = $this->createMock(\VuFind\Db\Service\SessionServiceInterface::class);
$sessionService->expects($this->once())->method('getSessionById');
$handler = new SystemStatus($sessionManager, $resultsManager, $config, $sessionService);
$params = $this->createMock(\Laminas\Mvc\Controller\Plugin\Params::class);
$response = $handler->handleRequest($params);
$response = $handler->handleRequest($this->getMockRequestParams());
$this->assertEquals([''], $response);
}

/**
* Get mock Params class for request params
*
* @param array $requestParams Parameters to return
*
* @return MockObject&Params
*/
protected function getMockRequestParams(array $requestParams = []): Params
{
$params = $this->getMockBuilder(Params::class)->getMock();
$params->expects($this->any())
->method('fromQuery')
->willReturnCallback(
function ($param, $default = null) use ($requestParams) {
return $requestParams[$param] ?? $default;
}
);
return $params;
}
}

0 comments on commit d9cfa58

Please sign in to comment.