Skip to content

Commit

Permalink
fix: null-safe dereferencing of record-typed variables fields via the…
Browse files Browse the repository at this point in the history
… `<fieldValue>` operand
  • Loading branch information
wazelin committed Jan 20, 2025
1 parent 2336405 commit dba0c71
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
11 changes: 8 additions & 3 deletions src/qtism/runtime/expressions/operators/FieldValueProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace qtism\runtime\expressions\operators;

use qtism\data\expressions\operators\FieldValue;
use qtism\runtime\common\RecordContainer;

/**
* The FieldValueProcessor class aims at processing FieldValue expressions.
Expand All @@ -45,16 +46,20 @@ class FieldValueProcessor extends OperatorProcessor
#[\ReturnTypeWillChange]
public function process()
{
$operands = $this->getOperands();
$operand = $this->getOperands()[0];

if ($operands->exclusivelyRecord() === false) {
if ($operand === null) {
return null;
}

if (!$operand instanceof RecordContainer) {
$msg = 'The FieldValue operator only accepts operands with a cardinality of record.';
throw new OperatorProcessingException($msg, $this, OperatorProcessingException::WRONG_CARDINALITY);
}

$fieldIdentifier = $this->getExpression()->getFieldIdentifier();

return $operands[0][$fieldIdentifier];
return $operand[$fieldIdentifier];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use qtism\common\datatypes\QtiInteger;
use qtism\common\datatypes\QtiPoint;
use qtism\common\enums\BaseType;
use qtism\data\expressions\Expression;
use qtism\data\QtiComponent;
use qtism\data\storage\xml\marshalling\MarshallerNotFoundException;
use qtism\runtime\common\MultipleContainer;
Expand All @@ -24,7 +25,7 @@ public function testNotEnoughOperands(): void
$expression = $this->createFakeExpression();
$operands = new OperandsCollection();
$this->expectException(ExpressionProcessingException::class);
$processor = new FieldValueProcessor($expression, $operands);
new FieldValueProcessor($expression, $operands);
}

public function testTooMuchOperands(): void
Expand All @@ -34,7 +35,7 @@ public function testTooMuchOperands(): void
$operands[] = new RecordContainer();
$operands[] = new RecordContainer();
$this->expectException(ExpressionProcessingException::class);
$processor = new FieldValueProcessor($expression, $operands);
new FieldValueProcessor($expression, $operands);
}

public function testNullOne(): void
Expand All @@ -55,9 +56,9 @@ public function testNullTwo(): void
$operands = new OperandsCollection();
// null value as operand.
$operands[] = null;
$this->expectException(ExpressionProcessingException::class);
$processor = new FieldValueProcessor($expression, $operands);
$result = $processor->process();
$this::assertNull($result);
}

public function testWrongCardinalityOne(): void
Expand All @@ -68,7 +69,7 @@ public function testWrongCardinalityOne(): void
$operands[] = new QtiInteger(10);
$processor = new FieldValueProcessor($expression, $operands);
$this->expectException(ExpressionProcessingException::class);
$result = $processor->process();
$processor->process();
}

public function testWrongCardinalityTwo(): void
Expand All @@ -79,7 +80,7 @@ public function testWrongCardinalityTwo(): void
$operands[] = new QtiPoint(1, 2);
$processor = new FieldValueProcessor($expression, $operands);
$this->expectException(ExpressionProcessingException::class);
$result = $processor->process();
$processor->process();
}

public function testWrongCardinalityThree(): void
Expand All @@ -91,7 +92,7 @@ public function testWrongCardinalityThree(): void
// Wrong container (Multiple, Ordered)
$processor = new FieldValueProcessor($expression, $operands);
$this->expectException(ExpressionProcessingException::class);
$result = $processor->process();
$processor->process();
}

public function testFieldValue(): void
Expand All @@ -116,7 +117,7 @@ public function testFieldValue(): void
* @return QtiComponent
* @throws MarshallerNotFoundException
*/
public function createFakeExpression($identifier = ''): QtiComponent
public function createFakeExpression($identifier = ''): Expression
{
// The following XML Component creation
// underlines the need of a <record> operator... :)
Expand Down

0 comments on commit dba0c71

Please sign in to comment.