Skip to content

Commit

Permalink
more tests, multilang tests, minor edgecase fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Meilick <b@bnomei.com>
  • Loading branch information
bnomei committed Aug 20, 2019
1 parent 50eaeed commit e5141f9
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 30 deletions.
33 changes: 22 additions & 11 deletions classes/Betterrest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,42 @@ public function getOptions(): array
* @param \Kirby\Http\Request|null $request
* @return array|null
*/
public function contentFromRequest(?\Kirby\Http\Request $request = null, bool $api = true): ?array
public function contentFromRequest(?\Kirby\Http\Request $request = null): ?array
{
// default to current request
$request = $request ?? $this->kirby->request();

// auto detect language
if (! \Kirby\Toolkit\A::get($this->options, 'language')) {
$languageCode = $request->header('x-language');
if ($languageCode) {
$this->kirby->setCurrentLanguage($languageCode);
$this->options['language'] = $languageCode;
$language = $request->header('x-language');
if ($language) {
$this->options['language'] = $language;
}

}

// if has language and is multilang setup...
$language = \Kirby\Toolkit\A::get($this->options, 'language');
if ($language && kirby()->languages()->count() !== 0) {
$this->kirby->setCurrentLanguage($language);
}

// method api() is @internal
$render = $this->kirby->api()->render(
(string)$request->path(),
(string)$request->method(),
[
'body' => $request->body()->toArray(),
// 'body' => $request->body()->toArray(),
'headers' => $request->headers(),
'query' => $request->query()->toArray(),
// 'query' => $request->query()->toArray(),
]
);

return json_decode($render->body(), true);
$json = json_decode($render->body(), true);
if (is_array($json) && intval(\Kirby\Toolkit\A::get($json, 'code')) === 404) {
return null;
}
return $json;
}

/**
Expand All @@ -99,7 +110,7 @@ public function contentFromRequest(?\Kirby\Http\Request $request = null, bool $a
*/
public function modifyContent(array $array = null): ?array
{
if (! $array) {
if (! $array || count($array) === 0) {
return null;
}

Expand Down Expand Up @@ -158,8 +169,8 @@ public function applySrcSet($value): string
*/
public function response(): array
{
$this->content = $this->contentFromRequest();
$this->data = $this->modifyContent($this->content);
$this->content = $this->content ?? $this->contentFromRequest();
$this->data = $this->data ?? $this->modifyContent($this->content);

if (! $this->data) {
$this->data = [];
Expand Down
3 changes: 3 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

@include_once __DIR__ . '/vendor/autoload.php';

Kirby::plugin('robinscholz/better-rest', [
'options' => [
'srcset' => [375, 667, 1024, 1680], // array|boolean|null
Expand Down
76 changes: 74 additions & 2 deletions tests/BetterrestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,81 @@ public function testContentModification()
kirby()->impersonate('kirby');

$rest = new Robinscholz\Betterrest();
$rest->setContent(kirby()->api()->call('pages/test'));
$rest->setData($rest->modifyContent($rest->content));
$content = kirby()->api()->call('pages/test');
$rest->setContent($content);
$this->assertTrue($rest->getContent() === $content);

// test no data to modify
$this->assertNull($rest->modifyContent(null));
$this->assertNull($rest->modifyContent([]));

$data = $rest->modifyContent($rest->content);
$rest->setData($data);
$this->assertTrue($rest->getData() === $data);

$this->assertIsArray($rest->data);
}

public function testNoDataResponse()
{
kirby()->impersonate('kirby');

$rest = new Robinscholz\Betterrest();
$rest->setContent(null);

// no data yet: empty array and 404
$response = $rest->response();
$this->assertIsArray($response);
$this->assertCount(0, $response);
$this->assertTrue(kirby()->response()->code() === 404);
}

public function testStaticRest()
{
$response = Robinscholz\Betterrest::rest();

// no data yet: empty array and 404
$this->assertIsArray($response);
$this->assertCount(0, $response);
$this->assertTrue(kirby()->response()->code() === 404);
}

public function testContentFromRequest()
{
kirby()->impersonate('kirby');

$rest = new Robinscholz\Betterrest();
$options = $rest->getOptions();
$this->assertNull($options['language']);

// trigger setting of language
$content = $rest->contentFromRequest(
new \Kirby\Http\Request([
'url' => 'pages/test'
])
);
$this->assertIsArray($content);
$this->assertTrue($content['code'] === 200);
}

public function testLanguage()
{
kirby()->impersonate('kirby');

$rest = new Robinscholz\Betterrest([
'language' => 'de',
]);
$options = $rest->getOptions();
$this->assertTrue($options['language'] === 'de');

// trigger setting of language
$content = $rest->contentFromRequest(
new \Kirby\Http\Request([
'url' => 'pages/test'
])
);
$this->assertIsArray($content);
$this->assertTrue($content['code'] === 200);
$this->assertTrue(kirby()->language()->code() === 'de');
}
}
42 changes: 26 additions & 16 deletions tests/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,39 @@ protected function setUp(): void
});
}

public function testFindsHomePage()
{
$response = kirby()->render('/');
$this->assertTrue($response->code() === 200);
$this->assertStringContainsString('Home', $response->body());
}
// public function testFindsHomePageEN()
// {
// $response = kirby()->render('en');
// $this->assertTrue($response->code() === 200);
// $this->assertStringContainsString('Home', $response->body());
// }
//
// public function testFindsHomePageDE()
// {
// $response = kirby()->render('de');
// $this->assertTrue($response->code() === 200);
// $this->assertStringContainsString('Home', $response->body());
// }

public function testFindsTestPage()
{
$response = kirby()->render('/test');
$this->assertTrue($response->code() === 200);
$response = kirby()->call('en/test');
$this->assertEquals('en', kirby()->language()->code());
$this->assertStringContainsString('Test EN', $response->title()->value());

// reads title from content
$this->assertStringContainsString('Test', $response->body());
$response = kirby()->call('de/test');
$this->assertEquals('de', kirby()->language()->code());
$this->assertStringContainsString('Test DE', $response->content('de')->title()->value());

// has image from field
$response = kirby()->render('/en/test');
$this->assertRegExp('/media\/pages\/test\/.*-.*\/test.jpeg/', $response->body());
}

public function testFindsFeedRoute()
{
$response = kirby()->render('/rest/test');
$this->assertTrue($response->code() === 200);
$this->assertTrue('application/json' === $response->type());
}
// public function testFindsRoute()
// {
// $response = kirby()->response('de/path/test');
// $this->assertTrue($response->code() === 200);
// $this->assertTrue('application/json' === $response->type());
// }
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Title: Test
Title: Test DE

----

Expand Down
5 changes: 5 additions & 0 deletions tests/content/test/default.en.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Title: Test EN

----

Testimage: test.jpeg
File renamed without changes.
13 changes: 13 additions & 0 deletions tests/site/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@
'api' => [
'allowInsecure' => 'true',
],
'languages' => true,
'routes' => function (\Kirby\Cms\App $kirby) {
return [
[
'pattern' => 'path/(:all)',
'method' => 'GET',
'language' => '*',
'action' => function (string $path = null) {
return ['path' => $path];
},
],
];
},
];
9 changes: 9 additions & 0 deletions tests/site/languages/de.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
'code' => 'de',
'default' => false,
'direction' => 'ltr',
'locale' => 'de_DE',
'name' => 'Deutsch',
];
9 changes: 9 additions & 0 deletions tests/site/languages/en.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
'code' => 'en',
'default' => true,
'direction' => 'ltr',
'locale' => 'en_US',
'name' => 'English',
];

0 comments on commit e5141f9

Please sign in to comment.