|
| 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\GraphQl\Catalog; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection; |
| 11 | +use Magento\TestFramework\Helper\Bootstrap; |
| 12 | +use Magento\TestFramework\ObjectManager; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | +use Magento\Catalog\Api\Data\CategoryInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Test for getting canonical url data from category |
| 18 | + */ |
| 19 | +class CategoryCanonicalUrlTest extends GraphQlAbstract |
| 20 | +{ |
| 21 | + /** @var ObjectManager $objectManager */ |
| 22 | + private $objectManager; |
| 23 | + |
| 24 | + /** |
| 25 | + * @magentoApiDataFixture Magento/Catalog/_files/categories.php |
| 26 | + * @magentoConfigFixture default_store catalog/seo/category_canonical_tag 1 |
| 27 | + */ |
| 28 | + public function testCategoryWithCanonicalLinksMetaTagSettingsEnabled() |
| 29 | + { |
| 30 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 31 | + /** @var CategoryCollection $categoryCollection */ |
| 32 | + $categoryCollection = $this->objectManager->create(CategoryCollection::class); |
| 33 | + $categoryCollection->addFieldToFilter('name', 'Category 1.1.1'); |
| 34 | + /** @var CategoryInterface $category */ |
| 35 | + $category = $categoryCollection->getFirstItem(); |
| 36 | + $categoryId = $category->getId(); |
| 37 | + $query = <<<QUERY |
| 38 | + { |
| 39 | +categoryList(filters: {ids: {in: ["$categoryId"]}}) { |
| 40 | + id |
| 41 | + name |
| 42 | + url_key |
| 43 | + url_suffix |
| 44 | + canonical_url |
| 45 | + } |
| 46 | +} |
| 47 | +QUERY; |
| 48 | + |
| 49 | + $response = $this->graphQlQuery($query); |
| 50 | + $this->assertNotEmpty($response['categoryList'], 'Category list should not be empty'); |
| 51 | + $this->assertEquals('.html', $response['categoryList'][0]['url_suffix']); |
| 52 | + $this->assertEquals( |
| 53 | + 'category-1/category-1-1/category-1-1-1.html', |
| 54 | + $response['categoryList'][0]['canonical_url'] |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @magentoApiDataFixture Magento/Catalog/_files/categories.php |
| 60 | + * @magentoConfigFixture default_store catalog/seo/category_canonical_tag 0 |
| 61 | + */ |
| 62 | + public function testCategoryWithCanonicalLinksMetaTagSettingsDisabled() |
| 63 | + { |
| 64 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 65 | + /** @var CategoryCollection $categoryCollection */ |
| 66 | + $categoryCollection = $this->objectManager->create(CategoryCollection::class); |
| 67 | + $categoryCollection->addFieldToFilter('name', 'Category 1.1'); |
| 68 | + /** @var CategoryInterface $category */ |
| 69 | + $category = $categoryCollection->getFirstItem(); |
| 70 | + $categoryId = $category->getId(); |
| 71 | + $query = <<<QUERY |
| 72 | + { |
| 73 | +categoryList(filters: {ids: {in: ["$categoryId"]}}) { |
| 74 | + id |
| 75 | + name |
| 76 | + url_key |
| 77 | + canonical_url |
| 78 | + } |
| 79 | +} |
| 80 | +QUERY; |
| 81 | + |
| 82 | + $response = $this->graphQlQuery($query); |
| 83 | + $this->assertNotEmpty($response['categoryList'], 'Category list should not be empty'); |
| 84 | + $this->assertNull( |
| 85 | + $response['categoryList'][0]['canonical_url'] |
| 86 | + ); |
| 87 | + $this->assertEquals('category-1-1', $response['categoryList'][0]['url_key']); |
| 88 | + } |
| 89 | +} |
0 commit comments