Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
WebsitesTest.php
Go to the documentation of this file.
1 <?php
7 
15 use Magento\Store\Model\Store as StoreView;
17 
24 {
25  const PRODUCT_ID = 1;
26  const WEBSITE_ID = 1;
27  const GROUP_ID = 1;
28  const STORE_VIEW_NAME = 'StoreView';
29  const STORE_VIEW_ID = 1;
30  const SECOND_WEBSITE_ID = 2;
31 
36 
41 
46 
50  protected $storeManagerMock;
51 
55  protected $websiteMock;
56 
60  protected $secondWebsiteMock;
61 
65  protected $assignedWebsites;
66 
70  protected $groupMock;
71 
75  protected $storeViewMock;
76 
80  protected function setUp()
81  {
82  parent::setUp();
83  $this->productMock->expects($this->any())
84  ->method('getId')
85  ->willReturn(self::PRODUCT_ID);
86  $this->assignedWebsites = [self::SECOND_WEBSITE_ID];
87  $this->websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
88  ->setMethods(['getId', 'getName'])
89  ->disableOriginalConstructor()
90  ->getMock();
91  $this->secondWebsiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
92  ->setMethods(['getId', 'getName'])
93  ->disableOriginalConstructor()
94  ->getMock();
95  $this->websiteRepositoryMock = $this->getMockBuilder(\Magento\Store\Api\WebsiteRepositoryInterface::class)
96  ->setMethods(['getList'])
97  ->getMockForAbstractClass();
98  $this->websiteRepositoryMock->expects($this->any())
99  ->method('getDefault')
100  ->willReturn($this->websiteMock);
101  $this->groupRepositoryMock = $this->getMockBuilder(\Magento\Store\Api\GroupRepositoryInterface::class)
102  ->setMethods(['getList'])
103  ->getMockForAbstractClass();
104  $this->storeRepositoryMock = $this->getMockBuilder(\Magento\Store\Api\StoreRepositoryInterface::class)
105  ->setMethods(['getList'])
106  ->getMockForAbstractClass();
107  $this->productMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductInterface::class)
108  ->setMethods(['getId'])
109  ->getMockForAbstractClass();
110  $this->locatorMock->expects($this->any())
111  ->method('getWebsiteIds')
112  ->willReturn($this->assignedWebsites);
113  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
114  ->setMethods(['isSingleStoreMode', 'getWesites'])
115  ->getMockForAbstractClass();
116  $this->storeManagerMock->method('getWebsites')
117  ->willReturn([$this->websiteMock, $this->secondWebsiteMock]);
118  $this->storeManagerMock->expects($this->any())
119  ->method('isSingleStoreMode')
120  ->willReturn(false);
121  $this->groupMock = $this->getMockBuilder(\Magento\Store\Model\ResourceModel\Group\Collection::class)
122  ->setMethods(['getId', 'getName', 'getWebsiteId'])
123  ->disableOriginalConstructor()
124  ->getMock();
125  $this->groupMock->expects($this->any())
126  ->method('getWebsiteId')
127  ->willReturn(self::WEBSITE_ID);
128  $this->groupMock->expects($this->any())
129  ->method('getId')
130  ->willReturn(self::GROUP_ID);
131  $this->groupRepositoryMock->expects($this->any())
132  ->method('getList')
133  ->willReturn([$this->groupMock]);
134  $this->storeViewMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
135  ->setMethods(['getName', 'getId', 'getStoreGroupId'])
136  ->disableOriginalConstructor()
137  ->getMock();
138  $this->storeViewMock->expects($this->any())
139  ->method('getName')
140  ->willReturn(self::STORE_VIEW_NAME);
141  $this->storeViewMock->expects($this->any())
142  ->method('getStoreGroupId')
143  ->willReturn(self::GROUP_ID);
144  $this->storeViewMock->expects($this->any())
145  ->method('getId')
146  ->willReturn(self::STORE_VIEW_ID);
147  $this->storeRepositoryMock->expects($this->any())
148  ->method('getList')
149  ->willReturn([$this->storeViewMock]);
150  $this->secondWebsiteMock->expects($this->any())
151  ->method('getId')
152  ->willReturn($this->assignedWebsites[0]);
153  $this->websiteMock->expects($this->any())
154  ->method('getId')
155  ->willReturn(self::WEBSITE_ID);
156  }
157 
161  protected function createModel()
162  {
163  return $this->objectManager->getObject(
164  Websites::class,
165  [
166  'locator' => $this->locatorMock,
167  'storeManager' => $this->storeManagerMock,
168  'websiteRepository' => $this->websiteRepositoryMock,
169  'groupRepository' => $this->groupRepositoryMock,
170  'storeRepository' => $this->storeRepositoryMock,
171  ]
172  );
173  }
174 
178  public function testModifyMeta()
179  {
180  $meta = $this->getModel()->modifyMeta([]);
181  $this->assertTrue(isset($meta['websites']));
182  $this->assertTrue(isset($meta['websites']['children'][self::SECOND_WEBSITE_ID]));
183  $this->assertTrue(isset($meta['websites']['children'][self::WEBSITE_ID]));
184  $this->assertTrue(isset($meta['websites']['children']['copy_to_stores.' . self::WEBSITE_ID]));
185  }
186 
190  public function testModifyData()
191  {
192  $expectedData = [
193  self::PRODUCT_ID => [
194  'product' => [
195  'copy_to_stores' => [
196  self::WEBSITE_ID => [
197  [
198  'storeView' => self::STORE_VIEW_NAME,
199  'copy_from' => 0,
200  'copy_to' => self::STORE_VIEW_ID,
201  ]
202  ]
203  ]
204  ]
205  ],
206  ];
207 
208  $this->assertEquals(
209  $expectedData,
210  $this->getModel()->modifyData([])
211  );
212  }
213 }