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 for issue 19983 Can't upload customer Image attribute programmatically #19988

Merged
merged 7 commits into from
Feb 25, 2019
Merged

Conversation

Nazar65
Copy link
Member

@Nazar65 Nazar65 commented Dec 27, 2018

Description (*)

Could not load image for customer attribute, error: Base64 is not defined "

Fixed Issues (if relevant)

  1. Can't work customer Image attribute programmatically #19983: Could not load image for customer attribute,

Manual testing scenarios (*)

1-Create customer attribute type file ->

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'sample_attribute2',
            [
                'type'         => 'text',
                'label'        => 'Sample Attribute',
                'input'        => 'file',
                'required'     => false,
                'visible'      => true,
                'user_defined' => false,
                'position'     => 999,
                'system'       => 0,
            ]
        );
        $sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');

        // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
        $sampleAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer','customer_register_address','customer_address_edit','customer_account_edit']

        );
        $sampleAttribute->save();

2. try to upload image on adminhtml customer edit.

Expected Result

No errors in Console, Image uploaded

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)

@magento-engcom-team
Copy link
Contributor

Hi @Nazar65. 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-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.3-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

@orlangur orlangur self-assigned this Dec 27, 2018
@@ -146,7 +146,7 @@ protected function _validateByRules($value)
return $this->_fileValidator->getMessages();
}

if (!empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
if (empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this ever evaluate to true?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes on event where empty value of ['tmp_name'] and file not uploaded

Copy link
Member

Choose a reason for hiding this comment

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

The condition looks correct to me. If you really need a check for empty($value['tmp_name']) - make it a separate condition

Copy link
Member

Choose a reason for hiding this comment

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

However, I'd leave php side as it is

@@ -16,7 +16,8 @@ define([
'Magento_Ui/js/form/element/abstract',
'mage/backend/notification',
'mage/translate',
'jquery/file-uploader'
'jquery/file-uploader',
'mage/adminhtml/tools'
Copy link
Contributor

Choose a reason for hiding this comment

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

Why need tools?

Copy link
Member Author

@Nazar65 Nazar65 Dec 27, 2018

Choose a reason for hiding this comment

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

"Base64 is not defined" when try upload image -> caused by line 173 ->

 if (!file.id && file.name) {
                file.id = Base64.mageEncode(file.name);
            }

Copy link
Member Author

Choose a reason for hiding this comment

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

This is core issue for -> #18688

Copy link
Member

Choose a reason for hiding this comment

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

That's interesting, there are more js files using Base64 without dependency on mage/adminhtml/tools - probably those files require to be fixed too

@Nazar65
Copy link
Member Author

Nazar65 commented Dec 27, 2018

Hi @orlangur I was leave a comment

@Nazar65 Nazar65 mentioned this pull request Dec 28, 2018
4 tasks
@Nazar65
Copy link
Member Author

Nazar65 commented Jan 8, 2019

@orlangur any updates ?

@sivaschenko
Copy link
Member

@magento-engcom-team give me test instance

@magento-engcom-team
Copy link
Contributor

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

@magento-engcom-team
Copy link
Contributor

Hi @sivaschenko, here is your new Magento instance.
Admin access: https://pr-19988.instances.magento-community.engineering/admin
Login: admin Password: 123123q

@@ -146,7 +146,7 @@ protected function _validateByRules($value)
return $this->_fileValidator->getMessages();
}

if (!empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
if (empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
Copy link
Member

Choose a reason for hiding this comment

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

The condition looks correct to me. If you really need a check for empty($value['tmp_name']) - make it a separate condition

@@ -146,7 +146,7 @@ protected function _validateByRules($value)
return $this->_fileValidator->getMessages();
}

if (!empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
if (empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
Copy link
Member

Choose a reason for hiding this comment

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

However, I'd leave php side as it is

@@ -16,7 +16,8 @@ define([
'Magento_Ui/js/form/element/abstract',
'mage/backend/notification',
'mage/translate',
'jquery/file-uploader'
'jquery/file-uploader',
'mage/adminhtml/tools'
Copy link
Member

Choose a reason for hiding this comment

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

That's interesting, there are more js files using Base64 without dependency on mage/adminhtml/tools - probably those files require to be fixed too

@sivaschenko sivaschenko self-assigned this Jan 16, 2019
@sivaschenko
Copy link
Member

Hi @Nazar65 thanks for the contribution please see my code review notes

@Nazar65
Copy link
Member Author

Nazar65 commented Jan 17, 2019

@sivaschenko Well, I will provide a better fix for this problem, thank you

@Nazar65
Copy link
Member Author

Nazar65 commented Jan 17, 2019

@sivaschenko Base64 defined by adminhtml tools please loook ->

// also depends on a mage/adminhtml/tools.js for Base64 encoding

There is no difference, all other files are using mage/adminhtml

@sivaschenko
Copy link
Member

@Nazar65 you are completely right about Base64, I just mentioned that it appears there may be more bugs in Magento because of Base64 usage without dependency on mage/adminhtml/tools in several other js files

@Nazar65
Copy link
Member Author

Nazar65 commented Jan 17, 2019

@sivaschenko i'm added adminhtml/tools where is used but not defined, what about php part ? for me this is the good condition on "if" statement.

Copy link
Member

@sivaschenko sivaschenko left a comment

Choose a reason for hiding this comment

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

Hi @Nazar65 thanks for your updates, please see my comment with the suggestion regarding the php condition

app/code/Magento/Eav/Model/Attribute/Data/File.php Outdated Show resolved Hide resolved
Co-Authored-By: Nazar65 <nazarn96@gmail.com>
@Nazar65
Copy link
Member Author

Nazar65 commented Jan 18, 2019

@sivaschenko I'm found the core problem of issue, the function is_uploaded_file return false because -> is_uploaded_file — Tells whether the file was uploaded via HTTP POST But the variable $_FILES['tmp_name'] on validate are empty, because file already downloaded, so i was suggest change this to !file_exists($value['tmp_name'])

@sivaschenko
Copy link
Member

@Nazar65 I am not sure there is a difference between file_exists and is_uploaded_file in case when $value['tmp_name']. is_uploaded_file should work fine in case when tmp_name is set, so I would keep is_uploaded_file

@Nazar65
Copy link
Member Author

Nazar65 commented Jan 18, 2019

@sivaschenko yes, but function is_uploaded_file return false but file exist. this function check if file uploaded via HTTP POST action returns TRUE if the file named by filename was uploaded via HTTP POST but not checks if file exist.

@Nazar65
Copy link
Member Author

Nazar65 commented Jan 18, 2019

@sivaschenko
is_uploaded
selection_276

file_exists
selection_277

@Nazar65
Copy link
Member Author

Nazar65 commented Jan 18, 2019

@sivaschenko so basically to fix this issue we need just add the following condition

if (!empty($value['tmp_name']) && !file_exists($value['tmp_name'])) {
            return [__('"%1" is not a valid file.', $label)];
        }

@Nazar65
Copy link
Member Author

Nazar65 commented Feb 1, 2019

@orlangur or @sivaschenko any update on this ?

@Zyles
Copy link

Zyles commented Feb 6, 2019

What is the fix? I can't do any development because I can't change themes.

@sivaschenko
Copy link
Member

Sorry for delay @Nazar65 the pull request is approved and will be further processed internally

@magento-engcom-team
Copy link
Contributor

Hi @sivaschenko, thank you for the review.
ENGCOM-4152 has been created to process this Pull Request

@ghost
Copy link

ghost commented Feb 25, 2019

Hi @Nazar65, 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.

@sivaschenko
Copy link
Member

@magento-engcom-team give me test instance

@magento-engcom-team
Copy link
Contributor

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

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