From f3af7bab50499e9f0a0f402adb59598ca0373735 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 6 Apr 2020 21:11:19 +0200 Subject: [PATCH] Fixed Schedule XML parser to support minimum import --- src/Helpers/Schedule/XmlParser.php | 14 ++++-- .../Schedule/Assets/schedule-basic.xml | 47 +++++++++++++++++++ .../{schedule.xml => schedule-extended.xml} | 8 +++- .../Schedule/Assets/schedule-minimal.xml | 27 +++++++++++ tests/Unit/Helpers/Schedule/XmlParserTest.php | 10 +++- 5 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 tests/Unit/Helpers/Schedule/Assets/schedule-basic.xml rename tests/Unit/Helpers/Schedule/Assets/{schedule.xml => schedule-extended.xml} (84%) create mode 100644 tests/Unit/Helpers/Schedule/Assets/schedule-minimal.xml diff --git a/src/Helpers/Schedule/XmlParser.php b/src/Helpers/Schedule/XmlParser.php index 1492aaca..4255be8b 100644 --- a/src/Helpers/Schedule/XmlParser.php +++ b/src/Helpers/Schedule/XmlParser.php @@ -34,6 +34,8 @@ class XmlParser /** * Parse the predefined XML content + * + * According to https://github.com/voc/voctosched/blob/master/schema/basic.xsd */ protected function parseXml(): void { @@ -93,9 +95,9 @@ class XmlParser $attachments = $this->getListFromSequence($event, 'attachments', 'attachment', 'href'); $recording = ''; - $recordingElement = $event->xpath('recording')[0]; - if ($this->getFirstXpathContent('optout', $recordingElement) == 'false') { - $recording = $this->getFirstXpathContent('license', $recordingElement); + $recordingElement = $event->xpath('recording'); + if ($recordingElement && $this->getFirstXpathContent('optout', $recordingElement[0]) == 'false') { + $recording = $this->getFirstXpathContent('license', $recordingElement[0]); } $events[] = new Event( @@ -155,8 +157,10 @@ class XmlParser ): array { $items = []; - foreach ($element->xpath($firstElement)[0]->xpath($secondElement) as $item) { - $items[(string)$item->attributes()[$idAttribute]] = (string)$item; + foreach ($element->xpath($firstElement) as $element) { + foreach ($element->xpath($secondElement) as $item) { + $items[(string)$item->attributes()[$idAttribute]] = (string)$item; + } } return $items; diff --git a/tests/Unit/Helpers/Schedule/Assets/schedule-basic.xml b/tests/Unit/Helpers/Schedule/Assets/schedule-basic.xml new file mode 100644 index 00000000..6aa8c69d --- /dev/null +++ b/tests/Unit/Helpers/Schedule/Assets/schedule-basic.xml @@ -0,0 +1,47 @@ + + + dolor + + Basic Test Event + Test2 + 2042-01-03 + 2042-01-03 + 1 + 00:30 + + + + + 2042-01-03T14:15:00+02:00 + Lorem Ipsum + Some sub + 14:15 + 00:45 + Some Room + lorem/ipsum + + WTFPL + true + + Tests + Talk + en + 45 minutes of awesome lorem ipsum + Be prepared for the best lorem ipsum of the entire day! + https://lorem.ipsum/foo/bar.png + + Some Person + + + Some Foo Bar + + + A PDF File + + + + + diff --git a/tests/Unit/Helpers/Schedule/Assets/schedule.xml b/tests/Unit/Helpers/Schedule/Assets/schedule-extended.xml similarity index 84% rename from tests/Unit/Helpers/Schedule/Assets/schedule.xml rename to tests/Unit/Helpers/Schedule/Assets/schedule-extended.xml index 45aaaf35..adb9fa6c 100644 --- a/tests/Unit/Helpers/Schedule/Assets/schedule.xml +++ b/tests/Unit/Helpers/Schedule/Assets/schedule-extended.xml @@ -1,9 +1,12 @@ - + Some version string Test Event - Test1 + Test3 2042-01-01 2042-01-01 1 @@ -24,6 +27,7 @@ WTFPL false + https://foo.bar/baz/schedule/ipsum/recording.mp4 Testing Talk de diff --git a/tests/Unit/Helpers/Schedule/Assets/schedule-minimal.xml b/tests/Unit/Helpers/Schedule/Assets/schedule-minimal.xml new file mode 100644 index 00000000..22c93138 --- /dev/null +++ b/tests/Unit/Helpers/Schedule/Assets/schedule-minimal.xml @@ -0,0 +1,27 @@ + + 0.0.1 + + Some Test Event + Test1 + + + + + 2042-01-02T13:30:00+00:00 + Minimum Setup Test + With a subtitle + 13:30 + 00:30 + Rooming + minimum-setup-test + Testing + Talk + A minimal description + + + + + + + + diff --git a/tests/Unit/Helpers/Schedule/XmlParserTest.php b/tests/Unit/Helpers/Schedule/XmlParserTest.php index 023e9c7d..729b3862 100644 --- a/tests/Unit/Helpers/Schedule/XmlParserTest.php +++ b/tests/Unit/Helpers/Schedule/XmlParserTest.php @@ -24,8 +24,15 @@ class XmlParserTest extends TestCase libxml_use_internal_errors(true); $parser = new XmlParser(); + + // Invalid XML $this->assertFalse($parser->load('foo')); - $this->assertTrue($parser->load(file_get_contents(__DIR__ . '/Assets/schedule.xml'))); + // Minimal import + $this->assertTrue($parser->load(file_get_contents(__DIR__ . '/Assets/schedule-minimal.xml'))); + // Basic import + $this->assertTrue($parser->load(file_get_contents(__DIR__ . '/Assets/schedule-basic.xml'))); + // Extended import + $this->assertTrue($parser->load(file_get_contents(__DIR__ . '/Assets/schedule-extended.xml'))); $schedule = $parser->getSchedule(); $this->assertEquals('Some version string', $schedule->getVersion()); @@ -50,5 +57,6 @@ class XmlParserTest extends TestCase $this->assertEquals('de', $event->getLanguage()); $this->assertEquals('12:30', $event->getStart()); $this->assertEquals([1234 => 'Some Person'], $event->getPersons()); + $this->assertEquals('https://foo.bar/baz/schedule/ipsum/recording.mp4', $event->getVideoDownloadUrl()); } }