Search code examples
symfonysymfony-security

How can I test a page behind a security firewall in Symfony?


In Symfony's cookbook, there is a page entitled How to Simulate Authentication with a Token in a Functional Test. In it, it is said that:

The technique described in How to Simulate HTTP Authentication in a Functional Test is cleaner and therefore the preferred way.

Also, on the page that the quotation above links to, the documentation says:

The trick is to include the http_basic key in your firewall, along with the form_login key

This tells me that it is all right to have the form_login key, along with the http_basic key, and somehow http_basic should take precedence.

Here is my config_test.yml configuration file:

imports:
    - { resource: config_dev.yml }

framework:
    test: ~
    session:
        storage_id: session.storage.mock_file
    profiler:
        collect: false

web_profiler:
    toolbar: false
    intercept_redirects: false

swiftmailer:
    disable_delivery: true

liip_functional_test:
    cache_sqlite_db: true

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_sqlite
                path:     %kernel.cache_dir%/test.db

security:
    firewalls:
        default:
            http_basic: ~

However, when I open my application in the test environment, I still get redirected to the login_form URL.

Why isn't setting the http_basic acting like the documentation says it should, namely it getting activated rather than form_login?


Solution

  • As commented here, having the code I pasted in my original question works just fine. The reason it is loading the login form is because I am not logging in via http_basic. In other words, when I have both form_login and http_basic enabled, I can login both by providing the PHP_AUTH_USER/PHP_AUTH_PASSWORD, and by logging in through the form. In effect, I don't need different security_*.yml files; I just need to add http_basic: ~ to the already-defined firewall.