Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateUrlPathTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 use PHPUnit_Framework_MockObject_MockObject as MockObject;
17 use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
18 
22 class UpdateUrlPathTest extends \PHPUnit\Framework\TestCase
23 {
27  private $objectManager;
28 
32  private $categoryUrlPathGenerator;
33 
37  private $categoryUrlRewriteGenerator;
38 
42  private $storeViewService;
43 
47  private $urlPersist;
48 
52  private $categoryResource;
53 
57  private $category;
58 
62  private $updateUrlPathPlugin;
63 
67  protected function setUp()
68  {
69  $this->objectManager = new ObjectManager($this);
70  $this->categoryUrlPathGenerator = $this->getMockBuilder(CategoryUrlPathGenerator::class)
71  ->disableOriginalConstructor()
72  ->setMethods(['getUrlPath'])
73  ->getMock();
74  $this->categoryUrlRewriteGenerator = $this->getMockBuilder(CategoryUrlRewriteGenerator::class)
75  ->disableOriginalConstructor()
76  ->setMethods(['generate'])
77  ->getMock();
78  $this->categoryResource = $this->getMockBuilder(CategoryResource::class)
79  ->disableOriginalConstructor()
80  ->setMethods(['saveAttribute'])
81  ->getMock();
82  $this->category = $this->getMockBuilder(Category::class)
83  ->disableOriginalConstructor()
84  ->setMethods(
85  [
86  'getStoreId',
87  'getParentId',
88  'isObjectNew',
89  'isInRootCategoryList',
90  'getStoreIds',
91  'setStoreId',
92  'unsUrlPath',
93  'setUrlPath',
94  ]
95  )
96  ->getMock();
97  $this->storeViewService = $this->getMockBuilder(StoreViewService::class)
98  ->disableOriginalConstructor()
99  ->setMethods(['doesEntityHaveOverriddenUrlPathForStore'])
100  ->getMock();
101  $this->urlPersist = $this->getMockBuilder(UrlPersistInterface::class)
102  ->disableOriginalConstructor()
103  ->setMethods(['replace'])
104  ->getMockForAbstractClass();
105 
106  $this->updateUrlPathPlugin = $this->objectManager->getObject(
107  \Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\UpdateUrlPath::class,
108  [
109  'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator,
110  'categoryUrlRewriteGenerator' => $this->categoryUrlRewriteGenerator,
111  'urlPersist' => $this->urlPersist,
112  'storeViewService' => $this->storeViewService,
113  ]
114  );
115  }
116 
118  {
119  $this->category->expects($this->atLeastOnce())->method('getParentId')->willReturn(0);
120  $this->category->expects($this->atLeastOnce())->method('isObjectNew')->willReturn(true);
121  $this->category->expects($this->atLeastOnce())->method('isInRootCategoryList')->willReturn(false);
122  $this->category->expects($this->never())->method('getStoreIds');
123 
124  $this->assertEquals(
125  $this->categoryResource,
126  $this->updateUrlPathPlugin->afterSave($this->categoryResource, $this->categoryResource, $this->category)
127  );
128  }
129 
131  {
132  $parentId = 1;
133  $categoryStoreIds = [0,1,2];
134  $generatedUrlPath = 'parent_category/child_category';
135 
136  $this->categoryUrlPathGenerator
137  ->expects($this->once())
138  ->method('getUrlPath')
139  ->with($this->category)
140  ->willReturn($generatedUrlPath);
141  $this->category->expects($this->atLeastOnce())->method('getParentId')->willReturn($parentId);
142  $this->category->expects($this->atLeastOnce())->method('isObjectNew')->willReturn(true);
143  $this->category->expects($this->atLeastOnce())->method('isInRootCategoryList')->willReturn(false);
144  $this->category->expects($this->atLeastOnce())->method('getStoreIds')->willReturn($categoryStoreIds);
145  $this->category->expects($this->once())->method('setStoreId')->with($categoryStoreIds[2])->willReturnSelf();
146  $this->category->expects($this->once())->method('unsUrlPath')->willReturnSelf();
147  $this->category->expects($this->once())->method('setUrlPath')->with($generatedUrlPath)->willReturnSelf();
148  $this->storeViewService->expects($this->exactly(2))->method('doesEntityHaveOverriddenUrlPathForStore')
149  ->willReturnMap(
150  [
151  [$categoryStoreIds[1], $parentId, 'catalog_category', false],
152  [$categoryStoreIds[2], $parentId, 'catalog_category', true],
153  ]
154  );
155  $this->categoryResource
156  ->expects($this->once())
157  ->method('saveAttribute')
158  ->with($this->category, 'url_path')
159  ->willReturnSelf();
160  $generatedUrlRewrite = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
161  ->disableOriginalConstructor()
162  ->getMock();
163  $this->categoryUrlRewriteGenerator->expects($this->once())->method('generate')->with($this->category)
164  ->willReturn([$generatedUrlRewrite]);
165  $this->urlPersist->expects($this->once())->method('replace')->with([$generatedUrlRewrite])->willReturnSelf();
166 
167  $this->assertEquals(
168  $this->categoryResource,
169  $this->updateUrlPathPlugin->afterSave($this->categoryResource, $this->categoryResource, $this->category)
170  );
171  }
172 }