Skip to content

Commit 103feca

Browse files
committed
Add more unit tests
1 parent 2ff6997 commit 103feca

File tree

9 files changed

+159
-16
lines changed

9 files changed

+159
-16
lines changed

src/Panel/Document.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function __construct()
3030
$this->kirby = App::instance();
3131
}
3232

33+
/**
34+
* Panel assets
35+
*/
3336
public function assets(): Assets
3437
{
3538
return $this->assets ??= new Assets();
@@ -67,7 +70,7 @@ public function cors(): string
6770
}
6871

6972
/**
70-
* Renders the panel document
73+
* Renders the Panel document
7174
*/
7275
public function render(array $fiber): Response
7376
{

src/Panel/Fiber.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,6 @@ public function filter(array $data): array
122122
// split include string into an array of fields
123123
$keys = Str::split($filter, ',');
124124

125-
// if a full request is made, return all data
126-
if ($keys === []) {
127-
return $data;
128-
}
129-
130125
// take care of potentially requested globals
131126
$globals = $this->globals();
132127
$keysEntries = A::map($keys, fn ($key) => Str::split($key, '.')[0]);
@@ -157,10 +152,6 @@ public function filter(array $data): array
157152
$keys = Str::split($filterGlobals, ',');
158153

159154
// add requested globals
160-
if ($keys === []) {
161-
return $data;
162-
}
163-
164155
$globals = $this->globals();
165156

166157
foreach ($keys as $key) {
@@ -319,6 +310,11 @@ public function translation(): array
319310
];
320311
}
321312

313+
public function url(): string
314+
{
315+
return $this->kirby->request()->url()->toString();
316+
}
317+
322318
public function urls(): array
323319
{
324320
return [
@@ -327,11 +323,6 @@ public function urls(): array
327323
];
328324
}
329325

330-
public function url(): string
331-
{
332-
return $this->kirby->request()->url()->toString();
333-
}
334-
335326
public function user(): array|null
336327
{
337328
if ($this->user) {

tests/Panel/AreasTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/**
1010
* @coversDefaultClass \Kirby\Panel\Areas
11+
* @covers ::__construct
1112
*/
1213
class AreasTest extends TestCase
1314
{

tests/Panel/AssetsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* @coversDefaultClass \Kirby\Panel\Assets
14+
* @covers ::__construct
1415
*/
1516
class AssetsTest extends TestCase
1617
{

tests/Panel/DocumentTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* @coversDefaultClass \Kirby\Panel\Document
12+
* @covers ::__construct
1213
*/
1314
class DocumentTest extends TestCase
1415
{

tests/Panel/FiberTest.php

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* @coversDefaultClass \Kirby\Panel\Fiber
14+
* @covers ::__construct
1415
*/
1516
class FiberTest extends TestCase
1617
{
@@ -35,6 +36,23 @@ public function tearDown(): void
3536
Dir::remove(static::TMP);
3637
}
3738

39+
/**
40+
* @covers ::config
41+
*/
42+
public function testConfig(): void
43+
{
44+
// without custom data
45+
$fiber = new Fiber();
46+
$config = $fiber->config();
47+
$config = A::apply($config);
48+
49+
$this->assertArrayHasKey('api', $config);
50+
$this->assertArrayHasKey('debug', $config);
51+
$this->assertArrayHasKey('kirbytext', $config);
52+
$this->assertArrayHasKey('translation', $config);
53+
$this->assertArrayHasKey('upload', $config);
54+
}
55+
3856
/**
3957
* @covers ::data
4058
*/
@@ -176,7 +194,7 @@ public function testFilterOnlyRequest(): void
176194
/**
177195
* @covers ::filter
178196
*/
179-
public function testFilterOnlyrequestWithGlobal(): void
197+
public function testFilterOnlyRequestWithGlobal(): void
180198
{
181199
// simulate a simple partial request
182200
$this->app = $this->app->clone([
@@ -480,6 +498,83 @@ public function testSearches()
480498
$this->assertArrayHasKey('test', $searches);
481499
}
482500

501+
/**
502+
* @covers ::system
503+
*/
504+
public function testSystem(): void
505+
{
506+
// without custom data
507+
$fiber = new Fiber();
508+
$system = $fiber->system();
509+
$system = A::apply($system);
510+
511+
$this->assertArrayHasKey('ascii', $system);
512+
$this->assertArrayHasKey('csrf', $system);
513+
$this->assertArrayHasKey('isLocal', $system);
514+
$this->assertArrayHasKey('locales', $system);
515+
$this->assertArrayHasKey('slugs', $system);
516+
$this->assertArrayHasKey('title', $system);
517+
}
518+
519+
/**
520+
* @covers ::toArray
521+
*/
522+
public function testToArray(): void
523+
{
524+
$fiber = new Fiber();
525+
$array = $fiber->toArray(globals: false);
526+
$this->assertArrayHasKey('user', $array);
527+
$this->assertArrayNotHasKey('config', $array);
528+
529+
$array = $fiber->toArray(globals: true);
530+
$this->assertArrayHasKey('user', $array);
531+
$this->assertArrayHasKey('config', $array);
532+
}
533+
534+
/**
535+
* @covers ::translation
536+
*/
537+
public function testTranslation(): void
538+
{
539+
$fiber = new Fiber();
540+
$translation = $fiber->translation();
541+
$translation = A::apply($translation);
542+
543+
$this->assertSame('en', $translation['code']);
544+
$this->assertArrayHasKey('data', $translation);
545+
$this->assertSame('ltr', $translation['direction']);
546+
$this->assertSame('English', $translation['name']);
547+
$this->assertSame(0, $translation['weekday']);
548+
}
549+
550+
/**
551+
* @covers ::translation
552+
*/
553+
public function testTranslationWithUserLanguage(): void
554+
{
555+
$this->app = $this->app->clone([
556+
'users' => [
557+
[
558+
'email' => '[email protected]',
559+
'language' => 'de',
560+
'role' => 'admin'
561+
]
562+
]
563+
]);
564+
565+
$this->app->impersonate('[email protected]');
566+
567+
$fiber = new Fiber();
568+
$translation = $fiber->translation();
569+
$translation = A::apply($translation);
570+
571+
$this->assertSame('de', $translation['code']);
572+
$this->assertArrayHasKey('data', $translation);
573+
$this->assertSame('ltr', $translation['direction']);
574+
$this->assertSame('Deutsch', $translation['name']);
575+
$this->assertSame(1, $translation['weekday']);
576+
}
577+
483578
/**
484579
* @covers ::url
485580
*/
@@ -502,6 +597,17 @@ public function testUrl(): void
502597
$this->assertSame('https://localhost.com:8888/foo/bar?foo=bar', $url);
503598
}
504599

600+
/**
601+
* @covers ::urls
602+
*/
603+
public function testUrls(): void
604+
{
605+
$fiber = new Fiber();
606+
$urls = $fiber->urls();
607+
$this->assertArrayHasKey('api', $urls);
608+
$this->assertArrayHasKey('site', $urls);
609+
}
610+
505611
/**
506612
* @covers ::permissions
507613
* @covers ::user

tests/Panel/HomeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
/**
1414
* @coversDefaultClass \Kirby\Panel\Home
15+
* @covers ::__construct
1516
*/
1617
class HomeTest extends TestCase
1718
{

tests/Panel/PanelTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* @coversDefaultClass \Kirby\Panel\Panel
12+
* @covers ::__construct
1213
*/
1314
class PanelTest extends TestCase
1415
{

tests/Panel/RouterTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
/**
1111
* @coversDefaultClass \Kirby\Panel\Router
12+
* @covers ::__construct
1213
*/
1314
class RouterTest extends TestCase
1415
{
@@ -170,6 +171,43 @@ public function testRoutesForDialogsWithoutHandlers(): void
170171
$this->assertSame('The submit handler is missing', $routes[1]['action']());
171172
}
172173

174+
/**
175+
* @covers ::routesForDrawers
176+
*/
177+
public function testRoutesForDrawers(): void
178+
{
179+
$area = [
180+
'drawers' => [
181+
'test' => [
182+
'load' => $load = function () {
183+
},
184+
'submit' => $submit = function () {
185+
},
186+
]
187+
]
188+
];
189+
190+
$routes = Router::routesForDrawers('test', $area);
191+
192+
$expected = [
193+
[
194+
'pattern' => 'drawers/test',
195+
'type' => 'drawer',
196+
'area' => 'test',
197+
'action' => $load,
198+
],
199+
[
200+
'pattern' => 'drawers/test',
201+
'type' => 'drawer',
202+
'area' => 'test',
203+
'method' => 'POST',
204+
'action' => $submit,
205+
]
206+
];
207+
208+
$this->assertSame($expected, $routes);
209+
}
210+
173211
/**
174212
* @covers ::routesForDropdowns
175213
*/

0 commit comments

Comments
 (0)