|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogUrlRewrite\Model; |
| 9 | + |
| 10 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 11 | +use Magento\Framework\ObjectManagerInterface; |
| 12 | +use Magento\Store\Api\StoreRepositoryInterface; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection; |
| 15 | +use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory; |
| 16 | +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * Base class for url rewrites tests logic |
| 21 | + * |
| 22 | + * @magentoDbIsolation enabled |
| 23 | + * @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1 |
| 24 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 25 | + */ |
| 26 | +abstract class AbstractUrlRewriteTest extends TestCase |
| 27 | +{ |
| 28 | + /** @var ObjectManagerInterface */ |
| 29 | + protected $objectManager; |
| 30 | + |
| 31 | + /** @var StoreRepositoryInterface */ |
| 32 | + protected $storeRepository; |
| 33 | + |
| 34 | + /** @var ScopeConfigInterface */ |
| 35 | + protected $config; |
| 36 | + |
| 37 | + /** @var UrlRewriteCollectionFactory */ |
| 38 | + protected $urlRewriteCollectionFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritdoc |
| 42 | + */ |
| 43 | + protected function setUp() |
| 44 | + { |
| 45 | + parent::setUp(); |
| 46 | + |
| 47 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class); |
| 49 | + $this->config = $this->objectManager->get(ScopeConfigInterface::class); |
| 50 | + $this->urlRewriteCollectionFactory = $this->objectManager->get(UrlRewriteCollectionFactory::class); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Retrieve all rewrite ids |
| 55 | + * |
| 56 | + * @return array |
| 57 | + */ |
| 58 | + protected function getAllRewriteIds(): array |
| 59 | + { |
| 60 | + $urlRewriteCollection = $this->urlRewriteCollectionFactory->create(); |
| 61 | + |
| 62 | + return $urlRewriteCollection->getAllIds(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Check that actual data contains of expected values |
| 67 | + * |
| 68 | + * @param UrlRewriteCollection $collection |
| 69 | + * @param array $expectedData |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + protected function assertRewrites(UrlRewriteCollection $collection, array $expectedData): void |
| 73 | + { |
| 74 | + $collectionItems = $collection->toArray()['items']; |
| 75 | + $this->assertTrue(count($collectionItems) === count($expectedData)); |
| 76 | + foreach ($expectedData as $expectedItem) { |
| 77 | + $found = false; |
| 78 | + foreach ($collectionItems as $item) { |
| 79 | + $found = array_intersect_assoc($item, $expectedItem) == $expectedItem; |
| 80 | + if ($found) { |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + $this->assertTrue($found, 'The actual data does not contains of expected values'); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Get category url rewrites collection |
| 90 | + * |
| 91 | + * @param string|array $entityId |
| 92 | + * @return UrlRewriteCollection |
| 93 | + */ |
| 94 | + protected function getEntityRewriteCollection($entityId): UrlRewriteCollection |
| 95 | + { |
| 96 | + $condition = is_array($entityId) ? ['in' => $entityId] : $entityId; |
| 97 | + $entityRewriteCollection = $this->urlRewriteCollectionFactory->create(); |
| 98 | + $entityRewriteCollection->addFieldToFilter(UrlRewrite::ENTITY_ID, $condition) |
| 99 | + ->addFieldToFilter(UrlRewrite::ENTITY_TYPE, ['eq' => $this->getEntityType()]); |
| 100 | + |
| 101 | + return $entityRewriteCollection; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Prepare expected data |
| 106 | + * |
| 107 | + * @param array $expectedData |
| 108 | + * @param int|null $id |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + protected function prepareData(array $expectedData, ?int $id = null): array |
| 112 | + { |
| 113 | + $newData = []; |
| 114 | + foreach ($expectedData as $key => $expectedItem) { |
| 115 | + $newData[$key] = str_replace(['%suffix%', '%id%'], [$this->getUrlSuffix(), $id], $expectedItem); |
| 116 | + } |
| 117 | + |
| 118 | + return $newData; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get entity type |
| 123 | + * |
| 124 | + * @return string |
| 125 | + */ |
| 126 | + abstract protected function getEntityType(): string; |
| 127 | + |
| 128 | + /** |
| 129 | + * Get config value for url suffix |
| 130 | + * |
| 131 | + * @return string |
| 132 | + */ |
| 133 | + abstract protected function getUrlSuffix(): string; |
| 134 | +} |
0 commit comments