Skip to content

Commit

Permalink
Add unit test for LanguageTranslations class
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora authored and distantnative committed Jul 22, 2024
1 parent fc5c25b commit 5861b51
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/Cms/Languages/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,31 @@ public function testSave()
$this->assertSame('test', $data['custom']);
}

/**
* @covers ::save
*/
public function testSaveCustomTranslations()
{
new App([
'roots' => [
'index' => static::TMP,
'languages' => static::TMP . '/languages',
'translations' => static::TMP . '/translations'
]
]);

$file = static::TMP . '/translations/en.php';
F::write($file, '');
$language = new Language([
'code' => 'en'
]);
$language->save();
$language->variable('foo')->update('bar');
$translations = $language->translationsObject();

$this->assertSame(['foo' => 'bar'], $translations->toArray());
}

/**
* @covers ::toArray
* @covers ::__debugInfo
Expand Down
185 changes: 185 additions & 0 deletions tests/Cms/Languages/LanguageTranslationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

namespace Kirby\Cms;

use Kirby\Filesystem\Dir;
use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Cms\LanguageTranslations
*/
class LanguageTranslationsTest extends TestCase
{
public const FIXTURES = __DIR__ . '/fixtures';
public const TMP = KIRBY_TMP_DIR . '/Cms.LanguageTranslations';

public function tearDown(): void
{
Dir::remove(static::TMP);
}

/**
* @covers ::root
*/
public function testRoot()
{
new App([
'roots' => [
'translations' => static::FIXTURES
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'en']));
$this->assertSame(static::FIXTURES . '/en.php', $translations->root());
}

/**
* @covers ::toArray
*/
public function testToArray()
{
new App([
'roots' => [
'translations' => static::FIXTURES
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'en']));
$this->assertSame([
'hello' => 'Hello world!',
'login' => 'Log-in',
], $translations->toArray());
}

/**
* @covers ::get
*/
public function testGet()
{
new App([
'roots' => [
'translations' => static::FIXTURES
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'de']));
$this->assertSame('Hallo Welt!', $translations->get('hello'));
}

/**
* @covers ::get
*/
public function testGetDefault()
{
new App([
'roots' => [
'translations' => static::FIXTURES
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'en']));
$this->assertSame('Hello dear!', $translations->get('not-exists', 'Hello dear!'));
}

/**
* @covers ::setTranslations
* @covers ::get
*/
public function testSetTranslations()
{
new App([
'roots' => [
'translations' => static::FIXTURES
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'en']));
$translations->setTranslations(['click' => 'Click here!']);
$this->assertSame('Click here!', $translations->get('click'));
}

/**
* @covers ::set
* @covers ::get
*/
public function testSet()
{
new App([
'roots' => [
'translations' => static::FIXTURES
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'en']));
$translations->set('contact', 'Contact us');
$this->assertSame('Contact us', $translations->get('contact'));
}

/**
* @covers ::save
*/
public function testSave()
{
new App([
'roots' => [
'translations' => static::TMP,
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'tr']));
$variables = ['write' => 'Bize yazın'];
$translations->save($variables);

$this->assertSame($variables, $translations->toArray());
}

/**
* @covers ::remove
*/
public function testRemove()
{
new App([
'roots' => [
'translations' => static::FIXTURES
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'en']));
$translations->remove('login');
$this->assertSame(['hello' => 'Hello world!'], $translations->toArray());
}

/**
* @covers ::load
*/
public function testLoad()
{
new App([
'roots' => [
'translations' => static::FIXTURES
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'en']));
$this->assertSame([
'hello' => 'Hello world!',
'login' => 'Log-in',
], $translations->load(['other' => 'Other variable']));
}

/**
* @covers ::load
*/
public function testLoadDefault()
{
new App([
'roots' => [
'index' => '/dev/null'
]
]);

$translations = new LanguageTranslations(new Language(['code' => 'en']));
$variables = ['hello' => 'Hello Kirby lovers!'];
$this->assertSame($variables, $translations->load($variables));
}
}
6 changes: 6 additions & 0 deletions tests/Cms/Languages/fixtures/de.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'hello' => 'Hallo Welt!',
'login' => 'Anmeldung'
];
6 changes: 6 additions & 0 deletions tests/Cms/Languages/fixtures/en.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'hello' => 'Hello world!',
'login' => 'Log-in',
];

0 comments on commit 5861b51

Please sign in to comment.