<?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="AdminSortingByWebsitesTest">
        <annotations>
            <stories value="View sorting by websites"/>
            <title value="Sorting by websites in Admin"/>
            <description value="Sorting products by websites in Admin"/>
            <severity value="AVERAGE"/>
        </annotations>
        <before>
            <createData entity="_defaultCategory" stepKey="createCategory"/>
            <createData entity="_defaultProduct" stepKey="productAssignedToCustomWebsite">
                <requiredEntity createDataKey="createCategory"/>
            </createData>
            <createData entity="SimpleProduct" stepKey="productAssignedToMainWebsite">
                <requiredEntity createDataKey="createCategory"/>
            </createData>

            <actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
            <!--Create new website -->
            <actionGroup ref="AdminCreateWebsiteActionGroup" stepKey="createAdditionalWebsite">
                <argument name="newWebsiteName" value="{{customWebsite.name}}"/>
                <argument name="websiteCode" value="{{customWebsite.code}}"/>
            </actionGroup>
            <actionGroup ref="EnableWebUrlOptionsActionGroup" stepKey="addStoreCodeToUrls"/>
            <actionGroup ref="CliCacheFlushActionGroup" stepKey="flushCacheAfterEnableWebUrlOptions">
                <argument name="tags" value=""/>
            </actionGroup>
        </before>
        <after>
            <deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
            <deleteData createDataKey="productAssignedToCustomWebsite" stepKey="deleteProductAssignedToCustomWebsite"/>
            <deleteData createDataKey="productAssignedToMainWebsite" stepKey="deleteProductAssignedToMainWebsite"/>
            <actionGroup ref="AdminDeleteWebsiteActionGroup" stepKey="deleteTestWebsite">
                <argument name="websiteName" value="{{customWebsite.name}}"/>
            </actionGroup>
            <actionGroup ref="AdminOpenCatalogProductPageActionGroup" stepKey="goToProductCatalogPage"/>
            <actionGroup ref="ResetProductGridToDefaultViewActionGroup" stepKey="resetProductGridColumnsInitial"/>
            <actionGroup ref="ResetWebUrlOptionsActionGroup" stepKey="resetUrlOption"/>
            <actionGroup ref="CliIndexerReindexActionGroup" stepKey="reindex">
                <argument name="indices" value=""/>
            </actionGroup>
            <actionGroup ref="CliCacheFlushActionGroup" stepKey="flushCache">
                <argument name="tags" value=""/>
            </actionGroup>
            <actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
        </after>

        <!--Assign Custom Website to Simple Product -->
        <actionGroup ref="AdminOpenProductIndexPageActionGroup" stepKey="navigateToCatalogProductGrid"/>

        <conditionalClick selector="{{AdminProductGridFilterSection.clearFilters}}" dependentSelector="{{AdminProductGridFilterSection.clearFilters}}" visible="true" stepKey="clickClearFiltersInitial"/>
        <actionGroup ref="OpenEditProductOnBackendActionGroup" stepKey="assignCustomWebsiteToProduct">
            <argument name="product" value="$$productAssignedToCustomWebsite$$"/>
        </actionGroup>
        <scrollTo selector="{{ProductInWebsitesSection.sectionHeader}}" stepKey="scrollToWebsites"/>
        <conditionalClick selector="{{ProductInWebsitesSection.sectionHeader}}" dependentSelector="{{AdminProductContentSection.sectionHeaderShow}}" visible="false" stepKey="expandSection"/>
        <waitForPageLoad stepKey="waitForPageOpened"/>
        <uncheckOption selector="{{ProductInWebsitesSection.website(_defaultWebsite.name)}}" stepKey="deselectMainWebsite"/>
        <checkOption selector="{{ProductInWebsitesSection.website(customWebsite.name)}}" stepKey="selectWebsite"/>

        <actionGroup ref="AdminProductFormSaveActionGroup" stepKey="clickSave"/>
        <seeElement selector="{{AdminProductMessagesSection.successMessage}}" stepKey="seeSaveProductMessageAgain"/>

        <!--Navigate To Product Grid To Check Website Sorting-->
        <actionGroup ref="AdminOpenProductIndexPageActionGroup" stepKey="navigateToCatalogProductGridToSortByWebsite"/>

        <!--Sorting works (By Websites) ASC-->
        <click selector="{{AdminProductGridSection.columnHeader('Websites')}}" stepKey="clickWebsitesHeaderToSortAsc"/>
        <see selector="{{AdminProductGridSection.productGridContentsOnRow('1')}}" userInput="Main Website" stepKey="checkIfProduct1WebsitesAsc"/>

        <!--Sorting works (By Websites) DESC-->
        <click selector="{{AdminProductGridSection.columnHeader('Websites')}}" stepKey="clickWebsitesHeaderToSortDesc"/>
        <see selector="{{AdminProductGridSection.productGridContentsOnRow('1')}}" userInput="{{customWebsite.name}}" stepKey="checkIfProduct1WebsitesDesc"/>
    </test>
</tests>
