Skip to content

Commit 7ab7427

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

File tree

6 files changed

+140
-3
lines changed

6 files changed

+140
-3
lines changed

classes/factories/ThothAbstractFactory.php

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

classes/factories/ThothBiographyFactory.php

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

classes/factories/ThothBookFactory.php

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

77-
return $workTypeMapping[$submissionWorkType];
77+
return $workTypeMapping[$submissionWorkType] ?? ThothWork::WORK_TYPE_MONOGRAPH;
7878
}
7979

8080
public function getWorkStatusByDatePublished($datePublished)

classes/notification/ThothNotification.php

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

3838
public function notifyError($request, $submission, $error)
3939
{
40+
$error = $this->normalizeError($error);
4041
error_log("Failed to send the request to Thoth: {$error}");
4142
$this->notify(
4243
$request,
@@ -62,6 +63,7 @@ public function notify($request, $submission, $notificationType, $messageKey, $e
6263

6364
public function logInfo($request, $submission, $messageKey, $error = null)
6465
{
66+
$error = $this->normalizeError($error);
6567
$currentUser = $request->getUser();
6668
$eventLog = Repo::eventLog()->newDataObject([
6769
'assocType' => Application::ASSOC_TYPE_SUBMISSION,
@@ -76,6 +78,23 @@ public function logInfo($request, $submission, $messageKey, $error = null)
7678
Repo::eventLog()->add($eventLog);
7779
}
7880

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

0 commit comments

Comments
 (0)