From fcb5cdec468b8631c95d0e858b5ec38b0f6baa44 Mon Sep 17 00:00:00 2001 From: peternuernberger Date: Mon, 6 Jan 2020 16:26:20 +0100 Subject: [PATCH] multiple root categories fix --- .../Webkul/Category/src/Models/Category.php | 15 ++++++- tests/unit/Category/CategoryCest.php | 44 ++++++++++++++++--- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/packages/Webkul/Category/src/Models/Category.php b/packages/Webkul/Category/src/Models/Category.php index 3c11b04f1..82224a1e8 100755 --- a/packages/Webkul/Category/src/Models/Category.php +++ b/packages/Webkul/Category/src/Models/Category.php @@ -54,6 +54,18 @@ class Category extends TranslatableModel implements CategoryContract return $this->belongsToMany(AttributeProxy::modelClass(), 'category_filterable_attributes')->with('options'); } + /** + * + */ + public function rootCategory(): Category + { + return $this->hasOne(Category::modelClass()) + ->where('parent_id', null) + ->andWhere('_lft', '<=', $this->_lft) + ->andWhere('_rgt', '>=', $this->_rgt) + ->first(); + } + /** * Returns all categories within the category's path * @@ -70,7 +82,6 @@ class Category extends TranslatableModel implements CategoryContract $categories[] = $category; } - array_pop($categories); return array_reverse($categories); } @@ -84,7 +95,7 @@ class Category extends TranslatableModel implements CategoryContract private function findInTree($categoryTree = null): Category { if (! $categoryTree) { - $rootCategoryId = core()->getCurrentChannel()->root_category_id; + $rootCategoryId = core()->getCurrentChannel()->rootCategory->id; $categoryTree = app(CategoryRepository::class)->getVisibleCategoryTree($rootCategoryId); } diff --git a/tests/unit/Category/CategoryCest.php b/tests/unit/Category/CategoryCest.php index ca611885b..bf415e95d 100644 --- a/tests/unit/Category/CategoryCest.php +++ b/tests/unit/Category/CategoryCest.php @@ -15,6 +15,8 @@ class CategoryCest /** @var Locale $localeEn */ private $localeEn; + /** @var Category $rootCategory */ + private $rootCategory; /** @var Category $category */ private $category; /** @var Category $childCategory */ @@ -22,6 +24,7 @@ class CategoryCest /** @var Category $grandChildCategory */ private $grandChildCategory; + private $rootCategoryAttributes; private $categoryAttributes; private $childCategoryAttributes; private $grandChildCategoryAttributes; @@ -34,8 +37,29 @@ 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, + ]); + $this->categoryAttributes = [ - 'parent_id' => 1, + 'parent_id' => $this->rootCategory->id, 'position' => 0, 'status' => 1, $this->localeEn->code => [ @@ -142,15 +166,25 @@ class CategoryCest $I->assertEquals($expectedUrlPath, $this->grandChildCategory->url_path); } + public function testGetRootCategory(UnitTester $I) + { + $I->wantTo('test rootCategory attribute of a category'); + $rootCategory = $this->grandChildCategory->rootCategory; + + $I->assertNotNull($rootCategory); + $I->assertEquals($rootCategory->id, $this->rootCategory->id); + } + public function testGetPathCategories(UnitTester $I) { $I->wantTo('test getPathCategories is returning the correct categories in the correct order'); $I->amGoingTo('get all categories wihin the path of the grand child category'); $pathCategories = $this->grandChildCategory->getPathCategories(); - $I->assertCount(3, $pathCategories); - $I->assertEquals($pathCategories[0]->id, $this->category->id); - $I->assertEquals($pathCategories[1]->id, $this->childCategory->id); - $I->assertEquals($pathCategories[2]->id, $this->grandChildCategory->id); + $I->assertCount(4, $pathCategories); + $I->assertEquals($pathCategories[0]->id, $this->rootCategory->id); + $I->assertEquals($pathCategories[1]->id, $this->category->id); + $I->assertEquals($pathCategories[2]->id, $this->childCategory->id); + $I->assertEquals($pathCategories[3]->id, $this->grandChildCategory->id); } }