Skip to content

Commit

Permalink
Merge pull request #3922 from magento-trigger/MC-5784
Browse files Browse the repository at this point in the history
- Fixed MC-5784: Image fields using imageUploader UIComponent cannot use gallery image
  • Loading branch information
irenelagno authored Mar 21, 2019
2 parents 77297a1 + 094dc81 commit 497e94a
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 17 deletions.
76 changes: 59 additions & 17 deletions app/code/Magento/Theme/Model/Design/Backend/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Magento\Theme\Model\Design\Config\FileUploader\FileProcessor;

/**
* File Backend
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class File extends BackendFile
Expand Down Expand Up @@ -88,36 +90,29 @@ public function beforeSave()
{
$values = $this->getValue();
$value = reset($values) ?: [];
if (!isset($value['file'])) {

// Need to check name when it is uploaded in the media gallary
$file = $value['file'] ?? $value['name'] ?? null;
if (!isset($file)) {
throw new LocalizedException(
__('%1 does not contain field \'file\'', $this->getData('field_config/field'))
);
}
if (isset($value['exists'])) {
$this->setValue($value['file']);
$this->setValue($file);
return $this;
}

$filename = basename($value['file']);
$result = $this->_mediaDirectory->copyFile(
$this->getTmpMediaPath($filename),
$this->_getUploadDir() . '/' . $filename
);
if ($result) {
$this->_mediaDirectory->delete($this->getTmpMediaPath($filename));
if ($this->_addWhetherScopeInfo()) {
$filename = $this->_prependScopeInfo($filename);
}
$this->setValue($filename);
} else {
$this->unsValue();
}
$this->updateMediaDirectory(basename($file), $value['url']);

return $this;
}

/**
* @return array
* After Load
*
* @return File
* @throws LocalizedException
*/
public function afterLoad()
{
Expand Down Expand Up @@ -166,6 +161,8 @@ protected function getUploadDirPath($uploadDir)
}

/**
* Get Value
*
* @return array
*/
public function getValue()
Expand Down Expand Up @@ -231,4 +228,49 @@ private function getMime()
}
return $this->mime;
}

/**
* Get Relative Media Path
*
* @param string $path
* @return string
*/
private function getRelativeMediaPath(string $path): string
{
return str_replace('/pub/media/', '', $path);
}

/**
* Move file to the correct media directory
*
* @param string $filename
* @param string $url
* @throws LocalizedException
*/
private function updateMediaDirectory(string $filename, string $url)
{
$relativeMediaPath = $this->getRelativeMediaPath($url);
$tmpMediaPath = $this->getTmpMediaPath($filename);
$mediaPath = $this->_mediaDirectory->isFile($relativeMediaPath) ? $relativeMediaPath : $tmpMediaPath;
$destinationMediaPath = $this->_getUploadDir() . '/' . $filename;

$result = $mediaPath === $destinationMediaPath;
if (!$result) {
$result = $this->_mediaDirectory->copyFile(
$mediaPath,
$destinationMediaPath
);
}
if ($result) {
if ($mediaPath === $tmpMediaPath) {
$this->_mediaDirectory->delete($mediaPath);
}
if ($this->_addWhetherScopeInfo()) {
$filename = $this->_prependScopeInfo($filename);
}
$this->setValue($filename);
} else {
$this->unsValue();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="NavigateToFaviconMediaFolderActionGroup">
<arguments>
<argument name="StoreFolder" type="string"/>
</arguments>
<conditionalClick selector="{{MediaGallerySection.StorageRootArrow}}" dependentSelector="{{MediaGallerySection.checkIfArrowExpand}}" stepKey="clickArrowIfClosed" visible="true"/>
<waitForElement selector="{{AdminDesignConfigSection.faviconArrow}}" stepKey="waitForFaviconFolder"/>
<conditionalClick selector="{{AdminDesignConfigSection.faviconArrow}}" dependentSelector="{{AdminDesignConfigSection.checkIfFaviconArrowExpand}}" stepKey="clickFaviconArrowIfClosed" visible="true"/>
<waitForElement selector="{{AdminDesignConfigSection.storesArrow}}" stepKey="waitForStoresFolder"/>
<conditionalClick selector="{{AdminDesignConfigSection.storesArrow}}" dependentSelector="{{AdminDesignConfigSection.checkIfStoresArrowExpand}}" stepKey="clickStoresArrowIfClosed" visible="true"/>
<waitForElement selector="{{StoreFolder}}" stepKey="waitForStoreFolder"/>
<click selector="{{StoreFolder}}" stepKey="clickOnCreatedFolder"/>
<waitForLoadingMaskToDisappear stepKey="waitForLoading"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@
<element name="watermarkSection" type="text" selector="[data-index='watermark'] .admin__fieldset-wrapper-content"/>
<element name="imageUploadInputByFieldsetName" type="input" selector="//*[contains(@class,'fieldset-wrapper')][child::*[contains(@class,'fieldset-wrapper-title')]//*[contains(text(),'{{arg1}}')]]//*[contains(@class,'file-uploader')]//input" parameterized="true"/>
<element name="imageUploadPreviewByFieldsetName" type="input" selector="//*[contains(@class,'fieldset-wrapper')][child::*[contains(@class,'fieldset-wrapper-title')]//*[contains(text(),'{{arg1}}')]]//*[contains(@class,'file-uploader-preview')]//img" parameterized="true"/>
<element name="addSelectedFromMediaGallery" type="input" selector="//button[contains(@title,'Add Selected')]"/>
<element name="htmlHeaderSection" type="text" selector="[data-index='head']"/>
<element name="selectFromGalleryByFieldsetName" type="input" selector="//*[contains(@class,'fieldset-wrapper')][child::*[contains(@class,'fieldset-wrapper-title')]//*[contains(text(),'{{arg1}}')]]//*[contains(@class,'file-uploader')]//label[contains(text(), 'Select from Gallery')]" parameterized="true"/>
<element name="imageUploadFromMediaGallery" type="input" selector="//input[contains(@class,'fileupload')]" />
<element name="saveConfiguration" type="input" selector="//button[contains(@title, 'Save Configuration')]" />
<element name="successNotification" type="text" selector="//div[contains(@data-ui-id, 'messages-message-success')]" />
<element name="useDefaultByFieldsetName" type="input" selector="//*[contains(@class,'fieldset-wrapper')][child::*[contains(@class,'fieldset-wrapper-title')]//*[contains(text(),'{{arg1}}')]]//*[contains(@class,'file-uploader')]//span[contains(text(), 'Use Default Value')]" parameterized="true" />
<element name="logoSectionHeader" type="text" selector="[data-index='email']"/>
<element name="logoSection" type="text" selector="[data-index='email'] .admin__fieldset-wrapper-content"/>
<element name="logoUpload" type ="input" selector="[name='email_logo']" />
<element name="logoWrapperOpen" type ="text" selector="[data-index='email'] [data-state-collapsible ='closed']"/>
<element name="logoPreview" type ="text" selector="[alt ='magento-logo.png']"/>
<element name="faviconArrow" type="button" selector="#ZmF2aWNvbg-- > .jstree-icon" />
<element name="checkIfFaviconArrowExpand" type="button" selector="//li[@id='ZmF2aWNvbg--' and contains(@class,'jstree-closed')]" />
<element name="storesArrow" type="button" selector="#ZmF2aWNvbi9zdG9yZXM- > .jstree-icon" />
<element name="checkIfStoresArrowExpand" type="button" selector="//li[@id='ZmF2aWNvbi9zdG9yZXM-' and contains(@class,'jstree-closed')]" />
<element name="storeLink" type="button" selector="#ZmF2aWNvbi9zdG9yZXMvMQ-- > a"/>
</section>
</sections>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="AdminDesignConfigMediaGalleryImageUploadTest">
<annotations>
<features value="Content"/>
<stories value="Content"/>
<title value="MC-5784: Image fields using imageUploader UIComponent cannot use gallery image"/>
<description value="Admin should be able to use Image Uploader to add Gallery Images"/>
<severity value="MAJOR"/>
<testCaseId value="MC-13832"/>
<group value="Content"/>
</annotations>
<before>
<actionGroup ref="LoginAsAdmin" stepKey="loginToAdminArea"/>
</before>
<after>
<actionGroup ref="logout" stepKey="logoutOfAdmin"/>
</after>
<!--Edit Store View-->
<comment userInput="Edit Store View" stepKey="editStoreViewComment"/>
<amOnPage url="{{DesignConfigPage.url}}" stepKey="navigateToDesignConfigPage" />
<waitForPageLoad stepKey="waitForPageload1"/>
<click selector="{{AdminDesignConfigSection.scopeRow('3')}}" stepKey="editStoreView"/>
<waitForPageLoad stepKey="waitForPageload2"/>
<scrollTo selector="{{AdminDesignConfigSection.htmlHeaderSection}}" stepKey="scrollToHtmlHeadSection"/>
<click selector="{{AdminDesignConfigSection.htmlHeaderSection}}" stepKey="openHtmlHeadSection"/>
<!--Upload Image-->
<comment userInput="Upload Image" stepKey="uploadImageComment"/>
<click selector="{{AdminDesignConfigSection.selectFromGalleryByFieldsetName('Head')}}" stepKey="openMediaGallery"/>
<actionGroup ref="VerifyMediaGalleryStorageActions" stepKey="verifyMediaGalleryStorageBtn"/>
<actionGroup ref="NavigateToMediaFolderActionGroup" stepKey="navigateToFolder">
<argument name="FolderName" value="Storage Root"/>
</actionGroup>
<actionGroup ref="attachImage" stepKey="selectImageFromMediaStorage">
<argument name="Image" value="ImageUpload3"/>
</actionGroup>
<actionGroup ref="saveImage" stepKey="insertImage"/>
<click selector="{{AdminDesignConfigSection.saveConfiguration}}" stepKey="saveConfiguration"/>
<waitForElementVisible selector="{{AdminDesignConfigSection.successNotification}}" stepKey="waitForSuccessNotification"/>
<waitForPageLoad stepKey="waitForPageloadSuccess"/>
<!--Edit Store View-->
<comment userInput="Edit Store View" stepKey="editStoreViewComment2"/>
<click selector="{{AdminDesignConfigSection.scopeRow('3')}}" stepKey="editStoreView2"/>
<waitForPageLoad stepKey="waitForPageload3"/>
<scrollTo selector="{{AdminDesignConfigSection.htmlHeaderSection}}" stepKey="scrollToHtmlHeadSection2"/>
<click selector="{{AdminDesignConfigSection.htmlHeaderSection}}" stepKey="openHtmlHeadSection2"/>
<!--Save Default Configuration-->
<comment userInput="Save Default Configuration" stepKey="saveDefaultConfigurationComment"/>
<click selector="{{AdminDesignConfigSection.useDefaultByFieldsetName('Head')}}" stepKey="clickUseDefault"/>
<waitForElementVisible selector="{{AdminDesignConfigSection.saveConfiguration}}" stepKey="waitForWrapperToClose2"/>
<click selector="{{AdminDesignConfigSection.saveConfiguration}}" stepKey="saveConfiguration2"/>
<waitForElementVisible selector="{{AdminDesignConfigSection.successNotification}}" stepKey="waitForSuccessNotification2"/>
<waitForPageLoad stepKey="waitForPageloadSuccess2"/>
<!--Delete Image: will be in both root and favicon-->
<comment userInput="Delete Image" stepKey="deleteImageComment"/>
<actionGroup ref="navigateToMediaGallery" stepKey="navigateToMediaGallery"/>
<actionGroup ref="NavigateToMediaFolderActionGroup" stepKey="navigateToFolder2">
<argument name="FolderName" value="Storage Root"/>
</actionGroup>
<actionGroup ref="DeleteImageFromStorageActionGroup" stepKey="deleteImageFromStorage">
<argument name="Image" value="ImageUpload3"/>
</actionGroup>
<actionGroup ref="NavigateToFaviconMediaFolderActionGroup" stepKey="navigateToFolder3">
<argument name="StoreFolder" value="{{AdminDesignConfigSection.storeLink}}"/>
</actionGroup>
<actionGroup ref="DeleteImageFromStorageActionGroup" stepKey="deleteImageFromStorage2">
<argument name="Image" value="ImageUpload3"/>
</actionGroup>
</test>
</tests>

0 comments on commit 497e94a

Please sign in to comment.