Merge remote-tracking branch 'bagisto-hb/category-url-structure-multiple-roots' into 18472-bugfix/add-category-url-path
This commit is contained in:
commit
6f4b7474cb
|
|
@ -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 [
|
||||
|
|
|
|||
|
|
@ -0,0 +1,111 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class AlterTriggerCategoryTranslations extends Migration
|
||||
{
|
||||
private const TRIGGER_NAME_INSERT = 'trig_category_translations_insert';
|
||||
private const TRIGGER_NAME_UPDATE = 'trig_category_translations_update';
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$triggerBody = $this->getTriggerBody();
|
||||
$insertTrigger = <<< SQL
|
||||
CREATE TRIGGER %s
|
||||
BEFORE INSERT ON category_translations
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
$triggerBody
|
||||
END;
|
||||
SQL;
|
||||
|
||||
$updateTrigger = <<< SQL
|
||||
CREATE TRIGGER %s
|
||||
BEFORE UPDATE ON category_translations
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
$triggerBody
|
||||
END;
|
||||
SQL;
|
||||
|
||||
$this->dropTriggers();
|
||||
|
||||
DB::unprepared(sprintf($insertTrigger, self::TRIGGER_NAME_INSERT));
|
||||
DB::unprepared(sprintf($updateTrigger, self::TRIGGER_NAME_UPDATE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$this->dropTriggers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the triggers
|
||||
*/
|
||||
private function dropTriggers()
|
||||
{
|
||||
DB::unprepared(sprintf('DROP TRIGGER IF EXISTS %s;', self::TRIGGER_NAME_INSERT));
|
||||
DB::unprepared(sprintf('DROP TRIGGER IF EXISTS %s;', self::TRIGGER_NAME_UPDATE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns trigger body as string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getTriggerBody()
|
||||
{
|
||||
return <<<SQL
|
||||
DECLARE parentUrlPath varchar(255);
|
||||
DECLARE urlPath varchar(255);
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT id
|
||||
FROM categories
|
||||
WHERE
|
||||
id = NEW.category_id
|
||||
AND parent_id IS NULL
|
||||
)
|
||||
THEN
|
||||
|
||||
SELECT
|
||||
GROUP_CONCAT(parent_translations.slug SEPARATOR '/') INTO parentUrlPath
|
||||
FROM
|
||||
categories AS node,
|
||||
categories AS parent
|
||||
JOIN category_translations AS parent_translations ON parent.id = parent_translations.category_id
|
||||
WHERE
|
||||
node._lft >= parent._lft
|
||||
AND node._rgt <= parent._rgt
|
||||
AND node.id = (SELECT parent_id FROM categories WHERE id = NEW.category_id)
|
||||
AND node.parent_id IS NOT NULL
|
||||
AND parent.parent_id IS NOT NULL
|
||||
AND parent_translations.locale = NEW.locale
|
||||
GROUP BY
|
||||
node.id;
|
||||
|
||||
IF parentUrlPath IS NULL
|
||||
THEN
|
||||
SET urlPath = NEW.slug;
|
||||
ELSE
|
||||
SET urlPath = concat(parentUrlPath, '/', NEW.slug);
|
||||
END IF;
|
||||
|
||||
SET NEW.url_path = urlPath;
|
||||
|
||||
END IF;
|
||||
SQL;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class AlterStoredFunctionUrlPathCategory extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$functionSQL = <<< SQL
|
||||
DROP FUNCTION IF EXISTS `get_url_path_of_category`;
|
||||
CREATE FUNCTION get_url_path_of_category(
|
||||
categoryId INT,
|
||||
localeCode VARCHAR(255)
|
||||
)
|
||||
RETURNS VARCHAR(255)
|
||||
DETERMINISTIC
|
||||
BEGIN
|
||||
|
||||
DECLARE urlPath VARCHAR(255);
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT id
|
||||
FROM categories
|
||||
WHERE
|
||||
id = categoryId
|
||||
AND parent_id IS NULL
|
||||
)
|
||||
THEN
|
||||
SELECT
|
||||
GROUP_CONCAT(parent_translations.slug SEPARATOR '/') INTO urlPath
|
||||
FROM
|
||||
categories AS node,
|
||||
categories AS parent
|
||||
JOIN category_translations AS parent_translations ON parent.id = parent_translations.category_id
|
||||
WHERE
|
||||
node._lft >= parent._lft
|
||||
AND node._rgt <= parent._rgt
|
||||
AND node.id = categoryId
|
||||
AND node.parent_id IS NOT NULL
|
||||
AND parent.parent_id IS NOT NULL
|
||||
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);
|
||||
END IF;
|
||||
ELSE
|
||||
SET urlPath = '';
|
||||
END IF;
|
||||
|
||||
RETURN urlPath;
|
||||
END;
|
||||
SQL;
|
||||
|
||||
DB::unprepared($functionSQL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
DB::unprepared('DROP FUNCTION IF EXISTS `get_url_path_of_category`;');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class AlterTriggerOnCategories extends Migration
|
||||
{
|
||||
private const TRIGGER_NAME_INSERT = 'trig_categories_insert';
|
||||
private const TRIGGER_NAME_UPDATE = 'trig_categories_update';
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$triggerBody = $this->getTriggerBody();
|
||||
|
||||
$insertTrigger = <<< SQL
|
||||
CREATE TRIGGER %s
|
||||
AFTER INSERT ON categories
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
$triggerBody
|
||||
END;
|
||||
SQL;
|
||||
|
||||
$updateTrigger = <<< SQL
|
||||
CREATE TRIGGER %s
|
||||
AFTER UPDATE ON categories
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
$triggerBody
|
||||
END;
|
||||
SQL;
|
||||
|
||||
$this->dropTriggers();
|
||||
|
||||
DB::unprepared(sprintf($insertTrigger, self::TRIGGER_NAME_INSERT));
|
||||
|
||||
DB::unprepared(sprintf($updateTrigger, self::TRIGGER_NAME_UPDATE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$this->dropTriggers();
|
||||
}
|
||||
|
||||
private function dropTriggers()
|
||||
{
|
||||
DB::unprepared(sprintf('DROP TRIGGER IF EXISTS %s;', self::TRIGGER_NAME_INSERT));
|
||||
DB::unprepared(sprintf('DROP TRIGGER IF EXISTS %s;', self::TRIGGER_NAME_UPDATE));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns trigger body as string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getTriggerBody(): string
|
||||
{
|
||||
return <<< SQL
|
||||
DECLARE urlPath VARCHAR(255);
|
||||
DECLARE localeCode VARCHAR(255);
|
||||
DECLARE done INT;
|
||||
DECLARE curs CURSOR FOR (SELECT category_translations.locale
|
||||
FROM category_translations
|
||||
WHERE category_id = NEW.id);
|
||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
|
||||
|
||||
|
||||
IF EXISTS (
|
||||
SELECT *
|
||||
FROM category_translations
|
||||
WHERE category_id = NEW.id
|
||||
)
|
||||
THEN
|
||||
|
||||
OPEN curs;
|
||||
|
||||
SET done = 0;
|
||||
REPEAT
|
||||
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
|
||||
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,27 @@ 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();
|
||||
|
||||
$I->assertNull($root2Category->refresh()->parent_id);
|
||||
|
||||
$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,25 @@ class TriggerCest
|
|||
'locale' => $this->localeDe->code,
|
||||
'url_path' => strtolower($this->parentCategoryName) . '/' . strtolower($this->categoryName)
|
||||
]);
|
||||
$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)
|
||||
|
|
|
|||
|
|
@ -37,29 +37,16 @@ class CategoryCest
|
|||
'code' => 'en',
|
||||
]);
|
||||
|
||||
$this->rootCategoryAttributes = [
|
||||
'parent_id' => null,
|
||||
'position' => 0,
|
||||
'status' => 1,
|
||||
$this->localeEn->code => [
|
||||
'name' => $this->faker->word,
|
||||
'slug' => $this->faker->slug,
|
||||
'description' => $this->faker->sentence(),
|
||||
'locale_id' => $this->localeEn->id,
|
||||
],
|
||||
];
|
||||
|
||||
$this->rootCategory = $I->have(Category::class, $this->rootCategoryAttributes);
|
||||
$I->assertNotNull($this->rootCategory);
|
||||
|
||||
$I->seeRecord(CategoryTranslation::class, [
|
||||
'category_id' => $this->rootCategory->id,
|
||||
'locale' => $this->localeEn->code,
|
||||
'url_path' => null,
|
||||
$rootCategoryTranslation = $I->grabRecord(CategoryTranslation::class, [
|
||||
'slug' => 'root',
|
||||
'locale' => 'en',
|
||||
]);
|
||||
$rootCategory = $I->grabRecord(Category::class, [
|
||||
'id' => $rootCategoryTranslation->category_id,
|
||||
]);
|
||||
|
||||
$this->categoryAttributes = [
|
||||
'parent_id' => $this->rootCategory->id,
|
||||
'parent_id' => $rootCategory->id,
|
||||
'position' => 0,
|
||||
'status' => 1,
|
||||
$this->localeEn->code => [
|
||||
|
|
@ -70,7 +57,8 @@ 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);
|
||||
|
||||
$I->seeRecord(CategoryTranslation::class, [
|
||||
|
|
@ -90,7 +78,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;
|
||||
|
|
@ -111,7 +100,8 @@ class CategoryCest
|
|||
'locale_id' => $this->localeEn->id,
|
||||
],
|
||||
];
|
||||
$this->grandChildCategory = $I->have(Category::class, $this->grandChildCategoryAttributes);
|
||||
$this->grandChildCategory = $I->make(Category::class, $this->grandChildCategoryAttributes)->first();
|
||||
$this->childCategory->prependNode($this->grandChildCategory);
|
||||
$I->assertNotNull($this->grandChildCategory);
|
||||
|
||||
$expectedUrlPath .= '/' . $this->grandChildCategory->slug;
|
||||
|
|
|
|||
Loading…
Reference in New Issue