|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\CmsUrlRewrite\Test\Unit\Model; |
| 10 | + |
| 11 | +use Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 13 | +use Magento\Framework\Filter\FilterManager; |
| 14 | +use Magento\Cms\Api\Data\PageInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Class \Magento\CmsUrlRewrite\Test\Unit\Model\CmsPageUrlPathGeneratorTest |
| 18 | + */ |
| 19 | +class CmsPageUrlPathGeneratorTest extends \PHPUnit\Framework\TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var ObjectManagerHelper |
| 23 | + */ |
| 24 | + private $objectManager; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \PHPUnit_Framework_MockObject_MockObject|FilterManager |
| 28 | + */ |
| 29 | + private $filterManagerMock; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var CmsPageUrlPathGenerator |
| 33 | + */ |
| 34 | + private $model; |
| 35 | + |
| 36 | + /** |
| 37 | + * Setup environment for test |
| 38 | + */ |
| 39 | + protected function setUp() |
| 40 | + { |
| 41 | + $this->objectManager = new ObjectManagerHelper($this); |
| 42 | + $this->filterManagerMock = $this->getMockBuilder(FilterManager::class) |
| 43 | + ->disableOriginalConstructor() |
| 44 | + ->setMethods(['translitUrl']) |
| 45 | + ->getMock(); |
| 46 | + |
| 47 | + $this->model = $this->objectManager->getObject( |
| 48 | + CmsPageUrlPathGenerator::class, |
| 49 | + [ |
| 50 | + 'filterManager' => $this->filterManagerMock |
| 51 | + ] |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test getUrlPath with page has identifier = cms-cookie |
| 57 | + */ |
| 58 | + public function testGetUrlPath() |
| 59 | + { |
| 60 | + /* @var PageInterface $cmsPageMock*/ |
| 61 | + $cmsPageMock = $this->getMockBuilder(PageInterface::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + |
| 65 | + $cmsPageMock->expects($this->any()) |
| 66 | + ->method('getIdentifier') |
| 67 | + ->willReturn('cms-cookie'); |
| 68 | + |
| 69 | + $this->assertEquals('cms-cookie', $this->model->getUrlPath($cmsPageMock)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Test getCanonicalUrlPath() with page has id = 1 |
| 74 | + */ |
| 75 | + public function testGetCanonicalUrlPath() |
| 76 | + { |
| 77 | + /* @var PageInterface $cmsPageMock*/ |
| 78 | + $cmsPageMock = $this->getMockBuilder(PageInterface::class) |
| 79 | + ->disableOriginalConstructor() |
| 80 | + ->getMock(); |
| 81 | + |
| 82 | + $cmsPageMock->expects($this->any()) |
| 83 | + ->method('getId') |
| 84 | + ->willReturn('1'); |
| 85 | + |
| 86 | + $this->assertEquals('cms/page/view/page_id/1', $this->model->getCanonicalUrlPath($cmsPageMock)); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Test generateUrlKey() with page has no identifier |
| 91 | + */ |
| 92 | + public function testGenerateUrlKeyWithNullIdentifier() |
| 93 | + { |
| 94 | + /** |
| 95 | + * Data set |
| 96 | + */ |
| 97 | + $page = [ |
| 98 | + 'identifier' => null, |
| 99 | + 'title' => 'CMS Cookie' |
| 100 | + ]; |
| 101 | + |
| 102 | + /* @var PageInterface $cmsPageMock*/ |
| 103 | + $cmsPageMock = $this->getMockBuilder(PageInterface::class) |
| 104 | + ->disableOriginalConstructor() |
| 105 | + ->getMock(); |
| 106 | + |
| 107 | + $cmsPageMock->expects($this->any()) |
| 108 | + ->method('getIdentifier') |
| 109 | + ->willReturn($page['identifier']); |
| 110 | + |
| 111 | + $cmsPageMock->expects($this->any()) |
| 112 | + ->method('getTitle') |
| 113 | + ->willReturn($page['title']); |
| 114 | + |
| 115 | + $this->filterManagerMock->expects($this->any()) |
| 116 | + ->method('translitUrl') |
| 117 | + ->with($page['title']) |
| 118 | + ->willReturn('cms-cookie'); |
| 119 | + |
| 120 | + $this->assertEquals('cms-cookie', $this->model->generateUrlKey($cmsPageMock)); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Test generateUrlKey() with page has identifier |
| 125 | + */ |
| 126 | + public function testGenerateUrlKeyWithIdentifier() |
| 127 | + { |
| 128 | + /** |
| 129 | + * Data set |
| 130 | + */ |
| 131 | + $page = [ |
| 132 | + 'identifier' => 'home', |
| 133 | + 'title' => 'Home Page' |
| 134 | + ]; |
| 135 | + |
| 136 | + /* @var PageInterface $cmsPageMock*/ |
| 137 | + $cmsPageMock = $this->getMockBuilder(PageInterface::class) |
| 138 | + ->disableOriginalConstructor() |
| 139 | + ->getMock(); |
| 140 | + |
| 141 | + $cmsPageMock->expects($this->any()) |
| 142 | + ->method('getIdentifier') |
| 143 | + ->willReturn($page['identifier']); |
| 144 | + |
| 145 | + $cmsPageMock->expects($this->any()) |
| 146 | + ->method('getTitle') |
| 147 | + ->willReturn($page['title']); |
| 148 | + |
| 149 | + $this->filterManagerMock->expects($this->any()) |
| 150 | + ->method('translitUrl') |
| 151 | + ->with($page['identifier']) |
| 152 | + ->willReturn('home'); |
| 153 | + |
| 154 | + $this->assertEquals('home', $this->model->generateUrlKey($cmsPageMock)); |
| 155 | + } |
| 156 | +} |
0 commit comments