From 99f5ecb94971240c3ff114d4520d38582c5c0450 Mon Sep 17 00:00:00 2001
From: Michelle Weidling <weidling@sub.uni-goettingen.de>
Date: Mon, 31 Aug 2020 12:32:47 +0200
Subject: [PATCH] test(textapi): always return a value for x-editor

#54
---
 exist-app/modules/tapi.xqm | 17 ++++++++++++-----
 exist-app/tests.xqm        |  8 ++++++++
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/exist-app/modules/tapi.xqm b/exist-app/modules/tapi.xqm
index 1ebafdb7..836ac8a4 100644
--- a/exist-app/modules/tapi.xqm
+++ b/exist-app/modules/tapi.xqm
@@ -247,12 +247,19 @@ as element(object) {
  :)
 declare function tapi:make-editors($documentNode as document-node()) as element(x-editor)* {
     let $role := "editor"
-    for $editor in $documentNode//tei:titleStmt//tei:editor
+    let $has-editor := exists($documentNode//tei:titleStmt//tei:editor)
     return
-        <x-editor>
-            <role>{$role}</role>
-            <name>{$editor/string()}</name>
-        </x-editor>
+        if ($has-editor) then
+            for $editor in $documentNode//tei:titleStmt//tei:editor
+            return
+                <x-editor>
+                    <role>{$role}</role>
+                    <name>{$editor/string()}</name>
+                </x-editor>
+        else
+            <x-editor>
+                <name>none</name>
+            </x-editor>
 };
 
 (:~
diff --git a/exist-app/tests.xqm b/exist-app/tests.xqm
index c3fcea38..ec5e58c6 100644
--- a/exist-app/tests.xqm
+++ b/exist-app/tests.xqm
@@ -302,6 +302,14 @@ function tests:make-editors() as element()+ {
         tapi:make-editors($documentNode)
 };
 
+declare
+    %test:assertXPath("$result[local-name(.) = 'x-editor' and name/text() = 'none']")
+function tests:make-editors-fail-gracefully() as element()+ {
+    let $documentNode := doc("/db/test-records/sample-tei.xml")
+    return
+        tapi:make-editors($documentNode)
+};
+
 
 declare
     %test:assertXPath("$result[local-name(.) = 'x-date'][text() = '18.10.1697']")
-- 
GitLab