Skip to content

Commit

Permalink
Merge pull request #7018 from GSadee/grid-date-filter
Browse files Browse the repository at this point in the history
[Grid][Admin][Order] Date filter on orders' list
  • Loading branch information
pjedrzejewski committed Dec 8, 2016
2 parents b05eaa3 + abd3bff commit 0a19513
Show file tree
Hide file tree
Showing 18 changed files with 537 additions and 5 deletions.
16 changes: 16 additions & 0 deletions docs/bundles/SyliusGridBundle/filters.rst
Expand Up @@ -39,3 +39,19 @@ Boolean
-------

This filter checks if a value is true or false.

Date
----

This filter checks if a chosen datetime field is between given dates.

.. code-block:: yaml
sylius_grid:
grids:
app_order:
filters:
date:
type: date
options:
field: completedAt
44 changes: 44 additions & 0 deletions features/order/managing_orders/filtering_orders_by_date.feature
@@ -0,0 +1,44 @@
@managing_orders
Feature: Filtering orders
In order to filter orders in specific period of time
As an Administrator
I want to be able to filter orders on the list

Background:
Given the store operates on a single channel in "United States"
And the store has customer "Mike Ross" with email "ross@teammike.com"
And this customer has placed an order "#00000001" at "2016-12-05 08:00:00"
And this customer has also placed an order "#00000002" at "2016-12-05 09:00:00"
And this customer has also placed an order "#00000003" at "2016-12-05 10:00:00"
And I am logged in as an administrator

@ui
Scenario: Filtering orders by date from
When I browse orders
And I specify filter date from as "2016-12-05 08:30:00"
And I filter
Then I should see 2 orders in the list
And I should see an order with "#00000002" number
And I should see an order with "#00000003" number
But I should not see an order with "#00000001" number

@ui
Scenario: Filtering orders by date to
When I browse orders
And I specify filter date to as "2016-12-05 09:30:00"
And I filter
Then I should see 2 orders in the list
And I should see an order with "#00000001" number
And I should see an order with "#00000002" number
But I should not see an order with "#00000003" number

@ui
Scenario: Filtering orders by date from to
When I browse orders
And I specify filter date from as "2016-12-05 08:30:00"
And I specify filter date to as "2016-12-05 09:30:00"
And I filter
Then I should see a single order in the list
And I should see an order with "#00000002" number
But I should not see an order with "#00000001" number
And I should not see an order with "#00000003" number
12 changes: 12 additions & 0 deletions src/Sylius/Behat/Context/Setup/OrderContext.php
Expand Up @@ -364,6 +364,18 @@ public function iHaveAlreadyPlacedOrderNthTimes(
}
}

/**
* @Given /^(this customer) has(?:| also) placed (an order "[^"]+") at "([^"]+)"$/
*/
public function thisCustomerHasPlacedAnOrderAtDate(CustomerInterface $customer, $number, $checkoutCompletedAt)
{
$order = $this->createOrder($customer, $number);
$order->setCheckoutCompletedAt(new \DateTime($checkoutCompletedAt));
$order->setState(OrderInterface::STATE_NEW);

$this->orderRepository->add($order);
}

/**
* @Given :numberOfCustomers customers have added products to the cart for total of :total
*/
Expand Down
42 changes: 39 additions & 3 deletions src/Sylius/Behat/Context/Ui/Admin/ManagingOrdersContext.php
Expand Up @@ -13,14 +13,13 @@

use Behat\Behat\Context\Context;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
use Sylius\Behat\Page\Admin\Order\IndexPageInterface;
use Sylius\Behat\Page\Admin\Order\ShowPageInterface;
use Sylius\Behat\Page\Admin\Order\UpdatePageInterface;
use Sylius\Behat\Service\NotificationCheckerInterface;
use Sylius\Behat\Service\SharedSecurityServiceInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Addressing\Model\AddressInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
Expand Down Expand Up @@ -146,6 +145,30 @@ public function iSwitchSortingBy($fieldName)
$this->indexPage->sortBy($fieldName);
}

/**
* @When I specify filter date from as :dateTime
*/
public function iSpecifyFilterDateFromAs($dateTime)
{
$this->indexPage->specifyFilterDateFrom(new \DateTime($dateTime));
}

/**
* @When I specify filter date to as :dateTime
*/
public function iSpecifyFilterDateToAs($dateTime)
{
$this->indexPage->specifyFilterDateTo(new \DateTime($dateTime));
}

/**
* @When I filter
*/
public function iFilter()
{
$this->indexPage->filter();
}

/**
* @Then I should see a single order from customer :customer
*/
Expand Down Expand Up @@ -236,8 +259,10 @@ public function itShouldBePaidWith($paymentMethodName)

/**
* @Then /^it should have (\d+) items$/
* @Then I should see :amount orders in the list
* @Then I should see a single order in the list
*/
public function itShouldHaveAmountOfItems($amount)
public function itShouldHaveAmountOfItems($amount = 1)
{
$itemsCount = $this->showPage->countItems();

Expand Down Expand Up @@ -652,6 +677,17 @@ public function iShouldSeeOrderWithNumber($orderNumber)
);
}

/**
* @Then I should not see an order with :orderNumber number
*/
public function iShouldNotSeeOrderWithNumber($orderNumber)
{
Assert::false(
$this->indexPage->isSingleResourceOnPage(['number' => $orderNumber]),
sprintf('Order with "%s" number should not be in the list.', $orderNumber)
);
}

/**
* @Then the first order should have number :number
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Sylius/Behat/Page/Admin/Crud/IndexPage.php
Expand Up @@ -134,6 +134,11 @@ public function deleteResourceOnPage(array $parameters)
$actionButtons->pressButton('Delete');
}

public function filter()
{
$this->getElement('filter')->press();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -164,6 +169,7 @@ protected function getTableAccessor()
protected function getDefinedElements()
{
return array_merge(parent::getDefinedElements(), [
'filter' => 'button:contains("Filter")',
'table' => '.table',
]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Behat/Page/Admin/Crud/IndexPageInterface.php
Expand Up @@ -56,4 +56,6 @@ public function deleteResourceOnPage(array $parameters);
* @return int
*/
public function countItems();

public function filter();
}
42 changes: 42 additions & 0 deletions src/Sylius/Behat/Page/Admin/Order/IndexPage.php
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Behat\Page\Admin\Order;

use Sylius\Behat\Page\Admin\Crud\IndexPage as BaseIndexPage;

/**
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
class IndexPage extends BaseIndexPage implements IndexPageInterface
{
/**
* {@inheritdoc}
*/
public function specifyFilterDateFrom(\DateTime $dateTime)
{
$timestamp = $dateTime->getTimestamp();

$this->getDocument()->fillField('criteria_date_from_date', date('Y-m-d', $timestamp));
$this->getDocument()->fillField('criteria_date_from_time', date('H:i', $timestamp));
}

/**
* {@inheritdoc}
*/
public function specifyFilterDateTo(\DateTime $dateTime)
{
$timestamp = $dateTime->getTimestamp();

$this->getDocument()->fillField('criteria_date_to_date', date('Y-m-d', $timestamp));
$this->getDocument()->fillField('criteria_date_to_time', date('H:i', $timestamp));
}
}
30 changes: 30 additions & 0 deletions src/Sylius/Behat/Page/Admin/Order/IndexPageInterface.php
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Behat\Page\Admin\Order;

use Sylius\Behat\Page\Admin\Crud\IndexPageInterface as BaseIndexPageInterface;

/**
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
interface IndexPageInterface extends BaseIndexPageInterface
{
/**
* @param \DateTime $dateTime
*/
public function specifyFilterDateFrom(\DateTime $dateTime);

/**
* @param \DateTime $dateTime
*/
public function specifyFilterDateTo(\DateTime $dateTime);
}
Expand Up @@ -13,7 +13,7 @@

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="sylius.behat.page.admin.order.index.class">%sylius.behat.page.admin.crud.index.class%</parameter>
<parameter key="sylius.behat.page.admin.order.index.class">Sylius\Behat\Page\Admin\Order\IndexPage</parameter>
<parameter key="sylius.behat.page.admin.order.show.class">Sylius\Behat\Page\Admin\Order\ShowPage</parameter>
<parameter key="sylius.behat.page.admin.order.update.class">Sylius\Behat\Page\Admin\Order\UpdatePage</parameter>
</parameters>
Expand Down
Expand Up @@ -49,6 +49,7 @@ sylius_grid:
filter:
string: "@SyliusUi/Grid/Filter/string.html.twig"
boolean: "@SyliusUi/Grid/Filter/boolean.html.twig"
date: "@SyliusUi/Grid/Filter/date.html.twig"

liip_imagine:
filter_sets:
Expand Down
Expand Up @@ -80,6 +80,11 @@ sylius_grid:
label: sylius.ui.customer
options:
fields: [customer.email, customer.firstName, customer.lastName]
date:
type: date
label: sylius.ui.date
options:
field: checkoutCompletedAt
actions:
item:
show:
Expand Down
19 changes: 18 additions & 1 deletion src/Sylius/Bundle/GridBundle/Doctrine/ORM/ExpressionBuilder.php
Expand Up @@ -206,6 +206,23 @@ private function getFieldName($field)
*/
private function getParameterName($field)
{
return str_replace('.', '_', $field);
$parameterName = str_replace('.', '_', $field);

$i = 1;
while ($this->hasParameterName($parameterName)) {
$parameterName .= $i;
}

return $parameterName;
}

/**
* @param string $parameterName
*
* @return bool
*/
private function hasParameterName($parameterName)
{
return null !== $this->queryBuilder->getParameter($parameterName);
}
}
69 changes: 69 additions & 0 deletions src/Sylius/Bundle/GridBundle/Form/Type/Filter/DateFilterType.php
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\GridBundle\Form\Type\Filter;

use Sylius\Component\Grid\Filter\StringFilter;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Grzegorz Sadowski <grzegorz.sadowski@lakion.com>
*/
final class DateFilterType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('from', DateTimeType::class, [
'label' => 'sylius.ui.from',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'required' => false,
])
->add('to', DateTimeType::class, [
'label' => 'sylius.ui.to',
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'required' => false,
])
;
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefaults([
'data_class' => null,
])
->setDefined('field')
->setAllowedTypes('field', 'string')
;
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'sylius_grid_filter_date';
}
}
Expand Up @@ -27,5 +27,11 @@
<service id="sylius.form.type.grid_filter.boolean" class="Sylius\Bundle\GridBundle\Form\Type\Filter\BooleanFilterType">
<tag name="form.type" />
</service>
<service id="sylius.grid_filter.date" class="Sylius\Component\Grid\Filter\DateFilter">
<tag name="sylius.grid_filter" type="date" form-type="Sylius\Bundle\GridBundle\Form\Type\Filter\DateFilterType" />
</service>
<service id="sylius.form.type.grid_filter.date" class="Sylius\Bundle\GridBundle\Form\Type\Filter\DateFilterType">
<tag name="form.type" />
</service>
</services>
</container>

0 comments on commit 0a19513

Please sign in to comment.