Skip to content

Commit a4fa352

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [#7507] fix component name [#7490] minor typo fix Added a note about redirections to absolute URLs in tests Added the changes suggested by reviewers Fixed status code test Improved a test Merged and improved the articles about testing + authentication Reworded the section about form overridding Update installation.rst Update prepend_extension.rst Update override.rst Update installation.rst Update inheritance.rst Unify placeholders in documentation and code
2 parents 4cabfef + d72d8fc commit a4fa352

9 files changed

+102
-102
lines changed

_build/redirection_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,4 @@
331331
/deployment/tools /deployment
332332
/install/bundles /setup/bundles
333333
/form /forms
334+
/testing/simulating_authentication /testing/http_authentication

bundles/inheritance.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ The same goes for routing files and some other resources.
9393

9494
The overriding of resources only works when you refer to resources with
9595
the ``@FOSUserBundle/Resources/config/routing/security.xml`` method.
96-
If you refer to resources without using the ``@BundleName`` shortcut, they
97-
can't be overridden in this way.
96+
You need to use the ``@BundleName`` shortcut when referring to resources
97+
so they can be successfully overridden (except templates, which are
98+
overridden in a different way, as explained in :doc:`/templating/overriding`).
9899

99100
.. caution::
100101

bundles/installation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ version, include it as the second argument of the `composer require`_ command:
4848
B) Enable the Bundle
4949
--------------------
5050

51-
At this point, the bundle is installed in your Symfony project (in
51+
At this point, the bundle is installed in your Symfony project (e.g.
5252
``vendor/friendsofsymfony/``) and the autoloader recognizes its classes.
5353
The only thing you need to do now is register the bundle in ``AppKernel``::
5454

bundles/override.rst

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Services & Configuration
3939

4040
If you want to modify service definitions of another bundle, you can use a compiler
4141
pass to change the class of the service or to modify method calls. In the following
42-
example, the implementing class for the ``original-service-id`` is changed to
42+
example, the implementing class for the ``original-service-id`` is changed to
4343
``Acme\DemoBundle\YourService``::
4444

4545
// src/Acme/DemoBundle/DependencyInjection/Compiler/OverrideServiceCompilerPass.php
@@ -72,16 +72,8 @@ associations. Learn more about this feature and its limitations in
7272
Forms
7373
-----
7474

75-
As of Symfony 2.8, form types are referred to by their fully-qualified class name::
76-
77-
$builder->add('name', CustomType::class);
78-
79-
This means that you cannot override this by creating a sub-class of ``CustomType``
80-
and registering it as a service, and tagging it with ``form.type`` (you *could*
81-
do this in earlier version).
82-
83-
Instead, you should use a "form type extension" to modify the existing form type.
84-
For more information, see :doc:`/form/create_form_type_extension`.
75+
Existing form types can be modified defining
76+
:doc:`form type extensions </form/create_form_type_extension>`.
8577

8678
.. _override-validation:
8779

@@ -92,7 +84,7 @@ Symfony loads all validation configuration files from every bundle and
9284
combines them into one validation metadata tree. This means you are able to
9385
add new constraints to a property, but you cannot override them.
9486

95-
To override this, the 3rd party bundle needs to have configuration for
87+
To overcome this, the 3rd party bundle needs to have configuration for
9688
:doc:`validation groups </validation/groups>`. For instance, the FOSUserBundle
9789
has this configuration. To create your own validation, add the constraints
9890
to a new validation group:

bundles/prepend_extension.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
single: Configuration; Semantic
33
single: Bundle; Extension configuration
44

5-
How to Simplify Configuration of multiple Bundles
5+
How to Simplify Configuration of Multiple Bundles
66
=================================================
77

88
When building reusable and extensible applications, developers are often
@@ -12,9 +12,9 @@ users to choose to remove functionality they are not using. Creating multiple
1212
bundles has the drawback that configuration becomes more tedious and settings
1313
often need to be repeated for various bundles.
1414

15-
Using the below approach, it is possible to remove the disadvantage of the
16-
multiple bundle approach by enabling a single Extension to prepend the settings
17-
for any bundle. It can use the settings defined in the ``app/config/config.yml``
15+
It is possible to remove the disadvantage of the multiple bundle approach
16+
by enabling a single Extension to prepend the settings for any bundle.
17+
It can use the settings defined in the ``app/config/config.yml``
1818
to prepend settings just as if they had been written explicitly by
1919
the user in the application configuration.
2020

testing.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,9 @@ document::
285285

286286
// Assert that the response is a redirect to /demo/contact
287287
$this->assertTrue(
288-
$client->getResponse()->isRedirect('/demo/contact'),
289-
'response is a redirect to /demo/contact'
288+
$client->getResponse()->isRedirect('/demo/contact')
289+
// if the redirection URL was generated as an absolute URL
290+
// $client->getResponse()->isRedirect('http://localhost/demo/contact')
290291
);
291292
// ...or simply check that the response is a redirect to any URL
292293
$this->assertTrue($client->getResponse()->isRedirect());

testing/http_authentication.rst

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@
44
How to Simulate HTTP Authentication in a Functional Test
55
========================================================
66

7-
If your application needs HTTP authentication, pass the username and password
8-
as server variables to ``createClient()``::
7+
Authenticating requests in functional tests can slow down the entire test suite.
8+
This could become an issue especially when the tests reproduce the same steps
9+
that users follow to authenticate, such as submitting a login form or using
10+
OAuth authentication services.
911

10-
$client = static::createClient(array(), array(
11-
'PHP_AUTH_USER' => 'username',
12-
'PHP_AUTH_PW' => 'pa$$word',
13-
));
12+
This article explains the two most popular techniques to avoid these issues and
13+
create fast tests when using authentication.
1414

15-
You can also override it on a per request basis::
15+
Using a Faster Authentication Mechanism Only for Tests
16+
------------------------------------------------------
1617

17-
$client->request('DELETE', '/post/12', array(), array(), array(
18-
'PHP_AUTH_USER' => 'username',
19-
'PHP_AUTH_PW' => 'pa$$word',
20-
));
18+
When your application is using a ``form_login`` authentication, you can make
19+
your tests faster by allowing them to use HTTP authentication. This way your
20+
tests authenticate with the simple and fast HTTP Basic method whilst your real
21+
users still log in via the normal login form.
2122

22-
When your application is using a ``form_login``, you can simplify your tests
23-
by allowing your test configuration to make use of HTTP authentication. This
24-
way you can use the above to authenticate in tests, but still have your users
25-
log in via the normal ``form_login``. The trick is to include the ``http_basic``
26-
key in your firewall, along with the ``form_login`` key:
23+
The trick is to use the ``http_basic`` authentication in your application
24+
firewall, but only in the configuration file used by tests:
2725

2826
.. configuration-block::
2927

@@ -54,3 +52,72 @@ key in your firewall, along with the ``form_login`` key:
5452
),
5553
),
5654
));
55+
56+
Tests can now authenticate via HTTP passing the username and password as server
57+
variables using the second argument of ``createClient()``::
58+
59+
$client = static::createClient(array(), array(
60+
'PHP_AUTH_USER' => 'username',
61+
'PHP_AUTH_PW' => 'pa$$word',
62+
));
63+
64+
The username and password can also be passed on a per request basis::
65+
66+
$client->request('DELETE', '/post/12', array(), array(), array(
67+
'PHP_AUTH_USER' => 'username',
68+
'PHP_AUTH_PW' => 'pa$$word',
69+
));
70+
71+
Creating the Authentication Token
72+
---------------------------------
73+
74+
If your application uses a more advanced authentication mechanism, you can't
75+
use the previous trick, but it's still possible to make tests faster. The trick
76+
now is to bypass the authentication process, create the *authentication token*
77+
yourself and store it in the session.
78+
79+
This technique requires some knowledge of the Security component internals,
80+
but the following example shows a complete example that you can adapt to your
81+
needs::
82+
83+
// src/AppBundle/Tests/Controller/DefaultControllerTest.php
84+
namespace Appbundle\Tests\Controller;
85+
86+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
87+
use Symfony\Component\BrowserKit\Cookie;
88+
use Symfony\Component\HttpFoundation\Response;
89+
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
90+
91+
class DefaultControllerTest extends WebTestCase
92+
{
93+
private $client = null;
94+
95+
public function setUp()
96+
{
97+
$this->client = static::createClient();
98+
}
99+
100+
public function testSecuredHello()
101+
{
102+
$this->logIn();
103+
$crawler = $this->client->request('GET', '/admin');
104+
105+
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
106+
$this->assertSame('Admin Dashboard', $crawler->filter('h1')->text());
107+
}
108+
109+
private function logIn()
110+
{
111+
$session = $this->client->getContainer()->get('session');
112+
113+
// the firewall context defaults to the firewall name
114+
$firewallContext = 'secured_area';
115+
116+
$token = new UsernamePasswordToken('admin', null, $firewallContext, array('ROLE_ADMIN'));
117+
$session->set('_security_'.$firewallContext, serialize($token));
118+
$session->save();
119+
120+
$cookie = new Cookie($session->getName(), $session->getId());
121+
$this->client->getCookieJar()->set($cookie);
122+
}
123+
}

testing/simulating_authentication.rst

Lines changed: 0 additions & 62 deletions
This file was deleted.

validation/custom_constraint.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
2424
*/
2525
class ContainsAlphanumeric extends Constraint
2626
{
27-
public $message = 'The string "%string%" contains an illegal character: it can only contain letters or numbers.';
27+
public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
2828
}
2929

3030
.. note::
@@ -67,14 +67,14 @@ The validator class is also simple, and only has one required method ``validate(
6767
if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {
6868
// If you're using the new 2.5 validation API (you probably are!)
6969
$this->context->buildViolation($constraint->message)
70-
->setParameter('%string%', $value)
70+
->setParameter('{{ string }}', $value)
7171
->addViolation();
7272

7373
// If you're using the old 2.4 validation API
7474
/*
7575
$this->context->addViolation(
7676
$constraint->message,
77-
array('%string%' => $value)
77+
array('{{ string }}' => $value)
7878
);
7979
*/
8080
}

0 commit comments

Comments
 (0)