Skip to content

Commit

Permalink
set content title from uploaded file (#157)
Browse files Browse the repository at this point in the history
* set content title from uploaded file

* remove getter

Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero authored Aug 19, 2022
1 parent 31767b1 commit 070a118
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/Repositories/Contracts/H5PFrameworkInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace EscolaLms\HeadlessH5P\Repositories\Contracts;

use H5PFrameworkInterface as H5PFrameworkInterfaceCore;

interface H5PFrameworkInterface extends H5PFrameworkInterfaceCore
{
public function setMainData(array $mainData): void;
}
1 change: 1 addition & 0 deletions src/Repositories/H5PContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public function upload($file, $content = null, $only_upgrade = null, $disable_h5
$valid = $this->hh5pService->validatePackage($file, false, false);

if ($valid) {
$this->hh5pService->getRepository()->setMainData($this->hh5pService->getCore()->mainJsonData);
$this->hh5pService->getStorage()->savePackage();
// $this->hh5pService->getRepository()->deleteLibraryUsage($content['id']);
$id = $this->hh5pService->getStorage()->contentId;
Expand Down
11 changes: 9 additions & 2 deletions src/Repositories/H5PRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use EscolaLms\HeadlessH5P\Models\H5PLibraryDependency;
use EscolaLms\HeadlessH5P\Models\H5PLibraryLanguage;
use EscolaLms\HeadlessH5P\Models\H5pLibrariesHubCache;
use EscolaLms\HeadlessH5P\Repositories\Contracts\H5PFrameworkInterface;
use H5PPermission;
use H5PFrameworkInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Facades\DB;
Expand All @@ -25,6 +25,13 @@ class H5PRepository implements H5PFrameworkInterface

private array $downloadFiles;

private array $mainData;

public function setMainData(array $mainData): void
{
$this->mainData = $mainData;
}

/**
* Returns info for the current platform.
*
Expand Down Expand Up @@ -519,7 +526,7 @@ public function insertContent($content, $contentMainId = null)

private function fixContentParamsMetadataLibraryTitle($content)
{
$defaultTitle = 'New Content (from file)';
$defaultTitle = isset($this->mainData['title']) ? $this->mainData['title'] : 'New Content (from file)';

if (is_array($content['library'])) {
$content['library_id'] = isset($content['library']['libraryId']) ? $content['library']['libraryId'] : $content['library']['id'];
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 @@ -2,10 +2,10 @@

namespace EscolaLms\HeadlessH5P\Services\Contracts;

use EscolaLms\HeadlessH5P\Repositories\Contracts\H5PFrameworkInterface;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;

use H5PFrameworkInterface;
use H5PFileStorage;
use H5PStorage;
use H5PCore;
Expand Down
2 changes: 1 addition & 1 deletion src/Services/HeadlessH5PService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use EscolaLms\HeadlessH5P\Exceptions\H5PException;
use EscolaLms\HeadlessH5P\Helpers\MargeFiles;
use EscolaLms\HeadlessH5P\Models\H5PLibrary;
use EscolaLms\HeadlessH5P\Repositories\Contracts\H5PFrameworkInterface;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use H5PContentValidator;
use H5PCore;
Expand All @@ -13,7 +14,6 @@
use H5peditorFile;
use H5peditorStorage;
use H5PFileStorage;
use H5PFrameworkInterface;
use H5PStorage;
use H5PValidator;
use H5PPermission;
Expand Down
43 changes: 41 additions & 2 deletions tests/Api/ContentApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,43 @@ public function testContentList(): void
$this->assertContentListResponse($response);
}

public function testContentUnpaginatedList(): void
{
H5PContent::factory()
->count(50)
->create(['library_id' => H5PLibrary::factory()->create(['runnable' => 1])->getKey()]);

$this->authenticateAsAdmin();
$response = $this->actingAs($this->user, 'api')
->get('/api/admin/hh5p/content?per_page=0');

$response
->assertOk()
->assertJsonCount(50, 'data')
->assertJsonStructure(['data' => [[
'id',
'created_at',
'updated_at',
'user_id',
'author',
'title',
'library_id',
'library' => [
'id',
'name',
'title',
'created_at',
'updated_at',
'machineName',
'uberName',
'libraryId',
],
'slug',
'filtered',
'disable',
]]]);
}

public function testContentListFilterByAuthorId(): void
{
H5PContent::factory()
Expand Down Expand Up @@ -489,11 +526,13 @@ public function testContentUploading(): void
$response->assertStatus(200);

$data = $response->getData()->data;

$this->assertTrue(is_integer($data->id));
$this->assertTrue(is_object($data->params));
$this->assertEquals('Arithmetic Quiz', $data->title);
$this->assertNotEquals('New Content (from file)', $data->title);
$this->assertDatabaseHas('hh5p_contents', [
'uuid' => $data->uuid
'uuid' => $data->uuid,
'title' => 'Arithmetic Quiz'
]);
}

Expand Down

0 comments on commit 070a118

Please sign in to comment.