Skip to content

Commit 9bf3734

Browse files
committed
[test/api] Add tests for getting instrument data from API.
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.
1 parent c2f41af commit 9bf3734

8 files changed

+151
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace LORIS\instruments;
4+
5+
use \LORIS\Data\Scope;
6+
use \LORIS\Data\Type;
7+
use \LORIS\Data\Cardinality;
8+
9+
/**
10+
* A DictionaryItem represents a description of a type of data
11+
* managed by LORIS. An Instrument DictionaryItem differs slightly
12+
* from the parent, in that it also has a fieldname property to
13+
* get the name of the field without the "testname_" prefix that
14+
* is added to the name in order to ensure that the name is unique
15+
* across instruments for the QueryEngine.
16+
*
17+
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
18+
*/
19+
class DictionaryItem extends \LORIS\Data\Dictionary\DictionaryItem
20+
{
21+
/**
22+
* Construct a DictionaryItem with the given parameters
23+
*
24+
* @param string $name The field name of the dictionary item
25+
* @param string $desc The dictionary item's description
26+
* @param Scope $scope The scope to which this DictionaryItem
27+
* applies
28+
* @param Type $t The data type of this dictionary item
29+
* @param Cardinality $c The data cardinality
30+
* @param string $fieldname The non-prefixed field name
31+
*/
32+
public function __construct(
33+
string $name,
34+
string $desc,
35+
Scope $scope,
36+
Type $t,
37+
Cardinality $c,
38+
public readonly string $fieldname,
39+
) {
40+
parent::__construct($name, $desc, $scope, $t, $c);
41+
}
42+
}

php/libraries/LorisFormDictionaryImpl.class.inc

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php declare(strict_types=1);
2-
use \LORIS\Data\Dictionary\DictionaryItem;
2+
use \LORIS\instruments\DictionaryItem;
33
use \LORIS\Data\Dictionary\Category;
44
use \LORIS\Data\Scope;
55
use \LORIS\Data\Cardinality;
@@ -142,7 +142,8 @@ trait LorisFormDictionaryImpl
142142
$label,
143143
$scope,
144144
$t,
145-
$card
145+
$card,
146+
$element['name'],
146147
);
147148
}
148149
return $items;

php/libraries/NDB_BVL_Instrument.class.inc

+26-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use \Psr\Http\Server\RequestHandlerInterface;
44
use \Psr\Http\Message\ResponseInterface;
55

66
use \Loris\StudyEntities\Candidate\CandID;
7-
use \LORIS\Data\Dictionary\DictionaryItem;
7+
use \LORIS\instruments\DictionaryItem;
88

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

13811381
foreach ($dictionary as $item) {
1382-
// The Field name to compare to in the data dictionary is
1383-
// prefixed with the testname
1384-
$dictionaryFieldName = $this->testName."_".$field;
1385-
if ($dictionaryFieldName === $item->getName()) {
1382+
if ($field === $item->fieldname) {
13861383
return $allValues[$field];
13871384
}
13881385
}
@@ -2086,6 +2083,24 @@ abstract class NDB_BVL_Instrument extends NDB_Page
20862083
return $diff;
20872084
}
20882085

2086+
2087+
/**
2088+
* Get the default instance data which consists of all field names
2089+
* in the dictionary and a null value.
2090+
*
2091+
* @return array
2092+
*/
2093+
protected function defaultInstanceData()
2094+
{
2095+
$val = [];
2096+
$dictionary = $this->getDataDictionary();
2097+
2098+
foreach ($dictionary as $item) {
2099+
$val[$item->fieldname] = null;
2100+
}
2101+
return $val;
2102+
}
2103+
20892104
/**
20902105
* Gets the data from an instrument out of the database and returns it
20912106
* as an array.
@@ -2106,7 +2121,10 @@ abstract class NDB_BVL_Instrument extends NDB_Page
21062121
['CID' => $this->getCommentID()]
21072122
);
21082123

2109-
$this->instanceData = json_decode($jsondata ?? '', true) ?? [];
2124+
$this->instanceData = json_decode(
2125+
$jsondata ?? '',
2126+
true
2127+
) ?? $this->defaultInstanceData();
21102128
} else {
21112129
$defaults = $db->pselectRow(
21122130
"SELECT * FROM $this->table WHERE CommentID=:CID",
@@ -2118,7 +2136,7 @@ abstract class NDB_BVL_Instrument extends NDB_Page
21182136
// NDB_BVL_Instrument->getCommentID()
21192137
unset($defaults['CommentID']);
21202138

2121-
$this->instanceData = $defaults ?? [];
2139+
$this->instanceData = $defaults ?? $this->defaultInstanceData();
21222140
}
21232141
return $this->instanceData;
21242142
}
@@ -2202,7 +2220,7 @@ abstract class NDB_BVL_Instrument extends NDB_Page
22022220
$newinst->instanceData = json_decode(
22032221
$row['Data'],
22042222
true,
2205-
) ?? [];
2223+
) ?? $this->defaultInstanceData();
22062224

22072225
return $newinst;
22082226
},

php/libraries/NDB_BVL_Instrument_LINST.class.inc

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @link https://www.github.com/aces/Loris-Trunk/
1212
*/
1313
namespace Loris\Behavioural;
14-
use \LORIS\Data\Dictionary\DictionaryItem;
14+
use \LORIS\instruments\DictionaryItem;
1515
use \LORIS\Data\Scope;
1616
use \LORIS\Data\Cardinality;
1717

@@ -500,6 +500,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
500500
$scope,
501501
new \LORIS\Data\Types\DateType(),
502502
new Cardinality(Cardinality::SINGLE),
503+
'Date_taken',
503504
),
504505
]
505506
);
@@ -514,6 +515,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
514515
$scope,
515516
new \LORIS\Data\Types\Duration(),
516517
new Cardinality(Cardinality::SINGLE),
518+
'Candidate_Age',
517519
),
518520
]
519521
);
@@ -527,6 +529,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
527529
$scope,
528530
new \LORIS\Data\Types\Duration(),
529531
new Cardinality(Cardinality::SINGLE),
532+
'Candidate_Age',
530533
),
531534
]
532535
);
@@ -540,6 +543,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
540543
$scope,
541544
new \LORIS\Data\Types\Duration(),
542545
new Cardinality(Cardinality::SINGLE),
546+
'Window_Difference',
543547
),
544548
]
545549
);
@@ -556,6 +560,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
556560
// array if CommentID is not set.
557561
new StringType(255),
558562
new Cardinality(Cardinality::SINGLE),
563+
'Examiner',
559564
),
560565
]
561566
);
@@ -616,6 +621,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
616621
$scope,
617622
new StringType(255),
618623
new Cardinality(Cardinality::SINGLE),
624+
$pieces[1],
619625
);
620626
if ($firstFieldOfPage) {
621627
$this->_requiredElements[] = $fieldname;
@@ -641,6 +647,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
641647
$scope,
642648
new StringType(),
643649
new Cardinality(Cardinality::SINGLE),
650+
$pieces[1],
644651
);
645652
if ($firstFieldOfPage) {
646653
$this->_requiredElements[] = $fieldname;
@@ -669,6 +676,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
669676
$scope,
670677
new DateType(),
671678
new Cardinality(Cardinality::SINGLE),
679+
$pieces[1],
672680
);
673681
// Set date format
674682
$dateFormat = isset($pieces[5]) ? trim($pieces[5]) : "";
@@ -741,6 +749,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
741749
$scope,
742750
new IntegerType(),
743751
new Cardinality(Cardinality::SINGLE),
752+
$pieces[1],
744753
);
745754
}
746755
if ($firstFieldOfPage) {
@@ -844,6 +853,7 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
844853
$scope,
845854
$t,
846855
$cardinality,
856+
$pieces[1],
847857
);
848858
$this->dictionary[] = $it;
849859
$this->LinstQuestions[$pieces[1]] = ['type' => 'select'];
@@ -881,7 +891,8 @@ class NDB_BVL_Instrument_LINST extends \NDB_BVL_Instrument
881891
$pieces[2],
882892
$scope,
883893
new StringType(255),
884-
new Cardinality(Cardinality::SINGLE)
894+
new Cardinality(Cardinality::SINGLE),
895+
$pieces[1],
885896
);
886897
}
887898
break;

raisinbread/test/api/LorisApiInstrumentsTest.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,35 @@ public function testGetCandidatesCandidVisitInstrumentsInstrument(): void
8989
$this->assertNotEmpty($body);
9090

9191
$bodystr = $response->getBody()->getContents();
92-
print "body: $bodystr";
92+
$this->assertNotEmpty($bodystr);
9393
$InstrumentsArray = json_decode(
9494
(string) utf8_encode($bodystr),
9595
true
9696
);
97-
$this->markTestIncomplete("Test body not validated");
98-
// $this->assertNotEmpty($InstrumentsArray['Data']);
97+
$this->assertArrayHasKey(
98+
'Candidate',
99+
$InstrumentsArray['Meta']
100+
);
101+
$this->assertArrayHasKey(
102+
'Visit',
103+
$InstrumentsArray['Meta']
104+
);
105+
$this->assertArrayHasKey(
106+
'Instrument',
107+
$InstrumentsArray['Meta']
108+
);
109+
$this->assertArrayHasKey(
110+
'DDE',
111+
$InstrumentsArray['Meta']
112+
);
113+
114+
$this->assertSame($InstrumentsArray['Meta']['DDE'], false);
115+
$this->assertSame($InstrumentsArray['Meta']['Candidate'], $this->candidTest);
116+
$this->assertSame($InstrumentsArray['Meta']['Visit'], $this->visitTest);
117+
$this->assertSame($InstrumentsArray['Meta']['Instrument'], $this->instrumentTest);
118+
119+
$this->assertArrayHasKey('Data', $InstrumentsArray);
120+
$this->assertNotEmpty($InstrumentsArray['Data']);
99121
}
100122

101123
/**

raisinbread/test/api/LorisApiInstruments_v0_0_3_Test.php

+32-3
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,44 @@ public function testGetCandidatesCandidVisitInstrumentsInstrument(): void
8787
// Verify the endpoint has a body
8888
$body = $response->getBody();
8989
$this->assertNotEmpty($body);
90+
$bodystr = $response->getBody()->getContents();
91+
$this->assertNotEmpty($bodystr);
9092

9193
$InstrumentsArray = json_decode(
9294
(string) utf8_encode(
93-
$response->getBody()->getContents()
95+
$bodystr,
9496
),
9597
true
9698
);
97-
$this->markTestIncomplete('Instrument body not validated');
98-
// $this->assertNotEmpty($InstrumentsArray[$this->instrumentTest]);
99+
100+
$InstrumentsArray = json_decode(
101+
(string) utf8_encode($bodystr),
102+
true
103+
);
104+
$this->assertArrayHasKey(
105+
'Candidate',
106+
$InstrumentsArray['Meta']
107+
);
108+
$this->assertArrayHasKey(
109+
'Visit',
110+
$InstrumentsArray['Meta']
111+
);
112+
$this->assertArrayHasKey(
113+
'Instrument',
114+
$InstrumentsArray['Meta']
115+
);
116+
$this->assertArrayHasKey(
117+
'DDE',
118+
$InstrumentsArray['Meta']
119+
);
120+
121+
$this->assertSame($InstrumentsArray['Meta']['DDE'], false);
122+
$this->assertSame($InstrumentsArray['Meta']['Candidate'], $this->candidTest);
123+
$this->assertSame($InstrumentsArray['Meta']['Visit'], $this->visitTest);
124+
$this->assertSame($InstrumentsArray['Meta']['Instrument'], $this->instrumentTest);
125+
126+
$this->assertArrayHasKey($this->instrumentTest, $InstrumentsArray);
127+
$this->assertNotEmpty($InstrumentsArray[$this->instrumentTest]);
99128
}
100129

101130
/**

test/unittests/NDB_BVL_Instrument_LINST_ToJSON_Test.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
set_include_path(get_include_path().":" . __DIR__ . "/../../php/libraries:");
44

55
require_once __DIR__ . '/../../vendor/autoload.php';
6+
require_once __DIR__ . '/../../modules/instruments/php/dictionaryitem.class.inc';
67
require_once __DIR__ . '/../../php/libraries/NDB_BVL_Instrument_LINST.class.inc';
78
require_once 'Smarty_hook.class.inc';
89
require_once 'NDB_Config.class.inc';

test/unittests/NDB_BVL_Instrument_Test.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
set_include_path(get_include_path().":" . __DIR__ . "/../../php/libraries:");
1515
use PHPUnit\Framework\TestCase;
1616
require_once __DIR__ . '/../../vendor/autoload.php';
17+
require_once __DIR__ . '/../../modules/instruments/dictionaryitem.class.inc';
1718
require_once __DIR__ . '/../../php/libraries/NDB_BVL_Instrument.class.inc';
1819
require_once 'Smarty_hook.class.inc';
1920
require_once 'NDB_Config.class.inc';
@@ -119,13 +120,19 @@ function setUp(): void
119120
$this->quickForm = new \LorisForm();
120121

121122
$dictionaryItem = $this->getMockBuilder(
122-
\LORIS\Data\Dictionary\DictionaryItem::class
123+
\LORIS\instruments\DictionaryItem::class
123124
)
124125
->disableOriginalConstructor()
125126
->getMock();
126127
$dictionaryItem->method('getName')
127128
->willReturn('Test_Date_taken');
128-
'@phan-var \LORIS\Data\Dictionary\DictionaryItem $dictionaryItem';
129+
'@phan-var \LORIS\instruments\DictionaryItem $dictionaryItem';
130+
131+
// Use reflection to modify fieldname, since the constructor
132+
// was disabled and it's a readonly property.
133+
$ref = new \ReflectionProperty(get_class($dictionaryItem), 'fieldname');
134+
$ref->setAccessible(true);
135+
$ref->setValue($dictionaryItem, 'Date_taken');
129136

130137
$instrument = $this->getMockBuilder(\NDB_BVL_Instrument::class)
131138
->disableOriginalConstructor()

0 commit comments

Comments
 (0)