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

[Symfony2] getInternalDomains returns invalid pattern 'localhost' #2717

Closed
raziel057 opened this Issue Jan 13, 2016 · 5 comments

Comments

Projects
None yet
2 participants
@raziel057

raziel057 commented Jan 13, 2016

Hi,

Using module Symfony2 (DomCrawler):
Codeception PHP Testing Framework v2.1.5
Powered by PHPUnit 4.8.21 by Sebastian Bergmann and contributors.

I got the following error when I write a simple test like this.

public function authenticate(FunctionalTester $I)
{
    $I->setHost('organisation');
    $I->amOnPage('/secured/login');
    $I->submitForm('#login', [
        'username' => 'admin',
        'password' => 'xxxxx'
    ]);
}

I created the following custom Helper:

public function setHost($sitename)
{
    $host = $this->getModule('Symfony2')->container->getParameter('host.'.$sitename);
    $this->getModule('Symfony2')->client->setServerParameter('HTTP_HOST', $host);
}
[PHPUnit_Framework_Exception] preg_match(): Delimiter must not be alphanumeric or backslash                                                                        

Scenario Steps:
 3. $I->submitForm("#login",{"_username":"admin","_password":"xxxxx"})
 2. $I->amOnPage("/secured/login")
 1. $I->setHost("organisation")

#1  Codeception\Subscriber\ErrorHandler->errorHandler
#2  Codeception\Lib\InnerBrowser->submitForm
#3  /srv/ic2m/novento/src/PTC/NoventoBundle/tests/_support/_generated/FunctionalTesterActions.php:1372
#4  /srv/ic2m/novento/src/PTC/NoventoBundle/tests/functional/AuthenticationCest.php:37
#5  NoventoBundle\AuthenticationCest->authenticate

There is no problem when writing:

$I->seeInFormFields('#login', ['username' => '']);

Naktibalda added a commit to Naktibalda/Codeception that referenced this issue Jan 13, 2016

@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Jan 13, 2016

Member

I tried to reproduce your issue, but my test passes.

Please run your test with -vvv flag to get stacktrace with more lines.

Member

Naktibalda commented Jan 13, 2016

I tried to reproduce your issue, but my test passes.

Please run your test with -vvv flag to get stacktrace with more lines.

@raziel057

This comment has been minimized.

Show comment
Hide comment
@raziel057

raziel057 Jan 14, 2016

Thanks for your advice. With the verbose mode I can see that the problem comes from the fact I set a custom domain through a custom Helper.

public function setHost($sitename)
{
    $host = $this->getModule('Symfony2')->container->getParameter('host.'.$sitename);
    $this->getModule('Symfony2')->client->setServerParameter('HTTP_HOST', $host);
}

I encounter the problem in the following method:

protected function isInternalDomain($domain)
{
    if ($this->internalDomains === null) {
        $this->internalDomains = $this->getInternalDomains();
    }

    foreach ($this->internalDomains as $pattern) {
        if (preg_match($pattern, $domain)) {
            return true;
        }
    }
    return false;
}

I can see from this method:
$pattern == localhost
$domain == organisation.mywebsite.eu

So there is an issue with "pattern".

Here you can see the full result of testing:

Authenticate (AuthenticationCest::authenticate)
Scenario:
* I set host "organisation"
* I am on page "/secured/login"

  [Page] /secured/login
  [Response] 200
  [Request Cookies] []
  [Response Headers] {"cache-control":["no-cache"],"date":["Thu, 14 Jan 2016 07:39:25 GMT"],
"content-type":["text/html; charset=UTF-8"],"x-debug-token":["3dedbc"],"Set-Cookie":[{}]}
  [User] Anonymous
* I submit form "#login",{"_username":"admin","_password":"xxxxxx"}
  [Uri] http://organisation.mywebsite.eu/secured/login_check
  [Method] POST
  [Parameters] {"_username":"admin","_password":"xxxxxx","_csrf_token":"8L4_KnfF_ARv7PMhDCjmQpAYeyVyUQ7TJPAk4ou1p8s"}
 ERROR 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
----------------------------------------------------------------------------------------------------------------------------

Time: 1.09 seconds, Memory: 48.00Mb

There was 1 error:

---------
1) Failed to authenticate in NoventoBundle\AuthenticationCest::authenticate (tests/functional/AuthenticationCest.php)

  [PHPUnit_Framework_Exception] preg_match(): Delimiter must not be alphanumeric or backslash                                                                                            

Scenario Steps:

 3. $I->submitForm("#login",{"_username":"admin","_password":"xxxxxx"})
 2. $I->amOnPage("/secured/login")
 1. $I->setHost("organisation")

myapp_path/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:171
myapp_path/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:141
myapp_path/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:598
myapp_path/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:609
myapp_path/vendor/codeception/codeception/src/Codeception/Step.php:200
myapp_path/vendor/codeception/codeception/src/Codeception/TestCase/Shared/Actor.php:99
myapp_path/vendor/codeception/codeception/src/Codeception/Scenario.php:101
myapp_path/src/PTC/NoventoBundle/tests/_support/_generated/FunctionalTesterActions.php:1373
myapp_path/src/PTC/NoventoBundle/tests/functional/AuthenticationCest.php:34
myapp_path/vendor/codeception/codeception/src/Codeception/Lib/Di.php:112
myapp_path/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php:127
myapp_path/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php:136
myapp_path/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php:66
myapp_path/vendor/phpunit/phpunit/src/Framework/TestCase.php:908
myapp_path/vendor/phpunit/phpunit/src/Framework/TestCase.php:768
myapp_path/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
myapp_path/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
myapp_path/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
myapp_path/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php:89
myapp_path/vendor/codeception/codeception/src/Codeception/SuiteManager.php:153
myapp_path/vendor/codeception/codeception/src/Codeception/Codecept.php:203
myapp_path/vendor/codeception/codeception/src/Codeception/Codecept.php:172
myapp_path/vendor/codeception/codeception/src/Codeception/Command/Run.php:262
myapp_path/vendor/codeception/codeception/src/Codeception/Command/Run.php:189
myapp_path/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:257
myapp_path/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:874
myapp_path/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:195
myapp_path/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
myapp_path/vendor/codeception/codeception/codecept:28

raziel057 commented Jan 14, 2016

Thanks for your advice. With the verbose mode I can see that the problem comes from the fact I set a custom domain through a custom Helper.

public function setHost($sitename)
{
    $host = $this->getModule('Symfony2')->container->getParameter('host.'.$sitename);
    $this->getModule('Symfony2')->client->setServerParameter('HTTP_HOST', $host);
}

I encounter the problem in the following method:

protected function isInternalDomain($domain)
{
    if ($this->internalDomains === null) {
        $this->internalDomains = $this->getInternalDomains();
    }

    foreach ($this->internalDomains as $pattern) {
        if (preg_match($pattern, $domain)) {
            return true;
        }
    }
    return false;
}

I can see from this method:
$pattern == localhost
$domain == organisation.mywebsite.eu

So there is an issue with "pattern".

Here you can see the full result of testing:

Authenticate (AuthenticationCest::authenticate)
Scenario:
* I set host "organisation"
* I am on page "/secured/login"

  [Page] /secured/login
  [Response] 200
  [Request Cookies] []
  [Response Headers] {"cache-control":["no-cache"],"date":["Thu, 14 Jan 2016 07:39:25 GMT"],
"content-type":["text/html; charset=UTF-8"],"x-debug-token":["3dedbc"],"Set-Cookie":[{}]}
  [User] Anonymous
* I submit form "#login",{"_username":"admin","_password":"xxxxxx"}
  [Uri] http://organisation.mywebsite.eu/secured/login_check
  [Method] POST
  [Parameters] {"_username":"admin","_password":"xxxxxx","_csrf_token":"8L4_KnfF_ARv7PMhDCjmQpAYeyVyUQ7TJPAk4ou1p8s"}
 ERROR 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
----------------------------------------------------------------------------------------------------------------------------

Time: 1.09 seconds, Memory: 48.00Mb

There was 1 error:

---------
1) Failed to authenticate in NoventoBundle\AuthenticationCest::authenticate (tests/functional/AuthenticationCest.php)

  [PHPUnit_Framework_Exception] preg_match(): Delimiter must not be alphanumeric or backslash                                                                                            

Scenario Steps:

 3. $I->submitForm("#login",{"_username":"admin","_password":"xxxxxx"})
 2. $I->amOnPage("/secured/login")
 1. $I->setHost("organisation")

myapp_path/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:171
myapp_path/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:141
myapp_path/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:598
myapp_path/vendor/codeception/codeception/src/Codeception/Lib/InnerBrowser.php:609
myapp_path/vendor/codeception/codeception/src/Codeception/Step.php:200
myapp_path/vendor/codeception/codeception/src/Codeception/TestCase/Shared/Actor.php:99
myapp_path/vendor/codeception/codeception/src/Codeception/Scenario.php:101
myapp_path/src/PTC/NoventoBundle/tests/_support/_generated/FunctionalTesterActions.php:1373
myapp_path/src/PTC/NoventoBundle/tests/functional/AuthenticationCest.php:34
myapp_path/vendor/codeception/codeception/src/Codeception/Lib/Di.php:112
myapp_path/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php:127
myapp_path/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php:136
myapp_path/vendor/codeception/codeception/src/Codeception/TestCase/Cest.php:66
myapp_path/vendor/phpunit/phpunit/src/Framework/TestCase.php:908
myapp_path/vendor/phpunit/phpunit/src/Framework/TestCase.php:768
myapp_path/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
myapp_path/vendor/phpunit/phpunit/src/Framework/TestCase.php:724
myapp_path/vendor/phpunit/phpunit/src/Framework/TestSuite.php:747
myapp_path/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php:89
myapp_path/vendor/codeception/codeception/src/Codeception/SuiteManager.php:153
myapp_path/vendor/codeception/codeception/src/Codeception/Codecept.php:203
myapp_path/vendor/codeception/codeception/src/Codeception/Codecept.php:172
myapp_path/vendor/codeception/codeception/src/Codeception/Command/Run.php:262
myapp_path/vendor/codeception/codeception/src/Codeception/Command/Run.php:189
myapp_path/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:257
myapp_path/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:874
myapp_path/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:195
myapp_path/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
myapp_path/vendor/codeception/codeception/codecept:28
@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Jan 14, 2016

Member

Thank you,

I can't believe that it took so long to discover that issue.

Member

Naktibalda commented Jan 14, 2016

Thank you,

I can't believe that it took so long to discover that issue.

@Naktibalda Naktibalda changed the title from Problem with Form CSS selector to [Symfony2] getInternalDomains returns invalid pattern 'localhost' Jan 14, 2016

@Naktibalda

This comment has been minimized.

Show comment
Hide comment
@Naktibalda

Naktibalda Jan 14, 2016

Member

Removing 'localhost' from getInternalDomains should fix it

Member

Naktibalda commented Jan 14, 2016

Removing 'localhost' from getInternalDomains should fix it

@raziel057

This comment has been minimized.

Show comment
Hide comment
@raziel057

raziel057 Jan 14, 2016

Endeed, I tried and it works now. Thanks!

raziel057 commented Jan 14, 2016

Endeed, I tried and it works now. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment