Skip to content

Issue#16425: Fixed "OR Condition in searchCriteria (REST API) doesn't work" #27588

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

Closed
wants to merge 13 commits into from

Conversation

andrewbess
Copy link
Contributor

@andrewbess andrewbess commented Apr 4, 2020

Description (*)

This PR fixes the problem with OR condition in searchCriteria.

Related Pull Requests

Fixed Issues (if relevant)

  1. fixes OR Condition in searchCriteria (REST API) dosn't work #16425: OR Condition in searchCriteria (REST API) dosn't work

Manual testing scenarios (*)

  1. Enable a REST API token
  2. Make a call to resource "/V1/products" with a search parameter like this:
    ?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=5&searchCriteria[filter_groups][0][filters][1][field]=category_id&searchCriteria[filter_groups][0][filters][1][value]=6

query-example

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)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Apr 4, 2020

Hi @andrewbess. Thank you for your contribution
Here is some useful tips 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

For more details, please, review the Magento Contributor Guide documentation.

@magento-engcom-team magento-engcom-team added Component: Eav Release Line: 2.4 Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner Event: Global-Contribution-Day labels Apr 4, 2020
@dmytro-ch dmytro-ch self-assigned this Apr 4, 2020
Copy link
Contributor

@dmytro-ch dmytro-ch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @andrewbess, thank you for your contribution!

Could you please check the minor CR recommendations and fix the failed tests?
Please note, the new changes should be covered by an appropriate autotest. The Web API Functional is preferable in this particular case.

Thank you!

$customFilter = $this->getCustomFilterForField($filter->getField());
if ($customFilter) {
$isApplied = $customFilter->apply($filter, $collection);
if ($filter->getConditionType() == 'eq') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use strict comparison whenever it's possible.

Suggested change
if ($filter->getConditionType() == 'eq') {
if ($filter->getConditionType() === 'eq') {


class FilterProcessorTest extends \PHPUnit\Framework\TestCase
class FilterProcessorTest extends TestCase
{
/**
* Return model
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the private method getModel to the bottom of the class. The private and protected methods should not be followed by public ones.

@@ -31,8 +35,11 @@ private function getModel(array $customFilters, array $fieldMapping)
*/
public function testProcess()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function testProcess()
public function testProcess(): void

@@ -144,25 +151,24 @@ public function testProcess()
$model->process($searchCriteriaMock, $collectionMock);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testProcessWithException()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function testProcessWithException()
public function testProcessWithException(): void

public function testProcessWithException()
{
/** @var \stdClass|\PHPUnit_Framework_MockObject_MockObject $customFilterMock */
$customFilterMock = $this->createPartialMock(\stdClass::class, ['apply']);
$this->expectException(InvalidArgumentException::class);
Copy link
Contributor

@dmytro-ch dmytro-ch Apr 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the definition for the assertion of an exception right before the original methods call.
E.g.

$this->expectException(InvalidArgumentException::class);
$model->process($searchCriteriaMock, $collectionMock);

Otherwise, some real unexpected exception can be interpreted incorrectly.

@@ -5,14 +5,18 @@
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
*/
declare(strict_types=1);

@andrewbess
Copy link
Contributor Author

Hello @dmytro-ch
Thank you for your remarks.
I fixed it according to your suggestions.
Please recheck.
Thank you in advance.

@dmytro-ch
Copy link
Contributor

@magento run all tests

@dmytro-ch
Copy link
Contributor

@andrewbess, thank you for the updates!
Could you please check and fix the failed Web API and Integration tests?

Thank you!

@VladimirZaets
Copy link
Contributor

Hi @andrewbess, did you have to change a look?

@dmytro-ch
Copy link
Contributor

Hello @andrewbess, I'm closing this PR now due to inactivity.
Feel free to reopen and update if you wish to continue.
Thank you!

@dmytro-ch dmytro-ch closed this Jun 10, 2020
@m2-assistant
Copy link

m2-assistant bot commented Jun 10, 2020

Hi @andrewbess, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

@rogyar rogyar reopened this Aug 12, 2020
@m2-assistant
Copy link

m2-assistant bot commented Aug 12, 2020

Hi @andrewbess. Thank you for your contribution
Here is some useful tips 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

You can find more information about the builds here

ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.

For more details, please, review the Magento Contributor Guide documentation.

@engcom-Delta
Copy link
Contributor

✔️ QA passed
Manual testing scenario:

  • Create some categories
  • Create products for each category:
    • Product1 -> Category1
    • Product2 -> Category2
    • Product3 -> Category3
    • Product4 -> Category4
      image
  • Enable a REST API token
  • Make a call to resource "/V1/products" with a search parameter like this:
    ?searchCriteria[filter_groups][0][filters][0][field]=category_id&searchCriteria[filter_groups][0][filters][0][value]=4&searchCriteria[filter_groups][0][filters][1][field]=category_id&searchCriteria[filter_groups][0][filters][1][value]=6
    image
    Result:
    ✔️ Products Product2 and Product4 are returned
{
    "items": [
        {
            "id": 2,
            "sku": "Product2",
            "name": "Product2",
            "attribute_set_id": 4,
            "price": 150,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "created_at": "2020-11-02 11:22:01",
            "updated_at": "2020-11-02 11:22:01",
            "weight": 1,
            "extension_attributes": {
                "website_ids": [
                    1
                ],
                "category_links": [
                    {
                        "position": 0,
                        "category_id": "4"
                    }
                ]
            },
            "product_links": [],
            "options": [],
            "media_gallery_entries": [],
            "tier_prices": [],
            "custom_attributes": [
                {
                    "attribute_code": "options_container",
                    "value": "container2"
                },
                {
                    "attribute_code": "url_key",
                    "value": "product2"
                },
                {
                    "attribute_code": "msrp_display_actual_price_type",
                    "value": "0"
                },
                {
                    "attribute_code": "gift_message_available",
                    "value": "2"
                },
                {
                    "attribute_code": "required_options",
                    "value": "0"
                },
                {
                    "attribute_code": "has_options",
                    "value": "0"
                },
                {
                    "attribute_code": "meta_title",
                    "value": "Product2"
                },
                {
                    "attribute_code": "meta_keyword",
                    "value": "Product2"
                },
                {
                    "attribute_code": "meta_description",
                    "value": "Product2 "
                },
                {
                    "attribute_code": "tax_class_id",
                    "value": "2"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "4"
                    ]
                }
            ]
        },
        {
            "id": 4,
            "sku": "Product4",
            "name": "Product4",
            "attribute_set_id": 4,
            "price": 250,
            "status": 1,
            "visibility": 4,
            "type_id": "simple",
            "created_at": "2020-11-02 11:23:18",
            "updated_at": "2020-11-02 11:23:18",
            "weight": 1,
            "extension_attributes": {
                "website_ids": [
                    1
                ],
                "category_links": [
                    {
                        "position": 0,
                        "category_id": "6"
                    }
                ]
            },
            "product_links": [],
            "options": [],
            "media_gallery_entries": [],
            "tier_prices": [],
            "custom_attributes": [
                {
                    "attribute_code": "options_container",
                    "value": "container2"
                },
                {
                    "attribute_code": "url_key",
                    "value": "product4"
                },
                {
                    "attribute_code": "msrp_display_actual_price_type",
                    "value": "0"
                },
                {
                    "attribute_code": "gift_message_available",
                    "value": "2"
                },
                {
                    "attribute_code": "required_options",
                    "value": "0"
                },
                {
                    "attribute_code": "has_options",
                    "value": "0"
                },
                {
                    "attribute_code": "meta_title",
                    "value": "Product4"
                },
                {
                    "attribute_code": "meta_keyword",
                    "value": "Product4"
                },
                {
                    "attribute_code": "meta_description",
                    "value": "Product4 "
                },
                {
                    "attribute_code": "tax_class_id",
                    "value": "2"
                },
                {
                    "attribute_code": "category_ids",
                    "value": [
                        "6"
                    ]
                }
            ]
        }
    ],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "category_id",
                        "value": "4",
                        "condition_type": "eq"
                    },
                    {
                        "field": "category_id",
                        "value": "6",
                        "condition_type": "eq"
                    }
                ]
            }
        ]
    },
    "total_count": 2
}

@engcom-Alfa engcom-Alfa added the QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope label Nov 2, 2020
@engcom-Delta
Copy link
Contributor

Note: Automation tests are passed

@engcom-Delta
Copy link
Contributor

Need to check additional conditions

@gabrieldagama
Copy link
Contributor

@magento run all tests

Copy link
Contributor

@engcom-Delta engcom-Delta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @andrewbess I have tested additional condition and there is issue if requests are with multiple filters and different condition_type we get empty response
Case 1:

  • Create some categories
  • Create products for each category:
    • Product1 -> Category1
    • Product2 -> Category2
    • Product3 -> Category3
    • Product4 -> Category4
      image
  • Enable a REST API token
  • Make a call to resource "/V1/products" with a search parameter:
    ?searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=4&searchCriteria[filterGroups][0][filters][0][conditionType]=gt&searchCriteria[filterGroups][0][filters][1][field]=category_id&searchCriteria[filterGroups][0][filters][1][value]=3&searchCriteria[filterGroups][0][filters][1][conditionType]=eq
    Result:
    Empty response
    Note: Expecting to see products from categories with ID's 5 and 6 and 3 (products Product1, Product3, Product4)
{
    "items": [],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "category_id",
                        "value": "4",
                        "condition_type": "gt"
                    },
                    {
                        "field": "category_id",
                        "value": "3",
                        "condition_type": "eq"
                    }
                ]
            }
        ]
    },
    "total_count": 0
}

Case 2:

  • Create some categories
  • Create products for each category:
    • Product1 -> Category1
    • Product2 -> Category2
    • Product3 -> Category3
    • Product4 -> Category4
      image
  • Enable a REST API token
  • Make a call to resource "/V1/products" with a search parameter:
    ?searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=4&searchCriteria[filterGroups][0][filters][0][conditionType]=gt&searchCriteria[filterGroups][0][filters][1][field]=name&searchCriteria[filterGroups][0][filters][1][value]=Product1&searchCriteria[filterGroups][0][filters][1][conditionType]=eq
    Result:
    Empty response
    Note: Expecting to see products from categories with ID's 5 and 6(products Product3, Product4) and product Product1
{
    "items": [],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "category_id",
                        "value": "4",
                        "condition_type": "gt"
                    },
                    {
                        "field": "name",
                        "value": "Product1",
                        "condition_type": "eq"
                    }
                ]
            }
        ]
    },
    "total_count": 0
}

Case 3 according comment:
Filter by category_id = ? or store_id = ? return no product in response

  • Create some categories
  • Create products for each category:
    • Product1 -> Category1
    • Product2 -> Category2
    • Product3 -> Category3
    • Product4 -> Category4
      image
  • Create additional website
    image
  • Create product ProductForStoreId and assign it only to additional website
  • Enable a REST API token
  • Make a call to resource "/V1/products" with a search parameter:
    ?searchCriteria[filterGroups][0][filters][0][field]=category_id&searchCriteria[filterGroups][0][filters][0][value]=4&searchCriteria[filterGroups][0][filters][0][conditionType]=gt&searchCriteria[filterGroups][0][filters][1][field]=name&searchCriteria[filterGroups][0][filters][1][value]=Product1&searchCriteria[filterGroups][0][filters][1][conditionType]=eq
    Result:
    Empty response
    Note: Expecting to see product from category with ID 4(product Product2) and product ProductForStoreId
{
    "items": [],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "category_id",
                        "value": "4",
                        "condition_type": "eq"
                    },
                    {
                        "field": "store_id",
                        "value": "3",
                        "condition_type": "eq"
                    }
                ]
            }
        ]
    },
    "total_count": 0
}

Could you take a look?

@andrewbess
Copy link
Contributor Author

Hello @engcom-Delta
Thank you for your detalization.
I will check it this week.

@m2-community-project m2-community-project bot added Priority: P2 A defect with this priority could have functionality issues which are not to expectations. labels Nov 18, 2020
@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Jan 12, 2021

Hi @gabrieldagama could you provide some info on why this PR was moved to 'on hold' status?

@gabrieldagama
Copy link
Contributor

Hi @ihor-sviziev, I've tried to start a conversation internally to help to find a solution for this but was deffered due to other priorities at the moment. We can try to discuss this problem and find a solution with the community. Thanks!

@andrewbess andrewbess closed this May 14, 2022
@andrewbess andrewbess deleted the issue-16425 branch May 14, 2022 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Award: bug fix Award: test coverage Component: Eav Event: Global-Contribution-Day Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Priority: P2 A defect with this priority could have functionality issues which are not to expectations. QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope Release Line: 2.4 Severity: S1 Affects critical data or functionality and forces users to employ a workaround.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OR Condition in searchCriteria (REST API) dosn't work