altered trigger to be compatible with multi root categories environment; tests still WIP
This commit is contained in:
parent
a22b98129e
commit
f4499b4728
|
|
@ -1,10 +1,9 @@
|
|||
<?php
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||
|
||||
use Faker\Generator as Faker;
|
||||
use Webkul\Category\Models\Category;
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||
$factory->define(Category::class, function (Faker $faker, array $attributes) {
|
||||
|
||||
return [
|
||||
|
|
|
|||
|
|
@ -23,8 +23,9 @@ class AddStoredFunctionToGetUrlPathOfCategory extends Migration
|
|||
BEGIN
|
||||
|
||||
DECLARE urlPath VARCHAR(255);
|
||||
-- Category with id 1 is root by default
|
||||
IF categoryId <> 1
|
||||
CREATE TEMPORARY TABLE IF NOT EXISTS root_categories AS (SELECT id FROM categories where parent_id IS NULL);
|
||||
|
||||
IF categoryId NOT IN (SELECT id FROM root_categories)
|
||||
THEN
|
||||
SELECT
|
||||
GROUP_CONCAT(parent_translations.slug SEPARATOR '/') INTO urlPath
|
||||
|
|
@ -36,14 +37,20 @@ class AddStoredFunctionToGetUrlPathOfCategory extends Migration
|
|||
node._lft >= parent._lft
|
||||
AND node._rgt <= parent._rgt
|
||||
AND node.id = categoryId
|
||||
AND parent.id <> 1
|
||||
AND parent.id NOT IN (SELECT id FROM root_categories)
|
||||
AND parent_translations.locale = localeCode
|
||||
GROUP BY
|
||||
node.id;
|
||||
|
||||
IF urlPath IS NULL
|
||||
THEN
|
||||
SET urlPath = (SELECT slug FROM category_translations WHERE category_translations.category_id = categoryId);
|
||||
THEN
|
||||
SET urlPath = (
|
||||
SELECT slug
|
||||
FROM category_translations
|
||||
WHERE
|
||||
category_translations.category_id = categoryId
|
||||
AND category_translations.locale = localeCode
|
||||
);
|
||||
END IF;
|
||||
ELSE
|
||||
SET urlPath = '';
|
||||
|
|
|
|||
|
|
@ -63,8 +63,9 @@ SQL;
|
|||
DECLARE parentUrlPath varchar(255);
|
||||
DECLARE urlPath varchar(255);
|
||||
|
||||
-- Category with id 1 is root by default
|
||||
IF NEW.category_id <> 1
|
||||
CREATE TEMPORARY TABLE IF NOT EXISTS root_categories AS (SELECT id FROM categories where parent_id IS NULL);
|
||||
|
||||
IF NEW.category_id NOT IN (SELECT id FROM root_categories)
|
||||
THEN
|
||||
|
||||
SELECT
|
||||
|
|
@ -77,7 +78,8 @@ SQL;
|
|||
node._lft >= parent._lft
|
||||
AND node._rgt <= parent._rgt
|
||||
AND node.id = (SELECT parent_id FROM categories WHERE id = NEW.category_id)
|
||||
AND parent.id <> 1
|
||||
AND node.parent_id IS NOT NULL
|
||||
AND parent.parent_id IS NOT NULL
|
||||
AND parent_translations.locale = NEW.locale
|
||||
GROUP BY
|
||||
node.id;
|
||||
|
|
|
|||
|
|
@ -69,7 +69,6 @@ SQL;
|
|||
WHERE category_id = NEW.id);
|
||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
|
||||
|
||||
|
||||
IF EXISTS (
|
||||
SELECT *
|
||||
FROM category_translations
|
||||
|
|
@ -79,21 +78,29 @@ SQL;
|
|||
|
||||
OPEN curs;
|
||||
|
||||
SET done = 0;
|
||||
SET done = 0;
|
||||
REPEAT
|
||||
FETCH curs INTO localeCode;
|
||||
FETCH curs INTO localeCode;
|
||||
|
||||
SELECT get_url_path_of_category(NEW.id, localeCode) INTO urlPath;
|
||||
|
||||
IF NEW.parent_id IS NULL
|
||||
THEN
|
||||
SET urlPath = '';
|
||||
END IF;
|
||||
|
||||
UPDATE category_translations
|
||||
SET url_path = urlPath
|
||||
WHERE category_translations.category_id = NEW.id;
|
||||
SET url_path = urlPath
|
||||
WHERE
|
||||
category_translations.category_id = NEW.id
|
||||
AND category_translations.locale = localeCode;
|
||||
|
||||
UNTIL done END REPEAT;
|
||||
|
||||
CLOSE curs;
|
||||
|
||||
END IF;
|
||||
|
||||
SQL;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use UnitTester;
|
|||
use Webkul\Category\Models\Category;
|
||||
use Faker\Factory;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Webkul\Category\Models\CategoryTranslation;
|
||||
use Webkul\Core\Models\Locale;
|
||||
|
||||
class DatabaseLogicCest
|
||||
|
|
@ -35,10 +36,18 @@ class DatabaseLogicCest
|
|||
|
||||
public function testGetUrlPathOfCategory(UnitTester $I)
|
||||
{
|
||||
$rootCategoryTranslation = $I->grabRecord(CategoryTranslation::class, [
|
||||
'slug' => 'root',
|
||||
'locale' => 'en',
|
||||
]);
|
||||
$rootCategory = $I->grabRecord(Category::class, [
|
||||
'id' => $rootCategoryTranslation->category_id,
|
||||
]);
|
||||
|
||||
$parentCategoryName = $this->faker->word;
|
||||
|
||||
$parentCategoryAttributes = [
|
||||
'parent_id' => 1,
|
||||
'parent_id' => $rootCategory->id,
|
||||
'position' => 1,
|
||||
'status' => 1,
|
||||
$this->localeEn->code => [
|
||||
|
|
@ -55,7 +64,8 @@ class DatabaseLogicCest
|
|||
],
|
||||
];
|
||||
|
||||
$parentCategory = $I->have(Category::class, $parentCategoryAttributes);
|
||||
$parentCategory = $I->make(Category::class, $parentCategoryAttributes)->first();
|
||||
$rootCategory->prependNode($parentCategory);
|
||||
$I->assertNotNull($parentCategory);
|
||||
|
||||
$categoryName = $this->faker->word;
|
||||
|
|
@ -77,7 +87,8 @@ class DatabaseLogicCest
|
|||
],
|
||||
];
|
||||
|
||||
$category = $I->have(Category::class, $categoryAttributes);
|
||||
$category = $I->make(Category::class, $categoryAttributes)->first();
|
||||
$parentCategory->prependNode($category);
|
||||
$I->assertNotNull($category);
|
||||
|
||||
$sqlStoredFunction = 'SELECT get_url_path_of_category(:category_id, :locale_code) AS url_path;';
|
||||
|
|
@ -97,5 +108,25 @@ class DatabaseLogicCest
|
|||
|
||||
$expectedUrlPath = strtolower($parentCategoryName) . '/' . strtolower($categoryName);
|
||||
$I->assertEquals($expectedUrlPath, $urlPathQueryResult->url_path);
|
||||
|
||||
$root2Category = $I->make(Category::class, [
|
||||
'position' => 1,
|
||||
'status' => 1,
|
||||
'parent_id' => null,
|
||||
$this->localeEn->code => [
|
||||
'name' => $this->faker->word,
|
||||
'slug' => strtolower($this->faker->word),
|
||||
'description' => $this->faker->word,
|
||||
'locale_id' => $this->localeEn->id,
|
||||
],
|
||||
])->first();
|
||||
$root2Category->save();
|
||||
|
||||
$urlPathQueryResult = DB::selectOne($sqlStoredFunction, [
|
||||
'category_id' => $root2Category->id,
|
||||
'locale_code' => $this->localeEn->code,
|
||||
]);
|
||||
$I->assertNotNull($urlPathQueryResult->url_path);
|
||||
$I->assertEquals('', $urlPathQueryResult->url_path);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,12 +14,19 @@ class TriggerCest
|
|||
|
||||
private $parentCategory;
|
||||
private $category;
|
||||
private $root2Category;
|
||||
private $childOfRoot2Category;
|
||||
|
||||
private $parentCategoryAttributes;
|
||||
private $categoryAttributes;
|
||||
private $root2CategoryAttributes;
|
||||
private $childOfRoot2CategoryAttributes;
|
||||
|
||||
private $parentCategoryName;
|
||||
private $categoryName;
|
||||
private $root2CategoryName;
|
||||
private $childOfRoot2CategoryName;
|
||||
|
||||
|
||||
/** @var Locale $localeEn */
|
||||
private $localeEn;
|
||||
|
|
@ -30,8 +37,18 @@ class TriggerCest
|
|||
{
|
||||
$this->faker = Factory::create();
|
||||
|
||||
$rootCategoryTranslation = $I->grabRecord(CategoryTranslation::class, [
|
||||
'slug' => 'root',
|
||||
'locale' => 'en',
|
||||
]);
|
||||
$rootCategory = $I->grabRecord(Category::class, [
|
||||
'id' => $rootCategoryTranslation->category_id,
|
||||
]);
|
||||
|
||||
$this->parentCategoryName = $this->faker->word;
|
||||
$this->categoryName = $this->faker->word . $this->faker->randomDigit;
|
||||
$this->root2CategoryName = $this->faker->word . $this->faker->randomDigit;
|
||||
$this->childOfRoot2CategoryName = $this->faker->word . $this->faker->randomDigit;
|
||||
|
||||
$this->localeEn = $I->grabRecord(Locale::class, [
|
||||
'code' => 'en',
|
||||
|
|
@ -42,7 +59,7 @@ class TriggerCest
|
|||
]);
|
||||
|
||||
$this->parentCategoryAttributes = [
|
||||
'parent_id' => 1,
|
||||
'parent_id' => $rootCategory->id,
|
||||
'position' => 1,
|
||||
'status' => 1,
|
||||
$this->localeEn->code => [
|
||||
|
|
@ -59,7 +76,8 @@ class TriggerCest
|
|||
],
|
||||
];
|
||||
|
||||
$this->parentCategory = $I->have(Category::class, $this->parentCategoryAttributes);
|
||||
$this->parentCategory = $I->make(Category::class, $this->parentCategoryAttributes)->first();
|
||||
$rootCategory->appendNode($this->parentCategory);
|
||||
$I->assertNotNull($this->parentCategory);
|
||||
|
||||
$this->categoryAttributes = [
|
||||
|
|
@ -80,8 +98,58 @@ class TriggerCest
|
|||
],
|
||||
];
|
||||
|
||||
$this->category = $I->have(Category::class, $this->categoryAttributes);
|
||||
$this->category = $I->make(Category::class, $this->categoryAttributes)->first();
|
||||
$this->parentCategory->appendNode($this->category);
|
||||
$I->assertNotNull($this->category);
|
||||
|
||||
|
||||
$this->root2CategoryAttributes = [
|
||||
'position' => 1,
|
||||
'status' => 1,
|
||||
'parent_id' => null,
|
||||
$this->localeEn->code => [
|
||||
'name' => $this->root2CategoryName,
|
||||
'slug' => strtolower($this->root2CategoryName),
|
||||
'description' => $this->root2CategoryName,
|
||||
'locale_id' => $this->localeEn->id,
|
||||
],
|
||||
$this->localeDe->code => [
|
||||
'name' => $this->root2CategoryName,
|
||||
'slug' => strtolower($this->root2CategoryName),
|
||||
'description' => $this->root2CategoryName,
|
||||
'locale_id' => $this->localeDe->id,
|
||||
],
|
||||
];
|
||||
|
||||
$this->root2Category = $I->make(Category::class, $this->root2CategoryAttributes)->first();
|
||||
$this->root2Category->save();
|
||||
|
||||
$I->assertNotNull($this->root2Category);
|
||||
$I->assertNull($this->root2Category->parent_id);
|
||||
$I->assertGreaterThan($rootCategory->_rgt, $this->root2Category->_lft);
|
||||
|
||||
$this->childOfRoot2CategoryAttributes = [
|
||||
'position' => 1,
|
||||
'status' => 1,
|
||||
'parent_id' => $this->root2Category->id,
|
||||
$this->localeEn->code => [
|
||||
'name' => $this->childOfRoot2CategoryName,
|
||||
'slug' => strtolower($this->childOfRoot2CategoryName),
|
||||
'description' => $this->childOfRoot2CategoryName,
|
||||
'locale_id' => $this->localeEn->id,
|
||||
],
|
||||
$this->localeDe->code => [
|
||||
'name' => $this->childOfRoot2CategoryName,
|
||||
'slug' => strtolower($this->childOfRoot2CategoryName),
|
||||
'description' => $this->childOfRoot2CategoryName,
|
||||
'locale_id' => $this->localeDe->id,
|
||||
],
|
||||
];
|
||||
|
||||
$this->childOfRoot2Category = $I->make(Category::class, $this->childOfRoot2CategoryAttributes)->first();
|
||||
$this->root2Category->appendNode($this->childOfRoot2Category);
|
||||
|
||||
$I->assertNotNull($this->childOfRoot2Category);
|
||||
}
|
||||
|
||||
public function testInsertTriggerOnCategoryTranslationsTable(UnitTester $I)
|
||||
|
|
@ -111,6 +179,26 @@ class TriggerCest
|
|||
'locale' => $this->localeDe->code,
|
||||
'url_path' => strtolower($this->parentCategoryName) . '/' . strtolower($this->categoryName)
|
||||
]);
|
||||
//eval(\psy\sh()); // TODO REMOVE
|
||||
$I->seeRecord(CategoryTranslation::class, [
|
||||
'category_id' => $this->root2Category->id,
|
||||
'name' => $this->root2CategoryName,
|
||||
'locale' => $this->localeEn->code,
|
||||
'url_path' => '',
|
||||
]);
|
||||
|
||||
$I->seeRecord(CategoryTranslation::class, [
|
||||
'category_id' => $this->childOfRoot2Category->id,
|
||||
'name' => $this->childOfRoot2CategoryName,
|
||||
'locale' => $this->localeDe->code,
|
||||
'url_path' => strtolower($this->childOfRoot2CategoryName)
|
||||
]);
|
||||
$I->seeRecord(CategoryTranslation::class, [
|
||||
'category_id' => $this->childOfRoot2Category->id,
|
||||
'name' => $this->childOfRoot2CategoryName,
|
||||
'locale' => $this->localeEn->code,
|
||||
'url_path' => strtolower($this->childOfRoot2CategoryName)
|
||||
]);
|
||||
}
|
||||
|
||||
public function testUpdateTriggersOnCategoryTranslationsTable(UnitTester $I)
|
||||
|
|
|
|||
|
|
@ -34,8 +34,16 @@ class CategoryCest
|
|||
'code' => 'en',
|
||||
]);
|
||||
|
||||
$rootCategoryTranslation = $I->grabRecord(CategoryTranslation::class, [
|
||||
'slug' => 'root',
|
||||
'locale' => 'en',
|
||||
]);
|
||||
$rootCategory = $I->grabRecord(Category::class, [
|
||||
'id' => $rootCategoryTranslation->category_id,
|
||||
]);
|
||||
|
||||
$this->categoryAttributes = [
|
||||
'parent_id' => 1,
|
||||
'parent_id' => $rootCategory->id,
|
||||
'position' => 0,
|
||||
'status' => 1,
|
||||
$this->localeEn->code => [
|
||||
|
|
@ -46,9 +54,9 @@ class CategoryCest
|
|||
],
|
||||
];
|
||||
|
||||
$this->category = $I->have(Category::class, $this->categoryAttributes);
|
||||
$this->category = $I->make(Category::class, $this->categoryAttributes)->first();
|
||||
$rootCategory->prependNode($this->category);
|
||||
$I->assertNotNull($this->category);
|
||||
//return true;
|
||||
|
||||
$I->seeRecord(CategoryTranslation::class, [
|
||||
'category_id' => $this->category->id,
|
||||
|
|
@ -67,7 +75,8 @@ class CategoryCest
|
|||
'locale_id' => $this->localeEn->id,
|
||||
],
|
||||
];
|
||||
$this->childCategory = $I->have(Category::class, $this->childCategoryAttributes);
|
||||
$this->childCategory = $I->make(Category::class, $this->childCategoryAttributes)->first();
|
||||
$this->category->prependNode($this->childCategory);
|
||||
$I->assertNotNull($this->childCategory);
|
||||
|
||||
$expectedUrlPath = $this->category->slug . '/' . $this->childCategory->slug;
|
||||
|
|
@ -88,7 +97,8 @@ class CategoryCest
|
|||
'locale_id' => $this->localeEn->id,
|
||||
],
|
||||
];
|
||||
$this->grandChildCategory = $I->have(Category::class, $this->grandChildCategoryAttributes);
|
||||
$this->grandChildCategory = $I->make(Category::class, $this->grandChildCategoryAttributes);
|
||||
$this->childCategory->prependNode($this->grandChildCategory);
|
||||
$I->assertNotNull($this->grandChildCategory);
|
||||
|
||||
$expectedUrlPath .= '/' . $this->grandChildCategory->slug;
|
||||
|
|
@ -104,7 +114,6 @@ class CategoryCest
|
|||
|
||||
public function testChildUrlPathIsUpdatedOnParentUpdate(UnitTester $I)
|
||||
{
|
||||
//return true;
|
||||
$newCategorySlug = $this->faker->slug;
|
||||
|
||||
$this->categoryAttributes[$this->localeEn->code]['slug'] = $newCategorySlug;
|
||||
|
|
|
|||
Loading…
Reference in New Issue