Skip to content

Added more tests for the backend #444

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

Merged
merged 1 commit into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions tests/AppBundle/Controller/Admin/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,55 @@
* Execute the application tests using this command (requires PHPUnit to be installed):
*
* $ cd your-symfony-project/
* $ phpunit -c app
* $ ./vendor/bin/phpunit
*/
class BlogControllerTest extends WebTestCase
{
public function testRegularUsersCannotAccessToTheBackend()
/**
* @dataProvider getUrlsForRegularUsers
*/
public function testRegularUsers($httpMethod, $url, $statusCode)
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'john_user',
'PHP_AUTH_PW' => 'kitten',
]);

$client->request('GET', '/en/admin/post/');
$client->request($httpMethod, $url);
$this->assertEquals($statusCode, $client->getResponse()->getStatusCode());
}

$this->assertEquals(Response::HTTP_FORBIDDEN, $client->getResponse()->getStatusCode());
public function getUrlsForRegularUsers()
{
yield ['GET', '/en/admin/post/', Response::HTTP_FORBIDDEN];
yield ['GET', '/en/admin/post/1', Response::HTTP_FORBIDDEN];
yield ['GET', '/en/admin/post/1/edit', Response::HTTP_FORBIDDEN];
yield ['POST', '/en/admin/post/1/delete', Response::HTTP_FORBIDDEN];
}

public function testAdministratorUsersCanAccessToTheBackend()
/**
* @dataProvider getUrlsForAdminUsers
*/
public function testAdminUsers($httpMethod, $url, $statusCode)
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'anna_admin',
'PHP_AUTH_PW' => 'kitten',
]);

$client->request('GET', '/en/admin/post/');
$client->request($httpMethod, $url);
$this->assertEquals($statusCode, $client->getResponse()->getStatusCode());
}

$this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
public function getUrlsForAdminUsers()
{
yield ['GET', '/en/admin/post/', Response::HTTP_OK];
yield ['GET', '/en/admin/post/1', Response::HTTP_OK];
yield ['GET', '/en/admin/post/1/edit', Response::HTTP_OK];
yield ['POST', '/en/admin/post/1/delete', Response::HTTP_FOUND];
Copy link
Member

Choose a reason for hiding this comment

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

wouldn't this break when running tests several times (or when changing the order of tests), due to changing the state of the DB ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch. I've opened #445 to improve this and check if we have any other related issue. Thanks!

Copy link
Member

@yceruto yceruto Jan 20, 2017

Choose a reason for hiding this comment

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

This test no change the DB status for now, because this redirect come from here.

Copy link
Member

@yceruto yceruto Jan 20, 2017

Choose a reason for hiding this comment

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

Only we checking here the security constraint for the author.

}

public function testIndex()
public function testBackendHomepage()
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'anna_admin',
Expand All @@ -70,26 +90,4 @@ public function testIndex()
'The backend homepage displays all the available posts.'
);
}

/**
* @dataProvider getPostUrls
*/
public function testOnlyAuthorCanAccessToThePostInTheBackend($method, $url, $statusCode)
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'anna_admin',
'PHP_AUTH_PW' => 'kitten',
]);

$client->request($method, $url);

$this->assertEquals($statusCode, $client->getResponse()->getStatusCode());
}

public function getPostUrls()
{
yield ['GET', '/en/admin/post/1', Response::HTTP_OK];
yield ['GET', '/en/admin/post/1/edit', Response::HTTP_OK];
yield ['POST', '/en/admin/post/1/delete', Response::HTTP_FOUND];
}
}
2 changes: 1 addition & 1 deletion tests/AppBundle/Controller/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* Execute the application tests using this command (requires PHPUnit to be installed):
*
* $ cd your-symfony-project/
* $ phpunit -c app
* $ ./vendor/bin/phpunit
*/
class BlogControllerTest extends WebTestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/AppBundle/Controller/DefaultControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Execute the application tests using this command (requires PHPUnit to be installed):
*
* $ cd your-symfony-project/
* $ phpunit -c app
* $ ./vendor/bin/phpunit
*/
class DefaultControllerTest extends WebTestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/AppBundle/Utils/SluggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Execute the application tests using this command (requires PHPUnit to be installed):
*
* $ cd your-symfony-project/
* $ phpunit -c app
* $ ./vendor/bin/phpunit
*/
class SluggerTest extends \PHPUnit_Framework_TestCase
{
Expand Down