From 7fd887c25699de57297a1658481109aa3458944a Mon Sep 17 00:00:00 2001 From: Luan Freitas <33601626+luanfreitasdev@users.noreply.github.com> Date: Fri, 2 Feb 2024 19:49:30 -0300 Subject: [PATCH] 1372 datepicker cant reset the datepicker filter undefined array key start (#1375) * Fix "Undefined array key start * Rollback pipelines - mysql and pgsql * Remove FilterDateTimeTest - "As we deal with javascript, I need to test using cypress later" --- .github/workflows/mysql.yml | 12 +- .github/workflows/pgsql.yml | 12 +- .../Filters/Builders/DatePicker.php | 8 + .../Filters/Builders/DateTimePicker.php | 8 + tests/Feature/Filters/FilterDateTimeTest.php | 200 ------------------ 5 files changed, 38 insertions(+), 202 deletions(-) delete mode 100644 tests/Feature/Filters/FilterDateTimeTest.php diff --git a/.github/workflows/mysql.yml b/.github/workflows/mysql.yml index d943c069..a5bbea2a 100644 --- a/.github/workflows/mysql.yml +++ b/.github/workflows/mysql.yml @@ -1,7 +1,17 @@ name: MySQL on: - workflow_dispatch: + push: + branches: + - "**" + paths-ignore: + - "art/**" + - "README.md" + pull_request: + types: [ready_for_review, synchronize, opened] + paths-ignore: + - "art/**" + - "README.md" jobs: build: diff --git a/.github/workflows/pgsql.yml b/.github/workflows/pgsql.yml index 27711e5c..0cf98624 100644 --- a/.github/workflows/pgsql.yml +++ b/.github/workflows/pgsql.yml @@ -1,7 +1,17 @@ name: PostGreSQL on: - workflow_dispatch: + push: + branches: + - "**" + paths-ignore: + - "art/**" + - "README.md" + pull_request: + types: [ready_for_review, synchronize, opened] + paths-ignore: + - "art/**" + - "README.md" jobs: build: diff --git a/src/Components/Filters/Builders/DatePicker.php b/src/Components/Filters/Builders/DatePicker.php index fb478d7c..dc29d934 100644 --- a/src/Components/Filters/Builders/DatePicker.php +++ b/src/Components/Filters/Builders/DatePicker.php @@ -14,6 +14,10 @@ public function builder(EloquentBuilder|QueryBuilder $builder, string $field, in return; } + if (!isset($values['start']) || isset($values['end'])) { + return; + } + /** @var array $values */ [$startDate, $endDate] = [ 0 => Carbon::parse($values['start'])->format('Y-m-d'), @@ -34,6 +38,10 @@ public function builder(EloquentBuilder|QueryBuilder $builder, string $field, in public function collection(Collection $collection, string $field, int|array|string|null $values): Collection { + if (!isset($values['start']) || isset($values['end'])) { + return $collection; + } + /** @var array $values */ [$startDate, $endDate] = [ 0 => Carbon::parse($values[0])->format('Y-m-d'), diff --git a/src/Components/Filters/Builders/DateTimePicker.php b/src/Components/Filters/Builders/DateTimePicker.php index 4bb35bb4..cf0de340 100644 --- a/src/Components/Filters/Builders/DateTimePicker.php +++ b/src/Components/Filters/Builders/DateTimePicker.php @@ -14,6 +14,10 @@ public function builder(EloquentBuilder|QueryBuilder $builder, string $field, in return; } + if (!isset($values['start']) || isset($values['end'])) { + return; + } + /** @var array $values */ [$startDate, $endDate] = [ 0 => Carbon::parse($values['start']), @@ -34,6 +38,10 @@ public function builder(EloquentBuilder|QueryBuilder $builder, string $field, in public function collection(Collection $collection, string $field, int|array|string|null $values): Collection { + if (!isset($values['start']) || isset($values['end'])) { + return $collection; + } + /** @var array $values */ [$startDate, $endDate] = [ 0 => Carbon::parse($values['start']), diff --git a/tests/Feature/Filters/FilterDateTimeTest.php b/tests/Feature/Filters/FilterDateTimeTest.php deleted file mode 100644 index defe45b2..00000000 --- a/tests/Feature/Filters/FilterDateTimeTest.php +++ /dev/null @@ -1,200 +0,0 @@ - [ - Filter::datepicker('produced_at'), - ], - ]) - ->call($params->theme) - ->set('filters', filterDate('produced_at', ['2021-02-02', '2021-04-04'])) - ->assertSee('Peixada da chef Nábia') - ->assertSee('Carne Louca') - ->assertSee('Bife à Rolê') - ->assertDontSeeHtml('Francesinha vegana'); -})->group('filters') - ->skipOnSQLite() - ->with('filter_datetime_themes_with_join', 'filter_datetime_query_builder'); - -it('properly filters by "between date"', function (string $component, object $params) { - $datepicker = Filter::datepicker('produced_at_formatted', 'produced_at'); - - livewire($component, [ - 'testFilters' => [$datepicker], - ]) - ->call($params->theme) - ->set('filters', filterDate('produced_at', ['2021-01-01', '2021-03-03'])) - ->assertSeeHtmlInOrder([ - 'id="input_produced_at_formatted"', - 'type="text"', - 'placeholder="Select a period"', - ]) - ->assertSee('Pastel de Nata') - ->assertSee('Peixada da chef Nábia') - ->assertSee('Carne Louca') - ->assertDontSee('Barco-Sushi Simples') - ->set('filters', filterDateTime('produced_at', ['2021-04-04', '2021-07-07 19:59:58'])) - ->assertSee('Bife à Rolê') - ->assertSee('Francesinha vegana') - ->assertSee('Francesinha') - ->assertDontSeeHtml('Barco-Sushi da Sueli'); -})->group('filters', 'filterDatePicker') - ->skipOnSQLite() - ->with('filter_datetime_themes_with_join', 'filter_datetime_query_builder'); - -it('properly filters by "between date" using incorrect filter', function (string $component, object $params) { - livewire($component) - ->call($params->theme) - ->set('testFilters', [ - Filter::datepicker('produced_at_formatted', 'produced_at'), - ]) - ->set('filters', filterDate('dishes.produced_at', ['2021-03-03', '2021-03-01'])) - ->assertSee('No records found'); -})->group('filters', 'filterDatePicker') - ->with('filter_datetime_themes_with_join', 'filter_datetime_query_builder'); - -it('properly filter the created_at field between two dates using collection & array table', function (string $component, string $theme) { - livewire($component, [ - 'testFilters' => [ - Filter::datepicker('produced_at'), - ], - ]) - ->call($theme) - ->set('filters', filterDateTime('created_at', ['2021-01-01 00:00:00', '2021-04-04 00:00:00'])) - ->assertSeeText('Name 1') - ->assertSeeText('Name 2') - ->assertSeeText('Name 3') - ->assertSeeText('Name 4') - ->assertDontSeeHtml('Name 5'); -})->group('filters') - ->with('filter_datetime_themes_collection', 'filter_datetime_themes_array'); - -$customCollection = new class () extends DishesCollectionTable { - public int $dishId; - - public function filters(): array - { - return [ - Filter::datepicker('created_at') - ->collection(function ($collection, $values) { - return $collection->where('id', 1); - }), - ]; - } -}; - -it('property filter the created_at field when we are using custom builder collection & array table', function (string $component, string $theme) { - $dateToFilter = ['2021-01-01 00:00:00', '2021-04-04 00:00:00']; - - livewire($component) - ->call($theme) - ->set('filters', filterDateTime('created_at', $dateToFilter)) - ->assertSeeText('Name 1') - ->assertDontSeeText('Name 2') - ->assertDontSeeText('Name 3') - ->assertDontSeeText('Name 4') - ->assertDontSeeText('Name 5'); -})->group('filters') - ->with([ - 'tailwind -> id' => [$customCollection::class, 'tailwind'], - 'bootstrap -> id' => [$customCollection::class, 'bootstrap'], - ]); - -it('properly filter the produced_at field between another two dates', function (string $component, object $params) { - livewire($component, [ - 'testFilters' => [ - Filter::datepicker('produced_at'), - ], - ]) - ->call($params->theme) - ->set('filters', filterDate('produced_at', ['2021-11-11 00:00:00', '2021-12-31 00:00:00'])) - ->assertDontSee('Peixada da chef Nábia') - ->assertDontSee('Carne Louca') - ->assertDontSee('Bife à Rolê'); -})->group('filters') - ->with('filter_datetime_themes_with_join'); - -$customBuilder = new class () extends DishesTable { - public int $dishId; - - public function filters(): array - { - return [ - Filter::datepicker('produced_at') - ->builder(function ($builder, $values) { - return $builder->where('dishes.id', 1); - }), - ]; - } -}; - -it('properly filter the produced_at field between another two dates - custom builder', function (string $component, string $theme) { - $dateToFilter = ['2021-11-11 00:00:00', '2021-12-31 00:00:00']; - livewire($component) - ->call($theme) - ->set('filters', filterDate('produced_at', $dateToFilter)) - ->assertSee('Pastel de Nata') - ->assertDontSee('Peixada da chef Nábia') - ->assertDontSee('Carne Louca') - ->assertDontSee('Bife à Rolê'); -})->group('filters') - ->with([ - 'tailwind -> id' => [$customBuilder::class, 'tailwind'], - 'bootstrap -> id' => [$customBuilder::class, 'bootstrap'], - ]); - -dataset('filter_datetime_themes_with_join', [ - 'tailwind -> id' => [DishesTable::class, (object) ['theme' => 'tailwind', 'field' => 'id']], - 'bootstrap -> id' => [DishesTable::class, (object) ['theme' => 'bootstrap', 'field' => 'id']], - 'tailwind -> dishes.id' => [DishesTableWithJoin::class, (object) ['theme' => 'tailwind', 'field' => 'dishes.id']], - 'bootstrap -> dishes.id' => [DishesTableWithJoin::class, (object) ['theme' => 'bootstrap', 'field' => 'dishes.id']], -]); - -dataset('filter_datetime_query_builder', [ - 'tailwind query builder -> id' => [DishesQueryBuilderTable::class, (object) ['theme' => 'tailwind', 'field' => 'id']], - 'bootstrap query builder -> id' => [DishesQueryBuilderTable::class, (object) ['theme' => 'bootstrap', 'field' => 'id']], -]); - -dataset('filter_datetime_themes_array', [ - [DishesArrayTable::class, 'tailwind'], - [DishesArrayTable::class, 'bootstrap'], -]); - -dataset('filter_datetime_themes_collection', [ - 'tailwind' => [DishesCollectionTable::class, 'tailwind'], - 'bootstrap' => [DishesCollectionTable::class, 'bootstrap'], -]); - -function filterDate(string $dataField, array $value): array -{ - return [ - 'date' => [ - $dataField => [ - 'start' => $value[0], - 'end' => $value[1], - ], - ], - ]; -} - -function filterDateTime(string $dataField, array $value): array -{ - return [ - 'datetime' => [ - $dataField => [ - 'start' => $value[0], - 'end' => $value[1], - ], - ], - ]; -}