Skip to content

Conversation

@josh-carter
Copy link

@josh-carter josh-carter commented Mar 8, 2018

Description

Take an example Product with SKU test_sku

In ProductRepository currently, if we're using get() and pass the SKU TesT_SKu we would get an undefined index as described inside #12073.

However It makes sense to be able to retrieve product using different cased SKU as the database is case insensitive and SKU is unique. But the PHP is case sensitive, therefore, to fix the problem we can lowercase the SKU in the PHP code.

Fixed Issues (if relevant)

  1. magento/magento2#12073: ProductRepository fails to load product with camelcase SKU

Manual testing scenarios

  1. Create Product with SKU test_sku
  2. Make a call to ProductRepository::get('tESt_sKu')
  3. Successfully retrieve created Product

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 on Travis CI are green)

- Then use lowercase sku in get() method which references the instances array via array key this way we make sure no matter which case is used to get the product we get it correctly

- This makes sense as the DB is case insensitive but the PHP is not and currently would throw undefined index if trying to get product with sku: test and passing tESt to get()
@orlangur
Copy link
Contributor

orlangur commented Mar 9, 2018

Hi @josh-carter, putting this PR on hold as hope to get fix done in scope of #12169 (slightly different approach: product is cached exactly with the same SKU how it was requested instead of strtolower call as it is done in this PR).

@josh-carter
Copy link
Author

@orlangur Ok cool, my thoughts would be, it saves multiple loading of the same product and keeps functionality correct.

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

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

Hi @josh-carter, let's continue in this PR :)

Please rework implementation of cacheProduct in cb3b5d1#diff-ff4893a5628d34910295bae32fbcddfdL241 so that code behavior remain unchanged.

Just change implementation of cacheProduct in such a way that we pass both productId and sku instead of obtaining them from product instance.

Another case is that MySQL does not distinguish not just lower/upper case but also е and ё, o and ö, a and ã and so on. Thus there is no need to complicate implementation with strtolower or other approaches for key normalization.

@josh-carter
Copy link
Author

@orlangur Ok that's cool with me. I'll get it updated and get this ready for review.

- Lets us pass the searched for SKU and use as instance key
- This is no longer called everytime we call the get() method because we pass the sku searched for when using the cacheProduct method()
- New line at end of ProductRepository
- Remove unused local variable in ProductRepositoryTest
* @return void
*/
private function cacheProduct($cacheKey, \Magento\Catalog\Api\Data\ProductInterface $product)
private function cacheProduct($cacheKey, \Magento\Catalog\Api\Data\ProductInterface $product, $sku = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

See: cb3b5d1#diff-ff4893a5628d34910295bae32fbcddfdL241

Please pass productId instead of product instance in both cases. Don't add default parameter and some logic into cacheProduct, pass both product ID and SKU.

$this->assertEquals($this->productMock, $this->model->get($sku, true));
}

public function testGetBySkuWithSpace()
Copy link
Contributor

Choose a reason for hiding this comment

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

This test does not seem to test initial case anymore. Please change so that it actually checks trimmed SKU.

$this->resourceModelMock->expects($this->once())->method('getIdBySku')->with($differentCasedSku)
->will($this->returnValue('test_id'));
$this->productMock->expects($this->once())->method('load')->with('test_id');
$this->productMock->expects($this->once())->method('getId')->willReturn('test_id');
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here: it looks like we don't check getting product by 'test_sku' anymore.

@ibecmattb
Copy link

Facing this same issue. How would I go about patching a Magento 2.2.3 install to fix this?

Copy link
Contributor

@ishakhsuvarov ishakhsuvarov left a comment

Choose a reason for hiding this comment

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

I would suggest covering this with different type of test, for instance api-functional.

@sidolov
Copy link
Contributor

sidolov commented Jun 18, 2018

@josh-carter , I am closing this PR now due to inactivity.
Please reopen and update if you wish to continue.
Thank you for collaboration!

@sidolov sidolov closed this Jun 18, 2018
@orlangur orlangur added this to the Release: 2.2.6 milestone Jun 18, 2018
@orlangur
Copy link
Contributor

Sorry @josh-carter, my bad, I was supposed to help you with test here, I remember we started a talk in Slack. Will do it this week.

@ishakhsuvarov
Copy link
Contributor

@orlangur I'm reopening then, to keep track of it

@ishakhsuvarov ishakhsuvarov reopened this Jun 19, 2018
@magento-cicd2
Copy link
Contributor

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.

Copy link
Contributor

@orlangur orlangur left a comment

Choose a reason for hiding this comment

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

Nowadays all changes must be applied to 2.3-develop first (see https://devdocs.magento.com/guides/v2.3/contributor-guide/contributing.html#rules). Please prepare a new pull request, this one should be on hold until the latter is merged.

@orlangur
Copy link
Contributor

Closing due to inactivity. Feel free to reach me out anytime later if you wish to continue work on this pull request and it will be reopened.
Thanks for collaboration!

@orlangur orlangur closed this Nov 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants