Skip to content

Commit

Permalink
Merge branch 'fixOfflineDataverse330-688' into 'stable-3_3_0'
Browse files Browse the repository at this point in the history
Corrige problema de quando Dataverse está fora do ar - 3.3.0

See merge request softwares-pkp/plugins_ojs/dataverse!166
  • Loading branch information
JhonathanLepidus committed Aug 16, 2024
2 parents 253f6ae + 4a2bb18 commit 3cab1c0
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 38 deletions.
8 changes: 4 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ variables:

include:
- project: 'documentacao-e-tarefas/modelosparaintegracaocontinua'
ref: main
ref: stable-3_3_0
file:
- 'templates/groups/pkp_plugin.yml'
- 'templates/groups/ops_plugins_unit_tests_model.yml'
- 'templates/groups/ojs_plugins_unit_tests_model.yml'
- 'templates/groups/ojs_plugins_cypress_tests_model.yml'
- 'templates/groups/ops/unit_tests.yml'
- 'templates/groups/ojs/unit_tests.yml'
- 'templates/groups/ojs/cypress_tests.yml'
10 changes: 6 additions & 4 deletions classes/DataverseMetadata.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ public function getDataverseSubjects(): array
];
}

public function getDataverseLicenses(): array
public function getDataverseLicenses(): ?array
{
$configuration = $this->getDataverseConfiguration();
$licensesUrl = $configuration->getDataverseServerUrl() . '/api/licenses';
$response = json_decode(file_get_contents($licensesUrl), true);
$this->dataverseLicenses = $response['data'];
$this->dataverseLicenses = $response['data'] ?? [];

return $this->dataverseLicenses;
}

public function getDefaultLicense(): string
public function getDefaultLicense(): ?string
{
if(is_null($this->dataverseLicenses)) {
if(empty($this->dataverseLicenses)) {
$this->getDataverseLicenses();
}

Expand All @@ -87,6 +87,8 @@ public function getDefaultLicense(): string
return $license['name'];
}
}

return null;
}

private function getDataverseConfiguration(): DataverseConfiguration
Expand Down
31 changes: 12 additions & 19 deletions classes/components/forms/DataStatementForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ public function __construct($action, $locales, $publication)
$this->action = $action;
$this->locales = $locales;

$dataStatementTypes = $this->getDataStatementTypes();

$dataStatementOptions = array_map(function ($value, $label) {
return [
'value' => $value,
'label' => $label,
];
}, array_keys($dataStatementTypes), array_values($dataStatementTypes));
$dataStatementService = new DataStatementService();
$dataStatementOptions = $this->getDataStatementOptions($dataStatementService);
$dataverseName = $dataStatementService->getDataverseName();

$request = Application::get()->getRequest();
$contextPath = $request->getContext()->getPath();
Expand Down Expand Up @@ -58,7 +53,7 @@ public function __construct($action, $locales, $publication)
[
'value' => true,
'label' => __('plugins.generic.dataverse.dataStatement.researchDataSubmitted', [
'dataverseName' => $this->getDataverseName(),
'dataverseName' => $dataverseName,
]),
'disabled' => true,
],
Expand All @@ -67,22 +62,20 @@ public function __construct($action, $locales, $publication)
]));
}

private function getDataStatementTypes(): array
private function getDataStatementOptions(): array
{
$dataStatementService = new DataStatementService();
$dataStatementTypes = $dataStatementService->getDataStatementTypes();
unset($dataStatementTypes[DATA_STATEMENT_TYPE_DATAVERSE_SUBMITTED]);

return $dataStatementTypes;
}

private function getDataverseName(): string
{
import('plugins.generic.dataverse.dataverseAPI.DataverseClient');
$dataverseClient = new DataverseClient();
$dataverseCollection = $dataverseClient->getDataverseCollectionActions()->get();
$dataStatementOptions = array_map(function ($value, $label) {
return [
'value' => $value,
'label' => $label,
];
}, array_keys($dataStatementTypes), array_values($dataStatementTypes));

return $dataverseCollection->getName();
return $dataStatementOptions;
}

private function hasDataset(Publication $publication): bool
Expand Down
3 changes: 2 additions & 1 deletion classes/components/forms/DraftDatasetFileForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ private function getTermsOfUseData($contextId)
'dataverseName' => $dataverseCollection->getName(),
'termsOfUseURL' => $termsOfUse
];
} catch (\Exception $e) {
} catch (DataverseException $e) {
error_log('Dataverse API error: ' . $e->getMessage());
return [];
}
}
}
10 changes: 9 additions & 1 deletion classes/services/DataStatementService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class DataStatementService
{
private $dataverseName;

public function getDataStatementTypes(): array
{
$dataverseName = $this->getDataverseName();
Expand All @@ -27,11 +29,17 @@ public function getDataStatementTypes(): array

public function getDataverseName(): ?string
{
if ($this->dataverseName) {
return $this->dataverseName;
}

try {
import('plugins.generic.dataverse.dataverseAPI.DataverseClient');
$dataverseClient = new DataverseClient();
$dataverseCollection = $dataverseClient->getDataverseCollectionActions()->get();
return $dataverseCollection->getName();
$this->dataverseName = $dataverseCollection->getName();

return $this->dataverseName;
} catch (DataverseException $e) {
error_log('Dataverse API error: ' . $e->getMessage());
return null;
Expand Down
8 changes: 4 additions & 4 deletions dataverseAPI/actions/DataverseActions.inc.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\TransferException;

import('plugins.generic.dataverse.classes.entities.DataverseResponse');
import('plugins.generic.dataverse.classes.exception.DataverseException');
Expand Down Expand Up @@ -60,11 +60,11 @@ public function nativeAPIRequest(string $method, string $uri, array $options = [

try {
$reponse = $this->client->request($method, $uri, $options);
} catch (RequestException $e) {
} catch (TransferException $e) {
$message = $e->getMessage();
$code = $e->getCode();

if ($e->hasResponse()) {
if (method_exists($e, 'hasResponse') and $e->hasResponse()) {
$response = $e->getResponse();
$code = $response->getStatusCode();

Expand All @@ -89,7 +89,7 @@ public function swordAPIRequest(string $method, string $uri, array $options = []

try {
$reponse = $this->client->request($method, $uri, $options);
} catch (RequestException $e) {
} catch (TransferException $e) {
$message = $e->getMessage();
$code = $e->getCode();

Expand Down
26 changes: 23 additions & 3 deletions tests/dataverseAPI/actions/DataverseActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;

import('lib.pkp.tests.PKPTestCase');
Expand Down Expand Up @@ -96,7 +97,7 @@ public function testSuccessfulNativeAPIRequest(): void
$this->assertEquals('{"foo": "bar"}', $response->getBody());
}

public function testRequestErrorWithoutResponseThrownDataverseException(): void
public function testRequestErrorWithoutResponseThrowsDataverseException(): void
{
$mockHandler = new MockHandler([
new RequestException(
Expand All @@ -115,7 +116,26 @@ public function testRequestErrorWithoutResponseThrownDataverseException(): void
$actions->nativeAPIRequest('GET', 'test');
}

public function testRequestErrorWithResponseThrownDataverseException(): void
public function testConnectionErrorThrowsDataverseException(): void
{
$mockHandler = new MockHandler([
new ConnectException(
'Failed to connect to Dataverse',
new Request('GET', 'test')
)
]);
$guzzleClient = new Client(['handler' => $mockHandler]);

$actions = $this->getMockBuilder(DataverseActions::class)
->setConstructorArgs([$this->configuration, $guzzleClient])
->getMockForAbstractClass();

$this->expectException(DataverseException::class);
$this->expectExceptionMessage('Failed to connect to Dataverse');
$actions->nativeAPIRequest('GET', 'test');
}

public function testRequestErrorWithResponseThrowsDataverseException(): void
{
$mockHandler = new MockHandler([
new RequestException(
Expand All @@ -136,7 +156,7 @@ public function testRequestErrorWithResponseThrownDataverseException(): void
$actions->nativeAPIRequest('GET', 'test');
}

public function testRequestErrorWithResponseBodyEmptyThrownDataverseException(): void
public function testRequestErrorWithResponseBodyEmptyThrowsDataverseException(): void
{
$mockHandler = new MockHandler([
new RequestException(
Expand Down
4 changes: 2 additions & 2 deletions version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<version>
<application>dataverse</application>
<type>plugins.generic</type>
<release>2.5.11.0</release>
<date>2024-01-12</date>
<release>2.5.12.0</release>
<date>2024-08-16</date>
<lazy-load>1</lazy-load>
<class>DataversePlugin</class>
</version>

0 comments on commit 3cab1c0

Please sign in to comment.