Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Issue Make Laminas Filters callable, and compatible with laminas-filters >=3.5.0 #38610

Conversation

Bashev
Copy link
Contributor

@Bashev Bashev commented Apr 10, 2024

Affected versions: Magento >=2.4.6-p4, 2.4.7

With 2.4.6-p4 laminas/laminas-filters was updated to version 3.5.1, but there is introduced additional validation of the filter classes, which checks is it callable. https://github.com/laminas/laminas-filter/blob/f98ddcfe5b145d84ccfe3cddd95ba60d09231843/src/FilterChain.php#L90

Because of that filtering from magento not works (skipped).

This PR add AbstractFilter (from laminas) where magic __invoke method is implemented.

This also affects all other date fields (like special price from/to, news from/to date) and etc, but not only...

Description (*)

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes Invalid Date when account Local is not en-US #38596
  2. Fixes Sales order report not being displayed, error with date formatting in UI #38589
  3. Fixes Sales Reports are not generated #38590

Manual testing scenarios (*)

  1. Change admin language to French / Bulgarian or some which have different date format then default English.
    A.1 Go to Reports - Sales - Orders (for example)
    A.2 Try to filter some orders select some random dates.
    A.3 Generate report.

B.1 Go to Catalog - Products
B.2 Create new product or edit existing product
B.3 Add "News from date" and/or "news to date" with random dates.
B.4 Save

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Copy link

m2-assistant bot commented Apr 10, 2024

Hi @Bashev. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.

Add the comment under your pull request to deploy test or vanilla Magento instance:
  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@Bashev
Copy link
Contributor Author

Bashev commented Apr 10, 2024

@magento run all tests

@Bashev
Copy link
Contributor Author

Bashev commented Apr 10, 2024

@magento give me test instance

Copy link

Hi @Bashev. Thank you for your request. I'm working on Magento instance for you.

@Bashev Bashev changed the title [FIX] Issue Make Laminas Filters callable, and compatible with laminas/laminas-fi… [FIX] Issue Make Laminas Filters callable, and compatible with laminas-filters >=3.5.0 Apr 10, 2024
Copy link

@Bashev
Copy link
Contributor Author

Bashev commented Apr 10, 2024

@magento run all tests

@hostep
Copy link
Contributor

hostep commented Apr 10, 2024

Shouldn't we fix this in laminas-filter itself? In my opinion the change introduced in https://github.com/laminas/laminas-filter/pull/124/files#diff-3a6c9663e22963eb296b993e0b7cc8335c9f7f02f8364fc66385ed7175b5639dL89 is not done in a backwards compatible way and should be considered a new bug that needs to be fixed.
Because they later check again for the is_callable and if it's not the case, actually do something instead of not doing anything.

What do you think @Bashev?

/cc @gsteel, @Ocramius

@Ocramius
Copy link

Can we have an upstream test case that highlights the BC boundary, please?

If the test passes before and fails afterwards, we can act and prevent a regression too, but it meeds to be done with some structure.

@hostep
Copy link
Contributor

hostep commented Apr 10, 2024

@Ocramius: I lack the time to send in a proper PR at the moment, but after some quick testing locally, here's a way to demonstrate the problem, apply these changes and run phpunit and you'll see it fail:

diff --git a/test/FilterChainTest.php b/test/FilterChainTest.php
index 33b3df7f..8d7fc58b 100644
--- a/test/FilterChainTest.php
+++ b/test/FilterChainTest.php
@@ -6,6 +6,7 @@ namespace LaminasTest\Filter;

 use ArrayIterator;
 use Laminas\Filter\FilterChain;
+use Laminas\Filter\FilterInterface;
 use Laminas\Filter\PregReplace;
 use Laminas\Filter\StringToLower;
 use Laminas\Filter\StringTrim;
@@ -116,7 +117,7 @@ class FilterChainTest extends TestCase
     {
         return [
             'callbacks' => [
-                ['callback' => [self::class, 'staticUcaseFilter']],
+                ['callback' => new TestFilter()],
                 [
                     'priority' => 10000,
                     'callback' => static fn(string $value): string => trim($value),
@@ -250,3 +251,11 @@ class FilterChainTest extends TestCase
         ], $filters);
     }
 }
+
+class TestFilter implements Filterinterface
+{
+    public function filter($value)
+    {
+        return strtoupper($value);
+    }
+}

Following change fixes the bug, but not sure if this is the best way:

diff --git a/src/FilterChain.php b/src/FilterChain.php
index 4c092ed2..f776a4d5 100644
--- a/src/FilterChain.php
+++ b/src/FilterChain.php
@@ -87,7 +87,7 @@ class FilterChain extends AbstractFilter implements Countable, IteratorAggregate
                     foreach ($value as $spec) {
                         $callback = $spec['callback'] ?? false;
                         $priority = $spec['priority'] ?? static::DEFAULT_PRIORITY;
-                        if (is_callable($callback)) {
+                        if (is_callable($callback) || $callback instanceof FilterInterface) {
                             $this->attach($callback, $priority);
                         }
                     }
@@ -172,12 +172,6 @@ class FilterChain extends AbstractFilter implements Countable, IteratorAggregate
     public function attach($callback, $priority = self::DEFAULT_PRIORITY)
     {
         if (! is_callable($callback)) {
-            if (! $callback instanceof FilterInterface) {
-                throw new Exception\InvalidArgumentException(sprintf(
-                    'Expected a valid PHP callback; received "%s"',
-                    get_debug_type($callback)
-                ));
-            }
             $callback = [$callback, 'filter'];
         }
         $this->filters->insert($callback, $priority);

@Bashev
Copy link
Contributor Author

Bashev commented Apr 11, 2024

@hostep it's make sense if can be fixed on laminas side, because this will help to most of the projects which using laminas-filter.

My approach is different - if i must wait on additional vendor, to find a way to fix it on my side, but in this case, we must wait magento for fix and laminas for fix ...

@Ocramius
Copy link

Fyi, if there's a patch+fix on our end it takes few seconds to tag a release, so please send a PR 😁

@hostep
Copy link
Contributor

hostep commented Apr 11, 2024

Here you go: laminas/laminas-filter#140

Let's move the discussion over there.

@Bashev
Copy link
Contributor Author

Bashev commented Apr 11, 2024

Issue was fixed in laminas-filter and this PR is not required anymore.

To fix it in current magento installations, just run composer update laminas/laminas-filter

@Bashev Bashev closed this Apr 11, 2024
@Bashev Bashev deleted the 38596-compatibility-with-laminas-filter-3.5.x branch April 11, 2024 08:57
@kanevbg
Copy link

kanevbg commented Apr 13, 2024

@Bashev Колега, много често те мяркам тук напоследък, евала за свършената работа! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants