|
2 | 2 |
|
3 | 3 | namespace Grayloon\MagentoStorage\Tests\Support; |
4 | 4 |
|
| 5 | +use Grayloon\MagentoStorage\Database\Factories\MagentoCategoryFactory; |
5 | 6 | use Grayloon\MagentoStorage\Database\Factories\MagentoCustomAttributeTypeFactory; |
6 | 7 | use Grayloon\MagentoStorage\Jobs\DownloadMagentoCategoryImage; |
7 | 8 | use Grayloon\MagentoStorage\Jobs\UpdateProductAttributeGroup; |
@@ -274,4 +275,245 @@ public function test_can_download_category_image() |
274 | 275 | Queue::assertPushed(DownloadMagentoCategoryImage::class, fn ($job) => $job->path === 'pub/media/catalog/category/foo.jpg'); |
275 | 276 | Queue::assertPushed(DownloadMagentoCategoryImage::class, fn ($job) => $job->category->id === $category->id); |
276 | 277 | } |
| 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 | + } |
277 | 519 | } |
0 commit comments