Skip to content

Commit 2d4ec21

Browse files
committed
Added support and tests for fileUrl attachments
1 parent 4495a44 commit 2d4ec21

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/RemoteLRS.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,9 @@ public function retrieveStatement($id, $options = array()) {
491491
}
492492

493493
foreach ($response->content->getAttachments() as $attachment) {
494-
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
494+
if (array_key_exists($attachment->getSha2(), $attachmentsByHash)) {
495+
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
496+
}
495497
}
496498
}
497499
else {
@@ -567,7 +569,9 @@ private function _queryStatementsResult(&$response) {
567569

568570
foreach ($response->content->getStatements() as $st) {
569571
foreach ($st->getAttachments() as $attachment) {
570-
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
572+
if (array_key_exists($attachment->getSha2(), $attachmentsByHash)) {
573+
$attachment->setContent($attachmentsByHash[$attachment->getSha2()]['body']);
574+
}
571575
}
572576
}
573577

tests/RemoteLRSTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,44 @@ public function testMoreStatementsWithAttachments() {
374374
$this->assertInstanceOf('TinCan\StatementsResult', $response->content, 'content');
375375
}
376376

377+
public function testRetrieveStatementWithFileUrlAttachments() {
378+
$lrs = new RemoteLRS(self::$endpoint, self::$version, self::$username, self::$password);
379+
$attachments = new Attachment();
380+
$pdfUrl = 'http://www.adlnet.gov/wp-content/uploads/2013/10/xAPI_v1.0.1-2013-10-01.pdf';
381+
// Store Attachments in and retrieve them from the LRS
382+
$attachments
383+
->setUsageType('http://adlnet.gov/expapi/activities/media')
384+
->setDisplay(['en-US' => 'Test PDF document'])
385+
->setContentType('application/pdf')
386+
->setLength(filesize('tests/files/attachment.pdf'))
387+
->setSha2(hash_file('sha256', 'tests/files/attachment.pdf')) // hash of the attachment data
388+
->setFileUrl($pdfUrl)
389+
->setDescription(['en-US' => 'A test document used in an Attachments object example.']);
390+
391+
// Compose statement for sending to the LRS
392+
$statement = new Statement(
393+
[
394+
'actor' => [
395+
'mbox' => COMMON_MBOX
396+
],
397+
'verb' => [
398+
'id' => COMMON_VERB_ID
399+
],
400+
'object' => new Activity([
401+
'id' => COMMON_ACTIVITY_ID
402+
])
403+
]
404+
);
405+
$statement->setAttachments([$attachments]);
406+
$saveResponse = $lrs->saveStatement($statement);
407+
$statementResponse = $lrs->retrieveStatement($saveResponse->content->getId(), ['attachments' => true]);
408+
409+
$this->assertInstanceOf('TinCan\LRSResponse', $statementResponse);
410+
$this->assertTrue($statementResponse->success);
411+
$this->assertInstanceOf('TinCan\Statement', $statementResponse->content);
412+
$this->assertEquals($pdfUrl, $statementResponse->content->getAttachments()[0]->getFileUrl());
413+
}
414+
377415
public function testRetrieveStateIds() {
378416
$lrs = new RemoteLRS(self::$endpoint, self::$version, self::$username, self::$password);
379417
$response = $lrs->retrieveStateIds(

tests/files/attachment.pdf

1.71 MB
Binary file not shown.

0 commit comments

Comments
 (0)