Skip to content

[BrowserKit] Emulate back/forward browser navigation #22341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

e-moe
Copy link
Contributor

@e-moe e-moe commented Apr 7, 2017

Q A
Branch? master
Bug fix? yes
New feature? no
BC breaks? yes
Deprecations? no
Tests pass? yes
Fixed tickets #22336
License MIT
Doc PR CHANGELOG.md updated

Hi all, please review this code for emulating back/forward browser navigation (skip redirects). If code is ok I will add tests and docs

@e-moe e-moe force-pushed the ticket_22336 branch 3 times, most recently from 637abad to 46b8e7b Compare April 7, 2017 12:30
@e-moe e-moe changed the title [BrowserKit] don't save redirects in history [BrowserKit] Emulate back/forward browser navigation Apr 7, 2017
@e-moe e-moe force-pushed the ticket_22336 branch 3 times, most recently from 05f2c01 to 44df302 Compare April 7, 2017 12:56
@sstok
Copy link
Contributor

sstok commented Apr 7, 2017

As this is a new feature please rebase this on the master and change the pull-request target. Also this is missing some tests.

Status: needs work

@e-moe e-moe changed the base branch from 2.7 to master April 7, 2017 13:52
@e-moe
Copy link
Contributor Author

e-moe commented Apr 7, 2017

@sstok , thanks for your comment. PR target is changed to master.

as for tests - I'm waiting for implementation review/approval. if it's done correctly I will add missing tests

@@ -328,6 +340,8 @@ public function request($method, $uri, array $parameters = array(), array $files
}

if ($this->followRedirects && $this->redirect) {
$this->redirects[] = spl_object_hash($this->history->current());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should use the hash as key rather than value. the array is used to check whether it contains a given hash, and O(1) is better than than O(n)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

protected $followRedirects = true;
protected $browserNavigation = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use private visibility for any new property. We don't want to make them extension points yet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return $this->requestFromRequest($this->history->back(), false);
do {
$request = $this->history->back();
} while ($this->browserNavigation && in_array(spl_object_hash($request), $this->redirects));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we reach the beginning of the history ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

History object will throw \LogicException('You are already on the first page.');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the behavior will be the same

@e-moe e-moe force-pushed the ticket_22336 branch 2 times, most recently from f321c80 to 4c8af71 Compare April 7, 2017 14:23
@javiereguiluz
Copy link
Member

I'd like to ask if this is something that browsers do contrary to the standard ... or if this is the behavior proposed by the related standard. That way we'll know if this is a new feature or a bug. Thanks!

@e-moe
Copy link
Contributor Author

e-moe commented Apr 8, 2017

@javiereguiluz , maybe we can check this document: https://www.w3.org/TR/html5/browsers.html#history

@curry684
Copy link
Contributor

curry684 commented Apr 9, 2017

Assuming the BrowserKit client should conform to that standard it's a bug, as history.back() is defined to:

Goes back one step in the joint session history.

If there is no previous page, does nothing.

Joint session history is defined as (emphasis moved):

The joint session history of a top-level browsing context is the union of all the session histories of all browsing contexts of all the fully active Document objects that share that top-level browsing context, with all the entries that are current entries in their respective session histories removed except for the current entry of the joint session history.

As a redirect is no fully active Document object navigation should bypass it, hence it's a bug.

But also a BC breaking bug as there may be tons of tests out there explicitly going back twice in history to circumvent this behavior.

@e-moe
Copy link
Contributor Author

e-moe commented Apr 9, 2017

@curry684 , thanks for you findings! Sine this is a bugfix should I rebase this PR to 2.7 branch?

@fabpot
Copy link
Member

fabpot commented Apr 9, 2017

As it might break existing tests, I think this needs to be merged in master, even if this is a bug fix.

*
* @param bool $browserNavigation Whether to emulate browser navigation
*/
public function browserNavigation($browserNavigation = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not make it configurable as browsers don't make this configurable either. I understand that you probably want to be able to keep BC here. But I don't think that this is a good idea. The current behavior is wrong and we should fix it. What needs to be done though is to add a note in the CHANGELOG/UPGRADE files to document this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@curry684
Copy link
Contributor

curry684 commented Apr 9, 2017

Given the possibly severe BC breaks I'd also recommend considering this a functionality change, hence master.

@e-moe
Copy link
Contributor Author

e-moe commented Apr 10, 2017

Tests and docs added, comments applied.

Also, I changed spl_object_hash to serialize due to clone usage in History object: hashes was new all the time (see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/BrowserKit/History.php#L68)

@nicolas-grekas nicolas-grekas modified the milestone: 3.4 Apr 10, 2017
@@ -328,6 +329,8 @@ public function request($method, $uri, array $parameters = array(), array $files
}

if ($this->followRedirects && $this->redirect) {
$this->redirects[serialize($this->history->current())] = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you consider spl_object_hash instead of serialize? It should be way faster and less memory intensive.

http://php.net/manual/en/function.spl-object-hash.php

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above done by @e-moe explaining why it was changed.

@willemstuursma
Copy link

Anything I can do to make this go forward?

@fabpot
Copy link
Member

fabpot commented Jul 6, 2017

Thank you @e-moe.

fabpot added a commit that referenced this pull request Jul 6, 2017
…e-moe)

This PR was submitted for the master branch but it was merged into the 3.4 branch instead (closes #22341).

Discussion
----------

[BrowserKit] Emulate back/forward browser navigation

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22336
| License       | MIT
| Doc PR        | CHANGELOG.md updated

Hi all, please review this code for emulating back/forward browser navigation (skip redirects). If code is ok I will add tests and docs

Commits
-------

680da44 [BrowserKit] Emulate back/forward browser navigation
@fabpot fabpot closed this Jul 6, 2017
This was referenced Oct 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.