|
7 | 7 |
|
8 | 8 | namespace Magento\Catalog\Model\ResourceModel;
|
9 | 9 |
|
| 10 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 11 | +use Magento\Catalog\Test\Fixture\Category as CategoryFixture; |
| 12 | +use Magento\Framework\Exception\CouldNotSaveException; |
| 13 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 14 | +use Magento\Store\Model\StoreManagerInterface; |
| 15 | +use Magento\TestFramework\Fixture\DataFixture; |
| 16 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 18 | +use Magento\TestFramework\Fixture\DbIsolation; |
10 | 19 | use Magento\TestFramework\Helper\Bootstrap;
|
11 | 20 | use PHPUnit\Framework\TestCase;
|
12 | 21 |
|
|
15 | 24 | */
|
16 | 25 | class UrlTest extends TestCase
|
17 | 26 | {
|
| 27 | + private CategoryRepositoryInterface $categoryRepository; |
| 28 | + private DataFixtureStorage $fixtures; |
| 29 | + private StoreManagerInterface $storeManager; |
18 | 30 | private Url $urlResource;
|
19 | 31 |
|
20 |
| - /** |
21 |
| - * @inheritdoc |
22 |
| - */ |
23 | 32 | protected function setUp(): void
|
24 | 33 | {
|
25 | 34 | $objectManager = Bootstrap::getObjectManager();
|
| 35 | + $this->categoryRepository = $objectManager->create(CategoryRepositoryInterface::class); |
| 36 | + $this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 37 | + $this->storeManager = $objectManager->create(StoreManagerInterface::class); |
26 | 38 | $this->urlResource = $objectManager->create(Url::class);
|
27 | 39 | }
|
28 | 40 |
|
29 | 41 | /**
|
30 | 42 | * Test that scope is respected for the is_active flag.
|
31 | 43 | *
|
32 | 44 | * @return void
|
33 |
| - * @magentoDataFixture Magento/Catalog/_files/categories_disabled_different_scopes.php |
| 45 | + * @throws NoSuchEntityException|CouldNotSaveException |
34 | 46 | */
|
| 47 | + #[ |
| 48 | + DbIsolation(true), |
| 49 | + DataFixture(CategoryFixture::class, [ |
| 50 | + 'name' => 'Enabled on default scope', |
| 51 | + 'is_active' => '1', |
| 52 | + ], 'c1'), |
| 53 | + DataFixture(CategoryFixture::class, [ |
| 54 | + 'name' => 'Disabled on default scope', |
| 55 | + 'is_active' => '0', |
| 56 | + ], 'c2'), |
| 57 | + DataFixture(CategoryFixture::class, [ |
| 58 | + 'name' => 'Enabled on default scope, disabled for store', |
| 59 | + 'is_active' => '1', |
| 60 | + ], 'c3'), |
| 61 | + DataFixture(CategoryFixture::class, [ |
| 62 | + 'name' => 'Disabled on default scope, enabled for store', |
| 63 | + 'is_active' => '0', |
| 64 | + ], 'c4'), |
| 65 | + ] |
35 | 66 | public function testIsActiveScope(): void
|
36 | 67 | {
|
37 |
| - $categories = $this->urlResource->getCategories([3, 4, 5], 1); |
38 |
| - $this->assertTrue((bool) $categories[3]->getIsActive()); |
39 |
| - $this->assertFalse((bool) $categories[4]->getIsActive()); |
40 |
| - $this->assertFalse((bool) $categories[5]->getIsActive()); |
| 68 | + // Get Store ID |
| 69 | + $storeId = (int) $this->storeManager->getStore('default')->getId(); |
| 70 | + |
| 71 | + // Get Category IDs |
| 72 | + $fixtureNames = ['c1', 'c2', 'c3', 'c4']; |
| 73 | + $categoryIds = array_combine($fixtureNames, array_map(function (string $fixtureName): int { |
| 74 | + return (int) $this->fixtures->get($fixtureName)->getId(); |
| 75 | + }, $fixtureNames)); |
| 76 | + |
| 77 | + // Disable c3 for store |
| 78 | + $c3 = $this->categoryRepository->get($categoryIds['c3'], $storeId); |
| 79 | + $c3->setIsActive(false); |
| 80 | + $this->categoryRepository->save($c3); |
| 81 | + |
| 82 | + // Enable c4 for store |
| 83 | + $c4 = $this->categoryRepository->get($categoryIds['c4'], $storeId); |
| 84 | + $c4->setIsActive(true); |
| 85 | + $this->categoryRepository->save($c4); |
| 86 | + |
| 87 | + // Check categories |
| 88 | + $categories = $this->urlResource->getCategories($categoryIds, $storeId); |
| 89 | + $this->assertSame('1', $categories[$categoryIds['c1']]->getIsActive()); |
| 90 | + $this->assertSame('0', $categories[$categoryIds['c2']]->getIsActive()); |
| 91 | + $this->assertSame('0', $categories[$categoryIds['c3']]->getIsActive()); |
| 92 | + $this->assertSame('1', $categories[$categoryIds['c4']]->getIsActive()); |
41 | 93 | }
|
42 | 94 | }
|
0 commit comments