Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build
composer.lock
coverage
docs
logs
phpunit.xml
phpstan.neon
testbench.yaml
Expand Down
6 changes: 3 additions & 3 deletions config/log-viewer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Opcodes\LogViewer\Enums\FolderSortingMethod;
use Opcodes\LogViewer\Enums\SortingMethod;
use Opcodes\LogViewer\Enums\SortingOrder;
use Opcodes\LogViewer\Enums\Theme;

Expand Down Expand Up @@ -283,13 +283,13 @@
'use_local_storage' => true,

// Method to sort the folders. Other options: `Alphabetical`, `ModifiedTime`
'folder_sorting_method' => FolderSortingMethod::ModifiedTime,
'folder_sorting_method' => SortingMethod::ModifiedTime,

// Order to sort the folders. Other options: `Ascending`, `Descending`
'folder_sorting_order' => SortingOrder::Descending,

// Method for sorting log-files into directories. Other options: `Alphabetical`, `ModifiedTime`
'file_sorting_method' => FolderSortingMethod::ModifiedTime,
'file_sorting_method' => SortingMethod::ModifiedTime,

// Order to sort the logs. Other options: `Ascending`, `Descending`
'log_sorting_order' => SortingOrder::Descending,
Expand Down
3 changes: 3 additions & 0 deletions src/Enums/FolderSortingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Opcodes\LogViewer\Enums;

/**
* @deprecated Use SortingMethod instead
*/
class FolderSortingMethod
{
public const Alphabetical = 'Alphabetical';
Expand Down
9 changes: 9 additions & 0 deletions src/Enums/SortingMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Opcodes\LogViewer\Enums;

class SortingMethod
{
public const Alphabetical = 'Alphabetical';
public const ModifiedTime = 'ModifiedTime';
}
21 changes: 16 additions & 5 deletions src/Http/Controllers/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\URL;
use Opcodes\LogViewer\Enums\FolderSortingMethod;
use Opcodes\LogViewer\Enums\SortingMethod;
use Opcodes\LogViewer\Enums\SortingOrder;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\Http\Resources\LogFileResource;

Expand All @@ -14,17 +15,18 @@ class FilesController
public function index(Request $request)
{
$files = LogViewer::getFiles();
$sortingMethod = config('log-viewer.defaults.file_sorting_method', FolderSortingMethod::ModifiedTime);
$sortingMethod = config('log-viewer.defaults.file_sorting_method', SortingMethod::ModifiedTime);
$direction = $this->validateDirection($request->query('direction'));

if ($sortingMethod === FolderSortingMethod::ModifiedTime) {
if ($request->query('direction', 'desc') === 'asc') {
if ($sortingMethod === SortingMethod::ModifiedTime) {
if ($direction === SortingOrder::Ascending) {
$files = $files->sortByEarliestFirst();
} else {
$files = $files->sortByLatestFirst();
}

} else {
if ($request->query('direction', 'desc') === 'asc') {
if ($direction === SortingOrder::Ascending) {
$files = $files->sortAlphabeticallyAsc();
} else {
$files = $files->sortAlphabeticallyDesc();
Expand All @@ -34,6 +36,15 @@ public function index(Request $request)
return LogFileResource::collection($files);
}

private function validateDirection(?string $direction): string
{
if ($direction === SortingOrder::Ascending) {
return SortingOrder::Ascending;
}

return SortingOrder::Descending;
}

public function requestDownload(Request $request, string $fileIdentifier)
{
$file = LogViewer::getFile($fileIdentifier);
Expand Down
27 changes: 18 additions & 9 deletions src/Http/Controllers/FoldersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\URL;
use Opcodes\LogViewer\Enums\FolderSortingMethod;
use Opcodes\LogViewer\Enums\SortingMethod;
use Opcodes\LogViewer\Enums\SortingOrder;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\Http\Resources\LogFolderResource;
Expand All @@ -17,20 +17,20 @@ public function index(Request $request)
{
$folders = LogViewer::getFilesGroupedByFolder();

$sortingMethod = config('log-viewer.defaults.folder_sorting_method', FolderSortingMethod::ModifiedTime);
$sortingMethod = config('log-viewer.defaults.folder_sorting_method', SortingMethod::ModifiedTime);
$sortingOrder = config('log-viewer.defaults.folder_sorting_order', SortingOrder::Descending);

$fileSortingMethod = config('log-viewer.defaults.file_sorting_method', FolderSortingMethod::ModifiedTime);
$fileSortingOrder = $request->query('direction', 'desc');
$fileSortingMethod = config('log-viewer.defaults.file_sorting_method', SortingMethod::ModifiedTime);
$fileSortingOrder = $this->validateDirection($request->query('direction'));

if ($sortingMethod === FolderSortingMethod::Alphabetical) {
if ($sortingMethod === SortingMethod::Alphabetical) {
if ($sortingOrder === SortingOrder::Ascending) {
$folders = $folders->sortAlphabeticallyAsc();
} else {
$folders = $folders->sortAlphabeticallyDesc();
}
} else { // ModifiedTime
if ($fileSortingOrder === 'asc') {
if ($fileSortingOrder === SortingOrder::Ascending) {
$folders = $folders->sortByEarliestFirstIncludingFiles();
} else {
$folders = $folders->sortByLatestFirstIncludingFiles();
Expand All @@ -39,15 +39,15 @@ public function index(Request $request)

// Sort files within folders after sorting folders
$folders->each(function ($folder) use ($fileSortingMethod, $fileSortingOrder) {
if ($fileSortingMethod === FolderSortingMethod::ModifiedTime) {
if ($fileSortingOrder === 'asc') {
if ($fileSortingMethod === SortingMethod::ModifiedTime) {
if ($fileSortingOrder === SortingOrder::Ascending) {
$folder->files()->sortByEarliestFirst();
} else {
$folder->files()->sortByLatestFirst();
}

} else {
if ($fileSortingOrder === 'asc') {
if ($fileSortingOrder === SortingOrder::Ascending) {
$folder->files()->sortAlphabeticallyAsc();
} else {
$folder->files()->sortAlphabeticallyDesc();
Expand All @@ -58,6 +58,15 @@ public function index(Request $request)
return LogFolderResource::collection($folders->values());
}

private function validateDirection(?string $direction): string
{
if ($direction === SortingOrder::Ascending) {
return SortingOrder::Ascending;
}

return SortingOrder::Descending;
}

public function requestDownload(Request $request, string $folderIdentifier)
{
$folder = LogViewer::getFolder($folderIdentifier);
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Opcodes\LogViewer\Http\Controllers;

use Opcodes\LogViewer\Enums\FolderSortingMethod;
use Opcodes\LogViewer\Enums\SortingMethod;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogFolder;
use Opcodes\LogViewer\Utils\Utils;
Expand All @@ -15,7 +15,7 @@ public function __invoke()
abort(404);
}

$files_sort_by_time = config('log-viewer.defaults.file_sorting_method') === FolderSortingMethod::ModifiedTime;
$files_sort_by_time = config('log-viewer.defaults.file_sorting_method') === SortingMethod::ModifiedTime;

return view(LogViewer::getViewLayout(), [
'logViewerScriptVariables' => [
Expand Down
178 changes: 178 additions & 0 deletions tests/Feature/InvalidSortingValuesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

use Opcodes\LogViewer\Enums\SortingMethod;
use Opcodes\LogViewer\Enums\SortingOrder;

use function Pest\Laravel\getJson;

beforeEach(function () {
config(['log-viewer.include_files' => ['*.log', '*/**.log']]);
});

// FilesController with invalid sorting values

it('files controller returns default desc order when invalid direction is provided', function () {
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
$names = ['one.log', 'two.log', 'three.log'];
generateLogFiles($names, randomContent: true);

array_map(function (string $name) {
$this->travelTo(now()->addSecond());
touch(storage_path('logs/'.$name), now()->timestamp);
}, $names);

$response = getJson(route('log-viewer.files', ['direction' => 'invalid']));

expect(array_column($response->json(), 'name'))->toBe([
'three.log',
'two.log',
'one.log',
]);
});

it('files controller returns default desc order when empty direction is provided', function () {
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
$names = ['one.log', 'two.log', 'three.log'];
generateLogFiles($names, randomContent: true);

array_map(function (string $name) {
$this->travelTo(now()->addSecond());
touch(storage_path('logs/'.$name), now()->timestamp);
}, $names);

$response = getJson(route('log-viewer.files', ['direction' => '']));

expect(array_column($response->json(), 'name'))->toBe([
'three.log',
'two.log',
'one.log',
]);
});

it('files controller returns default desc order when direction is null', function () {
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
$names = ['one.log', 'two.log', 'three.log'];
generateLogFiles($names, randomContent: true);

array_map(function (string $name) {
$this->travelTo(now()->addSecond());
touch(storage_path('logs/'.$name), now()->timestamp);
}, $names);

$response = getJson(route('log-viewer.files'));

expect(array_column($response->json(), 'name'))->toBe([
'three.log',
'two.log',
'one.log',
]);
});

it('files controller returns alphabetical sorting with default desc when invalid direction is provided', function () {
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::Alphabetical]);
generateLogFiles(['one.log', 'two.log', 'three.log', 'four.log'], randomContent: true);

$response = getJson(route('log-viewer.files', ['direction' => 'invalid']));

expect(array_column($response->json(), 'name'))->toBe([
'two.log',
'three.log',
'one.log',
'four.log',
]);
});

it('files controller returns correct order with valid asc direction and alphabetical sorting', function () {
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::Alphabetical]);
generateLogFiles(['one.log', 'two.log', 'three.log', 'four.log'], randomContent: true);

$response = getJson(route('log-viewer.files', ['direction' => SortingOrder::Ascending]));

expect(array_column($response->json(), 'name'))->toBe([
'four.log',
'one.log',
'three.log',
'two.log',
]);
});

// FoldersController with invalid sorting values

it('folders controller returns default desc order when invalid direction is provided', function () {
config(['log-viewer.include_files' => ['*/**.log']]);
config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]);
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
$names = ['sub/one.log', 'sub/two.log', 'sub/three.log'];
generateLogFiles($names, randomContent: true);

array_map(function (string $name) {
$this->travelTo(now()->addSecond());
touch(storage_path('logs/'.$name), now()->timestamp);
}, $names);

$response = getJson(route('log-viewer.folders', ['direction' => 'invalid']));

expect(array_column($response->json()[0]['files'], 'name'))->toBe([
'three.log',
'two.log',
'one.log',
]);
});

it('folders controller returns default desc order when empty direction is provided', function () {
config(['log-viewer.include_files' => ['*/**.log']]);
config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]);
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
$names = ['sub/one.log', 'sub/two.log', 'sub/three.log'];
generateLogFiles($names, randomContent: true);

array_map(function (string $name) {
$this->travelTo(now()->addSecond());
touch(storage_path('logs/'.$name), now()->timestamp);
}, $names);

$response = getJson(route('log-viewer.folders', ['direction' => '']));

expect(array_column($response->json()[0]['files'], 'name'))->toBe([
'three.log',
'two.log',
'one.log',
]);
});

it('folders controller returns default desc order when direction is null', function () {
config(['log-viewer.include_files' => ['*/**.log']]);
config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]);
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]);
$names = ['sub/one.log', 'sub/two.log', 'sub/three.log'];
generateLogFiles($names, randomContent: true);

array_map(function (string $name) {
$this->travelTo(now()->addSecond());
touch(storage_path('logs/'.$name), now()->timestamp);
}, $names);

$response = getJson(route('log-viewer.folders'));

expect(array_column($response->json()[0]['files'], 'name'))->toBe([
'three.log',
'two.log',
'one.log',
]);
});

it('folders controller returns correct order with valid asc direction and alphabetical sorting', function () {
config(['log-viewer.include_files' => ['*/**.log']]);
config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]);
config(['log-viewer.defaults.file_sorting_method' => SortingMethod::Alphabetical]);
generateLogFiles(['sub/one.log', 'sub/two.log', 'sub/three.log', 'sub/four.log'], randomContent: true);

$response = getJson(route('log-viewer.folders', ['direction' => SortingOrder::Ascending]));

expect(array_column($response->json()[0]['files'], 'name'))->toBe([
'four.log',
'one.log',
'three.log',
'two.log',
]);
});
Loading
Loading