'driver' => 'monolog',
'handler' => NullHandler::class,
],
+
+ // Testing channel
+ // Uses a shared testing instance during tests
+ // so that logs can be checked against.
+ 'testing' => [
+ 'driver' => 'testing',
+ ],
],
];
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
-use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
HttpException::class,
ModelNotFoundException::class,
ValidationException::class,
+ NotFoundException::class,
];
/**
<description>The coding standard for BookStack.</description>
<file>app</file>
<exclude-pattern>*/migrations/*</exclude-pattern>
+ <exclude-pattern>*/tests/*</exclude-pattern>
<arg value="np"/>
<rule ref="PSR2"/>
</ruleset>
\ No newline at end of file
<?php namespace Tests;
+use BookStack\Entities\Book;
+use Illuminate\Support\Facades\Log;
+
class ErrorTest extends TestCase
{
$notFound->assertDontSeeText('Log in');
$notFound->assertSeeText('tester');
}
+
+ public function test_item_not_found_does_not_get_logged_to_file()
+ {
+ $this->actingAs($this->getViewer());
+ $handler = $this->withTestLogger();
+ $book = Book::query()->first();
+
+ // Ensure we're seeing errors
+ Log::error('cat');
+ $this->assertTrue($handler->hasErrorThatContains('cat'));
+
+ $this->get('/books/arandomnotfouindbook');
+ $this->get($book->getUrl('/chapter/arandomnotfouindchapter'));
+ $this->get($book->getUrl('/chapter/arandomnotfouindpages'));
+
+ $this->assertCount(1, $handler->getRecords());
+ }
}
\ No newline at end of file
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
}
/**
- * 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;
}
$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;
+ }
+
}
\ No newline at end of file