From fdedbbf68a8692a52450bd3f3874ed0577622cd3 Mon Sep 17 00:00:00 2001 From: pxpm Date: Fri, 2 Aug 2024 14:31:52 +0100 Subject: [PATCH 1/9] init search trait tests --- src/app/Library/CrudPanel/Traits/Search.php | 4 +- tests/Unit/CrudPanel/CrudPanelSearchTest.php | 162 +++++++++++++++++++ 2 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/CrudPanel/CrudPanelSearchTest.php diff --git a/src/app/Library/CrudPanel/Traits/Search.php b/src/app/Library/CrudPanel/Traits/Search.php index db789df4bf..2e5a8313dd 100644 --- a/src/app/Library/CrudPanel/Traits/Search.php +++ b/src/app/Library/CrudPanel/Traits/Search.php @@ -45,7 +45,7 @@ public function applySearchLogicForColumn($query, $column, $searchTerm) $searchLogic = $column['searchLogic']; // if a closure was passed, execute it - if (is_callable($searchLogic)) { + if ($searchLogic instanceof \Closure) { return $searchLogic($query, $column, $searchTerm); } @@ -55,7 +55,7 @@ public function applySearchLogicForColumn($query, $column, $searchTerm) } // if false was passed, don't search this column - if ($searchLogic == false) { + if ($searchLogic === false) { return; } } diff --git a/tests/Unit/CrudPanel/CrudPanelSearchTest.php b/tests/Unit/CrudPanel/CrudPanelSearchTest.php new file mode 100644 index 0000000000..2096fba8bb --- /dev/null +++ b/tests/Unit/CrudPanel/CrudPanelSearchTest.php @@ -0,0 +1,162 @@ +crudPanel->setModel(User::class); + } + + #[DataProvider('columnsDefaultSearchLogic')] + public function testItCanApplyCustomSearchLogicOnColumns($searchTerm, $columnType, $resultSql) + { + $this->crudPanel->addColumn([ + 'name' => 'test', + 'type' => $columnType, + 'searchLogic' => $columnType, + 'tableColumn' => true, + 'entity' => $columnType === 'select' ? 'accountDetails' : ($columnType === 'select_multiple' ? 'articles' : false), + 'relation_type' => $columnType === 'select' ? 'HasOne' : ($columnType === 'select_multiple' ? 'HasMany' : false), + ]); + + $this->crudPanel->applySearchTerm($searchTerm); + + $this->assertEquals($resultSql, $this->crudPanel->query->toRawSql()); + } + + public function testItDoesNotAttemptToSearchTheColumnIfSearchLogicIsDisabled() + { + $this->crudPanel->addColumn([ + 'name' => 'test', + 'type' => 'text', + 'searchLogic' => false, + 'tableColumn' => true, + ]); + + $this->crudPanel->applySearchTerm('test'); + + $this->assertEquals('select * from "users"', $this->crudPanel->query->toRawSql()); + } + + public function testItDoesNotAttemptToApplyDefaultLogicIfColumnIsNotATableColumn() + { + $this->crudPanel->addColumn([ + 'name' => 'test', + 'type' => 'text', + 'searchLogic' => 'text', + 'tableColumn' => false, + ]); + + $this->crudPanel->applySearchTerm('test'); + + $this->assertEquals('select * from "users"', $this->crudPanel->query->toRawSql()); + } + + public function testItValidateDateAndDatetimeSearchTermsAndDoesNotApplySearchIfValidationFails() + { + $this->crudPanel->addColumn([ + 'name' => 'test', + 'type' => 'date', + 'searchLogic' => 'date', + 'tableColumn' => true, + ]); + + $this->crudPanel->addColumn([ + 'name' => 'test', + 'type' => 'datetime', + 'searchLogic' => 'datetime', + 'tableColumn' => true, + ]); + + $this->crudPanel->applySearchTerm('invalid-date'); + + $this->assertEquals('select * from "users"', $this->crudPanel->query->toRawSql()); + } + + public function testItCanApplySearchLogicFromClosure() + { + $this->crudPanel->addColumn([ + 'name' => 'test', + 'type' => 'my_custom_type', + 'searchLogic' => function ($query, $column, $searchTerm) { + $query->where($column['name'], 'like', "%{$searchTerm}%"); + }, + 'tableColumn' => true, + ]); + + $this->crudPanel->applySearchTerm('test'); + + $this->assertEquals('select * from "users" where ("test" like \'%test%\')', $this->crudPanel->query->toRawSql()); + } + + public function testItCanGetAndSetPersistentTable() + { + $this->crudPanel->enablePersistentTable(true); + + $this->assertTrue($this->crudPanel->getPersistentTable()); + + $this->crudPanel->disablePersistentTable(); + + $this->assertFalse($this->crudPanel->getPersistentTable()); + } + + public function testItCanGetAndSetTheResponsiveTable() + { + $this->crudPanel->enableResponsiveTable(true); + + $this->assertTrue($this->crudPanel->getResponsiveTable()); + + $this->crudPanel->disableResponsiveTable(); + + $this->assertFalse($this->crudPanel->getResponsiveTable()); + } + + public function testItCanGetPersistentTableDurationFromOperationSetting() + { + $this->crudPanel->setOperationSetting('persistentTableDuration', 10); + + $this->assertEquals(10, $this->crudPanel->getPersistentTableDuration()); + } + + public function testItCanGetPersistentTableDurantionFromConfig() + { + $this->assertEquals(false, $this->crudPanel->getPersistentTableDuration()); + + config(['backpack.crud.operations.list.persistentTableDuration' => 10]); + + $this->assertEquals(10, $this->crudPanel->getPersistentTableDuration()); + } + + public function testItCanGetResponsiveTableFromConfig() + { + $this->assertEquals(false, $this->crudPanel->getResponsiveTable()); + + config(['backpack.crud.operations.list.responsiveTable' => true]); + + $this->assertTrue($this->crudPanel->getResponsiveTable()); + } + + + + public static function columnsDefaultSearchLogic() + { + return [ + ['test', 'text', 'select * from "users" where ("users"."test" like \'%test%\')'], + ['test', 'email', 'select * from "users" where ("users"."test" like \'%test%\')'], + ['test', 'textarea', 'select * from "users" where ("users"."test" like \'%test%\')'], + ['2023-12-24', 'date', 'select * from "users" where (strftime(\'%Y-%m-%d\', "users"."test") = cast(\'2023-12-24\' as text))'], + ['2023-12-24', 'datetime', 'select * from "users" where (strftime(\'%Y-%m-%d\', "users"."test") = cast(\'2023-12-24\' as text))'], + ['test', 'select', 'select * from "users" where (exists (select * from "account_details" where "users"."id" = "account_details"."user_id" and "account_details"."nickname" like \'%test%\'))'], + ['test', 'select_multiple', 'select * from "users" where (exists (select * from "articles" where "users"."id" = "articles"."user_id" and "articles"."content" like \'%test%\'))'] + ]; + } +} From 6cfc2c88d524b88cde7a86d80aa37ea8e11cc6e6 Mon Sep 17 00:00:00 2001 From: pxpm Date: Fri, 2 Aug 2024 15:04:53 +0100 Subject: [PATCH 2/9] add tests for column view render --- tests/BaseTestClass.php | 11 ++++ tests/Unit/CrudPanel/CrudPanelSearchTest.php | 66 ++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/tests/BaseTestClass.php b/tests/BaseTestClass.php index afc3a7eebe..a7d349abda 100644 --- a/tests/BaseTestClass.php +++ b/tests/BaseTestClass.php @@ -63,6 +63,17 @@ protected function makeAnArticleModel(array $attributes = []) return \Backpack\CRUD\Tests\config\Models\Article::make($attributes); } + protected function makeAUserModel(array $attributes = []) + { + $attributes = array_merge([ + 'id' => 1, + 'name' => 'user', + 'email' => 'user@email.com' + ], $attributes); + + return \Backpack\CRUD\Tests\config\Models\User::make($attributes); + } + // allow us to run crud panel private/protected methods like `inferFieldTypeFromDbColumnType` public function invokeMethod(&$object, $methodName, array $parameters = []) { diff --git a/tests/Unit/CrudPanel/CrudPanelSearchTest.php b/tests/Unit/CrudPanel/CrudPanelSearchTest.php index 2096fba8bb..b231ed5682 100644 --- a/tests/Unit/CrudPanel/CrudPanelSearchTest.php +++ b/tests/Unit/CrudPanel/CrudPanelSearchTest.php @@ -10,6 +10,8 @@ */ class CrudPanelSearchTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel { + private string $expectedDefaultColumnValue = "\n user\n "; + public function setUp():void { parent::setUp(); @@ -145,8 +147,72 @@ public function testItCanGetResponsiveTableFromConfig() $this->assertTrue($this->crudPanel->getResponsiveTable()); } + public function testItCanGetTheRenderedViewsForTheColumns() + { + $this->crudPanel->addColumn([ + 'name' => 'name', + 'type' => 'test', + ]); + + $entries = [$this->makeAUserModel()]; + + $rowColumnsHtml = trim($this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0]); + + $this->assertEquals($this->expectedDefaultColumnValue, $rowColumnsHtml); + } + + public function testItRendersTheDetailsRow() + { + $this->crudPanel->addColumn([ + 'name' => 'name', + 'type' => 'test', + ]); + + $this->crudPanel->setOperationSetting('detailsRow', true); + $entries = [$this->makeAUserModel()]; + + $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0]; + $rowColumnsHtml = str_replace($this->expectedDefaultColumnValue, '', $rowColumnsHtml); + $this->assertStringContainsString('details-row-button', $rowColumnsHtml); + + } + + public function testItRendersTheBulkActions() + { + $this->crudPanel->addColumn([ + 'name' => 'name', + 'type' => 'test', + ]); + + $this->crudPanel->setOperationSetting('bulkActions', true); + $entries = [$this->makeAUserModel()]; + + $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0]; + + $rowColumnsHtml = str_replace($this->expectedDefaultColumnValue, '', $rowColumnsHtml); + + $this->assertStringContainsString('crud_bulk_actions_line_checkbox', $rowColumnsHtml); + + } + + public function testItRendersTheLineStackButtons() + { + $this->crudPanel->addColumn([ + 'name' => 'name', + 'type' => 'test', + ]); + + $this->crudPanel->button('test')->stack('line')->type('view')->content('backpack.theme-coreuiv2::buttons.test'); + $entries = [$this->makeAUserModel()]; + + $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][1]; + + $this->assertStringContainsString('btn-secondary', $rowColumnsHtml); + + } + public static function columnsDefaultSearchLogic() { return [ From 7e9ac711cac9eead38f63a5c587a58fc43d9bf02 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 2 Aug 2024 14:05:11 +0000 Subject: [PATCH 3/9] Apply fixes from StyleCI [ci skip] [skip ci] --- tests/BaseTestClass.php | 2 +- tests/Unit/CrudPanel/CrudPanelSearchTest.php | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/BaseTestClass.php b/tests/BaseTestClass.php index a7d349abda..f3ef9b9b32 100644 --- a/tests/BaseTestClass.php +++ b/tests/BaseTestClass.php @@ -68,7 +68,7 @@ protected function makeAUserModel(array $attributes = []) $attributes = array_merge([ 'id' => 1, 'name' => 'user', - 'email' => 'user@email.com' + 'email' => 'user@email.com', ], $attributes); return \Backpack\CRUD\Tests\config\Models\User::make($attributes); diff --git a/tests/Unit/CrudPanel/CrudPanelSearchTest.php b/tests/Unit/CrudPanel/CrudPanelSearchTest.php index b231ed5682..849f4389c9 100644 --- a/tests/Unit/CrudPanel/CrudPanelSearchTest.php +++ b/tests/Unit/CrudPanel/CrudPanelSearchTest.php @@ -12,7 +12,7 @@ class CrudPanelSearchTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrud { private string $expectedDefaultColumnValue = "\n user\n "; - public function setUp():void + public function setUp(): void { parent::setUp(); $this->crudPanel->setModel(User::class); @@ -27,7 +27,7 @@ public function testItCanApplyCustomSearchLogicOnColumns($searchTerm, $columnTyp 'searchLogic' => $columnType, 'tableColumn' => true, 'entity' => $columnType === 'select' ? 'accountDetails' : ($columnType === 'select_multiple' ? 'articles' : false), - 'relation_type' => $columnType === 'select' ? 'HasOne' : ($columnType === 'select_multiple' ? 'HasMany' : false), + 'relation_type' => $columnType === 'select' ? 'HasOne' : ($columnType === 'select_multiple' ? 'HasMany' : false), ]); $this->crudPanel->applySearchTerm($searchTerm); @@ -176,7 +176,6 @@ public function testItRendersTheDetailsRow() $rowColumnsHtml = str_replace($this->expectedDefaultColumnValue, '', $rowColumnsHtml); $this->assertStringContainsString('details-row-button', $rowColumnsHtml); - } public function testItRendersTheBulkActions() @@ -194,7 +193,6 @@ public function testItRendersTheBulkActions() $rowColumnsHtml = str_replace($this->expectedDefaultColumnValue, '', $rowColumnsHtml); $this->assertStringContainsString('crud_bulk_actions_line_checkbox', $rowColumnsHtml); - } public function testItRendersTheLineStackButtons() @@ -210,9 +208,8 @@ public function testItRendersTheLineStackButtons() $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][1]; $this->assertStringContainsString('btn-secondary', $rowColumnsHtml); - } - + public static function columnsDefaultSearchLogic() { return [ @@ -222,7 +219,7 @@ public static function columnsDefaultSearchLogic() ['2023-12-24', 'date', 'select * from "users" where (strftime(\'%Y-%m-%d\', "users"."test") = cast(\'2023-12-24\' as text))'], ['2023-12-24', 'datetime', 'select * from "users" where (strftime(\'%Y-%m-%d\', "users"."test") = cast(\'2023-12-24\' as text))'], ['test', 'select', 'select * from "users" where (exists (select * from "account_details" where "users"."id" = "account_details"."user_id" and "account_details"."nickname" like \'%test%\'))'], - ['test', 'select_multiple', 'select * from "users" where (exists (select * from "articles" where "users"."id" = "articles"."user_id" and "articles"."content" like \'%test%\'))'] + ['test', 'select_multiple', 'select * from "users" where (exists (select * from "articles" where "users"."id" = "articles"."user_id" and "articles"."content" like \'%test%\'))'], ]; } } From 2756c377849e6106cb182cfe1ed92fe9f5872b79 Mon Sep 17 00:00:00 2001 From: pxpm Date: Fri, 2 Aug 2024 15:11:07 +0100 Subject: [PATCH 4/9] use PHP_EOL for portability --- tests/Unit/CrudPanel/CrudPanelSearchTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/CrudPanel/CrudPanelSearchTest.php b/tests/Unit/CrudPanel/CrudPanelSearchTest.php index 849f4389c9..967e1a4e44 100644 --- a/tests/Unit/CrudPanel/CrudPanelSearchTest.php +++ b/tests/Unit/CrudPanel/CrudPanelSearchTest.php @@ -10,7 +10,7 @@ */ class CrudPanelSearchTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel { - private string $expectedDefaultColumnValue = "\n user\n "; + private string $expectedDefaultColumnValue = "".PHP_EOL." user".PHP_EOL." "; public function setUp(): void { From 0c880341eff94e8ca1ceeecd260d5a8280ffd995 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 2 Aug 2024 14:11:23 +0000 Subject: [PATCH 5/9] Apply fixes from StyleCI [ci skip] [skip ci] --- tests/Unit/CrudPanel/CrudPanelSearchTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/CrudPanel/CrudPanelSearchTest.php b/tests/Unit/CrudPanel/CrudPanelSearchTest.php index 967e1a4e44..7633fc2231 100644 --- a/tests/Unit/CrudPanel/CrudPanelSearchTest.php +++ b/tests/Unit/CrudPanel/CrudPanelSearchTest.php @@ -10,7 +10,7 @@ */ class CrudPanelSearchTest extends \Backpack\CRUD\Tests\config\CrudPanel\BaseCrudPanel { - private string $expectedDefaultColumnValue = "".PHP_EOL." user".PHP_EOL." "; + private string $expectedDefaultColumnValue = ''.PHP_EOL.' user'.PHP_EOL.' '; public function setUp(): void { From 5679ad70cfba9de7e81fa1afde93b48934f251bc Mon Sep 17 00:00:00 2001 From: pxpm Date: Fri, 2 Aug 2024 15:43:33 +0100 Subject: [PATCH 6/9] add more tests --- src/app/Library/CrudPanel/Traits/Search.php | 6 +- tests/Unit/CrudPanel/CrudPanelSearchTest.php | 60 +++++++++++++++++++ .../Models/TestModelWithTranslations.php | 12 +--- .../Models/Traits/HasTranslationsFake.php | 21 +++++++ tests/config/Models/UserWithTranslations.php | 17 ++++++ 5 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 tests/config/Models/Traits/HasTranslationsFake.php create mode 100644 tests/config/Models/UserWithTranslations.php diff --git a/src/app/Library/CrudPanel/Traits/Search.php b/src/app/Library/CrudPanel/Traits/Search.php index 2e5a8313dd..8dc6209d57 100644 --- a/src/app/Library/CrudPanel/Traits/Search.php +++ b/src/app/Library/CrudPanel/Traits/Search.php @@ -90,7 +90,6 @@ public function applySearchLogicForColumn($query, $column, $searchTerm) break; default: - return; break; } } @@ -101,13 +100,14 @@ public function applySearchLogicForColumn($query, $column, $searchTerm) */ public function applyDatatableOrder() { - if (request()->input('order')) { + if ($this->getRequest()->input('order')) { // clear any past orderBy rules $this->query->getQuery()->orders = null; - foreach ((array) request()->input('order') as $order) { + foreach ((array) $this->getRequest()->input('order') as $order) { $column_number = (int) $order['column']; $column_direction = (strtolower((string) $order['dir']) == 'asc' ? 'ASC' : 'DESC'); $column = $this->findColumnById($column_number); + if ($column['tableColumn'] && ! isset($column['orderLogic'])) { if (method_exists($this->model, 'translationEnabled') && $this->model->translationEnabled() && diff --git a/tests/Unit/CrudPanel/CrudPanelSearchTest.php b/tests/Unit/CrudPanel/CrudPanelSearchTest.php index 7633fc2231..51c1a883b7 100644 --- a/tests/Unit/CrudPanel/CrudPanelSearchTest.php +++ b/tests/Unit/CrudPanel/CrudPanelSearchTest.php @@ -3,6 +3,7 @@ namespace Backpack\CRUD\Tests\Unit\CrudPanel; use Backpack\CRUD\Tests\config\Models\User; +use Backpack\CRUD\Tests\config\Models\UserWithTranslations; use PHPUnit\Framework\Attributes\DataProvider; /** @@ -210,6 +211,65 @@ public function testItRendersTheLineStackButtons() $this->assertStringContainsString('btn-secondary', $rowColumnsHtml); } + public function testItAppliesCustomOrderByPrimaryKeyForDatatables() + { + $this->crudPanel->applyDatatableOrder(); + + $this->assertEquals('select * from "users" order by "id" desc', $this->crudPanel->query->toRawSql()); + } + + public function testItCanApplyDatatableOrderFromRequest() + { + $this->crudPanel->addColumn([ + 'name' => 'name', + 'type' => 'test', + 'tableColumn' => true, + ]); + $this->setupUserCreateRequest(); + $this->crudPanel->getRequest()->merge(['order' => [['column' => 0, 'dir' => 'asc']]]); + + $this->crudPanel->applyDatatableOrder(); + + $this->assertEquals('select * from "users" order by "name" asc, "id" desc', $this->crudPanel->query->toRawSql()); + + } + + public function testItCanApplySearchLogicForTranslatableJsonColumns() + { + $this->crudPanel->setModel(UserWithTranslations::class); + + $this->crudPanel->addColumn([ + 'name' => 'json', + 'type' => 'json', + 'tableColumn' => true, + ]); + $this->setupUserCreateRequest(); + $this->crudPanel->getRequest()->merge(['order' => [['column' => 0, 'dir' => 'asc']]]); + + $this->crudPanel->applyDatatableOrder(); + + $this->assertEquals('select * from "users" order by json_extract("json", \'$."en"\') asc, "id" desc', $this->crudPanel->query->toRawSql()); + + } + + public function testItCanApplySearchLogicForTranslatableColumns() + { + $this->crudPanel->setModel(UserWithTranslations::class); + + $this->crudPanel->addColumn([ + 'name' => 'name', + 'type' => 'text', + 'tableColumn' => true, + ]); + $this->setupUserCreateRequest(); + $this->crudPanel->getRequest()->merge(['order' => [['column' => 0, 'dir' => 'asc']]]); + + $this->crudPanel->applyDatatableOrder(); + + $this->assertEquals('select * from "users" order by "name" asc, "id" desc', $this->crudPanel->query->toRawSql()); + + } + public static function columnsDefaultSearchLogic() { return [ diff --git a/tests/config/Models/TestModelWithTranslations.php b/tests/config/Models/TestModelWithTranslations.php index 5c6556610a..bbd8722ca6 100644 --- a/tests/config/Models/TestModelWithTranslations.php +++ b/tests/config/Models/TestModelWithTranslations.php @@ -1,16 +1,8 @@ null]; - } + use Traits\HasTranslationsFake; } diff --git a/tests/config/Models/Traits/HasTranslationsFake.php b/tests/config/Models/Traits/HasTranslationsFake.php new file mode 100644 index 0000000000..43c4e0c706 --- /dev/null +++ b/tests/config/Models/Traits/HasTranslationsFake.php @@ -0,0 +1,21 @@ + null]; + } + + public function isTranslatableAttribute(string $attribute) + { + return true; + } +} \ No newline at end of file diff --git a/tests/config/Models/UserWithTranslations.php b/tests/config/Models/UserWithTranslations.php new file mode 100644 index 0000000000..ec2b34246c --- /dev/null +++ b/tests/config/Models/UserWithTranslations.php @@ -0,0 +1,17 @@ + Date: Fri, 2 Aug 2024 14:44:37 +0000 Subject: [PATCH 7/9] Apply fixes from StyleCI [ci skip] [skip ci] --- tests/Unit/CrudPanel/CrudPanelSearchTest.php | 3 --- tests/config/Models/Traits/HasTranslationsFake.php | 2 +- tests/config/Models/UserWithTranslations.php | 8 +++++--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/Unit/CrudPanel/CrudPanelSearchTest.php b/tests/Unit/CrudPanel/CrudPanelSearchTest.php index 51c1a883b7..0111ca7a7e 100644 --- a/tests/Unit/CrudPanel/CrudPanelSearchTest.php +++ b/tests/Unit/CrudPanel/CrudPanelSearchTest.php @@ -231,7 +231,6 @@ public function testItCanApplyDatatableOrderFromRequest() $this->crudPanel->applyDatatableOrder(); $this->assertEquals('select * from "users" order by "name" asc, "id" desc', $this->crudPanel->query->toRawSql()); - } public function testItCanApplySearchLogicForTranslatableJsonColumns() @@ -249,7 +248,6 @@ public function testItCanApplySearchLogicForTranslatableJsonColumns() $this->crudPanel->applyDatatableOrder(); $this->assertEquals('select * from "users" order by json_extract("json", \'$."en"\') asc, "id" desc', $this->crudPanel->query->toRawSql()); - } public function testItCanApplySearchLogicForTranslatableColumns() @@ -267,7 +265,6 @@ public function testItCanApplySearchLogicForTranslatableColumns() $this->crudPanel->applyDatatableOrder(); $this->assertEquals('select * from "users" order by "name" asc, "id" desc', $this->crudPanel->query->toRawSql()); - } public static function columnsDefaultSearchLogic() diff --git a/tests/config/Models/Traits/HasTranslationsFake.php b/tests/config/Models/Traits/HasTranslationsFake.php index 43c4e0c706..c498f06d36 100644 --- a/tests/config/Models/Traits/HasTranslationsFake.php +++ b/tests/config/Models/Traits/HasTranslationsFake.php @@ -18,4 +18,4 @@ public function isTranslatableAttribute(string $attribute) { return true; } -} \ No newline at end of file +} diff --git a/tests/config/Models/UserWithTranslations.php b/tests/config/Models/UserWithTranslations.php index ec2b34246c..d9aa58a707 100644 --- a/tests/config/Models/UserWithTranslations.php +++ b/tests/config/Models/UserWithTranslations.php @@ -6,10 +6,12 @@ class UserWithTranslations extends User { use Traits\HasTranslationsFake; - public static function getDbTableSchema() + public static function getDbTableSchema() { - return new class { - public function getColumnType($column) { + return new class + { + public function getColumnType($column) + { return $column; } }; From a80603d18b7f4729e09b36787b786bc4243b667a Mon Sep 17 00:00:00 2001 From: pxpm Date: Fri, 2 Aug 2024 15:46:15 +0100 Subject: [PATCH 8/9] remove un-needed replace --- tests/Unit/CrudPanel/CrudPanelSearchTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Unit/CrudPanel/CrudPanelSearchTest.php b/tests/Unit/CrudPanel/CrudPanelSearchTest.php index 0111ca7a7e..de34738024 100644 --- a/tests/Unit/CrudPanel/CrudPanelSearchTest.php +++ b/tests/Unit/CrudPanel/CrudPanelSearchTest.php @@ -174,8 +174,6 @@ public function testItRendersTheDetailsRow() $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0]; - $rowColumnsHtml = str_replace($this->expectedDefaultColumnValue, '', $rowColumnsHtml); - $this->assertStringContainsString('details-row-button', $rowColumnsHtml); } @@ -191,8 +189,6 @@ public function testItRendersTheBulkActions() $rowColumnsHtml = $this->crudPanel->getEntriesAsJsonForDatatables($entries, 1, 0)['data'][0][0]; - $rowColumnsHtml = str_replace($this->expectedDefaultColumnValue, '', $rowColumnsHtml); - $this->assertStringContainsString('crud_bulk_actions_line_checkbox', $rowColumnsHtml); } From b36c80257bc6b60c8d87709703aff520430134c3 Mon Sep 17 00:00:00 2001 From: pxpm Date: Fri, 2 Aug 2024 15:57:43 +0100 Subject: [PATCH 9/9] add few more test scenarios --- tests/Unit/CrudPanel/CrudPanelSearchTest.php | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/Unit/CrudPanel/CrudPanelSearchTest.php b/tests/Unit/CrudPanel/CrudPanelSearchTest.php index de34738024..a1c6f39ea4 100644 --- a/tests/Unit/CrudPanel/CrudPanelSearchTest.php +++ b/tests/Unit/CrudPanel/CrudPanelSearchTest.php @@ -112,6 +112,11 @@ public function testItCanGetAndSetPersistentTable() $this->assertFalse($this->crudPanel->getPersistentTable()); } + public function testItCanGetPersistentTableFromConfig() + { + $this->assertNull($this->crudPanel->getPersistentTable()); + } + public function testItCanGetAndSetTheResponsiveTable() { $this->crudPanel->enableResponsiveTable(true); @@ -214,6 +219,44 @@ public function testItAppliesCustomOrderByPrimaryKeyForDatatables() $this->assertEquals('select * from "users" order by "id" desc', $this->crudPanel->query->toRawSql()); } + public function testItCanApplyACustomSearchLogic() + { + $this->crudPanel->addColumn([ + 'name' => 'name', + 'type' => 'text', + 'tableColumn' => true, + 'orderLogic' => function ($query, $column, $searchTerm) { + $query->orderBy('name', 'asc'); + }, + ]); + + $this->setupUserCreateRequest(); + $this->crudPanel->getRequest()->merge(['order' => [['column' => 0, 'dir' => 'asc']]]); + + $this->crudPanel->applyDatatableOrder(); + + $this->assertEquals('select * from "users" order by "name" asc, "id" desc', $this->crudPanel->query->toRawSql()); + } + + public function testItDoesNotReplacePrimaryKeyIfItAlreadyExists() + { + $this->crudPanel->addColumn([ + 'name' => 'name', + 'type' => 'text', + 'tableColumn' => true, + 'orderLogic' => function ($query, $column, $searchTerm) { + $query->orderBy('id', 'asc'); + }, + ]); + + $this->setupUserCreateRequest(); + $this->crudPanel->getRequest()->merge(['order' => [['column' => 0, 'dir' => 'asc']]]); + + $this->crudPanel->applyDatatableOrder(); + + $this->assertEquals('select * from "users" order by "id" asc', $this->crudPanel->query->toRawSql()); + } + public function testItCanApplyDatatableOrderFromRequest() { $this->crudPanel->addColumn([