diff --git a/library/Class/WebService/SIGB/AbstractRESTService.php b/library/Class/WebService/SIGB/AbstractRESTService.php index 65379cc2fab8830da6bb684ebd84a5fb9fad1e80..4ccaad37fc3a0b5183b9d4e0c6ad71bdd98c418c 100644 --- a/library/Class/WebService/SIGB/AbstractRESTService.php +++ b/library/Class/WebService/SIGB/AbstractRESTService.php @@ -20,8 +20,6 @@ */ abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebService_SIGB_AbstractService { - use Trait_Translator; - protected $_server_root; protected $_web_client; @@ -94,6 +92,24 @@ abstract class Class_WebService_SIGB_AbstractRESTService extends Class_WebServic } + /** + * @param $options array url query part + * @param $datas array posted datas + * @return string response body + */ + public function httpPost($options, $datas) { + $url = $this->buildQueryURL($options); + try { + $response = $this->getWebClient()->postData($url, $datas); + $this->log(); + return $response; + } catch (Exception $e) { + $this->logError($url, $e->getMessage()); + return ''; + } + } + + /** * @param string $xml * @param string $tag diff --git a/library/Class/WebService/SIGB/AbstractService.php b/library/Class/WebService/SIGB/AbstractService.php index 5fc983e592ff9c6af898d7bbb6bdd85b2b01ffae..e4f80c1fe735aa13fefbd02820f72797cd4f0089 100644 --- a/library/Class/WebService/SIGB/AbstractService.php +++ b/library/Class/WebService/SIGB/AbstractService.php @@ -20,6 +20,8 @@ */ abstract class Class_WebService_SIGB_AbstractService { + use Trait_Translator; + protected static $_logger; protected $_notice_cache; @@ -127,14 +129,18 @@ abstract class Class_WebService_SIGB_AbstractService { } + public function suggest($user, $data) { + return $this->_error($this->_('Suggestions non prises en charge par ce connecteur')); + } + + protected function _success() { - return array('statut' => true, 'erreur' => ''); + return ['statut' => true, 'erreur' => '']; } protected function _error($message) { - return array('statut' => false, - 'erreur' => $message); + return ['statut' => false, 'erreur' => $message]; } @@ -144,8 +150,6 @@ abstract class Class_WebService_SIGB_AbstractService { public function test() { - return 'Ce service n\'est pas testable'; + return $this->_('Ce service n\'est pas testable'); } -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/library/Class/WebService/SIGB/Koha/RestfulService.php b/library/Class/WebService/SIGB/Koha/RestfulService.php index df5b44a0f15020bcf477a8cb16812557c8da31ff..1c9f130ca048584621189b35b98726e841246094 100644 --- a/library/Class/WebService/SIGB/Koha/RestfulService.php +++ b/library/Class/WebService/SIGB/Koha/RestfulService.php @@ -55,6 +55,22 @@ class Class_WebService_SIGB_Koha_RestfulService } + public function suggest($user, $datas=[]) { + $datas['suggestedby'] = $this->_authenticate($user); + $json = $this->restfulPost('suggestions', + ['data' => json_encode($datas)]); + + if ('' == $json || (!$data = json_decode($json))) + return $this->_error($this->_('Échec de la suggestion, une erreur inconnue est survenue.')); + + if (isset($data->error)) + return $this->_error($this->_('Échec de la suggestion, le webservice a répondu "%s"', + trim($data->error))); + + return $this->_success(); + } + + public function buildQueryURL($options) { return sprintf('%s/%s?%s', $this->getServerRoot(), @@ -69,6 +85,12 @@ class Class_WebService_SIGB_Koha_RestfulService } + public function restfulPost($action, $datas, $params=[]) { + $this->_current_action = $action; + return $this->httpPost($params, $datas); + } + + /** * Handled by ILSDI */ diff --git a/library/Class/WebService/SIGB/Koha/Service.php b/library/Class/WebService/SIGB/Koha/Service.php index 5d3f648afa43067ae491b1b09f8777b4cb1ec6dc..fc7a2124c1b4e8a5f469921378ee7c9c00088724 100644 --- a/library/Class/WebService/SIGB/Koha/Service.php +++ b/library/Class/WebService/SIGB/Koha/Service.php @@ -24,8 +24,7 @@ class Class_WebService_SIGB_Koha_Service extends Class_WebService_SIGB_AbstractR protected $interdire_resa_doc_dispo = false, $restful = false, - $codification_disponibilites = [], - $restful_service = null; + $codification_disponibilites = []; public static function newInstance() { return new self(); @@ -169,7 +168,7 @@ class Class_WebService_SIGB_Koha_Service extends Class_WebService_SIGB_AbstractR public function suggestionsOf($user) { if (!$this->providesSuggestions()) - return []; + return parent::suggestionsOf($user); return $this ->getRestfulService() @@ -177,12 +176,18 @@ class Class_WebService_SIGB_Koha_Service extends Class_WebService_SIGB_AbstractR } - public function getRestfulService() { - if (!$this->restful_service) { - $this->restful_service = Class_WebService_SIGB_Koha_RestfulService::newFromIlsdi($this); - } + public function suggest($user, $datas) { + if (!$this->providesSuggestions()) + return parent::suggest($user, $datas); - return $this->restful_service; + return $this + ->getRestfulService() + ->suggest($user, $datas); + } + + + public function getRestfulService() { + return Class_WebService_SIGB_Koha_RestfulService::newFromIlsdi($this); } diff --git a/tests/library/Class/WebService/SIGB/KohaRestfulTest.php b/tests/library/Class/WebService/SIGB/KohaRestfulTest.php index 277bf0b248805795cbd6afaff28f6cd4d163456a..9697c311489f3327af0b1a0ab7d640cfc8f8e592 100644 --- a/tests/library/Class/WebService/SIGB/KohaRestfulTest.php +++ b/tests/library/Class/WebService/SIGB/KohaRestfulTest.php @@ -35,9 +35,6 @@ abstract class KohaRestfulTestCase extends ModelTestCase { $this->mock_web_client = $this->mock(); - $params = ['url_serveur' => static::BASE_URL . 'ilsdi.pl', - 'restful' => '1']; - $logger = $this->mock() ->whenCalled('log')->answers(true) @@ -47,6 +44,8 @@ abstract class KohaRestfulTestCase extends ModelTestCase { }); Class_WebService_SIGB_AbstractService::setLogger($logger); + $params = ['url_serveur' => static::BASE_URL . 'ilsdi.pl', + 'restful' => '1']; $this->service = Class_WebService_SIGB_Koha::getService($params); $this->service->setWebClient($this->mock_web_client); } @@ -60,7 +59,7 @@ abstract class KohaRestfulTestCase extends ModelTestCase { -class KohaRestfulSuggestionOfTest extends KohaRestfulTestCase { +class KohaRestfulSuggestionsOfUserTest extends KohaRestfulTestCase { public function setUp() { parent::setUp(); @@ -137,4 +136,167 @@ class KohaRestfulSuggestionOfTest extends KohaRestfulTestCase { $this->assertEquals($note, $this->suggestions[$pos]->getNote()); $this->assertEquals($status, $this->suggestions[$pos]->getStatus()); } +} + + + +abstract class KohaRestfulSuggestTestCase extends KohaRestfulTestCase { + public function setUp() { + parent::setUp(); + + xdebug_break(); + + $user = $this->fixture('Class_Users', + ['id' => 34, + 'login' => 'harlock', + 'password' => 'arcadia', + 'idabon' => 'AO989IE']); + + $this->fixture('Class_CodifAnnexe', + ['id' => 15, + 'libelle' => 'Istres', + 'code' => 'IST']); + + $this->mock_web_client + ->whenCalled('open_url') + ->with(static::BASE_URL . 'ilsdi.pl?service=LookupPatron&id=AO989IE&id_type=cardnumber') + ->answers('<id>32007</id>') + + ->whenCalled('postData') + ->with('http://cat-aficg55.biblibre.com/cgi-bin/koha/rest.pl/suggestions?', + ['data' => '{"title":"CommitStrip - Le Livre","author":"CommitStrip","suggestedby":"32007"}']) + ->answers($this->_postAnswer()) + + ->beStrict(); + + $datas = ['title' => 'CommitStrip - Le Livre', 'author' => 'CommitStrip']; + $this->response = $this->service->suggest($user, $datas); + } + + + abstract protected function _postAnswer(); +} + + + +class KohaRestfulSuccessfulSuggestTest extends KohaRestfulSuggestTestCase { + protected function _postAnswer() { + return '{ + "date" : "2015-07-09 11:36:17", + "STATUS" : "ASKED", + "suggestionid" : "8824", + "accepteddate" : null, + "isbn" : null, + "branchcode" : null, + "copyrightdate" : null, + "budgetid" : null, + "reason" : null, + "total" : null, + "price" : null, + "title" : "CommitStrip - Le Livre", + "collectiontitle" : null, + "publicationyear" : "0", + "itemtype" : null, + "place" : null, + "author" : "CommitStrip", + "suggesteddate" : "2015-07-09", + "currency" : null, + "biblionumber" : null, + "manageddate" : null, + "acceptedby" : null, + "publishercode" : null, + "suggestedby" : "0", + "rejecteddate" : null, + "rejectedby" : null, + "quantity" : null, + "note" : null, + "patronreason" : null, + "volumedesc" : null, + "mailoverseeing" : "0", + "managedby" : null +}'; + } + + + /** @test */ + public function shouldHaveNoError() { + $this->assertTrue($this->response['statut']); + } +} + + + +class KohaRestfulEmptyResponseSuggestTest extends KohaRestfulSuggestTestCase { + protected function _postAnswer() { + return ''; + } + + + /** @test */ + public function shouldHaveError() { + $this->assertFalse($this->response['statut']); + } + + + + /** @test */ + public function errorShouldBeUnknown() { + $this->assertEquals('Échec de la suggestion, une erreur inconnue est survenue.', + $this->response['erreur']); + } +} + + + +class KohaRestfulSoftwareErrorResponseSuggestTest extends KohaRestfulSuggestTestCase { + protected function _postAnswer() { + return '<h1>Software error:</h1> +<pre>Can\'t locate object method "error" via package "Error executing run mode \'create_suggestion\': DBIx::Class::ResultSet::create(): Column \'suggestedby\' cannot be null at /home/koha/src/C4/Suggestions.pm line 450 + at /usr/share/perl5/CGI/Application/Dispatch.pm line 707 +" (perhaps you forgot to load "Error executing run mode \'create_suggestion\': DBIx::Class::ResultSet::create(): Column \'suggestedby\' cannot be null at /home/koha/src/C4/Suggestions.pm line 450 + at /usr/share/perl5/CGI/Application/Dispatch.pm line 707 +"?) at /usr/share/perl5/CGI/Application/Dispatch.pm line 438. +</pre> +<p> +For help, please send mail to the webmaster (<a href="mailto:support@server.com">support@server.com</a>), giving this error message +and the time and date of the error. + +</p>'; + } + + + /** @test */ + public function shouldHaveError() { + $this->assertFalse($this->response['statut']); + } + + + /** @test */ + public function errorShouldBeUnknown() { + $this->assertEquals('Échec de la suggestion, une erreur inconnue est survenue.', + $this->response['erreur']); + } +} + + + +class KohaRestfulJsonErrorSuggestTest extends KohaRestfulSuggestTestCase { + protected function _postAnswer() { + return '{ + "error" : "Failed to parse data parameter: malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before \"(end of string)\") at /usr/share/perl5/JSON.pm line 171.\n" +}'; + } + + + /** @test */ + public function shouldHaveError() { + $this->assertFalse($this->response['statut']); + } + + + /** @test */ + public function errorShouldBeKnown() { + $this->assertEquals('Échec de la suggestion, le webservice a répondu "Failed to parse data parameter: malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)") at /usr/share/perl5/JSON.pm line 171."', + $this->response['erreur']); + } } \ No newline at end of file