diff --git a/sourcecode/apis/contentauthor/app/Http/Libraries/LtiTrait.php b/sourcecode/apis/contentauthor/app/Http/Libraries/LtiTrait.php index 3579c6a883..c4fa720a70 100644 --- a/sourcecode/apis/contentauthor/app/Http/Libraries/LtiTrait.php +++ b/sourcecode/apis/contentauthor/app/Http/Libraries/LtiTrait.php @@ -9,6 +9,10 @@ trait LtiTrait { public function ltiShow($id) { + if (!method_exists($this, 'doShow')) { + abort(500, 'Requested action is not available'); + } + $ltiRequest = $this->lti->getRequest(request()); if (!$ltiRequest) { @@ -18,15 +22,15 @@ public function ltiShow($id) ); } - if (method_exists($this, 'doShow')) { - return $this->doShow($id, $ltiRequest->generateContextKey(), $ltiRequest->isPreview()); - } - - abort(500, 'Requested action is not available'); + return $this->doShow($id, $ltiRequest->generateContextKey(), $ltiRequest->isPreview()); } public function ltiCreate(Request $request) { + if (!method_exists($this, 'create')) { + abort(500, 'Requested action is not available'); + } + $ltiRequest = $this->lti->getRequest($request); if (!$ltiRequest) { @@ -36,15 +40,15 @@ public function ltiCreate(Request $request) ); } - if (method_exists($this, 'create')) { - return $this->create($request); - } - - abort(500, 'Requested action is not available'); + return $this->create($request); } public function ltiEdit(Request $request, $id) { + if (!method_exists($this, 'edit')) { + abort(500, 'Requested action is not available'); + } + $ltiRequest = $this->lti->getRequest($request); if (!$ltiRequest) { @@ -54,10 +58,6 @@ public function ltiEdit(Request $request, $id) ); } - if (method_exists($this, 'edit')) { - return $this->edit($request, $id); - } - - abort(500, 'Requested action is not available'); + return $this->edit($request, $id); } } diff --git a/sourcecode/apis/contentauthor/tests/Integration/Http/Libraries/LtiTraitTest.php b/sourcecode/apis/contentauthor/tests/Integration/Http/Libraries/LtiTraitTest.php index 3592e2fb00..2fe6b012f4 100644 --- a/sourcecode/apis/contentauthor/tests/Integration/Http/Libraries/LtiTraitTest.php +++ b/sourcecode/apis/contentauthor/tests/Integration/Http/Libraries/LtiTraitTest.php @@ -4,10 +4,12 @@ namespace Tests\Integration\Http\Libraries; +use App\Http\Libraries\LtiTrait; use App\Lti\LtiRequest; use Cerpus\EdlibResourceKit\Oauth1\ValidatorInterface; use Exception; use Illuminate\Http\Request; +use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; use Tests\Integration\Http\Libraries\Stubs\LtiTraitStubClass; use Tests\TestCase; @@ -23,15 +25,25 @@ public function setupLti(): void $this->instance(ValidatorInterface::class, $validator); } - public function test_ltiShow_exception(): void + public function test_ltiShow_unauthorized_exception(): void { - $this->expectException(Exception::class); + $this->expectException(UnauthorizedHttpException::class); $this->expectExceptionMessage('No valid LTI request'); $class = app(LtiTraitStubClass::class); $class->ltiShow(1); } + public function test_ltiShow_unavailable_exception(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Requested action is not available'); + + $class = $this->getMockForTrait(LtiTrait::class); + /** @phpstan-ignore-next-line */ + $class->ltiShow(1); + } + public function test_ltiShow(): void { $this->setupLti(); @@ -45,15 +57,25 @@ public function test_ltiShow(): void $this->assertSame('doShow', $testClass->ltiShow(42)); } - public function test_ltiCreate_exception(): void + public function test_ltiCreate_unauthorized_exception(): void { - $this->expectException(Exception::class); + $this->expectException(UnauthorizedHttpException::class); $this->expectExceptionMessage('No valid LTI request'); $class = app(LtiTraitStubClass::class); $class->ltiCreate(Request::create('')); } + public function test_ltiCreate_unavailable_exception(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Requested action is not available'); + + $class = $this->getMockForTrait(LtiTrait::class); + /** @phpstan-ignore-next-line */ + $class->ltiCreate(new Request()); + } + public function test_ltiCreate(): void { $this->setupLti(); @@ -63,15 +85,25 @@ public function test_ltiCreate(): void )); } - public function test_ltiEdit_exception(): void + public function test_ltiEdit_unauthorized_exception(): void { - $this->expectException(Exception::class); + $this->expectException(UnauthorizedHttpException::class); $this->expectExceptionMessage('No valid LTI request'); $class = app(LtiTraitStubClass::class); $class->ltiEdit(Request::create(''), 1); } + public function test_ltiEdit_unavailable_exception(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Requested action is not available'); + + $class = $this->getMockForTrait(LtiTrait::class); + /** @phpstan-ignore-next-line */ + $class->ltiEdit(new Request(), 1); + } + public function test_ltiEdit(): void { $this->setupLti();