From 77ad85d3e9aaae375384481e1273a487474b040f Mon Sep 17 00:00:00 2001 From: efalcy <efalcy@afi-sa.fr> Date: Tue, 21 Jun 2022 14:59:08 +0200 Subject: [PATCH] dev#152581 : fix RT --- library/Class/WebService/OAIHarvester.php | 25 ++++++++----- .../Bibliondemand/tests/BibliondemandTest.php | 2 +- .../PhasePrepareIntegrationNumelTest.php | 36 +++++++++++++++++-- tests/scenarios/Numel/numel_loop.xml | 26 ++++++++++++++ tests/scenarios/Numel/numel_loop2.xml | 26 ++++++++++++++ tests/scenarios/Numel/numel_loop3.xml | 26 ++++++++++++++ 6 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 tests/scenarios/Numel/numel_loop.xml create mode 100644 tests/scenarios/Numel/numel_loop2.xml create mode 100644 tests/scenarios/Numel/numel_loop3.xml diff --git a/library/Class/WebService/OAIHarvester.php b/library/Class/WebService/OAIHarvester.php index 3c2aa3b2ac9..5419348cf96 100644 --- a/library/Class/WebService/OAIHarvester.php +++ b/library/Class/WebService/OAIHarvester.php @@ -31,6 +31,7 @@ class Class_WebService_OAIHarvester { IDENTIFIER_PATTERN = '|<identifier[^>]*>[^<]*</identifier>|', LIST_RECORDS = 'ListRecords'; + public static int $MAX_REQUESTS = 50000; protected $_base_url, $_query = '', @@ -76,10 +77,15 @@ class Class_WebService_OAIHarvester { $start_url = $this->_base_url . '?' . $this->_query; $this->_debug($start_url, 'is the computed start harvest url'); $token = $this->_fetchUrl($start_url, $callback); - - while ($token) + $count = 0; + while ($token && $count < static::$MAX_REQUESTS ) { $token = $this->_fetchUrl($this->_nextUrlWith($token), $callback); - + $count++; + } + if ($count >= static::$MAX_REQUESTS) { + $this->_debug($start_url, 'Error : loop on request'); + throw new RuntimeException($this->_('Le nombre de requêtes a dépassé le nombre autorisé: '. static::$MAX_REQUESTS)); + } return $this; } @@ -103,11 +109,9 @@ class Class_WebService_OAIHarvester { preg_match_all(static::IDENTIFIER_PATTERN, $body, $matches); - if( empty($matches) || empty( reset($matches))) { - return preg_match(static::RESUMPTION_PATTERN, $body, $matches) - ? $matches[1] - : null; - } + if( empty($matches) || empty( reset($matches))) + return $this->_parseToken($body); + $checksum = md5(json_encode($matches)); if ($this->_previous_checksum === $checksum) throw new RuntimeException($this->_('Réponse identique à la précédente')); @@ -115,6 +119,11 @@ class Class_WebService_OAIHarvester { $callback($body . static::PAGE_SEPARATOR); + return $this->_parseToken($body); + } + + + protected function _parseToken($body) { return preg_match(static::RESUMPTION_PATTERN, $body, $matches) ? $matches[1] : null; diff --git a/library/digital_resources/Bibliondemand/tests/BibliondemandTest.php b/library/digital_resources/Bibliondemand/tests/BibliondemandTest.php index e0fbcabff71..9943a4d87bb 100644 --- a/library/digital_resources/Bibliondemand/tests/BibliondemandTest.php +++ b/library/digital_resources/Bibliondemand/tests/BibliondemandTest.php @@ -68,7 +68,7 @@ class BibliondemandModulesControllerTest extends AbstractControllerTestCase { /** @test */ public function dashboardCustomIntegrationShouldBeDisplay() { $this->dispatch('/Bibliondemand_Plugin', true); - $this->assertNotXpathContentContains('//h3', 'DIAGNOSTIC SSO'); + $this->assertXpathContentContains('//h3', 'Diagnostic SSO'); $this->assertNotXpathContentContains('//h3', 'Diagnostic moissonnage'); $this->assertXpathContentContains('//h3', 'Diagnostic complémentaire'); } diff --git a/tests/scenarios/Numel/PhasePrepareIntegrationNumelTest.php b/tests/scenarios/Numel/PhasePrepareIntegrationNumelTest.php index 8530d06cea2..e6fb50865fa 100644 --- a/tests/scenarios/Numel/PhasePrepareIntegrationNumelTest.php +++ b/tests/scenarios/Numel/PhasePrepareIntegrationNumelTest.php @@ -158,11 +158,43 @@ class PhasePrepareIntegrationNumelTest extends Class_Cosmogramme_Integration_Pha } + /** @test */ + public function withSameRecordsAndNoIdentifierShouldStop() { + $response_1 = $this->mock() + ->whenCalled('isSuccessful')->answers(true) + ->whenCalled('getBody') + ->answers(file_get_contents(__DIR__.'/numel_loop.xml')); + + $response_2 = $this->mock() + ->whenCalled('isSuccessful')->answers(true) + ->whenCalled('getBody') + ->answers(file_get_contents(__DIR__.'/numel_loop2.xml')); + $response_3 = $this->mock() + ->whenCalled('isSuccessful')->answers(true) + ->whenCalled('getBody') + ->answers(file_get_contents(__DIR__.'/numel_loop3.xml')); + Class_WebService_OAIHarvester::$MAX_REQUESTS=5; + $this->_http + ->whenCalled('getResponse') + ->with('https://server.org/oai/index.php?verb=ListRecords&metadataPrefix=oai_numel') + ->answers($response_1) + + ->whenCalled('getResponse') + ->with('https://server.org/oai/index.php?verb=ListRecords&resumptionToken=1646836602') + ->answers($response_2) + ->whenCalled('getResponse') + ->with('https://server.org/oai/index.php?verb=ListRecords&resumptionToken=1646836603') + ->answers($response_3); + + $this->_phase = $this->_buildPhase('PrepareIntegrations')->run(); + $this->assertLogContains('Le nombre de requêtes a dépassé le nombre autorisé: 5'); + } + + /** * @test */ public function withNoIdentifierShouldReadNextPage(){ - $expected_harvest_url = 'https://server.org/oai/index.php?verb=ListRecords&metadataPrefix=oai_numel'; $response_1 = $this->mock() ->whenCalled('isSuccessful')->answers(true) ->whenCalled('getBody') @@ -190,6 +222,6 @@ class PhasePrepareIntegrationNumelTest extends Class_Cosmogramme_Integration_Pha $this->_phase = $this->_buildPhase('PrepareIntegrations')->run(); - $this->assertNotLogContains('Impossible de moissonner https://server.org/oai/index.php : Réponse identique à la précédente'); + $this->assertLogContains('transfert de https://server.org/oai/index.php vers integre1179.pan'); } } diff --git a/tests/scenarios/Numel/numel_loop.xml b/tests/scenarios/Numel/numel_loop.xml new file mode 100644 index 00000000000..25fa2755c9f --- /dev/null +++ b/tests/scenarios/Numel/numel_loop.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:dcterms="http://purl.org/dc/terms/" + xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ + http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> +<responseDate>2022-03-09T14:36:42Z</responseDate> + <request verb="ListRecords" resumptionToken="1646836484">https://melun.prod-osiros.decalog.net/oai/index.php</request> + <ListRecords> + <record> + <header status="deleted"> + + <datestamp>2019-11-06</datestamp> + </header> + </record> + <record> + <header status="deleted"> + + <datestamp>2020-04-10</datestamp> + </header> + </record> + <resumptionToken expirationDate="2022-03-10T14:36:42Z" + completeListSize="23443" + cursor="0">1646836602</resumptionToken> + </ListRecords> +</OAI-PMH> diff --git a/tests/scenarios/Numel/numel_loop2.xml b/tests/scenarios/Numel/numel_loop2.xml new file mode 100644 index 00000000000..115f50d6c94 --- /dev/null +++ b/tests/scenarios/Numel/numel_loop2.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:dcterms="http://purl.org/dc/terms/" + xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ + http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> +<responseDate>2022-03-09T14:36:42Z</responseDate> + <request verb="ListRecords" resumptionToken="1646836484">https://melun.prod-osiros.decalog.net/oai/index.php</request> + <ListRecords> + <record> + <header status="deleted"> + + <datestamp>2019-11-06</datestamp> + </header> + </record> + <record> + <header status="deleted"> + + <datestamp>2020-04-10</datestamp> + </header> + </record> + <resumptionToken expirationDate="2022-03-10T14:36:42Z" + completeListSize="23443" + cursor="0">1646836603</resumptionToken> + </ListRecords> +</OAI-PMH> diff --git a/tests/scenarios/Numel/numel_loop3.xml b/tests/scenarios/Numel/numel_loop3.xml new file mode 100644 index 00000000000..25fa2755c9f --- /dev/null +++ b/tests/scenarios/Numel/numel_loop3.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="UTF-8"?> +<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:dcterms="http://purl.org/dc/terms/" + xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ + http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"> +<responseDate>2022-03-09T14:36:42Z</responseDate> + <request verb="ListRecords" resumptionToken="1646836484">https://melun.prod-osiros.decalog.net/oai/index.php</request> + <ListRecords> + <record> + <header status="deleted"> + + <datestamp>2019-11-06</datestamp> + </header> + </record> + <record> + <header status="deleted"> + + <datestamp>2020-04-10</datestamp> + </header> + </record> + <resumptionToken expirationDate="2022-03-10T14:36:42Z" + completeListSize="23443" + cursor="0">1646836602</resumptionToken> + </ListRecords> +</OAI-PMH> -- GitLab