diff --git a/VERSIONS_WIP/141983 b/VERSIONS_WIP/141983 new file mode 100644 index 0000000000000000000000000000000000000000..b44b07632233c0f4da1f6b3322e0fa83a6695be9 --- /dev/null +++ b/VERSIONS_WIP/141983 @@ -0,0 +1 @@ + - ticket #141983 : Hébergement : Rechargement automatique de la page lorsque la session n'a pas pu être ouverte \ No newline at end of file diff --git a/library/startup.php b/library/startup.php index 9e404f1a355b046ecbaab92fc457368d6becbbe7..7a935d05a098340ad35bed0bbb963c68d393d1f8 100644 --- a/library/startup.php +++ b/library/startup.php @@ -31,10 +31,17 @@ function defineConstant($name, $value) { class Bokeh_Engine { + + protected static + $_zend_session_namespace, + $_php_command; + + protected $_config, $_front_controller; + public function powerOn() { return $this @@ -202,19 +209,72 @@ class Bokeh_Engine { function setupSession() { - // Start Session - $session = new Zend_Session_Namespace(md5(BASE_URL)); - if (!isset($session->initialized)) - { - Zend_Session::regenerateId(); - $session->initialized = true; - } - if (!isset($session->baseUrl)) $session->baseUrl = BASE_URL; + try { + $session = $this->_getZendSessionNamespace()->get(md5(BASE_URL)); + } catch (Zend_Session_Exception $e) { + return $this->_timeoutReload(); + } + + if (!isset($session->initialized)) { + Zend_Session::regenerateId(); + $session->initialized = true; + } + + if (!isset($session->baseUrl)) + $session->baseUrl = BASE_URL; + Zend_Registry::set('session', $session); return $this; } + protected function _timeoutReload() { + $this->_getPhpCommand()->header('HTTP/1.1 503 Service Unavailable'); + $this->_getPhpCommand()->echoHtml('<html>' + . '<body style="text-align:center">' + . '<img src="' . URL_SHARED_IMG . 'patience.gif' . '" />' + . 'Attente de connexion...' + . '<script>' + . 'setTimeout(() => window.location.reload(),5000);' + . '</script>' + . '</body>' + .'</html>'); + $this->_getPhpCommand()->exitDispatch(); + } + + + protected function _getPhpCommand() { + if ( static::$_php_command) + return static::$_php_command; + + return new Bokeh_PhpCommand; + } + + + public static function setPhpCommand($instance) { + static::$_php_command = $instance; + } + + + protected function _getZendSessionNamespace() { + if ( static::$_zend_session_namespace ) + return static::$_zend_session_namespace; + + return new Bokeh_Session; + } + + + public function setZendSessionNamespace($instance) { + return static::$_zend_session_namespace = $instance; + } + + + public static function reset() { + static::$_zend_session_namespace = null; + static::$_php_command = null; + } + + function setupLanguage() { $default = Class_AdminVar::getDefaultLanguage(); ZendAfi_Locale::setDefault($default); @@ -426,6 +486,32 @@ class Bokeh_Engine { } + +class Bokeh_PhpCommand { + public function header($text) { + header($text); + } + + + public function echoHtml($html) { + echo $html; + } + + + public function exitDispatch() { + exit; + } +} + + + +class Bokeh_Session { + public function get($session_id) { + return new Zend_Session_Namespace($session_id); + } +} + + function setupOpac() { return (new Bokeh_Engine()) ->powerOn() diff --git a/tests/BokehEngineTest.php b/tests/BokehEngineTest.php index 3b73d006e40e4b64731953f66dc5ba2d9f78b7e9..d805ea44882f0e3276670445b9fe07ff9beaf502 100644 --- a/tests/BokehEngineTest.php +++ b/tests/BokehEngineTest.php @@ -28,4 +28,36 @@ class BokehEngineLanguageTest extends ModelTestCase { $engine = (new Bokeh_Engine)->setupLanguage(); $this->assertEquals('fr', Zend_Registry::get('translate')->getLocale()); } + + + /** @test */ + public function withZendSessionExceptionShouldReturnHtmlWithScript() { + $html = '<html><body style="text-align:center"><img src="' . BASE_URL . '/public/opac/images/patience.gif" />Attente de connexion...<script>setTimeout(() => window.location.reload(),5000);</script></body></html>'; + + $zend_session = $this + ->mock()->beStrict() + + ->whenCalled('get')->with(md5(BASE_URL)) + ->willDo(function() {throw new Zend_Session_Exception;}); + + $php_command = $this + ->mock()->beStrict() + + ->whenCalled('header')->with('HTTP/1.1 503 Service Unavailable') + ->answers('503') + + ->whenCalled('echoHtml')->with($html) + ->answers('') + + ->whenCalled('exitDispatch') + ->answers('') + ; + + Bokeh_Engine::setZendSessionNameSpace($zend_session); + Bokeh_Engine::setPhpCommand($php_command); + + $engine = (new Bokeh_Engine)->setupSession(); + + $this->assertNotNull(Zend_Registry::get('session')); + } } diff --git a/tests/library/Class/ModelTestCase.php b/tests/library/Class/ModelTestCase.php index 8ab4c5725e4f437414ce760789628b92f2056ab9..b8a6695dadb5a75d9281692d19ee8ac077a34c6f 100644 --- a/tests/library/Class/ModelTestCase.php +++ b/tests/library/Class/ModelTestCase.php @@ -98,6 +98,7 @@ abstract class ModelTestCase extends Storm_Test_ModelTestCase { protected function tearDown() { + Bokeh_Engine::reset(); Class_Url::setPhpMode(null); Class_Notice_Thumbnail_ResizeImage::reset(); Class_Album::setFileSystem(null);