Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative authored and bastianallgeier committed Jan 31, 2025
1 parent 1fb4e02 commit e6989e3
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/Panel/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function __construct()
$this->kirby = App::instance();
}

/**
* Panel assets
*/
public function assets(): Assets
{
return $this->assets ??= new Assets();
Expand Down Expand Up @@ -67,7 +70,7 @@ public function cors(): string
}

/**
* Renders the panel document
* Renders the Panel document
*/
public function render(array $fiber): Response
{
Expand Down
19 changes: 5 additions & 14 deletions src/Panel/Fiber.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,6 @@ public function filter(array $data): array
// split include string into an array of fields
$keys = Str::split($filter, ',');

// if a full request is made, return all data
if ($keys === []) {
return $data;
}

// take care of potentially requested globals
$globals = $this->globals();
$keysEntries = A::map($keys, fn ($key) => Str::split($key, '.')[0]);
Expand Down Expand Up @@ -157,10 +152,6 @@ public function filter(array $data): array
$keys = Str::split($filterGlobals, ',');

// add requested globals
if ($keys === []) {
return $data;
}

$globals = $this->globals();

foreach ($keys as $key) {
Expand Down Expand Up @@ -319,6 +310,11 @@ public function translation(): array
];
}

public function url(): string
{
return $this->kirby->request()->url()->toString();
}

public function urls(): array
{
return [
Expand All @@ -327,11 +323,6 @@ public function urls(): array
];
}

public function url(): string
{
return $this->kirby->request()->url()->toString();
}

public function user(): array|null
{
if ($this->user) {
Expand Down
1 change: 1 addition & 0 deletions tests/Panel/AreasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* @coversDefaultClass \Kirby\Panel\Areas
* @covers ::__construct
*/
class AreasTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions tests/Panel/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/**
* @coversDefaultClass \Kirby\Panel\Assets
* @covers ::__construct
*/
class AssetsTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions tests/Panel/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @coversDefaultClass \Kirby\Panel\Document
* @covers ::__construct
*/
class DocumentTest extends TestCase
{
Expand Down
108 changes: 107 additions & 1 deletion tests/Panel/FiberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/**
* @coversDefaultClass \Kirby\Panel\Fiber
* @covers ::__construct
*/
class FiberTest extends TestCase
{
Expand All @@ -35,6 +36,23 @@ public function tearDown(): void
Dir::remove(static::TMP);
}

/**
* @covers ::config
*/
public function testConfig(): void
{
// without custom data
$fiber = new Fiber();
$config = $fiber->config();
$config = A::apply($config);

$this->assertArrayHasKey('api', $config);
$this->assertArrayHasKey('debug', $config);
$this->assertArrayHasKey('kirbytext', $config);
$this->assertArrayHasKey('translation', $config);
$this->assertArrayHasKey('upload', $config);
}

/**
* @covers ::data
*/
Expand Down Expand Up @@ -176,7 +194,7 @@ public function testFilterOnlyRequest(): void
/**
* @covers ::filter
*/
public function testFilterOnlyrequestWithGlobal(): void
public function testFilterOnlyRequestWithGlobal(): void
{
// simulate a simple partial request
$this->app = $this->app->clone([
Expand Down Expand Up @@ -480,6 +498,83 @@ public function testSearches()
$this->assertArrayHasKey('test', $searches);
}

/**
* @covers ::system
*/
public function testSystem(): void
{
// without custom data
$fiber = new Fiber();
$system = $fiber->system();
$system = A::apply($system);

$this->assertArrayHasKey('ascii', $system);
$this->assertArrayHasKey('csrf', $system);
$this->assertArrayHasKey('isLocal', $system);
$this->assertArrayHasKey('locales', $system);
$this->assertArrayHasKey('slugs', $system);
$this->assertArrayHasKey('title', $system);
}

/**
* @covers ::toArray
*/
public function testToArray(): void
{
$fiber = new Fiber();
$array = $fiber->toArray(globals: false);
$this->assertArrayHasKey('user', $array);
$this->assertArrayNotHasKey('config', $array);

$array = $fiber->toArray(globals: true);
$this->assertArrayHasKey('user', $array);
$this->assertArrayHasKey('config', $array);
}

/**
* @covers ::translation
*/
public function testTranslation(): void
{
$fiber = new Fiber();
$translation = $fiber->translation();
$translation = A::apply($translation);

$this->assertSame('en', $translation['code']);
$this->assertArrayHasKey('data', $translation);
$this->assertSame('ltr', $translation['direction']);
$this->assertSame('English', $translation['name']);
$this->assertSame(0, $translation['weekday']);
}

/**
* @covers ::translation
*/
public function testTranslationWithUserLanguage(): void
{
$this->app = $this->app->clone([
'users' => [
[
'email' => '[email protected]',
'language' => 'de',
'role' => 'admin'
]
]
]);

$this->app->impersonate('[email protected]');

$fiber = new Fiber();
$translation = $fiber->translation();
$translation = A::apply($translation);

$this->assertSame('de', $translation['code']);
$this->assertArrayHasKey('data', $translation);
$this->assertSame('ltr', $translation['direction']);
$this->assertSame('Deutsch', $translation['name']);
$this->assertSame(1, $translation['weekday']);
}

/**
* @covers ::url
*/
Expand All @@ -502,6 +597,17 @@ public function testUrl(): void
$this->assertSame('https://localhost.com:8888/foo/bar?foo=bar', $url);
}

/**
* @covers ::urls
*/
public function testUrls(): void
{
$fiber = new Fiber();
$urls = $fiber->urls();
$this->assertArrayHasKey('api', $urls);
$this->assertArrayHasKey('site', $urls);
}

/**
* @covers ::permissions
* @covers ::user
Expand Down
1 change: 1 addition & 0 deletions tests/Panel/HomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @coversDefaultClass \Kirby\Panel\Home
* @covers ::__construct
*/
class HomeTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions tests/Panel/PanelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @coversDefaultClass \Kirby\Panel\Panel
* @covers ::__construct
*/
class PanelTest extends TestCase
{
Expand Down
38 changes: 38 additions & 0 deletions tests/Panel/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @coversDefaultClass \Kirby\Panel\Router
* @covers ::__construct
*/
class RouterTest extends TestCase
{
Expand Down Expand Up @@ -170,6 +171,43 @@ public function testRoutesForDialogsWithoutHandlers(): void
$this->assertSame('The submit handler is missing', $routes[1]['action']());
}

/**
* @covers ::routesForDrawers
*/
public function testRoutesForDrawers(): void
{
$area = [
'drawers' => [
'test' => [
'load' => $load = function () {
},
'submit' => $submit = function () {
},
]
]
];

$routes = Router::routesForDrawers('test', $area);

$expected = [
[
'pattern' => 'drawers/test',
'type' => 'drawer',
'area' => 'test',
'action' => $load,
],
[
'pattern' => 'drawers/test',
'type' => 'drawer',
'area' => 'test',
'method' => 'POST',
'action' => $submit,
]
];

$this->assertSame($expected, $routes);
}

/**
* @covers ::routesForDropdowns
*/
Expand Down

0 comments on commit e6989e3

Please sign in to comment.