Skip to content

Commit

Permalink
Throw exception in Translations::findByKey if the language does not e…
Browse files Browse the repository at this point in the history
…xist
  • Loading branch information
bastianallgeier committed Jun 21, 2024
1 parent 1cfb2e5 commit 78b12f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/Content/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Kirby\Cms\Language;
use Kirby\Cms\Languages;
use Kirby\Cms\ModelWithContent;
use Kirby\Exception\NotFoundException;

/**
* @package Kirby Content
Expand Down Expand Up @@ -52,11 +51,7 @@ public static function create(
*/
public function findByKey(string $key): Translation|null
{
try {
return parent::get(Language::ensure($key)->code());
} catch (NotFoundException) {
return null;
}
return parent::get(Language::ensure($key)->code());
}

/**
Expand Down
21 changes: 20 additions & 1 deletion tests/Content/TranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kirby\Content;

use Kirby\Exception\NotFoundException;

/**
* @coversDefaultClass \Kirby\Content\Translations
* @covers ::__construct
Expand Down Expand Up @@ -87,7 +89,6 @@ public function testFindByKeyMultiLanguage()
$this->assertSame('en', $translations->findByKey('default')->language()->code());
$this->assertSame('en', $translations->findByKey('current')->language()->code());
$this->assertSame('de', $translations->findByKey('de')->language()->code());
$this->assertNull($translations->findByKey('fr'));
}

/**
Expand All @@ -107,6 +108,24 @@ public function testFindByKeySingleLanguage()
$this->assertSame('en', $translations->findByKey('current')->language()->code());
}

/**
* @covers ::findByKey
*/
public function testFindByKeyWithInvalidLanguage()
{
$this->setUpMultiLanguage();

$translations = Translations::load(
model: $this->model,
version: $this->model->version()
);

$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('Invalid language: fr');

$translations->findByKey('fr');
}

/**
* @covers ::load
*/
Expand Down

0 comments on commit 78b12f5

Please sign in to comment.