Skip to content

Commit

Permalink
Add libraryName attribute (#76)
Browse files Browse the repository at this point in the history
* Add libraryName attribute

* Additional test

---------

Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero authored Nov 2, 2023
1 parent 706fc5b commit fa79d55
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Models/TopicContent/H5P.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public function getLengthAttribute(): ?int
}
}

public function getLibraryNameAttribute(): ?string
{
try {
$contentRepository = App::make(H5PContentRepositoryContract::class);
$content = $contentRepository->show($this->value);
$library = $content->library;

return isset($library) ? $library->name : null;
} catch (Throwable $e) {
return null;
}
}

public function getMorphClass()
{
return self::class;
Expand Down
21 changes: 21 additions & 0 deletions tests/H5P/TopicTypeH5PTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,41 @@ public function testH5PLength(string $filename, int $length): void
$this->assertEquals($length, $h5p->length);
}

/**
* @dataProvider h5pProvider
*/
public function testH5PLibraryName(string $filename, int $length, string $libraryName): void
{
$filepath = realpath(__DIR__.'/../mocks/'.$filename);
$storage_path = storage_path($filename);
copy($filepath, $storage_path);

/** @var H5PContentRepository $repository */
$repository = app(H5PContentRepository::class);
$content = $repository->upload(new UploadedFile($storage_path, $filename, null, null, true));

$h5p = H5P::factory()->create(['value' => $content->id]);

$this->assertEquals($libraryName, $h5p->libraryName);
}

public function h5pProvider(): array
{
return [
[
'filename' => 'accordion.h5p',
'length' => 4,
'libraryName' => 'H5P.Accordion',
],
[
'filename' => 'agamotto.h5p',
'length' => 3,
'libraryName' => 'H5P.Agamotto',
],
[
'filename' => 'find-the-hotspot.h5p',
'length' => 1,
'libraryName' => 'H5P.ImageHotspotQuestion',
],
];
}
Expand Down

0 comments on commit fa79d55

Please sign in to comment.