Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #22 from grayloon/ignore-non-root-categories
Browse files Browse the repository at this point in the history
Ignore and delete Categories if exists when global root category set
  • Loading branch information
ahinkle authored Apr 7, 2021
2 parents 8a4a38d + 228e56c commit 4f1f32d
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/Support/MagentoCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Grayloon\Magento\Magento;
use Grayloon\MagentoStorage\Jobs\DownloadMagentoCategoryImage;
use Grayloon\MagentoStorage\Models\MagentoCategory;
use Illuminate\Support\Str;

class MagentoCategories extends PaginatableMagentoService
{
Expand Down Expand Up @@ -37,9 +38,48 @@ public function updateCategories($categories)
}

foreach ($categories as $apiCategory) {
$this->updateCategory($apiCategory);
if ($this->validCategory($apiCategory)) {
$this->updateCategory($apiCategory);
}
}

return $this;
}

/**
* Determine if the category is valid.
*
* @param array $apiCategory
* @return bool
*/
protected function validCategory($apiCategory)
{
if (! env('MAGENTO_DEFAULT_CATEGORY')) {
return true;
}

if (env('MAGENTO_DEFAULT_CATEGORY') == $apiCategory['id']) {
return true;
}

if (Str::contains($apiCategory['path'], '/' . env('MAGENTO_DEFAULT_CATEGORY') . '/')) {
return true;
}

$this->deleteIfExists($apiCategory['id']);
return false;
}

/**
* Delete the category if it exists.
*
* @param int $id
* @return void
*/
protected function deleteIfExists($id)
{
MagentoCategory::where('id', $id)->delete();

return $this;
}

Expand Down
242 changes: 242 additions & 0 deletions tests/Support/MagentoCategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Grayloon\MagentoStorage\Tests\Support;

use Grayloon\MagentoStorage\Database\Factories\MagentoCategoryFactory;
use Grayloon\MagentoStorage\Database\Factories\MagentoCustomAttributeTypeFactory;
use Grayloon\MagentoStorage\Jobs\DownloadMagentoCategoryImage;
use Grayloon\MagentoStorage\Jobs\UpdateProductAttributeGroup;
Expand Down Expand Up @@ -274,4 +275,245 @@ public function test_can_download_category_image()
Queue::assertPushed(DownloadMagentoCategoryImage::class, fn ($job) => $job->path === 'pub/media/catalog/category/foo.jpg');
Queue::assertPushed(DownloadMagentoCategoryImage::class, fn ($job) => $job->category->id === $category->id);
}

/** @test */
public function it_deletes_old_category()
{
Queue::fake();
putenv('MAGENTO_DEFAULT_CATEGORY=3');

$category = MagentoCategoryFactory::new()->create([
'id' => 2,
]);

$categories = [
[
'id' => '2',
'parent_id' => 0,
'name' => 'Root Catalog',
'is_active' => true,
'position' => 0,
'level' => 0,
'children' => '2',
'created_at' => '2014-04-04 14:17:29',
'updated_at' => '2014-04-04 14:17:29',
'path' => '1',
'available_sort_by' => [],
'include_in_menu' => true,
'custom_attributes' => [
[
'attribute_code' => 'path',
'value' => '1',
],
[
'attribute_code' => 'url_path',
'value' => 'foo/bar',
],
[
'attribute_code' => 'children_count',
'value' => '124',
],
[
'attribute_code' => 'image',
'value' => 'pub/media/catalog/category/foo.jpg',
],
],
],
];

(new MagentoCategories())->updateCategories($categories);

$this->assertEquals(0, MagentoCategory::count());
$this->assertDeleted($category);
}

/** @test */
public function it_ignores_non_root_config_level_categories()
{
Queue::fake();

putenv('MAGENTO_DEFAULT_CATEGORY=3');

$categories = [
[
'id' => '1',
'parent_id' => 0,
'name' => 'Root Catalog',
'is_active' => true,
'position' => 0,
'level' => 0,
'children' => '2',
'created_at' => '2014-04-04 14:17:29',
'updated_at' => '2014-04-04 14:17:29',
'path' => '1',
'available_sort_by' => [],
'include_in_menu' => true,
'custom_attributes' => [
[
'attribute_code' => 'path',
'value' => '1',
],
[
'attribute_code' => 'url_path',
'value' => 'foo/bar',
],
[
'attribute_code' => 'children_count',
'value' => '124',
],
[
'attribute_code' => 'image',
'value' => 'pub/media/catalog/category/foo.jpg',
],
],
],
];

(new MagentoCategories())->updateCategories($categories);

$this->assertEquals(0, MagentoCategory::count());
}

/** @test */
public function it_ignores_category_without_root_path()
{
Queue::fake();

putenv('MAGENTO_DEFAULT_CATEGORY=3');

$categories = [
[
'id' => '5',
'parent_id' => 0,
'name' => 'Root Catalog',
'is_active' => true,
'position' => 0,
'level' => 0,
'children' => '2',
'created_at' => '2014-04-04 14:17:29',
'updated_at' => '2014-04-04 14:17:29',
'path' => '1/2/4',
'available_sort_by' => [],
'include_in_menu' => true,
'custom_attributes' => [
[
'attribute_code' => 'path',
'value' => '1',
],
[
'attribute_code' => 'url_path',
'value' => 'foo/bar',
],
[
'attribute_code' => 'children_count',
'value' => '124',
],
[
'attribute_code' => 'image',
'value' => 'pub/media/catalog/category/foo.jpg',
],
],
],
];

(new MagentoCategories())->updateCategories($categories);

$this->assertEquals(0, MagentoCategory::count());
}

/** @test */
public function it_allows_root_level_category()
{
Queue::fake();

putenv('MAGENTO_DEFAULT_CATEGORY=2');

$categories = [
[
'id' => '2',
'parent_id' => 0,
'name' => 'Root Catalog',
'is_active' => true,
'position' => 0,
'level' => 0,
'children' => '2',
'created_at' => '2014-04-04 14:17:29',
'updated_at' => '2014-04-04 14:17:29',
'path' => '1',
'available_sort_by' => [],
'include_in_menu' => true,
'custom_attributes' => [
[
'attribute_code' => 'path',
'value' => '1',
],
[
'attribute_code' => 'url_path',
'value' => 'foo/bar',
],
[
'attribute_code' => 'children_count',
'value' => '124',
],
[
'attribute_code' => 'image',
'value' => 'pub/media/catalog/category/foo.jpg',
],
],
],
];

(new MagentoCategories())->updateCategories($categories);

$this->assertEquals(1, MagentoCategory::count());
$this->assertEquals(2, MagentoCategory::first()->id);
}

/** @test */
public function it_allows_nested_level_category()
{
Queue::fake();

putenv('MAGENTO_DEFAULT_CATEGORY=2');

$categories = [
[
'id' => '10',
'parent_id' => 0,
'name' => 'Root Catalog',
'is_active' => true,
'position' => 0,
'level' => 0,
'children' => '2',
'created_at' => '2014-04-04 14:17:29',
'updated_at' => '2014-04-04 14:17:29',
'path' => '1/2/9/10',
'available_sort_by' => [],
'include_in_menu' => true,
'custom_attributes' => [
[
'attribute_code' => 'path',
'value' => '1',
],
[
'attribute_code' => 'url_path',
'value' => 'foo/bar',
],
[
'attribute_code' => 'children_count',
'value' => '124',
],
[
'attribute_code' => 'image',
'value' => 'pub/media/catalog/category/foo.jpg',
],
],
],
];

(new MagentoCategories())->updateCategories($categories);

$this->assertEquals(1, MagentoCategory::count());
$this->assertEquals(10, MagentoCategory::first()->id);
}
}

0 comments on commit 4f1f32d

Please sign in to comment.