Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/library fix #211

Merged
merged 12 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Http/Controllers/LibraryApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function libraries(LibraryListRequest $request): JsonResponse
$libraries = $this->hh5pService->getLibraries(
$request->get('machineName'),
$request->get('majorVersion'),
$request->get('minorVersion')
$request->get('minorVersion'),
$request->get('library_id')
);

return response()->json($libraries);
Expand Down
1 change: 1 addition & 0 deletions src/Models/H5PContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
* @property array $parameters
* @property int $user_id
* @property H5PLibrary $library
* @property int $library_id
*/
class H5PContent extends Model
{
Expand Down
1 change: 1 addition & 0 deletions src/Models/H5PLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
* @property string $name
* @property string $embedTypes
* @property int $count
* @property bool $restricted
*/
class H5PLibrary extends Model
{
Expand Down
15 changes: 11 additions & 4 deletions src/Repositories/H5PContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function create(string $library, string $params, string $nonce): int
['name', $libNames['machineName']],
['major_version', $libNames['majorVersion']],
['minor_version', $libNames['minorVersion']],
])->first();
])->latest()->first();

if ($libDb === null) {
throw new H5PException(H5PException::LIBRARY_NOT_FOUND);
Expand Down Expand Up @@ -70,26 +70,33 @@ public function create(string $library, string $params, string $nonce): int

public function edit(int $id, string $library, string $params, string $nonce): int
{
$content = H5PContent::where('id', $id)->first();
$previousLib = H5PLibrary::where('id', $content->library_id)->first();

$libNames = $this->hh5pService->getCore()->libraryFromString($library);

$libDb = H5PLibrary::where([
['name', $libNames['machineName']],
['major_version', $libNames['majorVersion']],
['minor_version', $libNames['minorVersion']],
])->first();
])->latest()->first();

if ($libDb === null) {
throw new H5PException(H5PException::LIBRARY_NOT_FOUND);
}

// don't update library version if there is new
if ($previousLib->name === $libDb->name) {
$libDb = $previousLib;
$library = $previousLib->uberName;
}

$json = json_decode($params);

if ($json === null) {
throw new H5PException(H5PException::INVALID_PARAMETERS_JSON);
}

$content = H5PContent::where('id', $id)->first();

if ($content === null) {
throw new H5PException(H5PException::CONTENT_NOT_FOUND);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Repositories/H5PEditorStorageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getLanguage($machineName, $majorVersion, $minorVersion, $languag
['major_version', $majorVersion],
['minor_version', $minorVersion],
['name', $machineName],
])->first();
])->latest()->first();

if ($library) {
$libraryLanguage = H5PLibraryLanguage::where([
Expand Down Expand Up @@ -118,7 +118,7 @@ public function getLibraries($libraries = null)
['name', $library->name],
['major_version', $library->majorVersion],
['minor_version', $library->minorVersion]
])->first())
])->latest()->first())
->reject(fn($library) => !$library)
->values()
->all();
Expand Down
5 changes: 3 additions & 2 deletions src/Repositories/H5PRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ public function saveLibraryDependencies($libraryId, $dependencies, $dependency_t
'name' => $dependency['machineName'],
'major_version' => $dependency['majorVersion'],
'minor_version' => $dependency['minorVersion'],
])->firstOrFail();
])->latest()->firstOrFail();

H5PLibraryDependency::firstOrCreate([
'library_id' => $libraryId,
Expand Down Expand Up @@ -863,7 +863,7 @@ public function loadLibrary($machineName, $majorVersion, $minorVersion)
'minor_version' => $minorVersion,
])
->with('dependencies.requiredLibrary')
->first();
->latest()->first();

if (is_null($library)) {
return false;
Expand Down Expand Up @@ -902,6 +902,7 @@ public function loadLibrarySemantics($machineName, $majorVersion, $minorVersion)
$library = H5PLibrary::where('name', $machineName)
->where('major_version', $majorVersion)
->where('minor_version', $minorVersion)
->latest()
->first();

$semanticsFile = $this->getSemanticsFromFile($machineName, $majorVersion, $minorVersion);
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Contracts/HeadlessH5PServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function listLibraries(): Collection;

public function getConfig(): array;

public function getLibraries(string $machineName = null, string $major_version = null, string $minor_version = null);
public function getLibraries(string $machineName = null, string $major_version = null, string $minor_version = null, int $library_id = null);

public function getEditorSettings($content = null): array;

Expand Down
17 changes: 16 additions & 1 deletion src/Services/HeadlessH5PService.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,26 @@ public function getConfig(): array
/**
* Calls editor ajax actions.
*/
public function getLibraries(string $machineName = null, string $major_version = null, string $minor_version = null)
public function getLibraries(string $machineName = null, string $major_version = null, string $minor_version = null, int $library_id = null)
{
$lang = config('hh5p.language');
$libraries_url = Storage::url(config('hh5p.h5p_library_url'));

if ($library_id) {
$library = H5PLibrary::findOrFail($library_id);

if (in_array($library->name, array('H5P.Questionnaire', 'H5P.FreeTextQuestion')) &&
!$this->core->h5pF->getOption('enable_lrs_content_types')) {
$library->restricted = TRUE;
}
$this->addMoreHtmlTags($library->semantics);
$library
->append('contentsCount')
->append('requiredLibrariesCount');

return collect([$library]);
}

if ($machineName) {
$defaultLang = $this->getEditor()->getLibraryLanguage($machineName, $major_version, $minor_version, $lang);
$data = $this->getEditor()->getLibraryData($machineName, $major_version, $minor_version, $lang, '', $libraries_url, $defaultLang);
Expand Down
29 changes: 29 additions & 0 deletions tests/Api/ContentApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,35 @@ public function testContentUpdateInvalidJson(): void
$response->assertStatus(422);
}

public function testContentUpdateNewLibraryVersion(): void
{
$this->authenticateAsAdmin();
$libraryName = 'DragTheWords';
$library = H5PLibrary::factory()->create(['name' => $libraryName, 'runnable' => 1]);
$newVersion = H5PLibrary::factory()->create(['name' => $libraryName, 'runnable' => 1, 'patch_version' => 10]);
$content = H5PContent::factory()->create([
'library_id' => $library->getKey()
]);
$id = $content->id;

$this->actingAs($this->user, 'api')->postJson("/api/admin/hh5p/content/$id", [
'nonce' => bin2hex(random_bytes(4)),
'library' => $newVersion->uberName,
'params' => '{"params":{"taskDescription":"Documentation tool","pagesList":[{"params":{"elementList":[{"params":{},"library":"H5P.Text 1.1","metadata":{"contentType":"Text","license":"U","title":"Untitled Text","authors":[],"changes":[],"extraTitle":"Untitled Text"},"subContentId":"da3387da-355a-49fb-92bc-3a9a4e4646a9"}],"helpTextLabel":"More information","helpText":""},"library":"H5P.StandardPage 1.5","metadata":{"contentType":"Standard page","license":"U","title":"Untitled Standard page","authors":[],"changes":[],"extraTitle":"Untitled Standard page"},"subContentId":"ac6ffdac-be02-448c-861c-969e6a09dbd5"}],"i10n":{"previousLabel":"poprzedni","nextLabel":"Next","closeLabel":"Close"}},"metadata":{"license":"U","authors":[],"changes":[],"extraTitle":"fdsfds","title":"fdsfds"}}',
])
->assertStatus(200);

$this->assertDatabaseHas('hh5p_contents', [
'id' => $id,
'library_id' => $library->getKey(),
]);

$this->assertDatabaseMissing('hh5p_contents', [
'id' => $id,
'library_id' => $newVersion->getKey(),
]);
}

public function testContentUpdateAuthor(): void
{
$this->authenticateAsUser();
Expand Down
63 changes: 63 additions & 0 deletions tests/Api/LibraryApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,69 @@ public function testGetLibrariesAdmin(): void
$this->assertEquals(7, current(array_filter($data, fn($item) => $item->id === $lib2->getKey()))->requiredLibrariesCount);
}

public function testGetLibraryById(): void
{
$this->authenticateAsAdmin();
$libraryName = 'DragTheWord';

$lib1 = H5PLibrary::factory()->create(['name' => $libraryName, 'patch_version' => 1]);
$lib2 = H5PLibrary::factory()->create(['name' => $libraryName, 'patch_version' => 2]);
H5PLibraryDependency::factory()->count(3)->create(['required_library_id' => $lib1->getKey()]);
H5PLibraryDependency::factory()->count(7)->create(['required_library_id' => $lib2->getKey()]);
H5PContent::factory()->count(2)->create(['library_id' => $lib1->getKey()]);
H5PContent::factory()->count(5)->create(['library_id' => $lib2->getKey()]);

$this
->actingAs($this->user, 'api')
->json('GET', 'api/hh5p/libraries', ['machnieName' => $libraryName, 'majorVersion' => 1, 'minorVersion' => 1])
->assertOk()
->assertJsonFragment([
'id' => $lib1->getKey(),
'patchVersion' => 1,
]);

$this
->actingAs($this->user, 'api')
->json('GET', 'api/hh5p/libraries', ['library_id' => $lib1->getKey()])
->assertOk()
->assertJsonFragment([
'id' => $lib1->getKey(),
'patchVersion' => 1,
]);
}

public function testGetLibraryByIdAdmin(): void
{
$this->authenticateAsAdmin();

$libraryName = 'DragTheWord';

$lib1 = H5PLibrary::factory()->create(['name' => $libraryName, 'patch_version' => 1]);
$lib2 = H5PLibrary::factory()->create(['name' => $libraryName, 'patch_version' => 2]);
H5PLibraryDependency::factory()->count(3)->create(['required_library_id' => $lib1->getKey()]);
H5PLibraryDependency::factory()->count(7)->create(['required_library_id' => $lib2->getKey()]);
H5PContent::factory()->count(2)->create(['library_id' => $lib1->getKey()]);
H5PContent::factory()->count(5)->create(['library_id' => $lib2->getKey()]);

$this
->actingAs($this->user, 'api')
->json('GET', 'api/admin/hh5p/libraries', ['machnieName' => $libraryName, 'majorVersion' => 1, 'minorVersion' => 1])
->assertOk()
->assertJsonFragment([
'id' => $lib2->getKey(),
'patchVersion' => 2,
]);

$this
->actingAs($this->user, 'api')
->json('GET', 'api/admin/hh5p/libraries', ['library_id' => $lib1->getKey()])
->assertOk()
->assertJsonFragment([
'id' => $lib1->getKey(),
'patchVersion' => 1,
]);
}

public function test_reinstall_library_dependencies()
{
$this->authenticateAsAdmin();
Expand Down
Loading