From e79ca23f2ce79687a5c782b67f5da208df344dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 8 Oct 2015 18:24:25 +0200 Subject: [PATCH] add test for complete activity crud (without errors) + fix some errors in controller (discovered with tests) --- Controller/ActivityController.php | 6 ++ Test/Fixtures/App/app/config/config.yml | 1 + Tests/Controller/ActivityControllerTest.php | 112 +++++++++++++++----- 3 files changed, 92 insertions(+), 27 deletions(-) diff --git a/Controller/ActivityController.php b/Controller/ActivityController.php index 7ff2e78ea..0a74497f9 100644 --- a/Controller/ActivityController.php +++ b/Controller/ActivityController.php @@ -44,6 +44,12 @@ class ActivityController extends Controller $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); + if ($person === NULL) { + throw $this->createNotFoundException('Person not found'); + } + + $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); + $reachableScopes = $this->get('chill.main.security.authorization.helper') ->getReachableScopes($this->getUser(), new Role('CHILL_ACTIVITY_SEE'), $person->getCenter()); diff --git a/Test/Fixtures/App/app/config/config.yml b/Test/Fixtures/App/app/config/config.yml index 971fe37b6..d799db6f8 100644 --- a/Test/Fixtures/App/app/config/config.yml +++ b/Test/Fixtures/App/app/config/config.yml @@ -67,6 +67,7 @@ security: intention: authenticate csrf_provider: form.csrf_provider logout: ~ + http_basic: access_control: #disable authentication for tests #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } diff --git a/Tests/Controller/ActivityControllerTest.php b/Tests/Controller/ActivityControllerTest.php index 6fd1dd06e..f3587f443 100644 --- a/Tests/Controller/ActivityControllerTest.php +++ b/Tests/Controller/ActivityControllerTest.php @@ -6,55 +6,113 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class ActivityControllerTest extends WebTestCase { - public function testToWrite() + + public function testAccessIsDeniedForUnauthenticated() { - $this->markTestSkipped(); + $client = $this->createClient(); + + $crawler = $client->request('GET', sprintf('fr/person/%d/activity/', + $this->getPersonFromFixtures()->getId())); + + $this->assertTrue($client->getResponse()->isRedirect('http://localhost/login'), + 'the page does not redirect to http://localhost/login'); } - /* public function testCompleteScenario() { // Create a new client to browse the application - $client = static::createClient(); + $client = $this->getAuthenticatedClient(); + $person = $this->getPersonFromFixtures(); // Create a new entry in the database - $crawler = $client->request('GET', '/activity/'); - $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /activity/"); - $crawler = $client->click($crawler->selectLink('Create a new entry')->link()); + $crawler = $client->request('GET', sprintf('en/person/%d/activity/', + $person->getId())); + $this->assertEquals(200, $client->getResponse()->getStatusCode(), + "Unexpected HTTP status code for GET /activity/"); + $crawler = $client->click($crawler->selectLink('Ajouter une nouvelle activité') + ->link()); // Fill in the form and submit it - $form = $crawler->selectButton('Create')->form(array( - 'chill_activitybundle_activity[field_name]' => 'Test', - // ... other fields to fill + $form = $crawler->selectButton('Ajouter une nouvelle activité')->form(array( + 'chill_activitybundle_activity'=> array( + 'date' => '15-01-2015', + 'durationTime' => array( + 'hour' => '1', + 'minute' => '30' + ), + 'remark' => 'blabla', + 'scope' => 1, + 'reason' => 2, + 'type' => 3 + ) )); $client->submit($form); + + $this->assertTrue($client->getResponse()->isRedirect()); $crawler = $client->followRedirect(); // Check data in the show view - $this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")'); + $this->assertGreaterThan(0, $crawler->filter('dd:contains("January 15, 2015")')->count(), + 'Missing element dd:contains("January 15, 2015")'); // Edit the entity - $crawler = $client->click($crawler->selectLink('Edit')->link()); - - $form = $crawler->selectButton('Update')->form(array( - 'chill_activitybundle_activity[field_name]' => 'Foo', - // ... other fields to fill + $crawler = $client->click($crawler->selectLink("Modifier l'activité")->link()); + + $form = $crawler->selectButton("Sauver l'activité")->form(array( + 'chill_activitybundle_activity' => array( + 'date' => '25-01-2015', + 'remark' => 'Foo' + ) )); $client->submit($form); + + $this->assertTrue($client->getResponse()->isRedirect()); + $crawler = $client->followRedirect(); - - // Check the element contains an attribute with value equals "Foo" - $this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]'); - - // Delete the entity - $client->submit($crawler->selectButton('Delete')->form()); - $crawler = $client->followRedirect(); - - // Check the entity has been delete on the list - $this->assertNotRegExp('/Foo/', $client->getResponse()->getContent()); + + // check that new data are present + $this->assertGreaterThan(0, + $crawler->filter('dd:contains("January 25, 2015")')->count(), + 'Missing element dd:contains("January 25, 2015")'); + $this->assertGreaterThan(0, + $crawler->filter('dd:contains("Foo")')->count(), + 'Missing element dd:contains("Foo")'); } - */ + /** + * + * @return \Symfony\Component\BrowserKit\Client + */ + private function getAuthenticatedClient() + { + return static::createClient(array(), array( + 'PHP_AUTH_USER' => 'center a_social', + 'PHP_AUTH_PW' => 'password', + )); + } + + /** + * + * @return \Chill\PersonBundle\Entity\Person + */ + private function getPersonFromFixtures() + { + $em = static::$kernel->getContainer() + ->get('doctrine.orm.entity_manager'); + + $person = $em->getRepository('ChillPersonBundle:Person') + ->findOneBy(array( + 'firstName' => 'Depardieu', + 'lastName' => 'Gérard' + )); + + if ($person === NULL) { + throw new \RuntimeException("We need a person with firstname Gérard and" + . " lastname Depardieu. Did you add fixtures ?"); + } + + return $person; + } }