first commit
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace KupShop\RestrictionsBundle\Tests;
|
||||
|
||||
use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
|
||||
use KupShop\RestrictionsBundle\Utils\Restrictions;
|
||||
|
||||
class RestrictionsProductListTest extends \DatabaseTestCase
|
||||
{
|
||||
use CartTestTrait;
|
||||
|
||||
/** @var ProductList */
|
||||
private $productList;
|
||||
/** @var Restrictions */
|
||||
private $restrictions;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->productList = new \ProductList();
|
||||
$this->restrictions = new Restrictions();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataSet_testProductListRestricts
|
||||
*
|
||||
* @param int $domainId
|
||||
* @param int $expectedCount
|
||||
*/
|
||||
public function testProductListRestricts($domainId, $expectedCount)
|
||||
{
|
||||
$this->prepareDomain($domainId);
|
||||
|
||||
$this->productList->applyDefaultFilterParams();
|
||||
$products = $this->productList->getProducts();
|
||||
$this->assertCount($expectedCount, $products);
|
||||
}
|
||||
|
||||
public function dataSet_testProductListRestricts()
|
||||
{
|
||||
return [
|
||||
'exclude category' => [1, 8],
|
||||
'exclude producer' => [2, 9],
|
||||
'exclude campaign' => [3, 7],
|
||||
'exclude product' => [4, 6],
|
||||
'exclude product in campaign' => [6, 9],
|
||||
'empty restriction' => [7, 11],
|
||||
];
|
||||
}
|
||||
|
||||
public function testThrowsOnInvalidJson()
|
||||
{
|
||||
$this->insertSQL('restrictions', [
|
||||
'domain' => 1, // countries
|
||||
'object_value' => 'invalid json should throw',
|
||||
]);
|
||||
|
||||
$this->prepareDomain(sqlGetConnection()->lastInsertId());
|
||||
|
||||
$this->expectException('JsonException');
|
||||
|
||||
$this->productList->applyDefaultFilterParams();
|
||||
$this->productList->getProducts();
|
||||
}
|
||||
|
||||
public function testGetRestrictionSpec()
|
||||
{
|
||||
$this->prepareDomain(4);
|
||||
|
||||
$specs = $this->restrictions->getRestrictionSpec();
|
||||
$this->assertInstanceOf('Closure', $specs);
|
||||
}
|
||||
|
||||
public function testGetRestrictionSpecSql()
|
||||
{
|
||||
$this->prepareDomain(1);
|
||||
|
||||
$snippet = $this->restrictions->getRestrictionSpecSnippet();
|
||||
$this->assertRegExp('/NOT \(EXISTS \(.+\)/', $snippet->where);
|
||||
$this->assertCount(1, $snippet->types);
|
||||
}
|
||||
|
||||
private function prepareDomain($domainId)
|
||||
{
|
||||
$this->insertSQL('restrictions_domains', [
|
||||
'id_restriction' => $domainId,
|
||||
'country' => 'CZ',
|
||||
]);
|
||||
|
||||
$this->loginUser(1);
|
||||
}
|
||||
|
||||
public function getDataSet()
|
||||
{
|
||||
$objects = findModule('restrictions', 'objects');
|
||||
$domains = findModule('restrictions', 'domains');
|
||||
|
||||
// Delete autogenerated product trash-cart
|
||||
$this->deleteSQL('products', ['id' => 0]);
|
||||
|
||||
$arr = [
|
||||
'users' => [
|
||||
[
|
||||
'id' => 1,
|
||||
'country' => 'CZ',
|
||||
],
|
||||
],
|
||||
'restrictions' => [
|
||||
[
|
||||
'id' => 1,
|
||||
'domain' => $domains['countries'],
|
||||
'object_value' => json_encode([$objects['categories'] => [3]]),
|
||||
],
|
||||
[
|
||||
'id' => 2,
|
||||
'domain' => $domains['countries'],
|
||||
'object_value' => json_encode([$objects['producers'] => [10, 12]]),
|
||||
],
|
||||
[
|
||||
'id' => 3,
|
||||
'domain' => $domains['countries'],
|
||||
'object_value' => json_encode([$objects['campaigns'] => ['N']]),
|
||||
],
|
||||
[
|
||||
'id' => 4,
|
||||
'domain' => $domains['countries'],
|
||||
'object_value' => json_encode([$objects['products'] => [1, 2, 3, 4, 5]]),
|
||||
],
|
||||
[
|
||||
'id' => 5,
|
||||
'domain' => $domains['countries'],
|
||||
'object_value' => json_encode([$objects['products'] => [6, 7]]),
|
||||
],
|
||||
[
|
||||
'id' => 6,
|
||||
'domain' => $domains['countries'],
|
||||
'object_value' => json_encode([
|
||||
$objects['products'] => [8, 11],
|
||||
$objects['campaigns'] => ['N'],
|
||||
]),
|
||||
],
|
||||
[
|
||||
'id' => 7,
|
||||
'domain' => $domains['countries'],
|
||||
'object_value' => '[]',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $this->getJsonDataSet(json_encode($arr));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
{
|
||||
"sections": [
|
||||
{
|
||||
"id": 1,
|
||||
"id_block": null,
|
||||
"name": "Kategorie s produktama",
|
||||
"name_short": "Kategorie s produktama",
|
||||
"figure": "Y",
|
||||
"priority": 0,
|
||||
"behaviour": "2",
|
||||
"orderby": "title",
|
||||
"orderdir": "ASC",
|
||||
"date_updated": "2025-03-28 13:47:07",
|
||||
"list_variations": "N",
|
||||
"lead_figure": "N",
|
||||
"lead_text": "",
|
||||
"lead_products": "",
|
||||
"photo": null,
|
||||
"meta_title": "",
|
||||
"meta_description": "",
|
||||
"meta_keywords": null,
|
||||
"feed_heureka": null,
|
||||
"feed_heureka_sk": null,
|
||||
"feed_google": null,
|
||||
"feed_glami": null,
|
||||
"flags": "",
|
||||
"feed_seznam": null,
|
||||
"producers_filter": "N",
|
||||
"url": null,
|
||||
"redirect_url": null,
|
||||
"data": "{\"enable_sort_by_in_store\": \"Y\", \"enable_sort_by_product_position\": \"Y\", \"filters_inherit_settings\": \"N\"}",
|
||||
"template": "",
|
||||
"virtual": "N",
|
||||
"filter_url": null,
|
||||
"title": null,
|
||||
"producers_indexing": "N",
|
||||
"producers_to_title": "N",
|
||||
"show_in_search": "Y",
|
||||
"labels_filter": "N"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"id_block": null,
|
||||
"name": "Kategorie bez produktu 1",
|
||||
"name_short": "Kategorie bez produktu 1",
|
||||
"figure": "Y",
|
||||
"priority": 0,
|
||||
"behaviour": "2",
|
||||
"orderby": "title",
|
||||
"orderdir": "ASC",
|
||||
"date_updated": "2025-03-28 13:47:22",
|
||||
"list_variations": "N",
|
||||
"lead_figure": "N",
|
||||
"lead_text": "",
|
||||
"lead_products": "",
|
||||
"photo": null,
|
||||
"meta_title": "",
|
||||
"meta_description": "",
|
||||
"meta_keywords": null,
|
||||
"feed_heureka": null,
|
||||
"feed_heureka_sk": null,
|
||||
"feed_google": null,
|
||||
"feed_glami": null,
|
||||
"flags": "",
|
||||
"feed_seznam": null,
|
||||
"annotation": null,
|
||||
"producers_filter": "N",
|
||||
"url": null,
|
||||
"redirect_url": null,
|
||||
"data": null,
|
||||
"template": "",
|
||||
"virtual": "N",
|
||||
"filter_url": null,
|
||||
"title": null,
|
||||
"producers_indexing": "N",
|
||||
"producers_to_title": "N",
|
||||
"show_in_search": "Y",
|
||||
"labels_filter": "N"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"id_block": null,
|
||||
"name": "Kategorie bez produktu 2",
|
||||
"name_short": "Kategorie bez produktu 2",
|
||||
"figure": "Y",
|
||||
"priority": 0,
|
||||
"behaviour": "2",
|
||||
"orderby": "title",
|
||||
"orderdir": "ASC",
|
||||
"date_updated": "2025-03-28 13:46:58",
|
||||
"list_variations": "N",
|
||||
"lead_figure": "N",
|
||||
"lead_text": "",
|
||||
"lead_products": "",
|
||||
"photo": null,
|
||||
"meta_title": "",
|
||||
"meta_description": "",
|
||||
"meta_keywords": null,
|
||||
"feed_heureka": null,
|
||||
"feed_heureka_sk": null,
|
||||
"feed_google": null,
|
||||
"feed_glami": null,
|
||||
"flags": "",
|
||||
"feed_seznam": null,
|
||||
"annotation": null,
|
||||
"producers_filter": "N",
|
||||
"url": null,
|
||||
"redirect_url": null,
|
||||
"data": null,
|
||||
"template": "",
|
||||
"virtual": "N",
|
||||
"filter_url": null,
|
||||
"title": null,
|
||||
"producers_indexing": "N",
|
||||
"producers_to_title": "N",
|
||||
"show_in_search": "Y",
|
||||
"labels_filter": "Y"
|
||||
}
|
||||
],
|
||||
"products_in_sections": [
|
||||
{
|
||||
"id_product": 1,
|
||||
"id_section": 1,
|
||||
"figure": "Y",
|
||||
"generated": 0
|
||||
},
|
||||
{
|
||||
"id_product": 2,
|
||||
"id_section": 1,
|
||||
"figure": "Y",
|
||||
"generated": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KupShop\RestrictionsBundle\Tests;
|
||||
|
||||
use KupShop\CatalogBundle\Section\SectionTree;
|
||||
use KupShop\RestrictionsBundle\Utils\RestrictionUtil;
|
||||
|
||||
class RestrictionsSectionsTest extends \DatabaseTestCase
|
||||
{
|
||||
private RestrictionUtil $restrictionUtil;
|
||||
private SectionTree $sectionTree;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->restrictionUtil = $this->get(RestrictionUtil::class);
|
||||
$this->sectionTree = $this->get(SectionTree::class);
|
||||
}
|
||||
|
||||
/** @dataProvider data_testRestrictSections */
|
||||
public function testRestrictSections(string $showEmptySections, int $expectedCount): void
|
||||
{
|
||||
\Settings::getDefault()->cat_show_empty = $showEmptySections;
|
||||
|
||||
$sections = $this->restrictionUtil->restrictSections(
|
||||
$this->sectionTree->getTree()
|
||||
);
|
||||
|
||||
$this->assertCount($expectedCount, $sections);
|
||||
}
|
||||
|
||||
public function data_testRestrictSections(): iterable
|
||||
{
|
||||
yield 'Show empty sections is disabled' => ['N', 1];
|
||||
yield 'Show empty sections is enabled' => ['Y', 3];
|
||||
}
|
||||
|
||||
protected function getDataSet()
|
||||
{
|
||||
return $this->getJsonDataSetFromFile();
|
||||
}
|
||||
}
|
||||
244
bundles/KupShop/RestrictionsBundle/Tests/RestrictionsTest.php
Normal file
244
bundles/KupShop/RestrictionsBundle/Tests/RestrictionsTest.php
Normal file
@@ -0,0 +1,244 @@
|
||||
<?php
|
||||
|
||||
namespace KupShop\RestrictionsBundle\Tests;
|
||||
|
||||
use KupShop\ContentBundle\View\ProductView;
|
||||
use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
|
||||
use KupShop\RestrictionsBundle\Utils\Restrictions;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class RestrictionsTest extends \DatabaseTestCase
|
||||
{
|
||||
use CartTestTrait;
|
||||
|
||||
/** @var Restrictions */
|
||||
private $restrictions;
|
||||
|
||||
/** @var ProductView */
|
||||
private $productView;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->restrictions = $this->get(Restrictions::class);
|
||||
$this->productView = $this->get(ProductView::class);
|
||||
}
|
||||
|
||||
public function getDataSet()
|
||||
{
|
||||
// Omezení Nepřihlášený uživatel - restricted product ID=4
|
||||
// Omezení wpj (id_user = 1,2,3) - restricted product ID=9
|
||||
// Omezení wpjwpj (id_user != 2) - restricted product ID=11
|
||||
|
||||
return $this->getJsonDataSet('
|
||||
{
|
||||
"restrictions": [
|
||||
{
|
||||
"id":3,
|
||||
"name":"Omezení Nepřihlášený uživatel",
|
||||
"domain":4,
|
||||
"domain_invert":"N",
|
||||
"object_value":"{\"4\":[\"4\"]}"
|
||||
},
|
||||
{
|
||||
"id":5,
|
||||
"name":"Omezení wpj",
|
||||
"domain":2,
|
||||
"domain_invert":"N",
|
||||
"object_value":"{\"4\":[\"9\"]}"
|
||||
},
|
||||
{
|
||||
"id":6,
|
||||
"name":"Omezení wpjwpj",
|
||||
"domain":2,
|
||||
"domain_invert":"Y",
|
||||
"object_value":"{\"4\":[\"11\"]}"
|
||||
}
|
||||
],
|
||||
"restrictions_domains": [
|
||||
{
|
||||
"id":210,
|
||||
"id_restriction":3,
|
||||
"id_user":null,
|
||||
"country":null,
|
||||
"login":"logOut",
|
||||
"id_price_level":null,
|
||||
"id_users_group":null
|
||||
},
|
||||
{
|
||||
"id":240,
|
||||
"id_restriction":5,
|
||||
"id_user":1,
|
||||
"country":null,
|
||||
"login":null,
|
||||
"id_price_level":null,
|
||||
"id_users_group":null
|
||||
},
|
||||
{
|
||||
"id":241,
|
||||
"id_restriction":5,
|
||||
"id_user":2,
|
||||
"country":null,
|
||||
"login":null,
|
||||
"id_price_level":null,
|
||||
"id_users_group":null
|
||||
},
|
||||
{
|
||||
"id":242,
|
||||
"id_restriction":5,
|
||||
"id_user":3,
|
||||
"country":null,
|
||||
"login":null,
|
||||
"id_price_level":null,
|
||||
"id_users_group":null
|
||||
},
|
||||
{
|
||||
"id":243,
|
||||
"id_restriction":6,
|
||||
"id_user":2,
|
||||
"country":null,
|
||||
"login":null,
|
||||
"id_price_level":null,
|
||||
"id_users_group":null
|
||||
}
|
||||
]
|
||||
}
|
||||
');
|
||||
}
|
||||
|
||||
/** @dataProvider data_testVisibleProduct1 */
|
||||
public function testVisibleProduct1($id_user, $restricted)
|
||||
{
|
||||
if ($id_user) {
|
||||
$this->loginUser($id_user);
|
||||
}
|
||||
|
||||
$this->productView->setProductId(1);
|
||||
|
||||
if ($restricted) {
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
$this->expectExceptionMessage('Product not found');
|
||||
$this->productView->checkProductExist();
|
||||
} else {
|
||||
$product = $this->productView->getProduct();
|
||||
$this->assertNotFalse($product);
|
||||
$this->assertInstanceOf(\Product::class, $product);
|
||||
$this->assertContains('NIKE Capri LACE Test Dlouheho Nazvu Produktu Test Dlouheho Produktu Tes', $product->title);
|
||||
}
|
||||
}
|
||||
|
||||
public function data_testVisibleProduct1()
|
||||
{
|
||||
// product ID=1 is visible for everyone
|
||||
|
||||
return [
|
||||
[1, false],
|
||||
[2, false],
|
||||
[3, false],
|
||||
[null, false],
|
||||
];
|
||||
}
|
||||
|
||||
/** @dataProvider data_RestrictedProduct4 */
|
||||
public function testRestrictedProduct4($id_user, $restricted)
|
||||
{
|
||||
if ($id_user) {
|
||||
$this->loginUser($id_user);
|
||||
}
|
||||
|
||||
$this->productView->setProductId(4);
|
||||
|
||||
if ($restricted) {
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
$this->expectExceptionMessage('Product not found');
|
||||
$this->productView->checkProductExist();
|
||||
} else {
|
||||
$product = $this->productView->getProduct();
|
||||
$this->assertNotFalse($product);
|
||||
$this->assertInstanceOf(\Product::class, $product);
|
||||
$this->assertContains('Columbia Lay D Down', $product->title);
|
||||
}
|
||||
}
|
||||
|
||||
public function data_RestrictedProduct4()
|
||||
{
|
||||
// Omezení Nepřihlášený uživatel:
|
||||
// product ID=4 is visible for logged in users only
|
||||
|
||||
return [
|
||||
[1, false],
|
||||
[2, false],
|
||||
[3, false],
|
||||
[null, true],
|
||||
];
|
||||
}
|
||||
|
||||
/** @dataProvider data_RestrictedProduct9 */
|
||||
public function testRestrictedProduct9($id_user, $restricted)
|
||||
{
|
||||
if ($id_user) {
|
||||
$this->loginUser($id_user);
|
||||
}
|
||||
|
||||
$this->productView->setProductId(9);
|
||||
|
||||
if ($restricted) {
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
$this->expectExceptionMessage('Product not found');
|
||||
$this->productView->checkProductExist();
|
||||
} else {
|
||||
$product = $this->productView->getProduct();
|
||||
$this->assertNotFalse($product);
|
||||
$this->assertInstanceOf(\Product::class, $product);
|
||||
$this->assertContains('DeWALT DCD780C2 aku vrtačka', $product->title);
|
||||
}
|
||||
}
|
||||
|
||||
public function data_RestrictedProduct9()
|
||||
{
|
||||
// Omezení wpj (id_user = 1,2,3):
|
||||
// product ID=9 is not visible for wpj users
|
||||
|
||||
return [
|
||||
[1, true],
|
||||
[2, true],
|
||||
[3, true],
|
||||
[null, false],
|
||||
];
|
||||
}
|
||||
|
||||
/** @dataProvider data_RestrictedProduct11 */
|
||||
public function testRestrictedProduct11($id_user, $restricted)
|
||||
{
|
||||
if ($id_user) {
|
||||
$this->loginUser($id_user);
|
||||
}
|
||||
|
||||
$this->productView->setProductId(11);
|
||||
|
||||
if ($restricted) {
|
||||
$this->expectException(NotFoundHttpException::class);
|
||||
$this->expectExceptionMessage('Product not found');
|
||||
$this->productView->checkProductExist();
|
||||
} else {
|
||||
$product = $this->productView->getProduct();
|
||||
$this->assertNotFalse($product);
|
||||
$this->assertInstanceOf(\Product::class, $product);
|
||||
$this->assertContains('iPhone wpj', $product->title);
|
||||
}
|
||||
}
|
||||
|
||||
public function data_RestrictedProduct11()
|
||||
{
|
||||
// Omezení wpjwpj (id_user != 2):
|
||||
// product ID=11 is visible for user wpj@wpj.cz only
|
||||
|
||||
return [
|
||||
[1, true],
|
||||
[2, false],
|
||||
[3, true],
|
||||
[null, true],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user