Skip to content

Commit

Permalink
fixes (#146)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero authored Aug 17, 2022
1 parent 8d64965 commit 900be78
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 13 deletions.
12 changes: 10 additions & 2 deletions src/Http/Controllers/ContentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ public function __construct(HeadlessH5PServiceContract $hh5pService, H5PContentR
public function index(ContentListRequest $request): JsonResponse
{
$contentFilterDto = ContentFilterCriteriaDto::instantiateFromRequest($request);
$columns = [
'hh5p_contents.title',
'hh5p_contents.id',
'hh5p_contents.uuid',
'hh5p_contents.library_id',
'hh5p_contents.user_id',
'hh5p_contents.author'
];
$list = $request->get('per_page') !== null && $request->get('per_page') == 0 ?
$this->contentRepository->unpaginatedList($contentFilterDto) :
$this->contentRepository->list($contentFilterDto, $request->get('per_page'));
$this->contentRepository->unpaginatedList($contentFilterDto, $columns) :
$this->contentRepository->list($contentFilterDto, $request->get('per_page'), $columns);

return $this->sendResponseForResource(ContentIndexResource::collection($list));
}
Expand Down
1 change: 0 additions & 1 deletion src/Repositories/H5PContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ private function moveTmpFilesToContentFolders($nonce, $contentId): bool

$files = H5PTempFile::where(['nonce' => $nonce])->get();


foreach ($files as $file) {
$old_path = $storage_path . $file->path;
if (strpos($file->path, '/editor') !== false) {
Expand Down
1 change: 1 addition & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ providers:
- Spatie\Permission\PermissionServiceProvider
- Laravel\Passport\PassportServiceProvider
- EscolaLms\Core\EscolaLmsServiceProvider
- EscolaLms\Settings\EscolaLmsSettingsServiceProvider
- EscolaLms\HeadlessH5P\HeadlessH5PServiceProvider
11 changes: 11 additions & 0 deletions tests/Models/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace EscolaLms\HeadlessH5P\Tests\Models;

class Client extends \Laravel\Passport\Client
{
public function getIdAttribute()
{
return $this->attributes['id'];
}
}
9 changes: 9 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace EscolaLms\HeadlessH5P\Tests\Models;

use EscolaLms\Courses\Models\User as CoursesUser;

class User extends CoursesUser
{
}
29 changes: 19 additions & 10 deletions tests/Repositories/H5PEditorAjaxRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace EscolaLms\HeadlessH5P\Tests\Repositories;


use Carbon\Carbon;
use EscolaLms\Core\Tests\CreatesUsers;
use EscolaLms\HeadlessH5P\Models\H5PContent;
use EscolaLms\HeadlessH5P\Models\H5PContentLibrary;
Expand Down Expand Up @@ -164,24 +165,32 @@ public function testGetAuthorsRecentlyUsedLibrariesShouldGetEmptyArrayWhenUserNo
$this->assertCount(0, $result);
}

public function testValidateEditorToken(): void
public function tokenDataProvider(): array
{
$this->authenticateAsAdmin();
$token = $this->user->createToken("test")->accessToken;

$result = $this->repository->validateEditorToken($token);

$this->assertTrue($result);
return [
[
'expire' => fn() => Passport::personalAccessTokensExpireIn(now()->subDay()),
'assert' => fn($result) => $this->assertFalse($result)
],
[
'expire' => fn() => Passport::personalAccessTokensExpireIn(now()->addDay()),
'assert' => fn($result) => $this->assertTrue($result)
],
];
}

public function testValidateEditorTokenExpiredToken(): void
/**
* @dataProvider tokenDataProvider
*/
public function testValidateEditorToken($expireIn, $assert): void
{
Passport::personalAccessTokensExpireIn(now()->subDay());
$expireIn();

$this->authenticateAsAdmin();
$token = $this->user->createToken("test")->accessToken;

$result = $this->repository->validateEditorToken($token);

$this->assertFalse($result);
$assert($result);
}
}
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use EscolaLms\Core\EscolaLmsServiceProvider;
use EscolaLms\Core\Models\User;
use EscolaLms\HeadlessH5P\Database\Seeders\PermissionTableSeeder;
use EscolaLms\HeadlessH5P\Tests\Models\Client;
use EscolaLms\Settings\EscolaLmsSettingsServiceProvider;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Laravel\Passport\Passport;
use Laravel\Passport\PassportServiceProvider;
use EscolaLms\HeadlessH5P\HeadlessH5PServiceProvider;
use Spatie\Permission\PermissionServiceProvider;
Expand All @@ -24,6 +26,7 @@ class TestCase extends \EscolaLms\Core\Tests\TestCase
protected function setUp(): void
{
parent::setUp();
Passport::useClientModel(Client::class);
$this->seed(PermissionTableSeeder::class);
}

Expand Down

0 comments on commit 900be78

Please sign in to comment.