From 0bc1e869e4bb550ed9f1a25e360975db5922ed40 Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Wed, 25 May 2022 17:11:35 +0200 Subject: [PATCH 01/13] feat: TR-4179 new html5 elements for QTI SDK --- .../data/content/xhtml/html5/Figcaption.php | 87 +++++++++++++ src/qtism/data/content/xhtml/html5/Figure.php | 87 +++++++++++++ .../marshalling/Html5FigcaptionMarshaller.php | 73 +++++++++++ .../xml/marshalling/Html5FigureMarshaller.php | 73 +++++++++++ .../content/xhtml/html5/FigcaptionTest.php | 55 ++++++++ .../data/content/xhtml/html5/FigureTest.php | 55 ++++++++ .../Html5FigcaptionMarshallerTest.php | 121 ++++++++++++++++++ .../marshalling/Html5FigureMarshallerTest.php | 121 ++++++++++++++++++ .../samples/custom/items/2_2/figure_html5.xml | 32 +++++ 9 files changed, 704 insertions(+) create mode 100644 src/qtism/data/content/xhtml/html5/Figcaption.php create mode 100644 src/qtism/data/content/xhtml/html5/Figure.php create mode 100644 src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php create mode 100644 src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php create mode 100644 test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php create mode 100644 test/qtismtest/data/content/xhtml/html5/FigureTest.php create mode 100644 test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php create mode 100644 test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php create mode 100644 test/samples/custom/items/2_2/figure_html5.xml diff --git a/src/qtism/data/content/xhtml/html5/Figcaption.php b/src/qtism/data/content/xhtml/html5/Figcaption.php new file mode 100644 index 000000000..61579c7a2 --- /dev/null +++ b/src/qtism/data/content/xhtml/html5/Figcaption.php @@ -0,0 +1,87 @@ +setContent(new InlineCollection()); + } + + public function getQtiClassName() + { + return self::QTI_CLASS_NAME; + } + + public function getComponents() + { + return $this->getContent(); + } + + /** + * Set the collection of Flow objects composing the Div. + * + * @param InlineCollection $content A collection of Flow objects. + */ + public function setContent(InlineCollection $content) + { + $this->content = $content; + } + + /** + * Get the collection of Flow objects composing the Div. + * + * @return InlineCollection + */ + public function getContent() + { + return $this->content; + } +} \ No newline at end of file diff --git a/src/qtism/data/content/xhtml/html5/Figure.php b/src/qtism/data/content/xhtml/html5/Figure.php new file mode 100644 index 000000000..944ef9ba5 --- /dev/null +++ b/src/qtism/data/content/xhtml/html5/Figure.php @@ -0,0 +1,87 @@ +setContent(new FlowCollection()); + } + + public function getQtiClassName() + { + return self::QTI_CLASS_NAME; + } + + public function getComponents() + { + return $this->getContent(); + } + + /** + * Set the collection of Flow objects composing the Div. + * + * @param FlowCollection $content A collection of Flow objects. + */ + public function setContent(FlowCollection $content) + { + $this->content = $content; + } + + /** + * Get the collection of Flow objects composing the Div. + * + * @return FlowCollection + */ + public function getContent() + { + return $this->content; + } +} \ No newline at end of file diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php new file mode 100644 index 000000000..ce4846a31 --- /dev/null +++ b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php @@ -0,0 +1,73 @@ +hasId()) { + $this->setDOMElementAttribute($element, 'id', $component->getId()); + } + + if ($component->hasClass()) { + $this->setDOMElementAttribute($element, 'class', $component->getClass()); + } + + return $element; + } + + /** + * @return Figcaption + * @throws UnmarshallingException + */ + protected function unmarshall(DOMElement $element) + { + try { + $id = $this->getDOMElementAttributeAs($element, 'id'); + $class = $this->getDOMElementAttributeAs($element, 'class'); + + $component = new Figcaption($id, $class); + } catch (InvalidArgumentException $exception) { + throw UnmarshallingException::createFromInvalidArgumentException($element, $exception); + } + + $this->fillBodyElement($component, $element); + + return $component; + } + + public function getExpectedQtiClassName() + { + return Version::compare($this->getVersion(), '2.2', '>=') ? Figcaption::QTI_CLASS_NAME : 'not_existing'; + } +} \ No newline at end of file diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php new file mode 100644 index 000000000..574c1881f --- /dev/null +++ b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php @@ -0,0 +1,73 @@ +hasId()) { + $this->setDOMElementAttribute($element, 'id', $component->getId()); + } + + if ($component->hasClass()) { + $this->setDOMElementAttribute($element, 'class', $component->getClass()); + } + + return $element; + } + + /** + * @return Figure + * @throws UnmarshallingException + */ + protected function unmarshall(DOMElement $element) + { + try { + $id = $this->getDOMElementAttributeAs($element, 'id'); + $class = $this->getDOMElementAttributeAs($element, 'class'); + + $component = new Figure($id, $class); + } catch (InvalidArgumentException $exception) { + throw UnmarshallingException::createFromInvalidArgumentException($element, $exception); + } + + $this->fillBodyElement($component, $element); + + return $component; + } + + public function getExpectedQtiClassName() + { + return Version::compare($this->getVersion(), '2.2', '>=') ? Figure::QTI_CLASS_NAME : 'not_existing'; + } +} \ No newline at end of file diff --git a/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php b/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php new file mode 100644 index 000000000..aadc1284f --- /dev/null +++ b/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php @@ -0,0 +1,55 @@ +getId()); + self::assertEquals($class, $subject->getClass()); + } + + public function testCreateWithDefaultValues(): void + { + $subject = new Figcaption(); + + self::assertEquals('', $subject->getId()); + self::assertEquals('', $subject->getClass()); + } + + public function testGetQtiClassName(): void + { + $subject = new Figcaption(); + + self::assertEquals(Figcaption::QTI_CLASS_NAME, $subject->getQtiClassName()); + } +} \ No newline at end of file diff --git a/test/qtismtest/data/content/xhtml/html5/FigureTest.php b/test/qtismtest/data/content/xhtml/html5/FigureTest.php new file mode 100644 index 000000000..74df9ed38 --- /dev/null +++ b/test/qtismtest/data/content/xhtml/html5/FigureTest.php @@ -0,0 +1,55 @@ +getId()); + self::assertEquals($class, $subject->getClass()); + } + + public function testCreateWithDefaultValues(): void + { + $subject = new Figure(); + + self::assertEquals('', $subject->getId()); + self::assertEquals('', $subject->getClass()); + } + + public function testGetQtiClassName(): void + { + $subject = new Figure(); + + self::assertEquals(Figure::QTI_CLASS_NAME, $subject->getQtiClassName()); + } +} \ No newline at end of file diff --git a/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php b/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php new file mode 100644 index 000000000..47fd84583 --- /dev/null +++ b/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php @@ -0,0 +1,121 @@ +assertHtml5MarshallingOnlyInQti22AndAbove(new Figcaption(), Figcaption::QTI_CLASS_NAME); + } + + /** + * @throws MarshallerNotFoundException + * @throws MarshallingException + */ + public function testMarshall22(): void + { + $id = 'id'; + $class = 'testclass'; + + $expected = sprintf( + '<%1$s id="%2$s " class="%3$s">text content', + $this->namespaceTag(Figcaption::QTI_CLASS_NAME), + $id, + $class, + ); + + $object = new Figcaption($id, $class); + + $this->assertMarshalling($expected, $object); + } + + /** + * @throws MarshallerNotFoundException + * @throws MarshallingException + */ + public function testMarshall22WithDefaultValues(): void + { + $expected = sprintf('<%1$s>text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME)); + + $video = new Figcaption(); + + $this->assertMarshalling($expected, $video); + } + + /** + * @throws MarshallerNotFoundException + */ + public function testUnMarshallerDoesNotExistInQti21(): void + { + $this->assertHtml5UnmarshallingOnlyInQti22AndAbove( + sprintf('<%1$s>', $this->namespaceTag(Figcaption::QTI_CLASS_NAME)), + Figcaption::QTI_CLASS_NAME + ); + } + + /** + * @throws MarshallerNotFoundException + */ + public function testUnmarshall22(): void + { + $id = 'id'; + $class = 'testclass'; + + $xml = sprintf( + '<%1$s id="%2$s " class="%3$s">text content', + $this->namespaceTag(Figcaption::QTI_CLASS_NAME), + $id, + $class, + ); + + $expected = new Figcaption($id, $class); + + $this->assertUnmarshalling($expected, $xml); + } + + public function testUnmarshall22WithDefaultValues(): void + { + $xml = sprintf('<%1$s>text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME)); + + $expected = new Figcaption(); + + $this->assertUnmarshalling($expected, $xml); + } + + /** + * @throws MarshallerNotFoundException + */ + public function testUnmarshallWithWrongTypesOrValues(string $xml, string $exception, string $message): void + { + $this->assertUnmarshallingException($xml, $exception, $message); + } +} diff --git a/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php b/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php new file mode 100644 index 000000000..664e8074e --- /dev/null +++ b/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php @@ -0,0 +1,121 @@ +assertHtml5MarshallingOnlyInQti22AndAbove(new Figure(), Figure::QTI_CLASS_NAME); + } + + /** + * @throws MarshallerNotFoundException + * @throws MarshallingException + */ + public function testMarshall22(): void + { + $id = 'id'; + $class = 'testclass'; + + $expected = sprintf( + '<%1$s id="%2$s " class="%3$s">', + $this->namespaceTag(Figure::QTI_CLASS_NAME), + $id, + $class, + ); + + $object = new Figure($id, $class); + + $this->assertMarshalling($expected, $object); + } + + /** + * @throws MarshallerNotFoundException + * @throws MarshallingException + */ + public function testMarshall22WithDefaultValues(): void + { + $expected = sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME)); + + $video = new Figure(); + + $this->assertMarshalling($expected, $video); + } + + /** + * @throws MarshallerNotFoundException + */ + public function testUnMarshallerDoesNotExistInQti21(): void + { + $this->assertHtml5UnmarshallingOnlyInQti22AndAbove( + sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME)), + Figure::QTI_CLASS_NAME + ); + } + + /** + * @throws MarshallerNotFoundException + */ + public function testUnmarshall22(): void + { + $id = 'id'; + $class = 'testclass'; + + $xml = sprintf( + '<%1$s id="%2$s " class="%3$s">', + $this->namespaceTag(Figure::QTI_CLASS_NAME), + $id, + $class, + ); + + $expected = new Figure($id, $class); + + $this->assertUnmarshalling($expected, $xml); + } + + public function testUnmarshall22WithDefaultValues(): void + { + $xml = sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME)); + + $expected = new Figure(); + + $this->assertUnmarshalling($expected, $xml); + } + + /** + * @throws MarshallerNotFoundException + */ + public function testUnmarshallWithWrongTypesOrValues(string $xml, string $exception, string $message): void + { + $this->assertUnmarshallingException($xml, $exception, $message); + } +} diff --git a/test/samples/custom/items/2_2/figure_html5.xml b/test/samples/custom/items/2_2/figure_html5.xml new file mode 100644 index 000000000..90455c55d --- /dev/null +++ b/test/samples/custom/items/2_2/figure_html5.xml @@ -0,0 +1,32 @@ + + + + + + + 0 + + + +
+
+ + AWS.jpg + A sample t-shirt size table. + +

 

+

 

+
+
+
+
+ + choice #1 + choice #2 + choice #3 + +
+
+
+ +
From 8e9a2a800f629f60958caa491f5d5e46296fcda1 Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Wed, 25 May 2022 17:18:41 +0200 Subject: [PATCH 02/13] fix: add empty lines to end of files --- src/qtism/data/content/xhtml/html5/Figcaption.php | 2 +- src/qtism/data/content/xhtml/html5/Figure.php | 2 +- .../data/storage/xml/marshalling/Html5FigcaptionMarshaller.php | 2 +- .../data/storage/xml/marshalling/Html5FigureMarshaller.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/qtism/data/content/xhtml/html5/Figcaption.php b/src/qtism/data/content/xhtml/html5/Figcaption.php index 61579c7a2..de88fb4c1 100644 --- a/src/qtism/data/content/xhtml/html5/Figcaption.php +++ b/src/qtism/data/content/xhtml/html5/Figcaption.php @@ -84,4 +84,4 @@ public function getContent() { return $this->content; } -} \ No newline at end of file +} diff --git a/src/qtism/data/content/xhtml/html5/Figure.php b/src/qtism/data/content/xhtml/html5/Figure.php index 944ef9ba5..19d3a9be8 100644 --- a/src/qtism/data/content/xhtml/html5/Figure.php +++ b/src/qtism/data/content/xhtml/html5/Figure.php @@ -84,4 +84,4 @@ public function getContent() { return $this->content; } -} \ No newline at end of file +} diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php index ce4846a31..c3e1b0402 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php @@ -70,4 +70,4 @@ public function getExpectedQtiClassName() { return Version::compare($this->getVersion(), '2.2', '>=') ? Figcaption::QTI_CLASS_NAME : 'not_existing'; } -} \ No newline at end of file +} diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php index 574c1881f..075e072fe 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php @@ -70,4 +70,4 @@ public function getExpectedQtiClassName() { return Version::compare($this->getVersion(), '2.2', '>=') ? Figure::QTI_CLASS_NAME : 'not_existing'; } -} \ No newline at end of file +} From 03441467e648ae861285bac0fe8c7360b24d7077 Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Wed, 25 May 2022 17:19:11 +0200 Subject: [PATCH 03/13] tests: add empty lines to end of files --- test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php | 2 +- test/qtismtest/data/content/xhtml/html5/FigureTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php b/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php index aadc1284f..a3141cf40 100644 --- a/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php +++ b/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php @@ -52,4 +52,4 @@ public function testGetQtiClassName(): void self::assertEquals(Figcaption::QTI_CLASS_NAME, $subject->getQtiClassName()); } -} \ No newline at end of file +} diff --git a/test/qtismtest/data/content/xhtml/html5/FigureTest.php b/test/qtismtest/data/content/xhtml/html5/FigureTest.php index 74df9ed38..cb69aac66 100644 --- a/test/qtismtest/data/content/xhtml/html5/FigureTest.php +++ b/test/qtismtest/data/content/xhtml/html5/FigureTest.php @@ -52,4 +52,4 @@ public function testGetQtiClassName(): void self::assertEquals(Figure::QTI_CLASS_NAME, $subject->getQtiClassName()); } -} \ No newline at end of file +} From 7ad3816e9500b315d3f014be993022f6f5c0d79f Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Wed, 25 May 2022 17:37:35 +0200 Subject: [PATCH 04/13] feat: TR-4179 added renderer classes --- .../xml/marshalling/ContentMarshaller.php | 2 + .../marshalling/Qti22MarshallerFactory.php | 2 + .../markup/xhtml/FigcaptionRenderer.php | 43 +++++++++++++++++++ .../rendering/markup/xhtml/FigureRenderer.php | 43 +++++++++++++++++++ .../markup/xhtml/XhtmlRenderingEngine.php | 4 ++ 5 files changed, 94 insertions(+) create mode 100644 src/qtism/runtime/rendering/markup/xhtml/FigcaptionRenderer.php create mode 100644 src/qtism/runtime/rendering/markup/xhtml/FigureRenderer.php diff --git a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php index 5d93e54e3..95f7ac27e 100644 --- a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php @@ -124,6 +124,8 @@ public function __construct($version) 'tfoot', 'thead', 'uploadInteraction', + 'figure', + 'figcaption' ]; private static $simpleComposites = [ diff --git a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php index f7fb7e541..93f97462d 100644 --- a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php +++ b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php @@ -41,6 +41,8 @@ public function __construct() { parent::__construct(); $this->addMappingEntry('bdo', SimpleInlineMarshaller::class); + $this->addMappingEntry('figure', Html5FigureMarshaller::class, self::HTML5_NAMESPACE); + $this->addMappingEntry('figcaption', Html5FigcaptionMarshaller::class, self::HTML5_NAMESPACE); } /** diff --git a/src/qtism/runtime/rendering/markup/xhtml/FigcaptionRenderer.php b/src/qtism/runtime/rendering/markup/xhtml/FigcaptionRenderer.php new file mode 100644 index 000000000..ec0585949 --- /dev/null +++ b/src/qtism/runtime/rendering/markup/xhtml/FigcaptionRenderer.php @@ -0,0 +1,43 @@ +hasId()) { + $fragment->firstChild->setAttribute('id', $component->getId()); + } + if ($component->hasClass()) { + $fragment->firstChild->setAttribute('class', $component->getClass()); + } + } +} diff --git a/src/qtism/runtime/rendering/markup/xhtml/FigureRenderer.php b/src/qtism/runtime/rendering/markup/xhtml/FigureRenderer.php new file mode 100644 index 000000000..862edce3f --- /dev/null +++ b/src/qtism/runtime/rendering/markup/xhtml/FigureRenderer.php @@ -0,0 +1,43 @@ +hasId()) { + $fragment->firstChild->setAttribute('id', $component->getId()); + } + if ($component->hasClass()) { + $fragment->firstChild->setAttribute('class', $component->getClass()); + } + } +} diff --git a/src/qtism/runtime/rendering/markup/xhtml/XhtmlRenderingEngine.php b/src/qtism/runtime/rendering/markup/xhtml/XhtmlRenderingEngine.php index 22ad527ec..a644292e1 100644 --- a/src/qtism/runtime/rendering/markup/xhtml/XhtmlRenderingEngine.php +++ b/src/qtism/runtime/rendering/markup/xhtml/XhtmlRenderingEngine.php @@ -23,6 +23,8 @@ namespace qtism\runtime\rendering\markup\xhtml; +use qtism\data\content\xhtml\html5\Figcaption; +use qtism\data\content\xhtml\html5\Figure; use qtism\runtime\rendering\markup\AbstractMarkupRenderingEngine; /** @@ -119,6 +121,8 @@ public function __construct() $this->registerRenderer('extendedTextInteraction', new ExtendedTextInteractionRenderer()); $this->registerRenderer('feedbackBlock', new FeedbackBlockRenderer()); $this->registerRenderer('feedbackInline', new FeedbackInlineRenderer()); + $this->registerRenderer(Figure::QTI_CLASS_NAME, new FigureRenderer()); + $this->registerRenderer(Figcaption::QTI_CLASS_NAME, new FigcaptionRenderer()); $this->registerRenderer('gap', new GapRenderer()); $this->registerRenderer('gapImg', new GapImgRenderer()); $this->registerRenderer('gapMatchInteraction', new GapMatchInteractionRenderer()); From ffa1fb79da41e9bbe8dd284975d18a194540abbd Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Wed, 25 May 2022 17:42:08 +0200 Subject: [PATCH 05/13] fix: use constant where it possible; change component placement to simples --- .../data/storage/xml/marshalling/ContentMarshaller.php | 6 ++++-- .../data/storage/xml/marshalling/Qti22MarshallerFactory.php | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php index 95f7ac27e..8eac54725 100644 --- a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php @@ -52,6 +52,8 @@ use qtism\data\content\SimpleInline; use qtism\data\content\TemplateBlock; use qtism\data\content\TemplateInline; +use qtism\data\content\xhtml\html5\Figcaption; +use qtism\data\content\xhtml\html5\Figure; use qtism\data\content\xhtml\lists\Dl; use qtism\data\content\xhtml\lists\DlElement; use qtism\data\content\xhtml\lists\Li; @@ -124,8 +126,6 @@ public function __construct($version) 'tfoot', 'thead', 'uploadInteraction', - 'figure', - 'figcaption' ]; private static $simpleComposites = [ @@ -179,6 +179,8 @@ public function __construct($version) 'modalFeedback', 'feedbackBlock', 'bdo', + Figure::QTI_CLASS_NAME, + Figcaption::QTI_CLASS_NAME ]; /** diff --git a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php index 93f97462d..10a8f4578 100644 --- a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php +++ b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php @@ -24,6 +24,8 @@ namespace qtism\data\storage\xml\marshalling; use qtism\common\utils\Reflection; +use qtism\data\content\xhtml\html5\Figcaption; +use qtism\data\content\xhtml\html5\Figure; use ReflectionClass; /** @@ -41,8 +43,8 @@ public function __construct() { parent::__construct(); $this->addMappingEntry('bdo', SimpleInlineMarshaller::class); - $this->addMappingEntry('figure', Html5FigureMarshaller::class, self::HTML5_NAMESPACE); - $this->addMappingEntry('figcaption', Html5FigcaptionMarshaller::class, self::HTML5_NAMESPACE); + $this->addMappingEntry(Figure::QTI_CLASS_NAME, Html5FigureMarshaller::class, self::HTML5_NAMESPACE); + $this->addMappingEntry(Figcaption::QTI_CLASS_NAME, Html5FigcaptionMarshaller::class, self::HTML5_NAMESPACE); } /** From 6b8e6ab23e61878e76ace30d1653f554f68b380f Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Thu, 26 May 2022 23:36:13 +0200 Subject: [PATCH 06/13] fix: small partly fix attribute orders --- src/qtism/data/content/xhtml/html5/Figcaption.php | 15 +++++---------- src/qtism/data/content/xhtml/html5/Figure.php | 15 +++++---------- .../storage/xml/marshalling/ContentMarshaller.php | 4 ++-- .../xml/marshalling/Html5FigcaptionMarshaller.php | 5 ++++- .../xml/marshalling/Html5FigureMarshaller.php | 5 ++++- .../xml/marshalling/Qti22MarshallerFactory.php | 4 ++-- .../rendering/markup/xhtml/FigcaptionRenderer.php | 3 +++ .../rendering/markup/xhtml/FigureRenderer.php | 3 +++ .../markup/xhtml/XhtmlRenderingEngine.php | 4 ++-- 9 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/qtism/data/content/xhtml/html5/Figcaption.php b/src/qtism/data/content/xhtml/html5/Figcaption.php index de88fb4c1..c99c80894 100644 --- a/src/qtism/data/content/xhtml/html5/Figcaption.php +++ b/src/qtism/data/content/xhtml/html5/Figcaption.php @@ -31,7 +31,7 @@ class Figcaption extends Html5Element implements BlockStatic, FlowStatic { use FlowTrait; - public const QTI_CLASS_NAME = 'figure'; + public const QTI_CLASS_NAME_FIGCAPTION = 'figcaption'; /** * The Block components composing the SimpleBlock object. @@ -42,22 +42,17 @@ class Figcaption extends Html5Element implements BlockStatic, FlowStatic private $content; /** - * Create a new Div object. - * - * @param string $id The id of the bodyElement. - * @param string $class The class of the bodyElement. - * @param string $lang The language of the bodyElement. - * @param string $label The label of the bodyElement. + * Create a new figcaption object. */ - public function __construct($id = '', $class = '', $lang = '', $label = '') + public function __construct($title = null, $role = null, $id = null, $class = null, $lang = null, $label = null) { - parent::__construct($id, $class, $lang, $label); + parent::__construct($title, $role, $id, $class, $lang, $label); $this->setContent(new InlineCollection()); } public function getQtiClassName() { - return self::QTI_CLASS_NAME; + return self::QTI_CLASS_NAME_FIGCAPTION; } public function getComponents() diff --git a/src/qtism/data/content/xhtml/html5/Figure.php b/src/qtism/data/content/xhtml/html5/Figure.php index 19d3a9be8..f0db43623 100644 --- a/src/qtism/data/content/xhtml/html5/Figure.php +++ b/src/qtism/data/content/xhtml/html5/Figure.php @@ -31,7 +31,7 @@ class Figure extends Html5Element implements BlockStatic, FlowStatic { use FlowTrait; - public const QTI_CLASS_NAME = 'figure'; + public const QTI_CLASS_NAME_FIGURE = 'figure'; /** * The Block components composing the SimpleBlock object. @@ -42,22 +42,17 @@ class Figure extends Html5Element implements BlockStatic, FlowStatic private $content; /** - * Create a new Div object. - * - * @param string $id The id of the bodyElement. - * @param string $class The class of the bodyElement. - * @param string $lang The language of the bodyElement. - * @param string $label The label of the bodyElement. + * Create a new figure object. */ - public function __construct($id = '', $class = '', $lang = '', $label = '') + public function __construct($title = null, $role = null, $id = null, $class = null, $lang = null, $label = null) { - parent::__construct($id, $class, $lang, $label); + parent::__construct($title, $role, $id, $class, $lang, $label); $this->setContent(new FlowCollection()); } public function getQtiClassName() { - return self::QTI_CLASS_NAME; + return self::QTI_CLASS_NAME_FIGURE; } public function getComponents() diff --git a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php index 8eac54725..f000a39f1 100644 --- a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php @@ -179,8 +179,8 @@ public function __construct($version) 'modalFeedback', 'feedbackBlock', 'bdo', - Figure::QTI_CLASS_NAME, - Figcaption::QTI_CLASS_NAME + Figure::QTI_CLASS_NAME_FIGURE, + Figcaption::QTI_CLASS_NAME_FIGCAPTION ]; /** diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php index c3e1b0402..bb3a8e716 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php @@ -30,6 +30,9 @@ class Html5FigcaptionMarshaller extends Html5ElementMarshaller { + /** + * @param QtiComponent&Figcaption $component + */ protected function marshall(QtiComponent $component): DOMElement { /** @var Figcaption $component */ @@ -68,6 +71,6 @@ protected function unmarshall(DOMElement $element) public function getExpectedQtiClassName() { - return Version::compare($this->getVersion(), '2.2', '>=') ? Figcaption::QTI_CLASS_NAME : 'not_existing'; + return Version::compare($this->getVersion(), '2.2', '>=') ? Figcaption::QTI_CLASS_NAME_FIGCAPTION : 'not_existing'; } } diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php index 075e072fe..bc22b843d 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php @@ -30,6 +30,9 @@ class Html5FigureMarshaller extends Html5ElementMarshaller { + /** + * @param QtiComponent&Figure $component + */ protected function marshall(QtiComponent $component): DOMElement { /** @var Figure $component */ @@ -68,6 +71,6 @@ protected function unmarshall(DOMElement $element) public function getExpectedQtiClassName() { - return Version::compare($this->getVersion(), '2.2', '>=') ? Figure::QTI_CLASS_NAME : 'not_existing'; + return Version::compare($this->getVersion(), '2.2', '>=') ? Figure::QTI_CLASS_NAME_FIGURE : 'not_existing'; } } diff --git a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php index 10a8f4578..90f836a9a 100644 --- a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php +++ b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php @@ -43,8 +43,8 @@ public function __construct() { parent::__construct(); $this->addMappingEntry('bdo', SimpleInlineMarshaller::class); - $this->addMappingEntry(Figure::QTI_CLASS_NAME, Html5FigureMarshaller::class, self::HTML5_NAMESPACE); - $this->addMappingEntry(Figcaption::QTI_CLASS_NAME, Html5FigcaptionMarshaller::class, self::HTML5_NAMESPACE); + $this->addMappingEntry(Figure::QTI_CLASS_NAME_FIGURE, Html5FigureMarshaller::class, self::HTML5_NAMESPACE); + $this->addMappingEntry(Figcaption::QTI_CLASS_NAME_FIGCAPTION, Html5FigcaptionMarshaller::class, self::HTML5_NAMESPACE); } /** diff --git a/src/qtism/runtime/rendering/markup/xhtml/FigcaptionRenderer.php b/src/qtism/runtime/rendering/markup/xhtml/FigcaptionRenderer.php index ec0585949..ba6c1d67a 100644 --- a/src/qtism/runtime/rendering/markup/xhtml/FigcaptionRenderer.php +++ b/src/qtism/runtime/rendering/markup/xhtml/FigcaptionRenderer.php @@ -28,6 +28,9 @@ class FigcaptionRenderer extends Html5ElementRenderer { + /** + * @param QtiComponent&Figcaption $component + */ protected function appendAttributes(DOMDocumentFragment $fragment, QtiComponent $component, $base = '') { parent::appendAttributes($fragment, $component, $base); diff --git a/src/qtism/runtime/rendering/markup/xhtml/FigureRenderer.php b/src/qtism/runtime/rendering/markup/xhtml/FigureRenderer.php index 862edce3f..4403f3549 100644 --- a/src/qtism/runtime/rendering/markup/xhtml/FigureRenderer.php +++ b/src/qtism/runtime/rendering/markup/xhtml/FigureRenderer.php @@ -28,6 +28,9 @@ class FigureRenderer extends Html5ElementRenderer { + /** + * @param QtiComponent&Figure $component + */ protected function appendAttributes(DOMDocumentFragment $fragment, QtiComponent $component, $base = '') { parent::appendAttributes($fragment, $component, $base); diff --git a/src/qtism/runtime/rendering/markup/xhtml/XhtmlRenderingEngine.php b/src/qtism/runtime/rendering/markup/xhtml/XhtmlRenderingEngine.php index a644292e1..415e09931 100644 --- a/src/qtism/runtime/rendering/markup/xhtml/XhtmlRenderingEngine.php +++ b/src/qtism/runtime/rendering/markup/xhtml/XhtmlRenderingEngine.php @@ -121,8 +121,8 @@ public function __construct() $this->registerRenderer('extendedTextInteraction', new ExtendedTextInteractionRenderer()); $this->registerRenderer('feedbackBlock', new FeedbackBlockRenderer()); $this->registerRenderer('feedbackInline', new FeedbackInlineRenderer()); - $this->registerRenderer(Figure::QTI_CLASS_NAME, new FigureRenderer()); - $this->registerRenderer(Figcaption::QTI_CLASS_NAME, new FigcaptionRenderer()); + $this->registerRenderer(Figure::QTI_CLASS_NAME_FIGURE, new FigureRenderer()); + $this->registerRenderer(Figcaption::QTI_CLASS_NAME_FIGCAPTION, new FigcaptionRenderer()); $this->registerRenderer('gap', new GapRenderer()); $this->registerRenderer('gapImg', new GapImgRenderer()); $this->registerRenderer('gapMatchInteraction', new GapMatchInteractionRenderer()); From 98fc4dcb091ebb4a5506484924c7520b94d94011 Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Thu, 26 May 2022 23:37:04 +0200 Subject: [PATCH 07/13] tests: small partly fix for attribute order --- .../content/xhtml/html5/FigcaptionTest.php | 4 ++-- .../data/content/xhtml/html5/FigureTest.php | 4 ++-- .../Html5FigcaptionMarshallerTest.php | 18 +++++++++--------- .../marshalling/Html5FigureMarshallerTest.php | 14 +++++++------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php b/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php index a3141cf40..74c75fbbb 100644 --- a/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php +++ b/test/qtismtest/data/content/xhtml/html5/FigcaptionTest.php @@ -32,7 +32,7 @@ public function testCreateWithValues(): void $id = 'testid'; $class = 'test_class'; - $subject = new Figcaption($id, $class); + $subject = new Figcaption(null, null, $id, $class); self::assertEquals($id, $subject->getId()); self::assertEquals($class, $subject->getClass()); @@ -50,6 +50,6 @@ public function testGetQtiClassName(): void { $subject = new Figcaption(); - self::assertEquals(Figcaption::QTI_CLASS_NAME, $subject->getQtiClassName()); + self::assertEquals(Figcaption::QTI_CLASS_NAME_FIGCAPTION, $subject->getQtiClassName()); } } diff --git a/test/qtismtest/data/content/xhtml/html5/FigureTest.php b/test/qtismtest/data/content/xhtml/html5/FigureTest.php index cb69aac66..fb2573625 100644 --- a/test/qtismtest/data/content/xhtml/html5/FigureTest.php +++ b/test/qtismtest/data/content/xhtml/html5/FigureTest.php @@ -32,7 +32,7 @@ public function testCreateWithValues(): void $id = 'testid'; $class = 'test_class'; - $subject = new Figure($id, $class); + $subject = new Figure(null, null, $id, $class); self::assertEquals($id, $subject->getId()); self::assertEquals($class, $subject->getClass()); @@ -50,6 +50,6 @@ public function testGetQtiClassName(): void { $subject = new Figure(); - self::assertEquals(Figure::QTI_CLASS_NAME, $subject->getQtiClassName()); + self::assertEquals(Figure::QTI_CLASS_NAME_FIGURE, $subject->getQtiClassName()); } } diff --git a/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php b/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php index 47fd84583..0e85a47d3 100644 --- a/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php +++ b/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php @@ -34,7 +34,7 @@ class Html5FigcaptionMarshallerTest extends Html5ElementMarshallerTest */ public function testMarshallerDoesNotExistInQti21(): void { - $this->assertHtml5MarshallingOnlyInQti22AndAbove(new Figcaption(), Figcaption::QTI_CLASS_NAME); + $this->assertHtml5MarshallingOnlyInQti22AndAbove(new Figcaption(), Figcaption::QTI_CLASS_NAME_FIGCAPTION); } /** @@ -48,12 +48,12 @@ public function testMarshall22(): void $expected = sprintf( '<%1$s id="%2$s " class="%3$s">text content', - $this->namespaceTag(Figcaption::QTI_CLASS_NAME), + $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION), $id, $class, ); - $object = new Figcaption($id, $class); + $object = new Figcaption(null, null, $id, $class); $this->assertMarshalling($expected, $object); } @@ -64,7 +64,7 @@ public function testMarshall22(): void */ public function testMarshall22WithDefaultValues(): void { - $expected = sprintf('<%1$s>text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME)); + $expected = sprintf('<%1$s>text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION)); $video = new Figcaption(); @@ -77,8 +77,8 @@ public function testMarshall22WithDefaultValues(): void public function testUnMarshallerDoesNotExistInQti21(): void { $this->assertHtml5UnmarshallingOnlyInQti22AndAbove( - sprintf('<%1$s>', $this->namespaceTag(Figcaption::QTI_CLASS_NAME)), - Figcaption::QTI_CLASS_NAME + sprintf('<%1$s>', $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION)), + Figcaption::QTI_CLASS_NAME_FIGCAPTION ); } @@ -92,19 +92,19 @@ public function testUnmarshall22(): void $xml = sprintf( '<%1$s id="%2$s " class="%3$s">text content', - $this->namespaceTag(Figcaption::QTI_CLASS_NAME), + $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION), $id, $class, ); - $expected = new Figcaption($id, $class); + $expected = new Figcaption(null, null, $id, $class); $this->assertUnmarshalling($expected, $xml); } public function testUnmarshall22WithDefaultValues(): void { - $xml = sprintf('<%1$s>text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME)); + $xml = sprintf('<%1$s>text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION)); $expected = new Figcaption(); diff --git a/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php b/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php index 664e8074e..1547e924f 100644 --- a/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php +++ b/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php @@ -34,7 +34,7 @@ class Html5FigureMarshallerTest extends Html5ElementMarshallerTest */ public function testMarshallerDoesNotExistInQti21(): void { - $this->assertHtml5MarshallingOnlyInQti22AndAbove(new Figure(), Figure::QTI_CLASS_NAME); + $this->assertHtml5MarshallingOnlyInQti22AndAbove(new Figure(), Figure::QTI_CLASS_NAME_FIGURE); } /** @@ -48,7 +48,7 @@ public function testMarshall22(): void $expected = sprintf( '<%1$s id="%2$s " class="%3$s">', - $this->namespaceTag(Figure::QTI_CLASS_NAME), + $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE), $id, $class, ); @@ -64,7 +64,7 @@ public function testMarshall22(): void */ public function testMarshall22WithDefaultValues(): void { - $expected = sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME)); + $expected = sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE)); $video = new Figure(); @@ -77,8 +77,8 @@ public function testMarshall22WithDefaultValues(): void public function testUnMarshallerDoesNotExistInQti21(): void { $this->assertHtml5UnmarshallingOnlyInQti22AndAbove( - sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME)), - Figure::QTI_CLASS_NAME + sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE)), + Figure::QTI_CLASS_NAME_FIGURE ); } @@ -92,7 +92,7 @@ public function testUnmarshall22(): void $xml = sprintf( '<%1$s id="%2$s " class="%3$s">', - $this->namespaceTag(Figure::QTI_CLASS_NAME), + $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE), $id, $class, ); @@ -104,7 +104,7 @@ public function testUnmarshall22(): void public function testUnmarshall22WithDefaultValues(): void { - $xml = sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME)); + $xml = sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE)); $expected = new Figure(); From d28ba91fc68a41f4a7acd230b3216bb9bde561fd Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Mon, 30 May 2022 17:04:02 +0200 Subject: [PATCH 08/13] fix: parse recrusive new tag for add content --- .../xml/marshalling/ContentMarshaller.php | 4 + .../marshalling/Html5ContentMarshaller.php | 102 ++++++++++++++++++ .../marshalling/Html5ElementMarshaller.php | 34 ++---- .../marshalling/Html5FigcaptionMarshaller.php | 46 ++++---- .../xml/marshalling/Html5FigureMarshaller.php | 43 ++++---- .../xml/marshalling/MarshallerFactory.php | 2 + .../marshalling/Qti22MarshallerFactory.php | 2 +- .../trait/QtiHtml5AttributeTrait.php | 62 +++++++++++ .../trait/QtiNamespacePrefixTrait.php | 53 +++++++++ 9 files changed, 274 insertions(+), 74 deletions(-) create mode 100644 src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php create mode 100644 src/qtism/data/storage/xml/marshalling/trait/QtiHtml5AttributeTrait.php create mode 100644 src/qtism/data/storage/xml/marshalling/trait/QtiNamespacePrefixTrait.php diff --git a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php index f000a39f1..b1ae61152 100644 --- a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php @@ -292,6 +292,10 @@ protected function getChildrenComponents(QtiComponent $component) return $component->getContent()->getArrayCopy(); } elseif ($component instanceof InfoControl) { return $component->getContent()->getArrayCopy(); + } elseif ($component instanceof Figure) { + return $component->getContent()->getArrayCopy(); + } elseif ($component instanceof Figcaption) { + return $component->getContent()->getArrayCopy(); } } diff --git a/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php new file mode 100644 index 000000000..9c6a17401 --- /dev/null +++ b/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php @@ -0,0 +1,102 @@ + + * @license GPLv2 + */ + +namespace qtism\data\storage\xml\marshalling; + +use DOMElement; +use qtism\common\utils\Version; +use qtism\data\content\FlowCollection; +use qtism\data\content\xhtml\html5\Html5Element; +use qtism\data\QtiComponent; +use qtism\data\storage\xml\marshalling\trait\QtiHtml5AttributeTrait; +use qtism\data\storage\xml\marshalling\trait\QtiNamespacePrefixTrait; +use qtism\data\QtiComponentCollection; + +/** + * Marshalling/Unmarshalling implementation for generic Html5. + */ +abstract class Html5ContentMarshaller extends ContextMarshaller +{ + use QtiNamespacePrefixTrait; + use QtiHtml5AttributeTrait; + + /** + * @param DOMElement $element + * @param QtiComponentCollection $children + * @return mixed + * @throws UnmarshallingException + */ + protected function unmarshallChildrenKnown(DOMElement $element, QtiComponentCollection $children) + { + $fqClass = $this->lookupClass($element); + $component = new $fqClass(); + $component->setContent(new FlowCollection($children->getArrayCopy())); + + if (($xmlBase = self::getXmlBase($element)) !== false) { + $component->setXmlBase($xmlBase); + } + + $this->fillBodyElementAttributes($component, $element); + $this->fillBodyElement($component, $element); + + return $component; + } + + + /** + * @param QtiComponent $component + * @param array $elements + * @return DOMElement + */ + protected function marshallChildrenKnown(QtiComponent $component, array $elements) + { + /** @var Html5Element $component */ + $element = $this->getNamespacedElement($component); + + if ($component->hasXmlBase() === true) { + self::setXmlBase($element, $component->getXmlBase()); + } + + foreach ($elements as $e) { + $element->appendChild($e); + } + + $this->fillElement($element, $component); + + return $element; + } + + protected function setLookupClasses() + { + $this->lookupClasses = [ + "qtism\\data\\content\\xhtml", + "qtism\\data\\content\\xhtml\\html5", + "qtism\\data\\content\\xhtml\\text", + ]; + } + + public function getExpectedQtiClassName() + { + return Version::compare($this->getVersion(), '2.2', '>=') ? parent::getExpectedQtiClassName() : 'not_existing'; + } +} diff --git a/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php index 13e16f172..f101e44be 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php @@ -29,6 +29,8 @@ use qtism\data\content\enums\Role; use qtism\data\content\xhtml\html5\Html5Element; use qtism\data\QtiComponent; +use qtism\data\storage\xml\marshalling\trait\QtiHtml5AttributeTrait; +use qtism\data\storage\xml\marshalling\trait\QtiNamespacePrefixTrait; use qtism\data\storage\xml\versions\QtiVersion; /** @@ -36,36 +38,24 @@ */ abstract class Html5ElementMarshaller extends Marshaller { + use QtiNamespacePrefixTrait; + use QtiHtml5AttributeTrait; + /** * Marshall a Html5 element object into a DOMElement object. * * @param QtiComponent $component * @return DOMElement The according DOMElement object. + * @throws \DOMException */ protected function marshall(QtiComponent $component): DOMElement { /** @var Html5Element $component */ - $prefix = $component->getTargetNamespacePrefix(); - $version = QtiVersion::create($this->getVersion()); - $namespace = $version->getExternalNamespace($prefix); - - $element = static::getDOMCradle()->createElementNS( - $namespace, - $prefix . ':' . $component->getQtiClassName() - ); - + $element = $this->getNamespacedElement($component); $this->fillElement($element, $component); - if ($component->hasTitle()) { - $this->setDOMElementAttribute($element, 'title', $component->getTitle()); - } - - if ($component->hasRole()) { - $this->setDOMElementAttribute($element, 'role', Role::getNameByConstant($component->getRole())); - } - - return $element; + return $this->marshallHtml5Attributes($component, $element); } /** @@ -80,13 +70,7 @@ protected function marshall(QtiComponent $component): DOMElement */ protected function fillBodyElement(BodyElement $bodyElement, DOMElement $element) { - if (Version::compare($this->getVersion(), '2.2.0', '>=') === true) { - $title = $this->getDOMElementAttributeAs($element, 'title'); - $bodyElement->setTitle($title); - - $role = $this->getDOMElementAttributeAs($element, 'role', Role::class); - $bodyElement->setRole($role); - } + $this->fillBodyElementAttributes($bodyElement); parent::fillBodyElement($bodyElement, $element); } diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php index bb3a8e716..f9c522135 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php @@ -23,20 +23,21 @@ namespace qtism\data\storage\xml\marshalling; use DOMElement; -use Psr\Log\InvalidArgumentException; -use qtism\common\utils\Version; -use qtism\data\content\xhtml\html5\Figcaption; +use qtism\data\content\xhtml\html5\Figure; use qtism\data\QtiComponent; +use qtism\data\QtiComponentCollection; -class Html5FigcaptionMarshaller extends Html5ElementMarshaller +class Html5FigcaptionMarshaller extends Html5ContentMarshaller { /** - * @param QtiComponent&Figcaption $component + * @param QtiComponent&Figure $component + * @param QtiComponentCollection $children + * @return mixed + * @throws UnmarshallingException */ - protected function marshall(QtiComponent $component): DOMElement + protected function unmarshallChildrenKnown(DOMElement $element, QtiComponentCollection $children) { - /** @var Figcaption $component */ - $element = parent::marshall($component); + $component = parent::unmarshallChildrenKnown($element, $children); if ($component->hasId()) { $this->setDOMElementAttribute($element, 'id', $component->getId()); @@ -46,31 +47,26 @@ protected function marshall(QtiComponent $component): DOMElement $this->setDOMElementAttribute($element, 'class', $component->getClass()); } - return $element; + return $component; } /** - * @return Figcaption - * @throws UnmarshallingException + * @param QtiComponent $component + * @param array $elements + * @return DOMElement */ - protected function unmarshall(DOMElement $element) + protected function marshallChildrenKnown(QtiComponent $component, array $elements) { - try { - $id = $this->getDOMElementAttributeAs($element, 'id'); - $class = $this->getDOMElementAttributeAs($element, 'class'); + $element = parent::marshallChildrenKnown($component, $elements); - $component = new Figcaption($id, $class); - } catch (InvalidArgumentException $exception) { - throw UnmarshallingException::createFromInvalidArgumentException($element, $exception); + if ($component->hasId()) { + $this->setDOMElementAttribute($element, 'id', $component->getId()); } - $this->fillBodyElement($component, $element); - - return $component; - } + if ($component->hasClass()) { + $this->setDOMElementAttribute($element, 'class', $component->getClass()); + } - public function getExpectedQtiClassName() - { - return Version::compare($this->getVersion(), '2.2', '>=') ? Figcaption::QTI_CLASS_NAME_FIGCAPTION : 'not_existing'; + return $element; } } diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php index bc22b843d..3a4bd717e 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php @@ -23,20 +23,22 @@ namespace qtism\data\storage\xml\marshalling; use DOMElement; -use Psr\Log\InvalidArgumentException; -use qtism\common\utils\Version; use qtism\data\content\xhtml\html5\Figure; use qtism\data\QtiComponent; +use qtism\data\QtiComponentCollection; -class Html5FigureMarshaller extends Html5ElementMarshaller +class Html5FigureMarshaller extends Html5ContentMarshaller { /** + * * @param QtiComponent&Figure $component + * @param QtiComponentCollection $children + * @return mixed + * @throws UnmarshallingException */ - protected function marshall(QtiComponent $component): DOMElement + protected function unmarshallChildrenKnown(DOMElement $element, QtiComponentCollection $children) { - /** @var Figure $component */ - $element = parent::marshall($component); + $component = parent::unmarshallChildrenKnown($element, $children); if ($component->hasId()) { $this->setDOMElementAttribute($element, 'id', $component->getId()); @@ -46,31 +48,26 @@ protected function marshall(QtiComponent $component): DOMElement $this->setDOMElementAttribute($element, 'class', $component->getClass()); } - return $element; + return $component; } /** - * @return Figure - * @throws UnmarshallingException + * @param QtiComponent $component + * @param array $elements + * @return DOMElement */ - protected function unmarshall(DOMElement $element) + protected function marshallChildrenKnown(QtiComponent $component, array $elements) { - try { - $id = $this->getDOMElementAttributeAs($element, 'id'); - $class = $this->getDOMElementAttributeAs($element, 'class'); + $element = parent::marshallChildrenKnown($component, $elements); - $component = new Figure($id, $class); - } catch (InvalidArgumentException $exception) { - throw UnmarshallingException::createFromInvalidArgumentException($element, $exception); + if ($component->hasId()) { + $this->setDOMElementAttribute($element, 'id', $component->getId()); } - $this->fillBodyElement($component, $element); - - return $component; - } + if ($component->hasClass()) { + $this->setDOMElementAttribute($element, 'class', $component->getClass()); + } - public function getExpectedQtiClassName() - { - return Version::compare($this->getVersion(), '2.2', '>=') ? Figure::QTI_CLASS_NAME_FIGURE : 'not_existing'; + return $element; } } diff --git a/src/qtism/data/storage/xml/marshalling/MarshallerFactory.php b/src/qtism/data/storage/xml/marshalling/MarshallerFactory.php index 8c3cd1fd5..81da816b8 100644 --- a/src/qtism/data/storage/xml/marshalling/MarshallerFactory.php +++ b/src/qtism/data/storage/xml/marshalling/MarshallerFactory.php @@ -26,6 +26,8 @@ use DOMElement; use InvalidArgumentException; use qtism\data\ExternalQtiComponent; +use qtism\data\content\xhtml\html5\Figure; +use qtism\data\content\xhtml\html5\Figcaption; use qtism\data\QtiComponent; use qtism\data\storage\xml\QtiNamespaced; use qtism\data\storage\xml\Utils; diff --git a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php index 90f836a9a..667d6741a 100644 --- a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php +++ b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php @@ -34,7 +34,7 @@ */ class Qti22MarshallerFactory extends MarshallerFactory { - private const HTML5_NAMESPACE = 'http://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0'; + private const HTML5_NAMESPACE = 'https://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0'; /** * Create a new Qti22MarshallerFactory object. diff --git a/src/qtism/data/storage/xml/marshalling/trait/QtiHtml5AttributeTrait.php b/src/qtism/data/storage/xml/marshalling/trait/QtiHtml5AttributeTrait.php new file mode 100644 index 000000000..51847445c --- /dev/null +++ b/src/qtism/data/storage/xml/marshalling/trait/QtiHtml5AttributeTrait.php @@ -0,0 +1,62 @@ +hasTitle()) { + $this->setDOMElementAttribute($element, 'title', $component->getTitle()); + } + + if ($component->hasRole()) { + $this->setDOMElementAttribute($element, 'role', Role::getNameByConstant($component->getRole())); + } + + return $element; + } + + protected function fillBodyElementAttributes(BodyElement &$bodyElement, DOMElement $element) + { + if (Version::compare($this->getVersion(), '2.2.0', '>=') === true) { + $title = $this->getDOMElementAttributeAs($element, 'title'); + $bodyElement->setTitle($title); + + $role = $this->getDOMElementAttributeAs($element, 'role', Role::class); + $bodyElement->setRole($role); + } + } +} diff --git a/src/qtism/data/storage/xml/marshalling/trait/QtiNamespacePrefixTrait.php b/src/qtism/data/storage/xml/marshalling/trait/QtiNamespacePrefixTrait.php new file mode 100644 index 000000000..cdce1bcd7 --- /dev/null +++ b/src/qtism/data/storage/xml/marshalling/trait/QtiNamespacePrefixTrait.php @@ -0,0 +1,53 @@ +getTargetNamespacePrefix(); + $version = QtiVersion::create($this->getVersion()); + $namespace = $version->getExternalNamespace($prefix); + + return Marshaller::getDOMCradle()->createElementNS( + $namespace, + $prefix . ':' . $component->getQtiClassName() + ); + } +} From a2c5506e955249b540b095c39632c661c2f0f76e Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Tue, 31 May 2022 00:14:36 +0200 Subject: [PATCH 09/13] fix: use proper class to build qti elements --- src/qtism/data/content/xhtml/Img.php | 2 ++ .../data/content/xhtml/html5/Figcaption.php | 3 +-- src/qtism/data/content/xhtml/html5/Figure.php | 3 +-- .../data/content/xhtml/html5/Html5Element.php | 2 +- .../xml/marshalling/ContentMarshaller.php | 9 ++++---- .../marshalling/Html5ContentMarshaller.php | 22 +++++++++++-------- .../marshalling/Html5FigcaptionMarshaller.php | 12 ++++++++++ .../xml/marshalling/Html5FigureMarshaller.php | 12 ++++++++++ .../marshalling/Qti22MarshallerFactory.php | 4 ++-- 9 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/qtism/data/content/xhtml/Img.php b/src/qtism/data/content/xhtml/Img.php index f6f27367f..d34cea471 100644 --- a/src/qtism/data/content/xhtml/Img.php +++ b/src/qtism/data/content/xhtml/Img.php @@ -32,6 +32,8 @@ */ class Img extends AtomicInline { + public const QTI_CLASS_NAME_IMG='img'; + /** * The img's src attribute. * diff --git a/src/qtism/data/content/xhtml/html5/Figcaption.php b/src/qtism/data/content/xhtml/html5/Figcaption.php index c99c80894..8fcb41ac0 100644 --- a/src/qtism/data/content/xhtml/html5/Figcaption.php +++ b/src/qtism/data/content/xhtml/html5/Figcaption.php @@ -22,12 +22,11 @@ namespace qtism\data\content\xhtml\html5; -use qtism\data\content\BlockStatic; use qtism\data\content\FlowStatic; use qtism\data\content\FlowTrait; use qtism\data\content\InlineCollection; -class Figcaption extends Html5Element implements BlockStatic, FlowStatic +class Figcaption extends Html5Element implements FlowStatic { use FlowTrait; diff --git a/src/qtism/data/content/xhtml/html5/Figure.php b/src/qtism/data/content/xhtml/html5/Figure.php index f0db43623..89d03ab57 100644 --- a/src/qtism/data/content/xhtml/html5/Figure.php +++ b/src/qtism/data/content/xhtml/html5/Figure.php @@ -22,12 +22,11 @@ namespace qtism\data\content\xhtml\html5; -use qtism\data\content\BlockStatic; use qtism\data\content\FlowCollection; use qtism\data\content\FlowStatic; use qtism\data\content\FlowTrait; -class Figure extends Html5Element implements BlockStatic, FlowStatic +class Figure extends Html5Element implements FlowStatic { use FlowTrait; diff --git a/src/qtism/data/content/xhtml/html5/Html5Element.php b/src/qtism/data/content/xhtml/html5/Html5Element.php index 077e06c19..f861da52b 100644 --- a/src/qtism/data/content/xhtml/html5/Html5Element.php +++ b/src/qtism/data/content/xhtml/html5/Html5Element.php @@ -35,7 +35,7 @@ */ abstract class Html5Element extends BodyElement implements QtiNamespaced { - private const HTML5_NAMESPACE = 'http://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0'; + private const HTML5_NAMESPACE = 'https://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0'; /** * The title characteristic represents advisory information for the tag, diff --git a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php index b1ae61152..73886c4da 100644 --- a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php @@ -66,6 +66,7 @@ use qtism\data\content\xhtml\tables\Tr; use qtism\data\content\xhtml\text\Blockquote; use qtism\data\content\xhtml\text\Div; +use qtism\data\content\xhtml\Img; use qtism\data\ExternalQtiComponent; use qtism\data\QtiComponent; use qtism\data\QtiComponentCollection; @@ -108,7 +109,7 @@ public function __construct($version) 'graphicGapMatchInteraction', 'hotspotChoice', 'hr', - 'img', + Img::QTI_CLASS_NAME_IMG, 'include', 'math', 'mediaInteraction', @@ -292,10 +293,6 @@ protected function getChildrenComponents(QtiComponent $component) return $component->getContent()->getArrayCopy(); } elseif ($component instanceof InfoControl) { return $component->getContent()->getArrayCopy(); - } elseif ($component instanceof Figure) { - return $component->getContent()->getArrayCopy(); - } elseif ($component instanceof Figcaption) { - return $component->getContent()->getArrayCopy(); } } @@ -349,6 +346,8 @@ protected function getChildrenElements(DOMElement $element) return self::getChildElements($element); } elseif ($localName === 'simpleMatchSet') { return $this->getChildElementsByTagName($element, 'simpleAssociableChoice'); + } elseif ($localName === Figure::QTI_CLASS_NAME_FIGURE) { + return $this->getChildElementsByTagName($element, [Figcaption::QTI_CLASS_NAME_FIGCAPTION, Img::QTI_CLASS_NAME_IMG]); } elseif ($localName === 'gapImg') { return $this->getChildElementsByTagName($element, 'object'); } else { diff --git a/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php index 9c6a17401..34b9b0040 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php @@ -26,6 +26,7 @@ use DOMElement; use qtism\common\utils\Version; use qtism\data\content\FlowCollection; +use qtism\data\content\InlineCollection; use qtism\data\content\xhtml\html5\Html5Element; use qtism\data\QtiComponent; use qtism\data\storage\xml\marshalling\trait\QtiHtml5AttributeTrait; @@ -35,11 +36,18 @@ /** * Marshalling/Unmarshalling implementation for generic Html5. */ -abstract class Html5ContentMarshaller extends ContextMarshaller +abstract class Html5ContentMarshaller extends ContentMarshaller { use QtiNamespacePrefixTrait; use QtiHtml5AttributeTrait; + public function getExpectedQtiClassName() + { + return Version::compare($this->getVersion(), '2.2', '>=') ? static::getExpectedQtiClassName() : 'not_existing'; + } + + abstract protected static function getContentCollectionClassName(); + /** * @param DOMElement $element * @param QtiComponentCollection $children @@ -50,7 +58,9 @@ protected function unmarshallChildrenKnown(DOMElement $element, QtiComponentColl { $fqClass = $this->lookupClass($element); $component = new $fqClass(); - $component->setContent(new FlowCollection($children->getArrayCopy())); + $collectionClassName = static::getContentCollectionClassName() ?? FlowCollection::class; + + $component->setContent(new $collectionClassName($children->getArrayCopy())); if (($xmlBase = self::getXmlBase($element)) !== false) { $component->setXmlBase($xmlBase); @@ -90,13 +100,7 @@ protected function setLookupClasses() { $this->lookupClasses = [ "qtism\\data\\content\\xhtml", - "qtism\\data\\content\\xhtml\\html5", - "qtism\\data\\content\\xhtml\\text", + "qtism\\data\\content\\xhtml\\html5" ]; } - - public function getExpectedQtiClassName() - { - return Version::compare($this->getVersion(), '2.2', '>=') ? parent::getExpectedQtiClassName() : 'not_existing'; - } } diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php index f9c522135..107f8b72e 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5FigcaptionMarshaller.php @@ -23,12 +23,19 @@ namespace qtism\data\storage\xml\marshalling; use DOMElement; +use qtism\data\content\InlineCollection; +use qtism\data\content\xhtml\html5\Figcaption; use qtism\data\content\xhtml\html5\Figure; use qtism\data\QtiComponent; use qtism\data\QtiComponentCollection; class Html5FigcaptionMarshaller extends Html5ContentMarshaller { + public function getExpectedQtiClassName() + { + return Figcaption::QTI_CLASS_NAME_FIGCAPTION; + } + /** * @param QtiComponent&Figure $component * @param QtiComponentCollection $children @@ -69,4 +76,9 @@ protected function marshallChildrenKnown(QtiComponent $component, array $element return $element; } + + protected static function getContentCollectionClassName() + { + return InlineCollection::class; + } } diff --git a/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php index 3a4bd717e..d4c9c87fd 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5FigureMarshaller.php @@ -23,12 +23,19 @@ namespace qtism\data\storage\xml\marshalling; use DOMElement; +use qtism\data\content\FlowCollection; +use qtism\data\content\InlineCollection; use qtism\data\content\xhtml\html5\Figure; use qtism\data\QtiComponent; use qtism\data\QtiComponentCollection; class Html5FigureMarshaller extends Html5ContentMarshaller { + public function getExpectedQtiClassName() + { + return Figure::QTI_CLASS_NAME_FIGURE; + } + /** * * @param QtiComponent&Figure $component @@ -70,4 +77,9 @@ protected function marshallChildrenKnown(QtiComponent $component, array $element return $element; } + + protected static function getContentCollectionClassName() + { + return FlowCollection::class; + } } diff --git a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php index 667d6741a..0094ed2ea 100644 --- a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php +++ b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php @@ -43,8 +43,8 @@ public function __construct() { parent::__construct(); $this->addMappingEntry('bdo', SimpleInlineMarshaller::class); - $this->addMappingEntry(Figure::QTI_CLASS_NAME_FIGURE, Html5FigureMarshaller::class, self::HTML5_NAMESPACE); - $this->addMappingEntry(Figcaption::QTI_CLASS_NAME_FIGCAPTION, Html5FigcaptionMarshaller::class, self::HTML5_NAMESPACE); + $this->addMappingEntry(Figure::QTI_CLASS_NAME_FIGURE, Html5FigureMarshaller::class); + $this->addMappingEntry(Figcaption::QTI_CLASS_NAME_FIGCAPTION, Html5FigcaptionMarshaller::class); } /** From 55df16fcc1c87524913515f14597fc119f37b257 Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Tue, 31 May 2022 13:46:18 +0200 Subject: [PATCH 10/13] tests: Simple marchalling and unmarchalling for new html5 elements --- .../Html5FigcaptionMarshallerTest.php | 43 +++++++++++------- .../marshalling/Html5FigureMarshallerTest.php | 44 ++++++++++++------- 2 files changed, 57 insertions(+), 30 deletions(-) diff --git a/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php b/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php index 0e85a47d3..0ed3a95f7 100644 --- a/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php +++ b/test/qtismtest/data/storage/xml/marshalling/Html5FigcaptionMarshallerTest.php @@ -22,9 +22,12 @@ namespace qtismtest\data\storage\xml\marshalling; +use qtism\data\content\InlineCollection; +use qtism\data\content\TextRun; use qtism\data\content\xhtml\html5\Figcaption; use qtism\data\storage\xml\marshalling\MarshallerNotFoundException; use qtism\data\storage\xml\marshalling\MarshallingException; +use Symfony\Component\Yaml\Inline; class Html5FigcaptionMarshallerTest extends Html5ElementMarshallerTest { @@ -47,13 +50,15 @@ public function testMarshall22(): void $class = 'testclass'; $expected = sprintf( - '<%1$s id="%2$s " class="%3$s">text content', + '<%1$s id="%2$s" class="%3$s">text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION), $id, $class, + $this->prefixTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION), ); $object = new Figcaption(null, null, $id, $class); + $object->setContent(new InlineCollection([new TextRun('text content')])); $this->assertMarshalling($expected, $object); } @@ -64,11 +69,16 @@ public function testMarshall22(): void */ public function testMarshall22WithDefaultValues(): void { - $expected = sprintf('<%1$s>text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION)); + $expected = sprintf( + '<%s>text content', + $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION), + $this->prefixTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION) + ); - $video = new Figcaption(); + $object = new Figcaption(); + $object->setContent(new InlineCollection([new TextRun('text content')])); - $this->assertMarshalling($expected, $video); + $this->assertMarshalling($expected, $object); } /** @@ -77,7 +87,11 @@ public function testMarshall22WithDefaultValues(): void public function testUnMarshallerDoesNotExistInQti21(): void { $this->assertHtml5UnmarshallingOnlyInQti22AndAbove( - sprintf('<%1$s>', $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION)), + sprintf( + '<%s>', + $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION), + $this->prefixTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION) + ), Figcaption::QTI_CLASS_NAME_FIGCAPTION ); } @@ -91,31 +105,30 @@ public function testUnmarshall22(): void $class = 'testclass'; $xml = sprintf( - '<%1$s id="%2$s " class="%3$s">text content', + '<%1$s id="%2$s" class="%3$s">text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION), $id, $class, + $this->prefixTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION) ); $expected = new Figcaption(null, null, $id, $class); + $expected->setContent(new InlineCollection([new TextRun('text content')])); $this->assertUnmarshalling($expected, $xml); } public function testUnmarshall22WithDefaultValues(): void { - $xml = sprintf('<%1$s>text content', $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION)); + $xml = sprintf( + '<%s>text content', + $this->namespaceTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION), + $this->prefixTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION) + ); $expected = new Figcaption(); + $expected->setContent(new InlineCollection([new TextRun('text content')])); $this->assertUnmarshalling($expected, $xml); } - - /** - * @throws MarshallerNotFoundException - */ - public function testUnmarshallWithWrongTypesOrValues(string $xml, string $exception, string $message): void - { - $this->assertUnmarshallingException($xml, $exception, $message); - } } diff --git a/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php b/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php index 1547e924f..7e5356797 100644 --- a/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php +++ b/test/qtismtest/data/storage/xml/marshalling/Html5FigureMarshallerTest.php @@ -22,6 +22,10 @@ namespace qtismtest\data\storage\xml\marshalling; +use qtism\data\content\FlowCollection; +use qtism\data\content\InlineCollection; +use qtism\data\content\TextRun; +use qtism\data\content\xhtml\html5\Figcaption; use qtism\data\content\xhtml\html5\Figure; use qtism\data\storage\xml\marshalling\MarshallerNotFoundException; use qtism\data\storage\xml\marshalling\MarshallingException; @@ -47,13 +51,19 @@ public function testMarshall22(): void $class = 'testclass'; $expected = sprintf( - '<%1$s id="%2$s " class="%3$s">', + '<%1$s id="%2$s" class="%3$s"><%5$s>text content', $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE), $id, $class, + $this->prefixTag(Figure::QTI_CLASS_NAME_FIGURE), + $this->prefixTag(Figcaption::QTI_CLASS_NAME_FIGCAPTION), ); - $object = new Figure($id, $class); + $figcaption = new Figcaption(); + $figcaption->setContent(new InlineCollection([new TextRun('text content')])); + + $object = new Figure(null, null, $id, $class); + $object->setContent(new FlowCollection([$figcaption])); $this->assertMarshalling($expected, $object); } @@ -64,7 +74,10 @@ public function testMarshall22(): void */ public function testMarshall22WithDefaultValues(): void { - $expected = sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE)); + $expected = sprintf( + '<%s/>', + $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE), + ); $video = new Figure(); @@ -77,7 +90,11 @@ public function testMarshall22WithDefaultValues(): void public function testUnMarshallerDoesNotExistInQti21(): void { $this->assertHtml5UnmarshallingOnlyInQti22AndAbove( - sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE)), + sprintf( + '<%s>', + $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE), + $this->prefixTag(Figure::QTI_CLASS_NAME_FIGURE) + ), Figure::QTI_CLASS_NAME_FIGURE ); } @@ -91,31 +108,28 @@ public function testUnmarshall22(): void $class = 'testclass'; $xml = sprintf( - '<%1$s id="%2$s " class="%3$s">', + '<%1$s id="%2$s" class="%3$s">', $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE), $id, $class, + $this->prefixTag(Figure::QTI_CLASS_NAME_FIGURE), ); - $expected = new Figure($id, $class); + $expected = new Figure(null, null, $id, $class); $this->assertUnmarshalling($expected, $xml); } public function testUnmarshall22WithDefaultValues(): void { - $xml = sprintf('<%1$s>', $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE)); + $xml = sprintf( + '<%s>', + $this->namespaceTag(Figure::QTI_CLASS_NAME_FIGURE), + $this->prefixTag(Figure::QTI_CLASS_NAME_FIGURE) + ); $expected = new Figure(); $this->assertUnmarshalling($expected, $xml); } - - /** - * @throws MarshallerNotFoundException - */ - public function testUnmarshallWithWrongTypesOrValues(string $xml, string $exception, string $message): void - { - $this->assertUnmarshallingException($xml, $exception, $message); - } } From ddfc46c3002c13cc8f00129a0ff89240ba4d3075 Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Tue, 31 May 2022 13:47:02 +0200 Subject: [PATCH 11/13] fix: correct marchalling for: figre and figcaption elements --- src/qtism/data/storage/xml/marshalling/ContentMarshaller.php | 4 ++++ .../data/storage/xml/marshalling/Html5ElementMarshaller.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php index 73886c4da..941dad67f 100644 --- a/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/ContentMarshaller.php @@ -293,6 +293,10 @@ protected function getChildrenComponents(QtiComponent $component) return $component->getContent()->getArrayCopy(); } elseif ($component instanceof InfoControl) { return $component->getContent()->getArrayCopy(); + } elseif ($component instanceof Figure) { + return $component->getContent()->getArrayCopy(); + } elseif ($component instanceof Figcaption) { + return $component->getContent()->getArrayCopy(); } } diff --git a/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php index f101e44be..32107ae00 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php @@ -70,7 +70,7 @@ protected function marshall(QtiComponent $component): DOMElement */ protected function fillBodyElement(BodyElement $bodyElement, DOMElement $element) { - $this->fillBodyElementAttributes($bodyElement); + $this->fillBodyElementAttributes($bodyElement, $element); parent::fillBodyElement($bodyElement, $element); } From 4fa5828658083725d0f21a9521c4e03276fa485e Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Wed, 1 Jun 2022 13:08:24 +0200 Subject: [PATCH 12/13] fix: remove https as it was before --- src/qtism/data/content/xhtml/html5/Html5Element.php | 2 +- .../data/storage/xml/marshalling/Qti22MarshallerFactory.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/qtism/data/content/xhtml/html5/Html5Element.php b/src/qtism/data/content/xhtml/html5/Html5Element.php index f861da52b..077e06c19 100644 --- a/src/qtism/data/content/xhtml/html5/Html5Element.php +++ b/src/qtism/data/content/xhtml/html5/Html5Element.php @@ -35,7 +35,7 @@ */ abstract class Html5Element extends BodyElement implements QtiNamespaced { - private const HTML5_NAMESPACE = 'https://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0'; + private const HTML5_NAMESPACE = 'http://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0'; /** * The title characteristic represents advisory information for the tag, diff --git a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php index 0094ed2ea..c4837864c 100644 --- a/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php +++ b/src/qtism/data/storage/xml/marshalling/Qti22MarshallerFactory.php @@ -34,7 +34,7 @@ */ class Qti22MarshallerFactory extends MarshallerFactory { - private const HTML5_NAMESPACE = 'https://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0'; + private const HTML5_NAMESPACE = 'http://www.imsglobal.org/xsd/imsqtiv2p2_html5_v1p0'; /** * Create a new Qti22MarshallerFactory object. From 2c0a34591426a639cbe6466a9087873852a337bc Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Wed, 1 Jun 2022 16:01:00 +0200 Subject: [PATCH 13/13] fix: TR-4179 remove namespace because it not support in php 7.4 --- .../data/storage/xml/marshalling/Html5ContentMarshaller.php | 3 --- .../data/storage/xml/marshalling/Html5ElementMarshaller.php | 5 ----- .../xml/marshalling/{trait => }/QtiHtml5AttributeTrait.php | 2 +- .../xml/marshalling/{trait => }/QtiNamespacePrefixTrait.php | 2 +- 4 files changed, 2 insertions(+), 10 deletions(-) rename src/qtism/data/storage/xml/marshalling/{trait => }/QtiHtml5AttributeTrait.php (97%) rename src/qtism/data/storage/xml/marshalling/{trait => }/QtiNamespacePrefixTrait.php (96%) diff --git a/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php index 34b9b0040..1fd89a933 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5ContentMarshaller.php @@ -26,11 +26,8 @@ use DOMElement; use qtism\common\utils\Version; use qtism\data\content\FlowCollection; -use qtism\data\content\InlineCollection; use qtism\data\content\xhtml\html5\Html5Element; use qtism\data\QtiComponent; -use qtism\data\storage\xml\marshalling\trait\QtiHtml5AttributeTrait; -use qtism\data\storage\xml\marshalling\trait\QtiNamespacePrefixTrait; use qtism\data\QtiComponentCollection; /** diff --git a/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php b/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php index 32107ae00..e8d59b10f 100644 --- a/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php +++ b/src/qtism/data/storage/xml/marshalling/Html5ElementMarshaller.php @@ -24,14 +24,9 @@ namespace qtism\data\storage\xml\marshalling; use DOMElement; -use qtism\common\utils\Version; use qtism\data\content\BodyElement; -use qtism\data\content\enums\Role; use qtism\data\content\xhtml\html5\Html5Element; use qtism\data\QtiComponent; -use qtism\data\storage\xml\marshalling\trait\QtiHtml5AttributeTrait; -use qtism\data\storage\xml\marshalling\trait\QtiNamespacePrefixTrait; -use qtism\data\storage\xml\versions\QtiVersion; /** * Marshalling/Unmarshalling implementation for generic Html5. diff --git a/src/qtism/data/storage/xml/marshalling/trait/QtiHtml5AttributeTrait.php b/src/qtism/data/storage/xml/marshalling/QtiHtml5AttributeTrait.php similarity index 97% rename from src/qtism/data/storage/xml/marshalling/trait/QtiHtml5AttributeTrait.php rename to src/qtism/data/storage/xml/marshalling/QtiHtml5AttributeTrait.php index 51847445c..c541ad82b 100644 --- a/src/qtism/data/storage/xml/marshalling/trait/QtiHtml5AttributeTrait.php +++ b/src/qtism/data/storage/xml/marshalling/QtiHtml5AttributeTrait.php @@ -20,7 +20,7 @@ declare(strict_types=1); -namespace qtism\data\storage\xml\marshalling\trait; +namespace qtism\data\storage\xml\marshalling; use qtism\common\utils\Version; use qtism\data\content\BodyElement; diff --git a/src/qtism/data/storage/xml/marshalling/trait/QtiNamespacePrefixTrait.php b/src/qtism/data/storage/xml/marshalling/QtiNamespacePrefixTrait.php similarity index 96% rename from src/qtism/data/storage/xml/marshalling/trait/QtiNamespacePrefixTrait.php rename to src/qtism/data/storage/xml/marshalling/QtiNamespacePrefixTrait.php index cdce1bcd7..7e6f4f381 100644 --- a/src/qtism/data/storage/xml/marshalling/trait/QtiNamespacePrefixTrait.php +++ b/src/qtism/data/storage/xml/marshalling/QtiNamespacePrefixTrait.php @@ -20,7 +20,7 @@ declare(strict_types=1); -namespace qtism\data\storage\xml\marshalling\trait; +namespace qtism\data\storage\xml\marshalling; use qtism\data\QtiComponent; use qtism\data\storage\xml\versions\QtiVersion;