multiple root categories fix
This commit is contained in:
parent
a50905781a
commit
fcb5cdec46
|
|
@ -54,6 +54,18 @@ class Category extends TranslatableModel implements CategoryContract
|
||||||
return $this->belongsToMany(AttributeProxy::modelClass(), 'category_filterable_attributes')->with('options');
|
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
|
* Returns all categories within the category's path
|
||||||
*
|
*
|
||||||
|
|
@ -70,7 +82,6 @@ class Category extends TranslatableModel implements CategoryContract
|
||||||
$categories[] = $category;
|
$categories[] = $category;
|
||||||
}
|
}
|
||||||
|
|
||||||
array_pop($categories);
|
|
||||||
return array_reverse($categories);
|
return array_reverse($categories);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,7 +95,7 @@ class Category extends TranslatableModel implements CategoryContract
|
||||||
private function findInTree($categoryTree = null): Category
|
private function findInTree($categoryTree = null): Category
|
||||||
{
|
{
|
||||||
if (! $categoryTree) {
|
if (! $categoryTree) {
|
||||||
$rootCategoryId = core()->getCurrentChannel()->root_category_id;
|
$rootCategoryId = core()->getCurrentChannel()->rootCategory->id;
|
||||||
$categoryTree = app(CategoryRepository::class)->getVisibleCategoryTree($rootCategoryId);
|
$categoryTree = app(CategoryRepository::class)->getVisibleCategoryTree($rootCategoryId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,8 @@ class CategoryCest
|
||||||
/** @var Locale $localeEn */
|
/** @var Locale $localeEn */
|
||||||
private $localeEn;
|
private $localeEn;
|
||||||
|
|
||||||
|
/** @var Category $rootCategory */
|
||||||
|
private $rootCategory;
|
||||||
/** @var Category $category */
|
/** @var Category $category */
|
||||||
private $category;
|
private $category;
|
||||||
/** @var Category $childCategory */
|
/** @var Category $childCategory */
|
||||||
|
|
@ -22,6 +24,7 @@ class CategoryCest
|
||||||
/** @var Category $grandChildCategory */
|
/** @var Category $grandChildCategory */
|
||||||
private $grandChildCategory;
|
private $grandChildCategory;
|
||||||
|
|
||||||
|
private $rootCategoryAttributes;
|
||||||
private $categoryAttributes;
|
private $categoryAttributes;
|
||||||
private $childCategoryAttributes;
|
private $childCategoryAttributes;
|
||||||
private $grandChildCategoryAttributes;
|
private $grandChildCategoryAttributes;
|
||||||
|
|
@ -34,8 +37,29 @@ class CategoryCest
|
||||||
'code' => 'en',
|
'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 = [
|
$this->categoryAttributes = [
|
||||||
'parent_id' => 1,
|
'parent_id' => $this->rootCategory->id,
|
||||||
'position' => 0,
|
'position' => 0,
|
||||||
'status' => 1,
|
'status' => 1,
|
||||||
$this->localeEn->code => [
|
$this->localeEn->code => [
|
||||||
|
|
@ -142,15 +166,25 @@ class CategoryCest
|
||||||
$I->assertEquals($expectedUrlPath, $this->grandChildCategory->url_path);
|
$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)
|
public function testGetPathCategories(UnitTester $I)
|
||||||
{
|
{
|
||||||
$I->wantTo('test getPathCategories is returning the correct categories in the correct order');
|
$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');
|
$I->amGoingTo('get all categories wihin the path of the grand child category');
|
||||||
$pathCategories = $this->grandChildCategory->getPathCategories();
|
$pathCategories = $this->grandChildCategory->getPathCategories();
|
||||||
|
|
||||||
$I->assertCount(3, $pathCategories);
|
$I->assertCount(4, $pathCategories);
|
||||||
$I->assertEquals($pathCategories[0]->id, $this->category->id);
|
$I->assertEquals($pathCategories[0]->id, $this->rootCategory->id);
|
||||||
$I->assertEquals($pathCategories[1]->id, $this->childCategory->id);
|
$I->assertEquals($pathCategories[1]->id, $this->category->id);
|
||||||
$I->assertEquals($pathCategories[2]->id, $this->grandChildCategory->id);
|
$I->assertEquals($pathCategories[2]->id, $this->childCategory->id);
|
||||||
|
$I->assertEquals($pathCategories[3]->id, $this->grandChildCategory->id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue