Skip to content

Commit d477d0f

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

6 files changed

Lines changed: 136 additions & 3 deletions

File tree

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
@@ -66,7 +66,7 @@ public function getWorkTypeBySubmissionWorkType($submissionWorkType)
6666
WORK_TYPE_AUTHORED_WORK => ThothWork::WORK_TYPE_MONOGRAPH
6767
];
6868

69-
return $workTypeMapping[$submissionWorkType];
69+
return $workTypeMapping[$submissionWorkType] ?? ThothWork::WORK_TYPE_MONOGRAPH;
7070
}
7171

7272
public function getWorkStatusByDatePublished($datePublished)

classes/notification/ThothNotification.inc.php

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

2323
public function notifyError($request, $submission, $error)
2424
{
25+
$error = $this->normalizeError($error);
2526
error_log("Failed to send the request to Thoth: {$error}");
2627
$this->notify(
2728
$request,
@@ -47,6 +48,7 @@ public function notify($request, $submission, $notificationType, $messageKey, $e
4748

4849
public function logInfo($request, $submission, $messageKey, $error = null)
4950
{
51+
$error = $this->normalizeError($error);
5052
import('lib.pkp.classes.log.SubmissionLog');
5153
import('classes.log.SubmissionEventLogEntry');
5254
SubmissionLog::logEvent(
@@ -58,6 +60,23 @@ public function logInfo($request, $submission, $messageKey, $error = null)
5860
);
5961
}
6062

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

0 commit comments

Comments
 (0)