Skip to content

Commit

Permalink
refactor #13611 Optimize variant autocomplete selector (rafal.swiercz…
Browse files Browse the repository at this point in the history
…ek@drophub.pl)

This PR was merged into the 1.11 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.11
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets |
| License         | MIT


Commits
-------

05531e8 Add product variant pagination
5c65af7 Remove pagination from product variant. Keep limit
abff837 Fix findByPhrase method name
f88b362 Fix functional test for partial product variants
83e6be1 Remove useless productId field
d4cf31b Make limit optional
04e3ea9 Clean code
8e1a32e Add copyright
b0cc5af Refactor product variant limit
25129d8 Adjust product variant test
4d51bbf Add void to test methods
1c7ac83 Make limit configurable from twig scope
17054db Improve product variants tests
b1b8e45 Move product variants limit to parameter bag
d475f45 Remove useless limit param
  • Loading branch information
Zales0123 committed Feb 10, 2022
2 parents 2fb6084 + d475f45 commit fea4b7c
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ sylius_admin_ajax_all_product_variants_by_phrase:
arguments:
phrase: $phrase
locale: expr:service('sylius.context.locale').getLocaleCode()
limit: "!!int %sylius.ajax.product.variant_autocomplete_limit%"

sylius_admin_ajax_product_variants_by_codes:
path: /
Expand Down
4 changes: 4 additions & 0 deletions src/Sylius/Bundle/AdminBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<import resource="services/provider.xml" />
</imports>

<parameters>
<parameter key="sylius.ajax.product.variant_autocomplete_limit">25</parameter>
</parameters>

<services>
<defaults public="true" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function findByPhraseAndProductCode(string $phrase, string $locale, strin
;
}

public function findByPhrase(string $phrase, string $locale): array
public function findByPhrase(string $phrase, string $locale, ?int $limit = null): array
{
$expr = $this->getEntityManager()->getExpressionBuilder();

Expand All @@ -162,6 +162,9 @@ public function findByPhrase(string $phrase, string $locale): array
))
->setParameter('phrase', '%' . $phrase . '%')
->setParameter('locale', $locale)
->orderBy('o.product', 'ASC')
->addOrderBy('o.position', 'ASC')
->setMaxResults($limit)
->getQuery()
->getResult()
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function findByPhraseAndProductCode(string $phrase, string $locale, strin
/**
* @return array|ProductVariantInterface[]
*/
public function findByPhrase(string $phrase, string $locale): array;
public function findByPhrase(string $phrase, string $locale, ?int $limit = null): array;

/**
* @return array|string[]
Expand Down
100 changes: 100 additions & 0 deletions tests/Controller/AdminProductVariantAjaxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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.
*/

declare(strict_types=1);

namespace Sylius\Tests\Controller;

use ApiTestCase\JsonApiTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

final class AdminProductVariantAjaxTest extends JsonApiTestCase
{
/** @test */
public function it_denies_access_to_product_variants_for_not_authenticated_user(): void
{
$this->client->request('GET', '/admin/ajax/product-variants/search-all');

$response = $this->client->getResponse();

$this->assertEquals($response->getStatusCode(), Response::HTTP_FOUND);
}

/** @test */
public function it_returns_only_specified_part_of_all_product_variants_for_empty_phrase(): void
{
$this->loadFixturesFromFile('authentication/administrator.yml');
$this->loadFixturesFromFiles(['resources/product_variants.yml']);

$this->authenticateAdminUser();

$this->client->request('GET', '/admin/ajax/product-variants/search-all?phrase=');

$response = $this->client->getResponse();

$this->assertResponse($response, 'ajax/product_variant/index_response', Response::HTTP_OK);
}

/** @test */
public function it_throws_type_error_when_phrase_is_not_specified(): void
{
$this->loadFixturesFromFile('authentication/administrator.yml');
$this->loadFixturesFromFiles(['resources/product_variants.yml']);

$this->authenticateAdminUser();

$this->expectException(\TypeError::class);

$this->client->request('GET', '/admin/ajax/product-variants/search-all');
}

/** @test */
public function it_returns_specific_product_variants_for_given_phrase(): void
{
$this->loadFixturesFromFile('authentication/administrator.yml');
$this->loadFixturesFromFiles(['resources/product_variants.yml']);

$this->authenticateAdminUser();

$this->client->request('GET', '/admin/ajax/product-variants/search-all?phrase=LA');

$response = $this->client->getResponse();

$productVariants = json_decode($response->getContent());

$this->assertEquals('LARGE_MUG', $productVariants[0]->code);
}

private function authenticateAdminUser(): void
{
$adminUserRepository = self::$container->get('sylius.repository.admin_user');
$user = $adminUserRepository->findOneByEmail('admin@sylius.com');

$session = self::$container->get('session');
$firewallName = 'admin';
$firewallContext = 'admin';

/** @deprecated parameter credential was deprecated in Symfony 5.4, so in Sylius 1.11 too, in Sylius 2.0 providing 4 arguments will be prohibited. */
if (3 === (new \ReflectionClass(UsernamePasswordToken::class))->getConstructor()->getNumberOfParameters()) {
$token = new UsernamePasswordToken($user, $firewallName, $user->getRoles());
} else {
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
}

$session->set(sprintf('_security_%s', $firewallContext), serialize($token));
$session->save();

$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
}
4 changes: 2 additions & 2 deletions tests/DataFixtures/ORM/resources/product_variants.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Sylius\Component\Core\Model\Product:
code: STICKER

Sylius\Component\Product\Model\ProductVariantTranslation:
productVariantTranslation{1..20}:
productVariantTranslation{1..30}:
name: "Mug <current()>"
locale: en_US
translatable: "@productVariant<current()>"
Expand All @@ -51,7 +51,7 @@ Sylius\Component\Product\Model\ProductVariantTranslation:
translatable: "@productVariant22"

Sylius\Component\Core\Model\ProductVariant:
productVariant{1..20}:
productVariant{1..30}:
code: MUG_<current()>
version: 1
product: "@product1"
Expand Down
127 changes: 127 additions & 0 deletions tests/Responses/ajax/product_variant/index_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
[
{
"descriptor":"Mug 1 (MUG_1)",
"id":@integer@,
"code":"MUG_1"
},
{
"descriptor":"Mug 2 (MUG_2)",
"id":@integer@,
"code":"MUG_2"
},
{
"descriptor":"Mug 3 (MUG_3)",
"id":@integer@,
"code":"MUG_3"
},
{
"descriptor":"Mug 4 (MUG_4)",
"id":@integer@,
"code":"MUG_4"
},
{
"descriptor":"Mug 5 (MUG_5)",
"id":@integer@,
"code":"MUG_5"
},
{
"descriptor":"Mug 6 (MUG_6)",
"id":@integer@,
"code":"MUG_6"
},
{
"descriptor":"Mug 7 (MUG_7)",
"id":@integer@,
"code":"MUG_7"
},
{
"descriptor":"Mug 8 (MUG_8)",
"id":@integer@,
"code":"MUG_8"
},
{
"descriptor":"Mug 9 (MUG_9)",
"id":@integer@,
"code":"MUG_9"
},
{
"descriptor":"Mug 10 (MUG_10)",
"id":@integer@,
"code":"MUG_10"
},
{
"descriptor":"Mug 11 (MUG_11)",
"id":@integer@,
"code":"MUG_11"
},
{
"descriptor":"Mug 12 (MUG_12)",
"id":@integer@,
"code":"MUG_12"
},
{
"descriptor":"Mug 13 (MUG_13)",
"id":@integer@,
"code":"MUG_13"
},
{
"descriptor":"Mug 14 (MUG_14)",
"id":@integer@,
"code":"MUG_14"
},
{
"descriptor":"Mug 15 (MUG_15)",
"id":@integer@,
"code":"MUG_15"
},
{
"descriptor":"Mug 16 (MUG_16)",
"id":@integer@,
"code":"MUG_16"
},
{
"descriptor":"Mug 17 (MUG_17)",
"id":@integer@,
"code":"MUG_17"
},
{
"descriptor":"Mug 18 (MUG_18)",
"id":@integer@,
"code":"MUG_18"
},
{
"descriptor":"Mug 19 (MUG_19)",
"id":@integer@,
"code":"MUG_19"
},
{
"descriptor":"Mug 20 (MUG_20)",
"id":@integer@,
"code":"MUG_20"
},
{
"descriptor":"Large Mug (LARGE_MUG)",
"id":@integer@,
"code":"LARGE_MUG"
},
{
"descriptor":"Mug 23 (MUG_23)",
"id":@integer@,
"code":"MUG_23"
},
{
"descriptor":"Mug 24 (MUG_24)",
"id":@integer@,
"code":"MUG_24"
},
{
"descriptor":"Mug 25 (MUG_25)",
"id":@integer@,
"code":"MUG_25"
},
{
"descriptor":"Mug 26 (MUG_26)",
"id":@integer@,
"code":"MUG_26"
}
]

0 comments on commit fea4b7c

Please sign in to comment.