Prevented entity "Not Found" events from being logged

- Added testing to cover, which was more hassle than thought
  since Laravel did not have built in log test helpers, so:
- Added Log testing helper.

Related to #2110
This commit is contained in:
Dan Brown
2020-05-23 11:26:48 +01:00
parent bf4a3b73f8
commit 19bfc8ad37
5 changed files with 55 additions and 6 deletions

View File

@ -16,7 +16,10 @@ use BookStack\Entities\Repos\PageRepo;
use BookStack\Settings\SettingService;
use BookStack\Uploads\HttpFetcher;
use Illuminate\Support\Env;
use Illuminate\Support\Facades\Log;
use Mockery;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use Throwable;
trait SharedTestHelpers
@ -69,14 +72,14 @@ trait SharedTestHelpers
}
/**
* Get an instance of a user with 'viewer' permissions
* @param $attributes
* @return mixed
* Get an instance of a user with 'viewer' permissions.
*/
protected function getViewer($attributes = [])
protected function getViewer(array $attributes = []): User
{
$user = Role::getRole('viewer')->users()->first();
if (!empty($attributes)) $user->forceFill($attributes)->save();
if (!empty($attributes)) {
$user->forceFill($attributes)->save();
}
return $user;
}
@ -277,4 +280,22 @@ trait SharedTestHelpers
$this->assertStringStartsWith('You do not have permission to access', $error);
}
/**
* Set a test handler as the logging interface for the application.
* Allows capture of logs for checking against during tests.
*/
protected function withTestLogger(): TestHandler
{
$monolog = new Logger('testing');
$testHandler = new TestHandler();
$monolog->pushHandler($testHandler);
Log::extend('testing', function() use ($monolog) {
return $monolog;
});
Log::setDefaultDriver('testing');
return $testHandler;
}
}