Skip to content

Commit

Permalink
Support empty string in <value> element.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Apr 19, 2016
1 parent dc86c3f commit f7c2cee
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
31 changes: 16 additions & 15 deletions qtism/data/storage/xml/marshalling/ValueMarshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
58 changes: 54 additions & 4 deletions test/qtism/data/storage/xml/marshalling/ValueMarshallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,38 @@ public function testMarshallNoBaseTypeButForcedAndEntities() {
$this->assertSame('<value>Hello &lt;b&gt;bold&lt;/b&gt;</value>', $element->ownerDocument->saveXML($element));
}

public function testUnmarshallNoValue() {
$this->setExpectedException('qtism\\data\\storage\\xml\\marshalling\\UnmarshallingException');

public function testUnmarshallNoValueStringExpected() {
// Just an empty <value>.
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXML('<value xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1"></value>');
$element = $dom->documentElement;

$marshaller = $this->getMarshallerFactory()->createMarshaller($element, array(BaseType::STRING));
$component = $marshaller->unmarshall($element);
$this->assertEquals('', $component->getValue());

// An empty <value>, with empty CDATA.
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadXML('<value xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1"><![CDATA[]]></value>');
$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('<value xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1"></value>');
$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('<value xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" baseType="pair" fieldIdentifier="fieldIdentifier1">A B</value>');
$element = $dom->documentElement;
Expand All @@ -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('<value xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" baseType="integer">0</value>');
$element = $dom->documentElement;

$marshaller = $this->getMarshallerFactory()->createMarshaller($element);
$component = $marshaller->unmarshall($element);

$this->assertSame(0, $component->getValue());

// Positive value.
$dom->loadXML('<value xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" baseType="integer">1</value>');
$element = $dom->documentElement;

$marshaller = $this->getMarshallerFactory()->createMarshaller($element);
$component = $marshaller->unmarshall($element);

$this->assertSame(1, $component->getValue());

// Negative value.
$dom->loadXML('<value xmlns="http://www.imsglobal.org/xsd/imsqti_v2p1" baseType="integer">-1</value>');
$element = $dom->documentElement;

$marshaller = $this->getMarshallerFactory()->createMarshaller($element);
$component = $marshaller->unmarshall($element);

$this->assertSame(-1, $component->getValue());
}
}

0 comments on commit f7c2cee

Please sign in to comment.