]> BookStack Code Mirror - bookstack/commitdiff
Prevented entity "Not Found" events from being logged
authorDan Brown <redacted>
Sat, 23 May 2020 10:26:48 +0000 (11:26 +0100)
committerDan Brown <redacted>
Sat, 23 May 2020 10:28:59 +0000 (11:28 +0100)
- 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

app/Config/logging.php
app/Exceptions/Handler.php
phpcs.xml
tests/ErrorTest.php
tests/SharedTestHelpers.php

index 0b55dc24db921f55bb01460fe249560349736c4f..375e84083f9cb647468521cb2a850397684b645e 100644 (file)
@@ -77,6 +77,13 @@ return [
             'driver' => 'monolog',
             'handler' => NullHandler::class,
         ],
+
+        // Testing channel
+        // Uses a shared testing instance during tests
+        // so that logs can be checked against.
+        'testing' => [
+            'driver' => 'testing',
+        ],
     ],
 
 ];
index a3bc1e8b9e222e102fe7a8c93b1d2161362a0e8b..57078522b748bbbf905b1836705a4d24c0695bbb 100644 (file)
@@ -9,7 +9,6 @@ use Illuminate\Database\Eloquent\ModelNotFoundException;
 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;
@@ -26,6 +25,7 @@ class Handler extends ExceptionHandler
         HttpException::class,
         ModelNotFoundException::class,
         ValidationException::class,
+        NotFoundException::class,
     ];
 
     /**
index 009791fc9d4cb33ddc88001dec5fef60172e7dc1..ccde28033ab9a3675dbf0203c073e09c01108925 100644 (file)
--- a/phpcs.xml
+++ b/phpcs.xml
@@ -3,6 +3,7 @@
     <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
index a5e4a4a5e58b9d1e237fa37240cca6d7079154de..8f6867cdeb0c357e16f9b7ce7df7df3813984200 100644 (file)
@@ -1,5 +1,8 @@
 <?php namespace Tests;
 
+use BookStack\Entities\Book;
+use Illuminate\Support\Facades\Log;
+
 class ErrorTest extends TestCase
 {
 
@@ -18,4 +21,21 @@ 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
index f7b7d5edf3ef23e4b3d59a0189f4707faa00f1dd..c7659a02dabae0168348553d4f0efd360f5598d9 100644 (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;
+    }
+
 }
\ No newline at end of file