Skip to content

Commit

Permalink
Merge pull request #28 from bedita/feat/middleware-disabled-by-default
Browse files Browse the repository at this point in the history
Middleware disabled by default
  • Loading branch information
stefanorosanelli authored May 12, 2020
2 parents 135a57b + edf1074 commit 49d67ca
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Middleware/I18nMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cake\Core\InstanceConfigTrait;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Exception\BadRequestException;
use Cake\Http\Exception\InternalErrorException;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\I18n\I18n;
Expand Down Expand Up @@ -176,6 +177,12 @@ protected function setupLocale(?string $locale): void
$locale = array_search($lang, $locales);
}

if ($lang === null || $locale === false) {
throw new InternalErrorException(
__('Something was wrong with I18n configuration. Check "I18n.locales" and "I18n.default"')
);
}

Configure::write('I18n.lang', $lang);
I18n::setLocale($locale);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class Plugin extends BasePlugin
*/
protected $routesEnabled = false;

/**
* Enable middleware
*
* @var bool
*/
protected $middlewareEnabled = false;

/**
* Setup the I8nMiddleware.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Middleware/I18nMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Cake\Core\Configure;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Exception\BadRequestException;
use Cake\Http\Exception\InternalErrorException;
use Cake\Http\Response;
use Cake\Http\ServerRequestFactory;
use Cake\I18n\I18n;
Expand Down Expand Up @@ -336,6 +337,26 @@ public function testSetupLocale(array $expected, array $server): void
static::assertEquals($expected['lang'], Configure::read('I18n.lang'));
}

/**
* Test that an exception is raised if missing required conf.
*
* @return void
*
* @covers ::setupLocale()
*/
public function testSetupLocaleMissingConfig(): void
{
$this->expectException(InternalErrorException::class);
$this->expectExceptionCode(500);

Configure::delete('I18n');
$request = ServerRequestFactory::fromGlobals([
'REQUEST_URI' => '/help',
]);
$middleware = new I18nMiddleware();
$middleware->process($request, $this->requestHandler);
}

/**
* Test that if middleware is not configured properly the locale cookie is ignored.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
ConnectionManager::setConfig('test', ['url' => getenv('db_dsn')]);
Router::reload();

Plugin::getCollection()->add(new \BEdita\I18n\Plugin());
Plugin::getCollection()->add(new \BEdita\I18n\Plugin(['middleware' => true]));

0 comments on commit 49d67ca

Please sign in to comment.