Skip to content

Commit

Permalink
[test/api] Add tests for getting instrument data from API.
Browse files Browse the repository at this point in the history
The previous test was "incomplete" (it got the data but didn't
do anything to verify it.) This adds real tests for the candidate
instrument data endpoint now that Issue aces#8781 was fixed by PR#8796.
  • Loading branch information
driusan committed Jun 21, 2023
1 parent c2f41af commit 6f4842c
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 18 deletions.
37 changes: 37 additions & 0 deletions modules/instruments/php/dictionaryitem.class.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace LORIS\instruments;

use \LORIS\Data\Scope;
use \LORIS\Data\Type;
use \LORIS\Data\Cardinality;

/**
* A DictionaryItem represents a description of a type of data
* managed by LORIS.
*
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
*/
class DictionaryItem extends \LORIS\Data\Dictionary\DictionaryItem
{
/**
* Construct a DictionaryItem with the given parameters
*
* @param string $name The field name of the dictionary item
* @param string $desc The dictionary item's description
* @param Scope $scope The scope to which this DictionaryItem
* applies
* @param Type $t The data type of this dictionary item
* @param Cardinality $c The data cardinality
*/
public function __construct(
string $name,
string $desc,
Scope $scope,
Type $t,
Cardinality $c,
public readonly string $fieldname,
) {
parent::__construct($name, $desc, $scope, $t, $c);
}
}
5 changes: 3 additions & 2 deletions php/libraries/LorisFormDictionaryImpl.class.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php declare(strict_types=1);
use \LORIS\Data\Dictionary\DictionaryItem;
use \LORIS\instruments\DictionaryItem;
use \LORIS\Data\Dictionary\Category;
use \LORIS\Data\Scope;
use \LORIS\Data\Cardinality;
Expand Down Expand Up @@ -142,7 +142,8 @@ trait LorisFormDictionaryImpl
$label,
$scope,
$t,
$card
$card,
$element['name'],
);
}
return $items;
Expand Down
24 changes: 16 additions & 8 deletions php/libraries/NDB_BVL_Instrument.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use \Psr\Http\Server\RequestHandlerInterface;
use \Psr\Http\Message\ResponseInterface;

use \Loris\StudyEntities\Candidate\CandID;
use \LORIS\Data\Dictionary\DictionaryItem;
use \LORIS\instruments\DictionaryItem;

/**
* Base class for all LORIS behavioural instruments.
Expand Down Expand Up @@ -1379,10 +1379,7 @@ abstract class NDB_BVL_Instrument extends NDB_Page
$dictionary = $this->getDataDictionary();

foreach ($dictionary as $item) {
// The Field name to compare to in the data dictionary is
// prefixed with the testname
$dictionaryFieldName = $this->testName."_".$field;
if ($dictionaryFieldName === $item->getName()) {
if ($field === $item->fieldname) {
return $allValues[$field];
}
}
Expand Down Expand Up @@ -2086,6 +2083,17 @@ abstract class NDB_BVL_Instrument extends NDB_Page
return $diff;
}


private function defaultInstanceData() {
$val = [];
$dictionary = $this->getDataDictionary();

foreach ($dictionary as $item) {
$val[$item->fieldname] = null;
}
return $val;
}

/**
* Gets the data from an instrument out of the database and returns it
* as an array.
Expand All @@ -2106,7 +2114,7 @@ abstract class NDB_BVL_Instrument extends NDB_Page
['CID' => $this->getCommentID()]
);

$this->instanceData = json_decode($jsondata ?? '', true) ?? [];
$this->instanceData = json_decode($jsondata ?? '', true) ?? $this->defaultInstanceData();
} else {
$defaults = $db->pselectRow(
"SELECT * FROM $this->table WHERE CommentID=:CID",
Expand All @@ -2118,7 +2126,7 @@ abstract class NDB_BVL_Instrument extends NDB_Page
// NDB_BVL_Instrument->getCommentID()
unset($defaults['CommentID']);

$this->instanceData = $defaults ?? [];
$this->instanceData = $defaults ?? $this->defaultInstanceData();
}
return $this->instanceData;
}
Expand Down Expand Up @@ -2202,7 +2210,7 @@ abstract class NDB_BVL_Instrument extends NDB_Page
$newinst->instanceData = json_decode(
$row['Data'],
true,
) ?? [];
) ?? $this->defaultInstanceData();

return $newinst;
},
Expand Down
15 changes: 13 additions & 2 deletions php/libraries/NDB_BVL_Instrument_LINST.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @link https://www.github.com/aces/Loris-Trunk/
*/
namespace Loris\Behavioural;
use \LORIS\Data\Dictionary\DictionaryItem;
use \LORIS\instruments\DictionaryItem;
use \LORIS\Data\Scope;
use \LORIS\Data\Cardinality;

Expand Down Expand Up @@ -500,6 +500,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$scope,
new \LORIS\Data\Types\DateType(),
new Cardinality(Cardinality::SINGLE),
'Date_taken',
),
]
);
Expand All @@ -514,6 +515,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$scope,
new \LORIS\Data\Types\Duration(),
new Cardinality(Cardinality::SINGLE),
'Candidate_Age',
),
]
);
Expand All @@ -527,6 +529,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$scope,
new \LORIS\Data\Types\Duration(),
new Cardinality(Cardinality::SINGLE),
'Candidate_Age',
),
]
);
Expand All @@ -540,6 +543,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$scope,
new \LORIS\Data\Types\Duration(),
new Cardinality(Cardinality::SINGLE),
'Window_Difference',
),
]
);
Expand All @@ -556,6 +560,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
// array if CommentID is not set.
new StringType(255),
new Cardinality(Cardinality::SINGLE),
'Examiner',
),
]
);
Expand Down Expand Up @@ -616,6 +621,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$scope,
new StringType(255),
new Cardinality(Cardinality::SINGLE),
$pieces[1],
);
if ($firstFieldOfPage) {
$this->_requiredElements[] = $fieldname;
Expand All @@ -641,6 +647,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$scope,
new StringType(),
new Cardinality(Cardinality::SINGLE),
$pieces[1],
);
if ($firstFieldOfPage) {
$this->_requiredElements[] = $fieldname;
Expand Down Expand Up @@ -669,6 +676,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$scope,
new DateType(),
new Cardinality(Cardinality::SINGLE),
$pieces[1],
);
// Set date format
$dateFormat = isset($pieces[5]) ? trim($pieces[5]) : "";
Expand Down Expand Up @@ -741,6 +749,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$scope,
new IntegerType(),
new Cardinality(Cardinality::SINGLE),
$pieces[1],
);
}
if ($firstFieldOfPage) {
Expand Down Expand Up @@ -844,6 +853,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$scope,
$t,
$cardinality,
$pieces[1],
);
$this->dictionary[] = $it;
$this->LinstQuestions[$pieces[1]] = ['type' => 'select'];
Expand Down Expand Up @@ -881,7 +891,8 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
$pieces[2],
$scope,
new StringType(255),
new Cardinality(Cardinality::SINGLE)
new Cardinality(Cardinality::SINGLE),
$pieces[1],
);
}
break;
Expand Down
28 changes: 25 additions & 3 deletions raisinbread/test/api/LorisApiInstrumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,35 @@ public function testGetCandidatesCandidVisitInstrumentsInstrument(): void
$this->assertNotEmpty($body);

$bodystr = $response->getBody()->getContents();
print "body: $bodystr";
$this->assertNotEmpty($bodystr);
$InstrumentsArray = json_decode(
(string) utf8_encode($bodystr),
true
);
$this->markTestIncomplete("Test body not validated");
// $this->assertNotEmpty($InstrumentsArray['Data']);
$this->assertArrayHasKey(
'Candidate',
$InstrumentsArray['Meta']
);
$this->assertArrayHasKey(
'Visit',
$InstrumentsArray['Meta']
);
$this->assertArrayHasKey(
'Instrument',
$InstrumentsArray['Meta']
);
$this->assertArrayHasKey(
'DDE',
$InstrumentsArray['Meta']
);

$this->assertSame($InstrumentsArray['Meta']['DDE'], false);
$this->assertSame($InstrumentsArray['Meta']['Candidate'], $this->candidTest);
$this->assertSame($InstrumentsArray['Meta']['Visit'], $this->visitTest);
$this->assertSame($InstrumentsArray['Meta']['Instrument'], $this->instrumentTest);

$this->assertArrayHasKey('Data', $InstrumentsArray);
$this->assertNotEmpty($InstrumentsArray['Data']);
}

/**
Expand Down
35 changes: 32 additions & 3 deletions raisinbread/test/api/LorisApiInstruments_v0_0_3_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,44 @@ public function testGetCandidatesCandidVisitInstrumentsInstrument(): void
// Verify the endpoint has a body
$body = $response->getBody();
$this->assertNotEmpty($body);
$bodystr = $response->getBody()->getContents();
$this->assertNotEmpty($bodystr);

$InstrumentsArray = json_decode(
(string) utf8_encode(
$response->getBody()->getContents()
$bodystr,
),
true
);
$this->markTestIncomplete('Instrument body not validated');
// $this->assertNotEmpty($InstrumentsArray[$this->instrumentTest]);

$InstrumentsArray = json_decode(
(string) utf8_encode($bodystr),
true
);
$this->assertArrayHasKey(
'Candidate',
$InstrumentsArray['Meta']
);
$this->assertArrayHasKey(
'Visit',
$InstrumentsArray['Meta']
);
$this->assertArrayHasKey(
'Instrument',
$InstrumentsArray['Meta']
);
$this->assertArrayHasKey(
'DDE',
$InstrumentsArray['Meta']
);

$this->assertSame($InstrumentsArray['Meta']['DDE'], false);
$this->assertSame($InstrumentsArray['Meta']['Candidate'], $this->candidTest);
$this->assertSame($InstrumentsArray['Meta']['Visit'], $this->visitTest);
$this->assertSame($InstrumentsArray['Meta']['Instrument'], $this->instrumentTest);

$this->assertArrayHasKey($this->instrumentTest, $InstrumentsArray);
$this->assertNotEmpty($InstrumentsArray[$this->instrumentTest]);
}

/**
Expand Down

0 comments on commit 6f4842c

Please sign in to comment.