Skip to content

Commit 1fa0951

Browse files
author
Aaron Gustavo Nieves
committed
Fix moveBack() to move the history pointer instead of appending
`moveBack()` replayed the previous request through `_loadPage()`, which called `clientRequest()` with the default `$changeHistory = true`. That appended a new entry to the BrowserKit history and discarded the forward entries, so going back behaved like a fresh navigation rather than a real Back button: the history pointer ended up on the last page again and a second `moveBack()` no longer walked further back. Thread a `$changeHistory` flag through `_loadPage()` (default `true`, so it is backward compatible) and pass `false` from `moveBack()`. The replayed request no longer mutates the history, matching Symfony's native `AbstractBrowser::back()` semantics. This also makes the framework history assertions (`assertBrowserHistoryIsNotOnLastPage()`) observable through the public `moveBack()` step.
1 parent 8af7f84 commit 1fa0951

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/Codeception/Lib/InnerBrowser.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,10 @@ public function _loadPage(
280280
array $parameters = [],
281281
array $files = [],
282282
array $server = [],
283-
?string $content = null
283+
?string $content = null,
284+
bool $changeHistory = true
284285
): void {
285-
$this->crawler = $this->clientRequest($method, $uri, $parameters, $files, $server, $content);
286+
$this->crawler = $this->clientRequest($method, $uri, $parameters, $files, $server, $content, $changeHistory);
286287
$this->baseUrl = $this->retrieveBaseUrl();
287288
$this->forms = [];
288289
}
@@ -2000,13 +2001,17 @@ public function moveBack(int $numberOfSteps = 1): void
20002001
), $exception->getCode(), $exception);
20012002
}
20022003

2004+
// Replay the previous request without appending to the history, so the
2005+
// browser history pointer moves back (like a real Back button) instead of
2006+
// pushing a new entry and discarding the forward entries.
20032007
$this->_loadPage(
20042008
$request->getMethod(),
20052009
$request->getUri(),
20062010
$request->getParameters(),
20072011
$request->getFiles(),
20082012
$request->getServer(),
2009-
$request->getContent()
2013+
$request->getContent(),
2014+
false
20102015
);
20112016
}
20122017

tests/data/app/db

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a:0:{}
1+
a:4:{s:6:"params";a:0:{}s:5:"query";a:0:{}s:4:"form";a:4:{s:8:"wireless";s:5:"mouse";s:6:"coffee";s:1:"0";s:3:"tea";s:1:"1";s:7:"vanilla";s:2:"on";}s:5:"files";a:0:{}}

tests/unit/Codeception/Module/FrameworksTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public function testMoveBackTwoSteps()
6868
$this->module->seeCurrentUrlEquals('/iframe');
6969
}
7070

71+
public function testMoveBackPreservesForwardHistory()
72+
{
73+
$this->module->amOnPage('/iframe');
74+
$this->module->amOnPage('/info');
75+
$this->module->amOnPage('/');
76+
$this->module->moveBack();
77+
$this->module->seeCurrentUrlEquals('/info');
78+
$this->module->moveBack();
79+
$this->module->seeCurrentUrlEquals('/iframe');
80+
}
81+
7182
public function testMoveBackThrowsExceptionIfNumberOfStepsIsInvalid()
7283
{
7384
$this->module->amOnPage('/iframe');

0 commit comments

Comments
 (0)