Skip to content

Commit 1a843af

Browse files
committed
MC-40538: When entering example.com/0 into browser the homepage is shown (example.com)
1 parent 90cb51c commit 1a843af

File tree

2 files changed

+120
-64
lines changed

2 files changed

+120
-64
lines changed

lib/internal/Magento/Framework/App/Router/Base.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,16 @@ protected function matchModuleFrontName(\Magento\Framework\App\RequestInterface
202202
// get module name
203203
if ($request->getModuleName()) {
204204
$moduleFrontName = $request->getModuleName();
205-
} elseif (!empty($param)) {
205+
} elseif (strlen($param)) {
206206
$moduleFrontName = $param;
207207
} else {
208208
$moduleFrontName = $this->_defaultPath->getPart('module');
209209
$request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, '');
210+
if (!$moduleFrontName) {
211+
return null;
212+
}
210213
}
211-
if (!$moduleFrontName) {
212-
return null;
213-
}
214+
214215
return $moduleFrontName;
215216
}
216217

@@ -270,15 +271,14 @@ protected function getNotFoundAction($currentModuleName)
270271
protected function matchAction(\Magento\Framework\App\RequestInterface $request, array $params)
271272
{
272273
$moduleFrontName = $this->matchModuleFrontName($request, $params['moduleFrontName']);
273-
if (empty($moduleFrontName)) {
274+
if (!strlen($moduleFrontName)) {
274275
return null;
275276
}
276277

277278
/**
278279
* Searching router args by module name from route using it as key
279280
*/
280281
$modules = $this->_routeConfig->getModulesByFrontName($moduleFrontName);
281-
282282
if (empty($modules) === true) {
283283
return null;
284284
}

lib/internal/Magento/Framework/App/Test/Unit/Router/BaseTest.php

Lines changed: 114 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,17 @@ protected function setUp(): void
9898
* @param MockObject|Http $requestMock
9999
* @param string $defaultPath
100100
* @param string $moduleFrontName
101-
* @param string $actionPath
102-
* @param string $actionName
103-
* @param string $moduleName
101+
* @param string|null $actionPath
102+
* @param string|null $actionName
103+
* @param string|null $moduleName
104104
*/
105105
public function testMatch(
106106
MockObject $requestMock,
107107
string $defaultPath,
108108
string $moduleFrontName,
109-
string $actionPath,
110-
string $actionName,
111-
string $moduleName
109+
?string $actionPath,
110+
?string $actionName,
111+
?string $moduleName
112112
) {
113113
$actionInstance = 'Magento_TestFramework_ActionInstance';
114114

@@ -153,87 +153,143 @@ public function matchDataProvider(): array
153153
$actionPath = 'action_path';
154154
$actionName = 'action_name';
155155
$moduleName = 'module_name';
156-
$paramList = $moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/';
157156

158157
$requestMock = $this->createMock(Http::class);
159158
$requestMock->expects($this->atLeastOnce())->method('getModuleName')->willReturn($moduleFrontName);
160159
$requestMock->expects($this->atLeastOnce())->method('getControllerName')->willReturn($actionPath);
161160
$requestMock->expects($this->atLeastOnce())->method('getActionName')->willReturn($actionName);
162-
$requestMock->expects($this->atLeastOnce())->method('getPathInfo')->willReturn($paramList);
161+
$requestMock->expects($this->atLeastOnce())
162+
->method('getPathInfo')
163+
->willReturn($moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/');
163164

164165
$emptyRequestMock = $this->createMock(Http::class);
165166
$emptyRequestMock->expects($this->atLeastOnce())->method('getModuleName')->willReturn('');
166167
$emptyRequestMock->expects($this->atLeastOnce())->method('getControllerName')->willReturn('');
167168
$emptyRequestMock->expects($this->atLeastOnce())->method('getActionName')->willReturn('');
168169
$emptyRequestMock->expects($this->atLeastOnce())->method('getPathInfo')->willReturn('');
169170

170-
$emptyRequestMock2 = clone $emptyRequestMock;
171-
$emptyRequestMock2->expects($this->once())->method('getOriginalPathInfo')->willReturn('');
171+
$emptyRequestMock2 = $this->createMock(Http::class);
172+
$emptyRequestMock2->expects($this->atLeastOnce())->method('getModuleName')->willReturn('');
173+
$emptyRequestMock2->expects($this->atLeastOnce())->method('getControllerName')->willReturn('');
174+
$emptyRequestMock2->expects($this->atLeastOnce())->method('getActionName')->willReturn('');
175+
$emptyRequestMock2->expects($this->atLeastOnce())->method('getPathInfo')->willReturn('');
176+
$emptyRequestMock2->expects($this->atLeastOnce())->method('getOriginalPathInfo')->willReturn('');
172177

173178
return [
174-
[$requestMock, '', $moduleFrontName, $actionPath, $actionName, $moduleName],
175-
[$emptyRequestMock, $paramList, $moduleFrontName, $actionPath, $actionName, $moduleName],
176-
[$emptyRequestMock2, '', $moduleFrontName, $actionPath, $actionName, $moduleName],
179+
[
180+
$requestMock,
181+
'val1/val2/val3/',
182+
$moduleFrontName,
183+
$actionPath,
184+
$actionName,
185+
$moduleName
186+
],
187+
[
188+
$emptyRequestMock,
189+
$moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/',
190+
$moduleFrontName,
191+
$actionPath,
192+
$actionName,
193+
$moduleName
194+
],
195+
[
196+
$emptyRequestMock2,
197+
'',
198+
$moduleFrontName,
199+
$actionPath,
200+
$actionName,
201+
$moduleName
202+
],
177203
];
178204
}
179205

180-
public function testMatchEmptyModuleList()
181-
{
182-
$moduleFrontName = 'module front name';
183-
$actionPath = 'action path';
184-
$actionName = 'action name';
185-
$paramList = $moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/';
206+
/**
207+
* @dataProvider matchEmptyActionDataProvider
208+
* @param MockObject|Http $requestMock
209+
* @param string $defaultPath
210+
* @param string $moduleFrontName
211+
* @param string|null $actionPath
212+
* @param string|null $actionName
213+
* @param string|null $moduleName
214+
*/
215+
public function testMatchEmptyAction(
216+
MockObject $requestMock,
217+
string $defaultPath,
218+
string $moduleFrontName,
219+
?string $actionPath,
220+
?string $actionName,
221+
?string $moduleName
222+
) {
223+
$defaultReturnMap = [
224+
['module', $moduleFrontName],
225+
['controller', $actionPath],
226+
['action', $actionName],
227+
];
228+
$this->defaultPathMock->method('getPart')
229+
->willReturnMap($defaultReturnMap);
230+
$this->pathConfigMock->method('getDefaultPath')
231+
->willReturn($defaultPath);
186232

187-
$requestMock = $this->createMock(Http::class);
188-
$requestMock->expects($this->atLeastOnce())
189-
->method('getModuleName')
190-
->willReturn($moduleFrontName);
191-
$requestMock->expects($this->atLeastOnce())
192-
->method('getPathInfo')
193-
->willReturn($paramList);
194233
$this->routeConfigMock->expects($this->once())
195234
->method('getModulesByFrontName')
196235
->with($moduleFrontName)
197-
->willReturn([]);
198-
$this->actionListMock->expects($this->never())->method('get');
199-
$this->actionFactoryMock->expects($this->never())->method('create');
236+
->willReturn($moduleName ? [$moduleName] : []);
237+
$this->actionFactoryMock->expects($this->never())
238+
->method('create');
200239

201240
$this->assertNull($this->model->match($requestMock));
202241
}
203242

204-
public function testMatchEmptyActionInstance()
243+
public function matchEmptyActionDataProvider(): array
205244
{
206-
$moduleFrontName = 'module front name';
207-
$actionPath = 'action path';
208-
$actionName = 'action name';
209-
$moduleName = 'module name';
210-
$paramList = $moduleFrontName . '/' . $actionPath . '/' . $actionName . '/key/val/key2/val2/';
245+
$moduleFrontName = 'module_front_name';
246+
$actionPath = 'action_path';
247+
$actionName = 'action_name';
211248

212-
$requestMock = $this->createMock(Http::class);
213-
$requestMock->expects($this->atLeastOnce())
214-
->method('getModuleName')
215-
->willReturn($moduleFrontName);
216-
$requestMock->expects($this->atLeastOnce())
249+
$requestMock1 = $this->createMock(Http::class);
250+
$requestMock1->expects($this->atLeastOnce())->method('getModuleName')->willReturn($moduleFrontName);
251+
$requestMock1->expects($this->atLeastOnce())->method('getControllerName')->willReturn($actionPath);
252+
$requestMock1->expects($this->atLeastOnce())->method('getActionName')->willReturn($actionName);
253+
$requestMock1->expects($this->atLeastOnce())
217254
->method('getPathInfo')
218-
->willReturn($paramList);
219-
$requestMock->expects($this->atLeastOnce())
220-
->method('getControllerName')
221-
->willReturn($actionPath);
222-
$requestMock->expects($this->once())
223-
->method('getActionName')
224-
->willReturn($actionName);
225-
$this->routeConfigMock->expects($this->once())
226-
->method('getModulesByFrontName')
227-
->with($moduleFrontName)
228-
->willReturn([$moduleName]);
229-
$this->actionListMock->expects($this->once())
230-
->method('get')
231-
->with($moduleName)
232-
->willReturn(null);
233-
$this->actionFactoryMock->expects($this->never())
234-
->method('create');
255+
->willReturn($moduleFrontName . '/' . $actionPath . '/' . $actionName . '/');
235256

236-
$this->assertNull($this->model->match($requestMock));
257+
$requestMock2 = $this->createMock(Http::class);
258+
$requestMock2->expects($this->atLeastOnce())->method('getModuleName')->willReturn($moduleFrontName);
259+
$requestMock2->expects($this->atLeastOnce())
260+
->method('getPathInfo')
261+
->willReturn($moduleFrontName . '/' . $actionPath . '/' . $actionName . '/');
262+
263+
$requestMock3 = $this->createMock(Http::class);
264+
$requestMock3->expects($this->atLeastOnce())->method('getModuleName')->willReturn('');
265+
$requestMock3->expects($this->atLeastOnce())->method('getPathInfo')->willReturn('0');
266+
267+
return [
268+
[
269+
$requestMock1,
270+
'',
271+
$moduleFrontName,
272+
$actionPath,
273+
$actionName,
274+
'module_name',
275+
],
276+
[
277+
$requestMock2,
278+
'',
279+
$moduleFrontName,
280+
$actionPath,
281+
$actionName,
282+
null,
283+
],
284+
[
285+
$requestMock3,
286+
$moduleFrontName . '/' . $actionPath . '/' . $actionName . '/',
287+
'0',
288+
null,
289+
null,
290+
null
291+
],
292+
];
237293
}
238294

239295
public function testGetActionClassName()

0 commit comments

Comments
 (0)