diff --git a/packages/Webkul/Category/src/Database/Factories/CategoryFactory.php b/packages/Webkul/Category/src/Database/Factories/CategoryFactory.php index 941601997..359cae23c 100644 --- a/packages/Webkul/Category/src/Database/Factories/CategoryFactory.php +++ b/packages/Webkul/Category/src/Database/Factories/CategoryFactory.php @@ -1,10 +1,9 @@ define(Category::class, function (Faker $faker, array $attributes) { return [ diff --git a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194608_add_stored_function_to_get_url_path_of_category.php b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194608_add_stored_function_to_get_url_path_of_category.php index 72d995819..4abcaa192 100644 --- a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194608_add_stored_function_to_get_url_path_of_category.php +++ b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194608_add_stored_function_to_get_url_path_of_category.php @@ -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 = ''; diff --git a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194627_add_trigger_to_category_translations.php b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194627_add_trigger_to_category_translations.php index b62a6133f..786be8f9f 100644 --- a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194627_add_trigger_to_category_translations.php +++ b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194627_add_trigger_to_category_translations.php @@ -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; diff --git a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194703_add_trigger_to_categories.php b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194703_add_trigger_to_categories.php index f58739d75..6445f8eed 100644 --- a/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194703_add_trigger_to_categories.php +++ b/packages/Webkul/Shop/src/Database/Migrations/2019_11_21_194703_add_trigger_to_categories.php @@ -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; } } diff --git a/tests/trigger/Shop/DatabaseLogicCest.php b/tests/trigger/Shop/DatabaseLogicCest.php index 3899c4fc3..4bee0507f 100644 --- a/tests/trigger/Shop/DatabaseLogicCest.php +++ b/tests/trigger/Shop/DatabaseLogicCest.php @@ -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); } } diff --git a/tests/trigger/Shop/TriggerCest.php b/tests/trigger/Shop/TriggerCest.php index 89bdc542e..e4ec06e1e 100644 --- a/tests/trigger/Shop/TriggerCest.php +++ b/tests/trigger/Shop/TriggerCest.php @@ -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) diff --git a/tests/unit/Category/CategoryCest.php b/tests/unit/Category/CategoryCest.php index c3c2779b0..b45f90c70 100644 --- a/tests/unit/Category/CategoryCest.php +++ b/tests/unit/Category/CategoryCest.php @@ -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;