From cdd9f4dfbeb26faa4f077d4fb091a0219e36e764 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Thu, 2 May 2019 21:54:49 -0400 Subject: [PATCH] More OPML parser tests --- lib/ImportExport/OPML.php | 4 ++-- tests/cases/ImportExport/TestOPML.php | 6 +++++- tests/docroot/Import/OPML/BrokenOPML.1.opml | 1 + tests/docroot/Import/OPML/BrokenOPML.2.opml | 1 + tests/docroot/Import/OPML/BrokenOPML.3.opml | 1 + tests/docroot/Import/OPML/BrokenOPML.4.opml | 5 +++++ tests/docroot/Import/OPML/BrokenXML.opml | 1 + tests/docroot/Import/OPML/Empty.1.opml | 4 ++++ tests/docroot/Import/OPML/Empty.2.opml | 9 +++++++++ tests/docroot/Import/OPML/Empty.3.opml | 11 +++++++++++ 10 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/docroot/Import/OPML/BrokenOPML.4.opml create mode 100644 tests/docroot/Import/OPML/Empty.1.opml create mode 100644 tests/docroot/Import/OPML/Empty.2.opml create mode 100644 tests/docroot/Import/OPML/Empty.3.opml diff --git a/lib/ImportExport/OPML.php b/lib/ImportExport/OPML.php index b8711bdd..ca02f2d4 100644 --- a/lib/ImportExport/OPML.php +++ b/lib/ImportExport/OPML.php @@ -145,8 +145,8 @@ class OPML { $err = libxml_get_last_error(); throw new Exception("invalidSyntax", ['line' => $err->line, 'column' => $err->column]); } - $body = $d->getElementsByTagName("body"); - if ($d->documentElement->nodeName !== "opml" || !$body->length || !$body->item(0)->parentNode->isSameNode($d->documentElement)) { + $body = (new \DOMXPath($d))->query("/opml/body"); + if ($body->length != 1) { // not a valid OPML document throw new Exception("invalidSemantics", ['type' => "OPML"]); } diff --git a/tests/cases/ImportExport/TestOPML.php b/tests/cases/ImportExport/TestOPML.php index a17cdaff..33904447 100644 --- a/tests/cases/ImportExport/TestOPML.php +++ b/tests/cases/ImportExport/TestOPML.php @@ -114,7 +114,7 @@ OPML_EXPORT_SERIALIZATION; $this->assertException($exp); $parser->parse($data, $flat); } else { - $this->assertSame($exp, $parse->parse($data, $flat)); + $this->assertSame($exp, $parser->parse($data, $flat)); } } @@ -124,6 +124,10 @@ OPML_EXPORT_SERIALIZATION; ["BrokenOPML.1.opml", false, new Exception("invalidSemantics")], ["BrokenOPML.2.opml", false, new Exception("invalidSemantics")], ["BrokenOPML.3.opml", false, new Exception("invalidSemantics")], + ["BrokenOPML.4.opml", false, new Exception("invalidSemantics")], + ["Empty.1.opml", false, [[], []]], + ["Empty.2.opml", false, [[], []]], + ["Empty.3.opml", false, [[], []]], ]; } } diff --git a/tests/docroot/Import/OPML/BrokenOPML.1.opml b/tests/docroot/Import/OPML/BrokenOPML.1.opml index 1f551eab..a626ae06 100644 --- a/tests/docroot/Import/OPML/BrokenOPML.1.opml +++ b/tests/docroot/Import/OPML/BrokenOPML.1.opml @@ -1 +1,2 @@ + diff --git a/tests/docroot/Import/OPML/BrokenOPML.2.opml b/tests/docroot/Import/OPML/BrokenOPML.2.opml index a6c08015..ac70153f 100644 --- a/tests/docroot/Import/OPML/BrokenOPML.2.opml +++ b/tests/docroot/Import/OPML/BrokenOPML.2.opml @@ -1 +1,2 @@ + diff --git a/tests/docroot/Import/OPML/BrokenOPML.3.opml b/tests/docroot/Import/OPML/BrokenOPML.3.opml index 466ca0ca..b087a1b6 100644 --- a/tests/docroot/Import/OPML/BrokenOPML.3.opml +++ b/tests/docroot/Import/OPML/BrokenOPML.3.opml @@ -3,3 +3,4 @@ + diff --git a/tests/docroot/Import/OPML/BrokenOPML.4.opml b/tests/docroot/Import/OPML/BrokenOPML.4.opml new file mode 100644 index 00000000..544e4c36 --- /dev/null +++ b/tests/docroot/Import/OPML/BrokenOPML.4.opml @@ -0,0 +1,5 @@ + + + + + diff --git a/tests/docroot/Import/OPML/BrokenXML.opml b/tests/docroot/Import/OPML/BrokenXML.opml index 95028ac2..0cbc6fe2 100644 --- a/tests/docroot/Import/OPML/BrokenXML.opml +++ b/tests/docroot/Import/OPML/BrokenXML.opml @@ -1 +1,2 @@ + diff --git a/tests/docroot/Import/OPML/Empty.1.opml b/tests/docroot/Import/OPML/Empty.1.opml new file mode 100644 index 00000000..4999faaf --- /dev/null +++ b/tests/docroot/Import/OPML/Empty.1.opml @@ -0,0 +1,4 @@ + + + + diff --git a/tests/docroot/Import/OPML/Empty.2.opml b/tests/docroot/Import/OPML/Empty.2.opml new file mode 100644 index 00000000..6dcd03f2 --- /dev/null +++ b/tests/docroot/Import/OPML/Empty.2.opml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/tests/docroot/Import/OPML/Empty.3.opml b/tests/docroot/Import/OPML/Empty.3.opml new file mode 100644 index 00000000..59fd9b46 --- /dev/null +++ b/tests/docroot/Import/OPML/Empty.3.opml @@ -0,0 +1,11 @@ + + + + + + + + + + +