Skip to content

Commit 294db66

Browse files
committed
fix(metadata): wrap abstract and biography content
Refs: documentacao-e-tarefas/desenvolvimento_e_infra#1176
1 parent 2ec6249 commit 294db66

File tree

6 files changed

+138
-3
lines changed

6 files changed

+138
-3
lines changed

classes/factories/ThothAbstractFactory.inc.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private function create($entity, string $workId, ?string $preferredLocale = null
4646
$thothAbstracts[$this->getLocaleKey($localeCode)] = new ThothAbstract([
4747
'workId' => $workId,
4848
'localeCode' => $localeCode,
49-
'content' => $abstract,
49+
'content' => $this->wrapInParagraph($abstract),
5050
'canonical' => $locale === $canonicalLocale,
5151
'abstractType' => 'LONG',
5252
]);
@@ -108,4 +108,14 @@ private function logUnsupportedLocale(string $entityType, ?string $locale): void
108108
$normalizedLocaleCode
109109
));
110110
}
111+
112+
private function wrapInParagraph($content)
113+
{
114+
$content = trim($content);
115+
if (preg_match('/^<p\b[^>]*>.*<\/p>$/is', $content) === 1) {
116+
return $content;
117+
}
118+
119+
return sprintf('<p>%s</p>', $content);
120+
}
111121
}

classes/factories/ThothBiographyFactory.inc.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function createFromAuthor($author, string $contributionId, ?string $prefe
3636
$thothBiographies[$this->getLocaleKey($localeCode)] = new ThothBiography([
3737
'contributionId' => $contributionId,
3838
'localeCode' => $localeCode,
39-
'content' => $biography,
39+
'content' => $this->wrapInParagraph($biography),
4040
'canonical' => $locale === $canonicalLocale,
4141
]);
4242
}
@@ -97,4 +97,14 @@ private function logUnsupportedLocale(string $entityType, ?string $locale): void
9797
$normalizedLocaleCode
9898
));
9999
}
100+
101+
private function wrapInParagraph($content)
102+
{
103+
$content = trim($content);
104+
if (preg_match('/^<p\b[^>]*>.*<\/p>$/is', $content) === 1) {
105+
return $content;
106+
}
107+
108+
return sprintf('<p>%s</p>', $content);
109+
}
100110
}

classes/factories/ThothBookFactory.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getWorkTypeBySubmissionWorkType($submissionWorkType)
7070
Submission::WORK_TYPE_AUTHORED_WORK => ThothWork::WORK_TYPE_MONOGRAPH
7171
];
7272

73-
return $workTypeMapping[$submissionWorkType];
73+
return $workTypeMapping[$submissionWorkType] ?? ThothWork::WORK_TYPE_MONOGRAPH;
7474
}
7575

7676
public function getWorkStatusByDatePublished($datePublished)

classes/notification/ThothNotification.inc.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function notifySuccess($request, $submission)
2828

2929
public function notifyError($request, $submission, $error)
3030
{
31+
$error = $this->normalizeError($error);
3132
error_log("Failed to send the request to Thoth: {$error}");
3233
$this->notify(
3334
$request,
@@ -53,6 +54,7 @@ public function notify($request, $submission, $notificationType, $messageKey, $e
5354

5455
public function logInfo($request, $submission, $messageKey, $error = null)
5556
{
57+
$error = $this->normalizeError($error);
5658
$currentUser = $request->getUser();
5759
$eventLog = Repo::eventLog()->newDataObject([
5860
'assocType' => Application::ASSOC_TYPE_SUBMISSION,
@@ -67,6 +69,23 @@ public function logInfo($request, $submission, $messageKey, $error = null)
6769
Repo::eventLog()->add($eventLog);
6870
}
6971

72+
protected function normalizeError($error)
73+
{
74+
if ($error === null || is_scalar($error)) {
75+
return $error;
76+
}
77+
78+
if ($error instanceof Throwable) {
79+
return $error->getMessage();
80+
}
81+
82+
if (is_object($error) && method_exists($error, 'getMessage')) {
83+
return $error->getMessage();
84+
}
85+
86+
return json_encode($error);
87+
}
88+
7089
public function addJavaScriptData($request, $templateMgr)
7190
{
7291
$data = ['notificationUrl' => $request->url(null, 'notification', 'fetchNotification')];
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use PKP\tests\PKPTestCase;
4+
5+
import('plugins.generic.thoth.classes.factories.ThothAbstractFactory');
6+
7+
class ThothAbstractFactoryTest extends PKPTestCase
8+
{
9+
public function testCreateFromPublicationWrapsAbstractWithoutParagraph()
10+
{
11+
$publication = new class () {
12+
public function getData($key)
13+
{
14+
$values = [
15+
'locale' => 'en_US',
16+
'abstract' => ['en_US' => 'English abstract'],
17+
];
18+
19+
return $values[$key] ?? null;
20+
}
21+
};
22+
23+
$factory = new ThothAbstractFactory();
24+
$thothAbstracts = $factory->createFromPublication($publication, 'work-id', 'en_US');
25+
26+
$this->assertSame('<p>English abstract</p>', $thothAbstracts['EN_US']->getContent());
27+
}
28+
29+
public function testCreateFromPublicationPreservesAbstractAlreadyWrappedInParagraph()
30+
{
31+
$publication = new class () {
32+
public function getData($key)
33+
{
34+
$values = [
35+
'locale' => 'en_US',
36+
'abstract' => ['en_US' => '<p>English abstract</p>'],
37+
];
38+
39+
return $values[$key] ?? null;
40+
}
41+
};
42+
43+
$factory = new ThothAbstractFactory();
44+
$thothAbstracts = $factory->createFromPublication($publication, 'work-id', 'en_US');
45+
46+
$this->assertSame('<p>English abstract</p>', $thothAbstracts['EN_US']->getContent());
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use PKP\tests\PKPTestCase;
4+
5+
import('plugins.generic.thoth.classes.factories.ThothBiographyFactory');
6+
7+
class ThothBiographyFactoryTest extends PKPTestCase
8+
{
9+
public function testCreateFromAuthorWrapsBiographyWithoutParagraph()
10+
{
11+
$author = new class () {
12+
public function getData($key)
13+
{
14+
$values = [
15+
'locale' => 'en_US',
16+
'biography' => ['en_US' => 'English biography'],
17+
];
18+
19+
return $values[$key] ?? null;
20+
}
21+
};
22+
23+
$factory = new ThothBiographyFactory();
24+
$thothBiographies = $factory->createFromAuthor($author, 'contribution-id', 'en_US');
25+
26+
$this->assertSame('<p>English biography</p>', $thothBiographies['EN_US']->getContent());
27+
}
28+
29+
public function testCreateFromAuthorPreservesBiographyAlreadyWrappedInParagraph()
30+
{
31+
$author = new class () {
32+
public function getData($key)
33+
{
34+
$values = [
35+
'locale' => 'en_US',
36+
'biography' => ['en_US' => '<p>English biography</p>'],
37+
];
38+
39+
return $values[$key] ?? null;
40+
}
41+
};
42+
43+
$factory = new ThothBiographyFactory();
44+
$thothBiographies = $factory->createFromAuthor($author, 'contribution-id', 'en_US');
45+
46+
$this->assertSame('<p>English biography</p>', $thothBiographies['EN_US']->getContent());
47+
}
48+
}

0 commit comments

Comments
 (0)