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

Commit 4f1f32d

Browse files
authored
Merge pull request #22 from grayloon/ignore-non-root-categories
Ignore and delete Categories if exists when global root category set
2 parents 8a4a38d + 228e56c commit 4f1f32d

File tree

2 files changed

+283
-1
lines changed

2 files changed

+283
-1
lines changed

src/Support/MagentoCategories.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Grayloon\Magento\Magento;
66
use Grayloon\MagentoStorage\Jobs\DownloadMagentoCategoryImage;
77
use Grayloon\MagentoStorage\Models\MagentoCategory;
8+
use Illuminate\Support\Str;
89

910
class MagentoCategories extends PaginatableMagentoService
1011
{
@@ -37,9 +38,48 @@ public function updateCategories($categories)
3738
}
3839

3940
foreach ($categories as $apiCategory) {
40-
$this->updateCategory($apiCategory);
41+
if ($this->validCategory($apiCategory)) {
42+
$this->updateCategory($apiCategory);
43+
}
44+
}
45+
46+
return $this;
47+
}
48+
49+
/**
50+
* Determine if the category is valid.
51+
*
52+
* @param array $apiCategory
53+
* @return bool
54+
*/
55+
protected function validCategory($apiCategory)
56+
{
57+
if (! env('MAGENTO_DEFAULT_CATEGORY')) {
58+
return true;
4159
}
4260

61+
if (env('MAGENTO_DEFAULT_CATEGORY') == $apiCategory['id']) {
62+
return true;
63+
}
64+
65+
if (Str::contains($apiCategory['path'], '/' . env('MAGENTO_DEFAULT_CATEGORY') . '/')) {
66+
return true;
67+
}
68+
69+
$this->deleteIfExists($apiCategory['id']);
70+
return false;
71+
}
72+
73+
/**
74+
* Delete the category if it exists.
75+
*
76+
* @param int $id
77+
* @return void
78+
*/
79+
protected function deleteIfExists($id)
80+
{
81+
MagentoCategory::where('id', $id)->delete();
82+
4383
return $this;
4484
}
4585

tests/Support/MagentoCategoriesTest.php

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Grayloon\MagentoStorage\Tests\Support;
44

5+
use Grayloon\MagentoStorage\Database\Factories\MagentoCategoryFactory;
56
use Grayloon\MagentoStorage\Database\Factories\MagentoCustomAttributeTypeFactory;
67
use Grayloon\MagentoStorage\Jobs\DownloadMagentoCategoryImage;
78
use Grayloon\MagentoStorage\Jobs\UpdateProductAttributeGroup;
@@ -274,4 +275,245 @@ public function test_can_download_category_image()
274275
Queue::assertPushed(DownloadMagentoCategoryImage::class, fn ($job) => $job->path === 'pub/media/catalog/category/foo.jpg');
275276
Queue::assertPushed(DownloadMagentoCategoryImage::class, fn ($job) => $job->category->id === $category->id);
276277
}
278+
279+
/** @test */
280+
public function it_deletes_old_category()
281+
{
282+
Queue::fake();
283+
putenv('MAGENTO_DEFAULT_CATEGORY=3');
284+
285+
$category = MagentoCategoryFactory::new()->create([
286+
'id' => 2,
287+
]);
288+
289+
$categories = [
290+
[
291+
'id' => '2',
292+
'parent_id' => 0,
293+
'name' => 'Root Catalog',
294+
'is_active' => true,
295+
'position' => 0,
296+
'level' => 0,
297+
'children' => '2',
298+
'created_at' => '2014-04-04 14:17:29',
299+
'updated_at' => '2014-04-04 14:17:29',
300+
'path' => '1',
301+
'available_sort_by' => [],
302+
'include_in_menu' => true,
303+
'custom_attributes' => [
304+
[
305+
'attribute_code' => 'path',
306+
'value' => '1',
307+
],
308+
[
309+
'attribute_code' => 'url_path',
310+
'value' => 'foo/bar',
311+
],
312+
[
313+
'attribute_code' => 'children_count',
314+
'value' => '124',
315+
],
316+
[
317+
'attribute_code' => 'image',
318+
'value' => 'pub/media/catalog/category/foo.jpg',
319+
],
320+
],
321+
],
322+
];
323+
324+
(new MagentoCategories())->updateCategories($categories);
325+
326+
$this->assertEquals(0, MagentoCategory::count());
327+
$this->assertDeleted($category);
328+
}
329+
330+
/** @test */
331+
public function it_ignores_non_root_config_level_categories()
332+
{
333+
Queue::fake();
334+
335+
putenv('MAGENTO_DEFAULT_CATEGORY=3');
336+
337+
$categories = [
338+
[
339+
'id' => '1',
340+
'parent_id' => 0,
341+
'name' => 'Root Catalog',
342+
'is_active' => true,
343+
'position' => 0,
344+
'level' => 0,
345+
'children' => '2',
346+
'created_at' => '2014-04-04 14:17:29',
347+
'updated_at' => '2014-04-04 14:17:29',
348+
'path' => '1',
349+
'available_sort_by' => [],
350+
'include_in_menu' => true,
351+
'custom_attributes' => [
352+
[
353+
'attribute_code' => 'path',
354+
'value' => '1',
355+
],
356+
[
357+
'attribute_code' => 'url_path',
358+
'value' => 'foo/bar',
359+
],
360+
[
361+
'attribute_code' => 'children_count',
362+
'value' => '124',
363+
],
364+
[
365+
'attribute_code' => 'image',
366+
'value' => 'pub/media/catalog/category/foo.jpg',
367+
],
368+
],
369+
],
370+
];
371+
372+
(new MagentoCategories())->updateCategories($categories);
373+
374+
$this->assertEquals(0, MagentoCategory::count());
375+
}
376+
377+
/** @test */
378+
public function it_ignores_category_without_root_path()
379+
{
380+
Queue::fake();
381+
382+
putenv('MAGENTO_DEFAULT_CATEGORY=3');
383+
384+
$categories = [
385+
[
386+
'id' => '5',
387+
'parent_id' => 0,
388+
'name' => 'Root Catalog',
389+
'is_active' => true,
390+
'position' => 0,
391+
'level' => 0,
392+
'children' => '2',
393+
'created_at' => '2014-04-04 14:17:29',
394+
'updated_at' => '2014-04-04 14:17:29',
395+
'path' => '1/2/4',
396+
'available_sort_by' => [],
397+
'include_in_menu' => true,
398+
'custom_attributes' => [
399+
[
400+
'attribute_code' => 'path',
401+
'value' => '1',
402+
],
403+
[
404+
'attribute_code' => 'url_path',
405+
'value' => 'foo/bar',
406+
],
407+
[
408+
'attribute_code' => 'children_count',
409+
'value' => '124',
410+
],
411+
[
412+
'attribute_code' => 'image',
413+
'value' => 'pub/media/catalog/category/foo.jpg',
414+
],
415+
],
416+
],
417+
];
418+
419+
(new MagentoCategories())->updateCategories($categories);
420+
421+
$this->assertEquals(0, MagentoCategory::count());
422+
}
423+
424+
/** @test */
425+
public function it_allows_root_level_category()
426+
{
427+
Queue::fake();
428+
429+
putenv('MAGENTO_DEFAULT_CATEGORY=2');
430+
431+
$categories = [
432+
[
433+
'id' => '2',
434+
'parent_id' => 0,
435+
'name' => 'Root Catalog',
436+
'is_active' => true,
437+
'position' => 0,
438+
'level' => 0,
439+
'children' => '2',
440+
'created_at' => '2014-04-04 14:17:29',
441+
'updated_at' => '2014-04-04 14:17:29',
442+
'path' => '1',
443+
'available_sort_by' => [],
444+
'include_in_menu' => true,
445+
'custom_attributes' => [
446+
[
447+
'attribute_code' => 'path',
448+
'value' => '1',
449+
],
450+
[
451+
'attribute_code' => 'url_path',
452+
'value' => 'foo/bar',
453+
],
454+
[
455+
'attribute_code' => 'children_count',
456+
'value' => '124',
457+
],
458+
[
459+
'attribute_code' => 'image',
460+
'value' => 'pub/media/catalog/category/foo.jpg',
461+
],
462+
],
463+
],
464+
];
465+
466+
(new MagentoCategories())->updateCategories($categories);
467+
468+
$this->assertEquals(1, MagentoCategory::count());
469+
$this->assertEquals(2, MagentoCategory::first()->id);
470+
}
471+
472+
/** @test */
473+
public function it_allows_nested_level_category()
474+
{
475+
Queue::fake();
476+
477+
putenv('MAGENTO_DEFAULT_CATEGORY=2');
478+
479+
$categories = [
480+
[
481+
'id' => '10',
482+
'parent_id' => 0,
483+
'name' => 'Root Catalog',
484+
'is_active' => true,
485+
'position' => 0,
486+
'level' => 0,
487+
'children' => '2',
488+
'created_at' => '2014-04-04 14:17:29',
489+
'updated_at' => '2014-04-04 14:17:29',
490+
'path' => '1/2/9/10',
491+
'available_sort_by' => [],
492+
'include_in_menu' => true,
493+
'custom_attributes' => [
494+
[
495+
'attribute_code' => 'path',
496+
'value' => '1',
497+
],
498+
[
499+
'attribute_code' => 'url_path',
500+
'value' => 'foo/bar',
501+
],
502+
[
503+
'attribute_code' => 'children_count',
504+
'value' => '124',
505+
],
506+
[
507+
'attribute_code' => 'image',
508+
'value' => 'pub/media/catalog/category/foo.jpg',
509+
],
510+
],
511+
],
512+
];
513+
514+
(new MagentoCategories())->updateCategories($categories);
515+
516+
$this->assertEquals(1, MagentoCategory::count());
517+
$this->assertEquals(10, MagentoCategory::first()->id);
518+
}
277519
}

0 commit comments

Comments
 (0)