From 14af2fa45f5abb50eb35beec8f3c7a76cc5cfae2 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Sat, 27 Dec 2025 03:34:23 +0100 Subject: [PATCH] Fix bad endpoint call --- app/Services/TmdbClient.php | 10 ++++ app/Services/TraktService.php | 11 +++++ tests/Unit/TmdbClientTest.php | 90 ++++++++++++++++++++++++++++++++++- 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/app/Services/TmdbClient.php b/app/Services/TmdbClient.php index 900b417c3..9bb8edd73 100644 --- a/app/Services/TmdbClient.php +++ b/app/Services/TmdbClient.php @@ -248,6 +248,11 @@ class TmdbClient */ public function getTvSeason(int $tvId, int $seasonNumber): ?array { + // Validate parameters to avoid unnecessary API calls with invalid IDs + if ($tvId <= 0 || $seasonNumber < 0) { + return null; + } + return $this->get('/tv/'.$tvId.'/season/'.$seasonNumber); } @@ -261,6 +266,11 @@ class TmdbClient */ public function getTvEpisode(int $tvId, int $seasonNumber, int $episodeNumber): ?array { + // Validate parameters to avoid unnecessary API calls with invalid IDs + if ($tvId <= 0 || $seasonNumber < 0 || $episodeNumber < 0) { + return null; + } + return $this->get('/tv/'.$tvId.'/season/'.$seasonNumber.'/episode/'.$episodeNumber); } diff --git a/app/Services/TraktService.php b/app/Services/TraktService.php index 3489eaac2..4f7e14451 100644 --- a/app/Services/TraktService.php +++ b/app/Services/TraktService.php @@ -138,6 +138,17 @@ class TraktService int|string $episode, string $extended = 'min' ): ?array { + // Validate parameters to avoid unnecessary API calls with invalid IDs + $showIdInt = is_numeric($showId) ? (int) $showId : 0; + $seasonInt = is_numeric($season) ? (int) $season : -1; + $episodeInt = is_numeric($episode) ? (int) $episode : -1; + + // For numeric show IDs, validate they are positive + // Season and episode must be non-negative (season 0 can be specials) + if (($showIdInt <= 0 && is_numeric($showId)) || $seasonInt < 0 || $episodeInt < 0) { + return null; + } + $extended = match ($extended) { 'aliases', 'full', 'full,aliases' => $extended, default => 'min', diff --git a/tests/Unit/TmdbClientTest.php b/tests/Unit/TmdbClientTest.php index 90ecb0d6d..3536a024d 100644 --- a/tests/Unit/TmdbClientTest.php +++ b/tests/Unit/TmdbClientTest.php @@ -540,5 +540,93 @@ class TmdbClientTest extends TestCase $result = $this->client->searchMovies('Test'); $this->assertNull($result); } -} + // ========================================================================= + // PARAMETER VALIDATION TESTS + // ========================================================================= + + public function test_get_tv_season_returns_null_for_invalid_tv_id(): void + { + // Should not make any API call + Http::fake(); + + $this->assertNull($this->client->getTvSeason(0, 1)); + $this->assertNull($this->client->getTvSeason(-1, 1)); + + Http::assertNothingSent(); + } + + public function test_get_tv_season_returns_null_for_negative_season(): void + { + // Should not make any API call + Http::fake(); + + $this->assertNull($this->client->getTvSeason(1396, -1)); + + Http::assertNothingSent(); + } + + public function test_get_tv_episode_returns_null_for_invalid_tv_id(): void + { + // Should not make any API call + Http::fake(); + + $this->assertNull($this->client->getTvEpisode(0, 1, 1)); + $this->assertNull($this->client->getTvEpisode(-1, 1, 1)); + + Http::assertNothingSent(); + } + + public function test_get_tv_episode_returns_null_for_negative_season(): void + { + // Should not make any API call + Http::fake(); + + $this->assertNull($this->client->getTvEpisode(1396, -1, 1)); + + Http::assertNothingSent(); + } + + public function test_get_tv_episode_returns_null_for_negative_episode(): void + { + // Should not make any API call + Http::fake(); + + $this->assertNull($this->client->getTvEpisode(1396, 1, -1)); + + Http::assertNothingSent(); + } + + public function test_get_tv_season_allows_season_zero_for_specials(): void + { + Http::fake([ + 'api.themoviedb.org/3/tv/1396/season/0*' => Http::response([ + 'id' => 3577, + 'season_number' => 0, + 'name' => 'Specials', + ]), + ]); + + $result = $this->client->getTvSeason(1396, 0); + + $this->assertNotNull($result); + $this->assertSame(0, $result['season_number']); + } + + public function test_get_tv_episode_allows_season_and_episode_zero(): void + { + Http::fake([ + 'api.themoviedb.org/3/tv/1396/season/0/episode/0*' => Http::response([ + 'id' => 12345, + 'name' => 'Special Episode', + 'episode_number' => 0, + 'season_number' => 0, + ]), + ]); + + $result = $this->client->getTvEpisode(1396, 0, 0); + + $this->assertNotNull($result); + $this->assertSame(0, $result['episode_number']); + } +}