Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/h5p-editor-de…
Browse files Browse the repository at this point in the history
…pendency
  • Loading branch information
daVitekPL committed Jul 19, 2024
2 parents 73c1e7f + 67cc9ee commit 6c67372
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 14 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/stale-bot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: 'Close stale issues and PRs'
on:
schedule:
- cron: '30 1 * * *'

jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v9
with:
stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.'
days-before-stale: 30
days-before-close: 5
2 changes: 1 addition & 1 deletion .github/workflows/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: composer update

- name: Require composer swagger package
run: composer require darkaonline/l5-swagger:8.1.0
run: composer require darkaonline/l5-swagger

- name: Discover packages
run: vendor/bin/testbench package:discover --ansi
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/JSONHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static function clearJson($json): string
$json = json_encode($json);
}

$json = str_replace(['\n', '\t'], '', $json);
$json = str_replace(['\t'], '', $json);
$json = str_replace(['\"', '"'], '\'', $json);
return str_replace(['\/'], '/', $json);
}
Expand Down
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
19 changes: 14 additions & 5 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 All @@ -99,9 +106,11 @@ public function edit(int $id, string $library, string $params, string $nonce): i
'library_id' => $libDb->id,
'library' => $library,
'parameters' => $params,
'filtered' => isset($json->params) ? json_encode($json->params) : $content['filtered']
//'filtered' => isset($json->params) ? json_encode($json->params) : $content['filtered']
], $id);


$this->filterParameters($content, $libDb);
$this->moveTmpFilesToContentFolders($nonce, $id);

return $id;
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 @@ -683,7 +683,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 @@ -868,7 +868,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 @@ -907,6 +907,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

0 comments on commit 6c67372

Please sign in to comment.