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

[Admin][Product] Access the variants management from product edit page #10316

Merged
merged 3 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/customization/menu.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Adding new positions in your menu is done **via events**.

You have got the ``Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent`` with ``FactoryInterface`` and ``ItemInterface`` of `KnpMenu`_, this lets you manipulate the whole menu.

You've got six events that you should be subscribing to:
You've got eight events that you should be subscribing to:

.. code-block:: php

Expand All @@ -14,7 +14,9 @@ You've got six events that you should be subscribing to:
sylius.menu.admin.customer.show # For the buttons menu on top of the show page of the Customer (/admin/customers/{id})
sylius.menu.admin.order.show # For the buttons menu on top of the show page of the Order (/admin/orders/{id})
sylius.menu.admin.product.form # For the tabular menu on the left hand side of the new/edit pages of the Product (/admin/products/new & /admin/products/{id}/edit)
sylius.menu.admin.product.update # For the buttons menu on top of the update page of the Product (/admin/products/{id}/edit)
sylius.menu.admin.product_variant.form # For the tabular menu on the left hand side of the new/edit pages of the ProductVariant (/admin/products/{productId}/variants/new & /admin/products/{productId}/variants/{id}/edit)
sylius.menu.admin.promotion.update # For the buttons menu on top of the update page of the Promotion (/admin/promotions/{id}/edit)

How to customize Admin Menu?
----------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@managing_products
Feature: Accessing the variants management from the product edit page
In order to facilitate work with the management of variants
As an Administrator
I want to be able to access the variants management directly from the product edit page

Background:
Given the store operates on a single channel in "United States"
And the store has a "Audi" configurable product
And this product has option "Model" with values "RS6" and "RS7"
And I am logged in as an administrator

@ui
Scenario: Being able to access the variants list page
When I modify the "Audi" product
And I go to the variants list
Then I should be on the list of this product's variants

@ui
Scenario: Being able to access the variant creation page
When I modify the "Audi" product
And I go to the variant creation page
Then I should be on the variant creation page for this product

@ui
Scenario: Being able to access the variant generation page
When I modify the "Audi" product
And I go to the variant generation page
Then I should be on the variant generation page for this product
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ public function thisVariantShouldHaveAItemCurrentlyInStock(ProductVariantInterfa
Assert::same($this->indexPage->getOnHandQuantityFor($productVariant), (int) $amountInStock);
}

/**
* @Then /^I should be on the list of (this product)'s variants$/
*/
public function iShouldBeOnTheListOfThisProductVariants(ProductInterface $product): void
{
Assert::true($this->indexPage->isOpen(['productId' => $product->getId()]));
}

/**
* @param int $expectedAmount
* @param ProductVariantInterface $variant
Expand Down
55 changes: 54 additions & 1 deletion src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Sylius\Behat\Page\Admin\Product\UpdateConfigurableProductPageInterface;
use Sylius\Behat\Page\Admin\Product\UpdateSimpleProductPageInterface;
use Sylius\Behat\Page\Admin\ProductReview\IndexPageInterface as ProductReviewIndexPageInterface;
use Sylius\Behat\Page\Admin\ProductVariant\CreatePageInterface as VariantCreatePageInterface;
use Sylius\Behat\Page\Admin\ProductVariant\GeneratePageInterface;
use Sylius\Behat\Service\NotificationCheckerInterface;
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
use Sylius\Behat\Service\SharedStorageInterface;
Expand Down Expand Up @@ -60,6 +62,12 @@ final class ManagingProductsContext implements Context
/** @var IndexPerTaxonPageInterface */
private $indexPerTaxonPage;

/** @var VariantCreatePageInterface */
private $variantCreatePage;

/** @var GeneratePageInterface */
private $variantGeneratePage;

/** @var CurrentPageResolverInterface */
private $currentPageResolver;

Expand All @@ -75,6 +83,8 @@ public function __construct(
UpdateConfigurableProductPageInterface $updateConfigurableProductPage,
ProductReviewIndexPageInterface $productReviewIndexPage,
IndexPerTaxonPageInterface $indexPerTaxonPage,
VariantCreatePageInterface $variantCreatePage,
GeneratePageInterface $variantGeneratePage,
CurrentPageResolverInterface $currentPageResolver,
NotificationCheckerInterface $notificationChecker
) {
Expand All @@ -86,6 +96,8 @@ public function __construct(
$this->updateConfigurableProductPage = $updateConfigurableProductPage;
$this->productReviewIndexPage = $productReviewIndexPage;
$this->indexPerTaxonPage = $indexPerTaxonPage;
$this->variantCreatePage = $variantCreatePage;
$this->variantGeneratePage = $variantGeneratePage;
$this->currentPageResolver = $currentPageResolver;
$this->notificationChecker = $notificationChecker;
}
Expand Down Expand Up @@ -371,8 +383,9 @@ public function productShouldExistInTheProductCatalog(ProductInterface $product)
/**
* @When I want to modify the :product product
* @When /^I want to modify (this product)$/
* @When I modify the :product product
*/
public function iWantToModifyAProduct(ProductInterface $product)
public function iWantToModifyAProduct(ProductInterface $product): void
{
$this->sharedStorage->set('product', $product);

Expand Down Expand Up @@ -645,6 +658,30 @@ public function iRemoveAnAssociatedProductFromProductAssociation(
$currentPage->removeAssociatedProduct($productName, $productAssociationType);
}

/**
* @When I go to the variants list
*/
public function iGoToTheVariantsList(): void
{
$this->resolveCurrentPage()->goToVariantsList();
}

/**
* @When I go to the variant creation page
*/
public function iGoToTheVariantCreationPage(): void
{
$this->resolveCurrentPage()->goToVariantCreation();
}

/**
* @When I go to the variant generation page
*/
public function iGoToTheVariantGenerationPage(): void
{
$this->resolveCurrentPage()->goToVariantGeneration();
}

/**
* @Then /^(?:this product|the product "[^"]+"|it) should(?:| also) have an image with "([^"]*)" type$/
*/
Expand Down Expand Up @@ -934,6 +971,22 @@ public function iShouldBeNotifiedThatTheAttributeInShouldBeLongerThan($attribute
);
}

/**
* @Then /^I should be on the variant creation page for (this product)$/
*/
public function iShouldBeOnTheVariantCreationPageForThisProduct(ProductInterface $product): void
{
Assert::true($this->variantCreatePage->isOpen(['productId' => $product->getId()]));
}

/**
* @Then /^I should be on the variant generation page for (this product)$/
*/
public function iShouldBeOnTheVariantGenerationPageForThisProduct(ProductInterface $product): void
{
Assert::true($this->variantGeneratePage->isOpen(['productId' => $product->getId()]));
}

/**
* @param string $element
* @param string $value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ public function countImages(): int
return count($imageElements);
}

public function goToVariantsList(): void
{
$this->getDocument()->clickLink('List variants');
}

public function goToVariantCreation(): void
{
$this->getDocument()->clickLink('Create');
}

public function goToVariantGeneration(): void
{
$this->getDocument()->clickLink('Generate');
}

protected function getCodeElement(): NodeElement
{
return $this->getElement('code');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ public function removeFirstImage(): void;
public function modifyFirstImageType(string $type): void;

public function countImages(): int;

public function goToVariantsList(): void;

public function goToVariantCreation(): void;

public function goToVariantGeneration(): void;
}
15 changes: 15 additions & 0 deletions src/Sylius/Behat/Page/Admin/Product/UpdateSimpleProductPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,21 @@ public function isShippingRequired(): bool
return $this->getElement('shipping_required')->isChecked();
}

public function goToVariantsList(): void
{
$this->getDocument()->clickLink('List variants');
}

public function goToVariantCreation(): void
{
$this->getDocument()->clickLink('Create');
}

public function goToVariantGeneration(): void
{
$this->getDocument()->clickLink('Generate');
}

protected function getCodeElement(): NodeElement
{
return $this->getElement('code');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@ public function getPriceForChannel(string $channelName): string;
public function getOriginalPriceForChannel(string $channelName): string;

public function isShippingRequired(): bool;

public function goToVariantsList(): void;

public function goToVariantCreation(): void;

public function goToVariantGeneration(): void;
}
3 changes: 2 additions & 1 deletion src/Sylius/Behat/Resources/config/services/contexts/ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@
<argument type="service" id="sylius.behat.page.admin.product.update_configurable" />
<argument type="service" id="sylius.behat.page.admin.product_review.index" />
<argument type="service" id="sylius.behat.page.admin.product.index_per_taxon" />
<argument type="service" id="sylius.behat.page.admin.product_variant.create" />
<argument type="service" id="sylius.behat.page.admin.product_variant.generate" />
<argument type="service" id="sylius.behat.current_page_resolver" />
<argument type="service" id="sylius.behat.notification_checker" />
<argument type="service" id="liip_imagine.cache.manager" />
</service>

<service id="sylius.behat.context.ui.admin.managing_product_association_types" class="Sylius\Behat\Context\Ui\Admin\ManagingProductAssociationTypesContext">
Expand Down
89 changes: 89 additions & 0 deletions src/Sylius/Bundle/AdminBundle/Menu/ProductUpdateMenuBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Sylius\Bundle\AdminBundle\Menu;

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Sylius\Bundle\AdminBundle\Event\ProductMenuBuilderEvent;
use Sylius\Component\Core\Model\ProductInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final class ProductUpdateMenuBuilder
{
public const EVENT_NAME = 'sylius.menu.admin.product.update';

/** @var FactoryInterface */
private $factory;

/** @var EventDispatcherInterface */
private $eventDispatcher;

public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
{
$this->factory = $factory;
$this->eventDispatcher = $eventDispatcher;
}

public function createMenu(array $options): ItemInterface
{
$menu = $this->factory->createItem('root');

if (!isset($options['product'])) {
return $menu;
}

$product = $options['product'];
if (!$product instanceof ProductInterface) {
GSadee marked this conversation as resolved.
Show resolved Hide resolved
return $menu;
}

$manageVariantsItem = $this->factory
->createItem('manage_variants')
->setAttribute('type', 'links')
->setLabel('sylius.ui.manage_variants')
->setLabelAttribute('icon', 'cubes')
;

$manageVariantsItem
->addChild('product_variant_index', [
'route' => 'sylius_admin_product_variant_index',
'routeParameters' => ['productId' => $product->getId()],
])
->setAttribute('type', 'link')
->setLabel('sylius.ui.list_variants')
->setLabelAttribute('icon', 'list')
;
$manageVariantsItem
->addChild('product_variant_create', [
'route' => 'sylius_admin_product_variant_create',
'routeParameters' => ['productId' => $product->getId()],
])
->setAttribute('type', 'link')
->setLabel('sylius.ui.create')
->setLabelAttribute('icon', 'plus')
;

if ($product->hasOptions()) {
$manageVariantsItem
->addChild('product_variant_generate', [
'route' => 'sylius_admin_product_variant_generate',
'routeParameters' => ['productId' => $product->getId()],
])
->setAttribute('type', 'link')
->setLabel('sylius.ui.generate')
->setLabelAttribute('icon', 'random')
;
}

$menu->addChild($manageVariantsItem);

$this->eventDispatcher->dispatch(
self::EVENT_NAME,
new ProductMenuBuilderEvent($this->factory, $menu, $product)
);

return $menu;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sylius_admin_product:
alias: sylius.product
section: admin
templates: "@SyliusAdmin\\Crud"
except: ['show', 'index']
except: ['show', 'index', 'update']
redirect: update
grid: sylius_admin_product
permission: true
Expand All @@ -30,6 +30,23 @@ sylius_admin_product_index:
subheader: sylius.ui.manage_your_product_catalog
icon: cube

sylius_admin_product_update:
path: /products/{id}/edit
methods: [GET, PUT, PATCH]
defaults:
_controller: sylius.controller.product:updateAction
_sylius:
section: admin
permission: true
redirect: referer
template: "@SyliusAdmin/Crud/update.html.twig"
vars:
subheader: sylius.ui.manage_your_product_catalog
icon: cube
templates:
form: "@SyliusAdmin/Product/_form.html.twig"
toolbar: "@SyliusAdmin/Product/Update/_toolbar.html.twig"

sylius_admin_product_per_taxon_index:
path: /products/taxon/{taxonId}
methods: [GET]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
<tag name="knp_menu.menu_builder" method="createMenu" alias="sylius.admin.product_form" />
</service>

<service id="sylius.admin.menu_builder.product.update" class="Sylius\Bundle\AdminBundle\Menu\ProductUpdateMenuBuilder">
<argument type="service" id="knp_menu.factory" />
<argument type="service" id="event_dispatcher" />
<tag name="knp_menu.menu_builder" method="createMenu" alias="sylius.admin.product.update" />
</service>

<service id="sylius.admin.menu_builder.product_variant_form" class="Sylius\Bundle\AdminBundle\Menu\ProductVariantFormMenuBuilder">
<argument type="service" id="knp_menu.factory" />
<argument type="service" id="event_dispatcher" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="ui right floated buttons">
{% set menu = knp_menu_get('sylius.admin.product.update', [], {'product': product}) %}
{{ knp_menu_render(menu, {'template': '@SyliusUi/Menu/top.html.twig'}) }}
</div>
Loading