diff --git a/composer.json b/composer.json index 557272bf7..4b7fd2272 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "qtism/qtism", "description": "OAT QTI Software Module Library", "type": "library", - "version": "0.9.26", + "version": "0.9.27", "authors": [ { "name": "Open Assessment Technologies S.A.", diff --git a/qtism/data/storage/xml/marshalling/ValueMarshaller.php b/qtism/data/storage/xml/marshalling/ValueMarshaller.php index 5f2817fef..c3c779c8e 100644 --- a/qtism/data/storage/xml/marshalling/ValueMarshaller.php +++ b/qtism/data/storage/xml/marshalling/ValueMarshaller.php @@ -135,21 +135,22 @@ protected function unmarshall(DOMElement $element) { else { // baseType attribute not set -> not part of a record. $nodeValue = trim($element->nodeValue); - if ($nodeValue !== '') { - // Try to use the marshaller as parametric to know how to unserialize the value. - if ($this->getBaseType() != -1) { - $object = new Value(Utils::stringToDatatype($nodeValue, $this->getBaseType()), $this->getBaseType()); - } - else { - // value used as plain string (at your own risks). - $object = new Value($nodeValue); - } - - } - else { - $msg = "The element '" . $element->localName . "' has no value."; - throw new UnmarshallingException($msg, $element); - } + + // Try to use the marshaller as parametric to know how to unserialize the value. + if ($this->getBaseType() != -1) { + + // Empty value only accepted if base type is string (consider empty string). + if ($this->getBaseType() !== BaseType::STRING && $nodeValue === '') { + $msg = "The element '" . $element->localName . "' has no value."; + throw new UnmarshallingException($msg, $element); + } + + $object = new Value(Utils::stringToDatatype($nodeValue, $this->getBaseType()), $this->getBaseType()); + } + else { + // value used as plain string (at your own risks). + $object = new Value($nodeValue); + } } if (($value = static::getDOMElementAttributeAs($element, 'fieldIdentifier', 'string')) !== null) { diff --git a/test/qtism/data/storage/xml/marshalling/ValueMarshallerTest.php b/test/qtism/data/storage/xml/marshalling/ValueMarshallerTest.php index bcdda13d8..d9386fe70 100644 --- a/test/qtism/data/storage/xml/marshalling/ValueMarshallerTest.php +++ b/test/qtism/data/storage/xml/marshalling/ValueMarshallerTest.php @@ -104,18 +104,38 @@ public function testMarshallNoBaseTypeButForcedAndEntities() { $this->assertSame('Hello <b>bold</b>', $element->ownerDocument->saveXML($element)); } - public function testUnmarshallNoValue() { - $this->setExpectedException('qtism\\data\\storage\\xml\\marshalling\\UnmarshallingException'); - + public function testUnmarshallNoValueStringExpected() { + // Just an empty . $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadXML(''); $element = $dom->documentElement; + $marshaller = $this->getMarshallerFactory()->createMarshaller($element, array(BaseType::STRING)); + $component = $marshaller->unmarshall($element); + $this->assertEquals('', $component->getValue()); + + // An empty , with empty CDATA. + $dom = new DOMDocument('1.0', 'UTF-8'); + $dom->loadXML(''); + $element = $dom->documentElement; + $marshaller = $this->getMarshallerFactory()->createMarshaller($element); $component = $marshaller->unmarshall($element); + $this->assertEquals('', $component->getValue()); } + + public function testUnmarshallNoValueIntegerExpected() { + $this->setExpectedException('qtism\\data\\storage\\xml\\marshalling\\UnmarshallingException'); + $dom = new DOMDocument('1.0', 'UTF-8'); + $dom->loadXML(''); + $element = $dom->documentElement; + + $marshaller = $this->getMarshallerFactory()->createMarshaller($element, array(BaseType::INTEGER)); + $component = $marshaller->unmarshall($element); + $this->assertEquals('', $component->getValue()); + } - public function testUnmarshallBaseType() { + public function testUnmarshallBaseTypePairWithFieldIdentifier() { $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadXML('A B'); $element = $dom->documentElement; @@ -129,4 +149,34 @@ public function testUnmarshallBaseType() { $this->assertEquals($component->getValue()->getSecond(), 'B'); $this->assertEquals($component->getFieldIdentifier(), 'fieldIdentifier1'); } + + public function testUnmarshallBaseTypeInteger() { + $dom = new DOMDocument('1.0', 'UTF-8'); + // 0 value + $dom->loadXML('0'); + $element = $dom->documentElement; + + $marshaller = $this->getMarshallerFactory()->createMarshaller($element); + $component = $marshaller->unmarshall($element); + + $this->assertSame(0, $component->getValue()); + + // Positive value. + $dom->loadXML('1'); + $element = $dom->documentElement; + + $marshaller = $this->getMarshallerFactory()->createMarshaller($element); + $component = $marshaller->unmarshall($element); + + $this->assertSame(1, $component->getValue()); + + // Negative value. + $dom->loadXML('-1'); + $element = $dom->documentElement; + + $marshaller = $this->getMarshallerFactory()->createMarshaller($element); + $component = $marshaller->unmarshall($element); + + $this->assertSame(-1, $component->getValue()); + } }