diff --git a/src/Support/MagentoCategories.php b/src/Support/MagentoCategories.php index bab0d65..8eb0d04 100644 --- a/src/Support/MagentoCategories.php +++ b/src/Support/MagentoCategories.php @@ -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 { @@ -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; } diff --git a/tests/Support/MagentoCategoriesTest.php b/tests/Support/MagentoCategoriesTest.php index 0b0fe17..94f967b 100644 --- a/tests/Support/MagentoCategoriesTest.php +++ b/tests/Support/MagentoCategoriesTest.php @@ -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; @@ -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); + } }