PhpBrowser does not honouring <base href=""> meta tag #3764

Closed
hubbyist opened this Issue Nov 22, 2016 · 8 comments

Projects

None yet

2 participants

@hubbyist
hubbyist commented Nov 22, 2016 edited

What are you trying to achieve?

By using PhpBrowser trying to click an anchor link.
page url is in 'http://example.com/sitefolder/folder/service/panel' format.
page is containing <base href="http://example.com/sitefolder/folder/"/> tag.
anchor href attribute is "service/action".
after click operation current URL must be 'http://example.com/sitefolder/folder/service/action.

What do you get instead?

current URL becomes 'http://example.com/sitefolder/folder/service/service/action' as if there is no <base href=""> meta tag in the page. From a normal browser link is working.

Provide test source code if related

    $I->amOnPage('/service/panel');
    $I->seeLink('Action', 'service/action');
    $I->click(['link' => 'Action']);
    $I->seeCurrentUrlEquals('http://example.com/sitefolder/folder/service/action');

Details

  • Codeception version: Codeception version 2.2.6
  • PHP Version: PHP 7.0.8-0ubuntu0.16.04.3
  • Operating System: linux mint 18.0
  • Installation type: Phar
  • List of installed packages (composer show) : none
  • Suite configuration:
class_name: AcceptanceTester
modules:
    enabled:
        - Asserts
        - PhpBrowser:
            url: http://example.com/sitefolder/folder
        - \Module\RESTAPI:
            depends: PhpBrowser
        - Db:
            dsn: 'pgsql:host=localhost;dbname=dbname'
            user: 'username'
            password: 'password'
            dump: dump.sql
            populate: true
            cleanup: false
            reconnect: false
@hubbyist
hubbyist commented Nov 23, 2016 edited

It seems modifying click() function from InnerBrowser class, within a Module which extends \Codeception\Module\REST is nearly impossible due to lots of missing function errors caused by deep rooted dependency tree. No luck with getAbsoluteUrlFor() function either.

I have to clone whole InnerBrowser class into my custom module to fix this issue without modifying codeception itself I think. Any suggestions?

@hubbyist
hubbyist commented Nov 23, 2016 edited

Only related change since november 2015 seems to be:

function getAbsoluteUrlFor($uri)
    ....
---    if (empty($uri) || $uri === '#') 
+++    if (empty($uri) || $uri[0] === '#')
@hubbyist
hubbyist commented Nov 23, 2016 edited

By comparing 2.1.5 with 2.2.6 I found that anchor link processing code of click() function in InnerBrowser
class seem to be moved to openHrefFromDomNode() function.

v2.1.5 :

public function click($link, $context = null)
{
	....
	if (count($anchor)) {
		$this->crawler = $this->clientClick($anchor->first()->link());
		$this->forms = [];
		return;
	}
	...
}

v2.2.6:

public function click($link, $context = null)
{
	....
	if (count($anchor)) {
		$this->openHrefFromDomNode($anchor->getNode(0));
		return;
	}
	...
}

private function openHrefFromDomNode(\DOMNode $node)
{
	$this->amOnPage($this->getAbsoluteUrlFor($node->getAttribute('href')));
}

Does these two code block doing exactly the same thing?

@hubbyist
hubbyist commented Nov 23, 2016 edited

What is the source of Link class in clientClick function?

	protected function clientClick(Link $link)
	{
		...
		return $this->clientRequest($link->getMethod(), $link->getUri());
	}

It seems to have getUri() function of its own. May it be using the baseHref value from Crawler somehow?

@Naktibalda
Contributor

It is Symfony\Component\DomCrawler\Link.

Don't try to clone InnerBrowser, fix it instead.
Please add some tests for base href to https://github.com/Codeception/Codeception/blob/2.2/tests/unit/Codeception/Module/TestsForWeb.php
HTML goes to: https://github.com/Codeception/Codeception/tree/2.2/tests/data/app/view

Or I can try to fix it this weekend.

@hubbyist
hubbyist commented Nov 24, 2016 edited

Are these sufficient for testing?
hubbyist@b973637

And how can I directly trigger TestsForWeb.php or related group. I couldn't find the group definition for these tests?

@Naktibalda
Contributor

Your tests look good.
Run them with php codecept run tests/unit/Codeception/Module/PhpBrowserTest.php:Base

@Naktibalda Naktibalda added a commit to Naktibalda/Codeception that referenced this issue Nov 26, 2016
@Naktibalda Naktibalda [PhpBrowser][Frameworks] Honour base tag
Fixes #3764
98a3f90
@hubbyist
hubbyist commented Nov 28, 2016 edited

I have done work on the subject at hubbyist@812f0a3 in paralel. So if any portion of my work is usefull I can make a seperate PR of them.

@Naktibalda Naktibalda added a commit to Naktibalda/Codeception that referenced this issue Nov 29, 2016
@Naktibalda Naktibalda [PhpBrowser][Frameworks] Honour base tag
Fixes #3764
ac54709
@DavertMik DavertMik closed this in #3771 Nov 30, 2016
@DavertMik DavertMik added a commit that referenced this issue Nov 30, 2016
@Naktibalda @DavertMik Naktibalda + DavertMik [PhpBrowser][Frameworks] Honour base tag (#3771)
* [PhpBrowser][Frameworks] Honour base tag

Fixes #3764

* Bumped minimum version of DomCrawler to 2.7.5 because it doesn't honour base tag in lower versions
70d9598
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment