From 30ddb51de372847d97b0363396e8a2db828e9053 Mon Sep 17 00:00:00 2001 From: gloas <gloas@afi-sa.fr> Date: Wed, 28 Mar 2018 11:01:07 +0200 Subject: [PATCH] dev #68179 add view helper + refacto config loading --- library/Class/DigitalResource.php | 3 +- .../Class/DigitalResource/AlbumViewHelper.php | 47 ++++++++++++++++ library/Class/DigitalResource/Config.php | 6 --- library/Class/DigitalResource/Controller.php | 2 +- library/Class/DigitalResource/Name.php | 12 ++--- .../digital_resources/StoryPlayR/Config.php | 34 ++++++++++-- .../digital_resources/StoryPlayR/Service.php | 2 +- .../StoryPlayR/View/Helper/Album.php | 23 ++++++++ .../StoryPlayR/tests/StoryPlayRTest.php | 54 ++++++++++++++++++- 9 files changed, 160 insertions(+), 23 deletions(-) create mode 100644 library/Class/DigitalResource/AlbumViewHelper.php create mode 100644 library/digital_resources/StoryPlayR/View/Helper/Album.php diff --git a/library/Class/DigitalResource.php b/library/Class/DigitalResource.php index 6641cc19f68..974c24ea25c 100644 --- a/library/Class/DigitalResource.php +++ b/library/Class/DigitalResource.php @@ -175,7 +175,8 @@ class Class_DigitalResource extends Class_Entity { if (is_readable($this->getBaseDir() . '/' . $type . '/View/Helper/Album.php')) { $class_name = $type . '_View_Helper_Album'; - $helper = (new $class_name())->setView($view); + $helper = $this->build($class_name, $this->configFor($type)) + ->setView($view); return $helper->album($album); } diff --git a/library/Class/DigitalResource/AlbumViewHelper.php b/library/Class/DigitalResource/AlbumViewHelper.php new file mode 100644 index 00000000000..f1e5e7faa19 --- /dev/null +++ b/library/Class/DigitalResource/AlbumViewHelper.php @@ -0,0 +1,47 @@ +<?php +/** + * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class Class_DigitalResource_AlbumViewHelper extends ZendAfi_View_Helper_BaseHelper { + protected $_config; + + public function __construct($config) { + parent::__construct(); + $this->_config = $config; + } + + + public function album($album) { + if(!$album) + return ''; + + $url = $this->view->url(['module' => 'opac', + 'controller' => 'modules', + 'action' => $this->_config->getSsoAction(), + 'album_id' => $album->getId()], + null, + true); + + return $this->view->tagAnchor($url, + $this->_('Accéder à "%s" dans un nouvel onglet', $album->getTitre()), + ['target' => '_blank']); + } +} \ No newline at end of file diff --git a/library/Class/DigitalResource/Config.php b/library/Class/DigitalResource/Config.php index aee72a1e588..6e0eb99b83a 100644 --- a/library/Class/DigitalResource/Config.php +++ b/library/Class/DigitalResource/Config.php @@ -49,12 +49,6 @@ class Class_DigitalResource_Config extends Class_Entity { } - - public static function getInstanceFromController($controller) { - return Class_DigitalResource::getInstance()->configFor((new Class_DigitalResource_Name())->getNameFromController($controller)); - } - - public function __construct($digital_resource) { $this->setDigitalResource($digital_resource); $this->updateAttributes($this->getConfig()); diff --git a/library/Class/DigitalResource/Controller.php b/library/Class/DigitalResource/Controller.php index 55fda6522f2..311a8676ee8 100644 --- a/library/Class/DigitalResource/Controller.php +++ b/library/Class/DigitalResource/Controller.php @@ -24,7 +24,7 @@ class Class_DigitalResource_Controller extends ZendAfi_Controller_Action { public function preDispatch() { parent::preDispatch(); - $this->_config = Class_DigitalResource_Config::getInstanceFromController(get_called_class()); + $this->_config = Class_DigitalResource_Config::getInstanceFor(static::class); if ($this->_config->getSsoAction() == $this->_request->getActionName()) return $this->_forward('sso', diff --git a/library/Class/DigitalResource/Name.php b/library/Class/DigitalResource/Name.php index fdc589a83ff..203a70cfe47 100644 --- a/library/Class/DigitalResource/Name.php +++ b/library/Class/DigitalResource/Name.php @@ -22,19 +22,13 @@ class Class_DigitalResource_Name { - public function getPath($class) { - return dirname((new ReflectionClass($class))->getFileName()); + public function getPath($instance) { + return dirname((new ReflectionClass($instance))->getFileName()); } public function getName($class) { - $path = array_reverse(explode('/', static::getPath($class))); - return array_shift($path); - } - - - public function getNameFromController($class) { - $path = array_reverse(explode('/', dirname(static::getPath($class)))); + $path = explode('_', $class); return array_shift($path); } } \ No newline at end of file diff --git a/library/digital_resources/StoryPlayR/Config.php b/library/digital_resources/StoryPlayR/Config.php index abf2e371f3e..b9c424b2174 100644 --- a/library/digital_resources/StoryPlayR/Config.php +++ b/library/digital_resources/StoryPlayR/Config.php @@ -30,7 +30,7 @@ class StoryPlayR_Config extends Class_DigitalResource_Config { 'OPDS_CATALOG_URL' => Class_AdminVar_Meta::newDefault($this->_('URL du catalogue OPDS de la plateforme StoryPlay*r')) ->bePrivate()], 'PermissionLabel' => $this->_('Bibliothèque numérique: accéder aux albums StoryPlay*r'), - 'NotAllowedMessage' => $this->_('Vous devez être abonné pour accéder à cette ressource.'), + 'NotAllowedMessage' => $this->_('Votre compte n\'est pas authorisé à accéder à cette ressource.'), 'SsoAction' => true, 'SsoValidateUrl' => true, @@ -50,11 +50,27 @@ class StoryPlayR_Config extends Class_DigitalResource_Config { public function getSsoUrl($user) { + if(!$user) + return ''; + return $this->getAdminVar('SSO_URL') . '?' - . http_build_query(['userid' => $user->getLogin(), - 'mediathequeid' => $this->getAdminVar('MEDIATHEQUE_ID'), - 'sessionid' => (new Class_CasTicket())->getTicketForUser($user)]); + . $this->_getSsoQuery($user); + } + + + public function getAlbumSsoUrl($user, $album) { + return parent::getAlbumSsoUrl($user, $album); + if(!$album) + return ''; + + if(!$base_url = $album->getExternalUri()) + return ''; + + if(!$user) + return ''; + + return $base_url . '&' . $this->_getSsoQuery($user); } @@ -74,6 +90,16 @@ class StoryPlayR_Config extends Class_DigitalResource_Config { public function getHarvestUrl($page = 1) { $query = ['portalProvider' => 'bokeh', 'mediathequeid' => $this->getAdminVar('MEDIATHEQUE_ID')]; + return $this->getAdminVar('OPDS_CATALOG_URL') . '?' . http_build_query(array_filter($query)); } + + + protected function _getSsoQuery($user) { + return $user + ? http_build_query(['userid' => $user->getLogin(), + 'mediathequeid' => $this->getAdminVar('MEDIATHEQUE_ID'), + 'sessionid' => (new Class_CasTicket())->getTicketForUser($user)]) + : ''; + } } \ No newline at end of file diff --git a/library/digital_resources/StoryPlayR/Service.php b/library/digital_resources/StoryPlayR/Service.php index 02256a2ac01..2f8b58c54d8 100644 --- a/library/digital_resources/StoryPlayR/Service.php +++ b/library/digital_resources/StoryPlayR/Service.php @@ -20,7 +20,7 @@ */ -class StoryPlayr_Service extends Class_DigitalResource_Service { +class StoryPlayR_Service extends Class_DigitalResource_Service { public function getPageCount() { return 1; } diff --git a/library/digital_resources/StoryPlayR/View/Helper/Album.php b/library/digital_resources/StoryPlayR/View/Helper/Album.php new file mode 100644 index 00000000000..02c856a77fa --- /dev/null +++ b/library/digital_resources/StoryPlayR/View/Helper/Album.php @@ -0,0 +1,23 @@ +<?php +/** + * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved. + * + * BOKEH is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * BOKEH is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with BOKEH; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +class StoryPlayR_View_Helper_Album extends Class_DigitalResource_AlbumViewHelper {} \ No newline at end of file diff --git a/library/digital_resources/StoryPlayR/tests/StoryPlayRTest.php b/library/digital_resources/StoryPlayR/tests/StoryPlayRTest.php index a46ee76f218..fe51eea85eb 100644 --- a/library/digital_resources/StoryPlayR/tests/StoryPlayRTest.php +++ b/library/digital_resources/StoryPlayR/tests/StoryPlayRTest.php @@ -185,7 +185,7 @@ class StoryPlayRHarvestTest extends StoryPlayRActivatedTestCase { public function setUp() { parent::setUp(); - Class_AdminVar::set('StoryPlayR_OPDS_CATALOG_URL', 'https://preprod.storyplayr.com/api/opds-with-links?portalProvider=bokeh'); + Class_AdminVar::set('StoryPlayR_OPDS_CATALOG_URL', 'https://preprod.storyplayr.com/api/opds-with-links'); $opds = file_get_contents(__DIR__ . '/StoryPlayrCatalog.xml'); @@ -235,3 +235,55 @@ class StoryPlayRHarvestTest extends StoryPlayRActivatedTestCase { $this->assertEquals(13, count(explode(';', $this->_album->getMatiere()))); } } + + + +class StoryPlayRNoticeAjaxTest extends StoryPlayRActivatedTestCase { + public function setUp() { + parent::setUp(); + + $album = $this->fixture('Class_Album', + ['id' => 12, + 'titre' => 'Once upon a time', + 'type_doc_id' => 'StoryPlayR', + 'visible' => 1, + 'status' => Class_Album::STATUS_VALIDATED + ]); + + $album->index(); + + $this->dispatch('/noticeajax/resnumeriques/id_notice/' . $album->getNoticeId(), true); + } + + + /** @test */ + public function linkToModulesStoryPlayRAlbumShouldBePresent() { + $this->assertXPath('//a[contains(@href, "/modules/story-play-r/album_id/12")]'); + } +} + + + +class StoryPlayRAlbumSsoUrlTest extends StoryPlayRActivatedTestCase { + public function setUp() { + parent::setUp(); + + ZendAfi_Auth::getInstance()->logUser($this->_user); + + $album = $this->fixture('Class_Album', + ['id' => 12, + 'titre' => 'Once upon a time', + 'type_doc_id' => 'StoryPlayR' + ]); + + $album->setExternalUri('https://preprod.storyplayr.com/api/bokeh/login?target=/histoire/a-coat-of-many-colors/playr'); + + $this->dispatch('/modules/story-play-r/album_id/12', true); + } + + + /** @test */ + public function shouldRedirectToStoryPlayRSsoUrlWithJavascript() { + $this->assertXPathContentContains('//script', 'https://preprod.storyplayr.com/api/bokeh/login?target=/histoire/a-coat-of-many-colors/playr&userid=Tom&mediathequeid=123456&sessionid=ST-', $this->_response->getBody()); + } +} -- GitLab