add test for complete activity crud (without errors)

+ fix some errors in controller (discovered with tests)
This commit is contained in:
Julien Fastré 2015-10-08 18:24:25 +02:00
parent fa5da95bf1
commit e79ca23f2c
3 changed files with 92 additions and 27 deletions

View File

@ -44,6 +44,12 @@ class ActivityController extends Controller
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); $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') $reachableScopes = $this->get('chill.main.security.authorization.helper')
->getReachableScopes($this->getUser(), new Role('CHILL_ACTIVITY_SEE'), ->getReachableScopes($this->getUser(), new Role('CHILL_ACTIVITY_SEE'),
$person->getCenter()); $person->getCenter());

View File

@ -67,6 +67,7 @@ security:
intention: authenticate intention: authenticate
csrf_provider: form.csrf_provider csrf_provider: form.csrf_provider
logout: ~ logout: ~
http_basic:
access_control: access_control:
#disable authentication for tests #disable authentication for tests
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

View File

@ -6,55 +6,113 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ActivityControllerTest extends 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() public function testCompleteScenario()
{ {
// Create a new client to browse the application // Create a new client to browse the application
$client = static::createClient(); $client = $this->getAuthenticatedClient();
$person = $this->getPersonFromFixtures();
// Create a new entry in the database // Create a new entry in the database
$crawler = $client->request('GET', '/activity/'); $crawler = $client->request('GET', sprintf('en/person/%d/activity/',
$this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /activity/"); $person->getId()));
$crawler = $client->click($crawler->selectLink('Create a new entry')->link()); $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 // Fill in the form and submit it
$form = $crawler->selectButton('Create')->form(array( $form = $crawler->selectButton('Ajouter une nouvelle activité')->form(array(
'chill_activitybundle_activity[field_name]' => 'Test', 'chill_activitybundle_activity'=> array(
// ... other fields to fill 'date' => '15-01-2015',
'durationTime' => array(
'hour' => '1',
'minute' => '30'
),
'remark' => 'blabla',
'scope' => 1,
'reason' => 2,
'type' => 3
)
)); ));
$client->submit($form); $client->submit($form);
$this->assertTrue($client->getResponse()->isRedirect());
$crawler = $client->followRedirect(); $crawler = $client->followRedirect();
// Check data in the show view // 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 // Edit the entity
$crawler = $client->click($crawler->selectLink('Edit')->link()); $crawler = $client->click($crawler->selectLink("Modifier l'activité")->link());
$form = $crawler->selectButton('Update')->form(array( $form = $crawler->selectButton("Sauver l'activité")->form(array(
'chill_activitybundle_activity[field_name]' => 'Foo', 'chill_activitybundle_activity' => array(
// ... other fields to fill 'date' => '25-01-2015',
'remark' => 'Foo'
)
)); ));
$client->submit($form); $client->submit($form);
$this->assertTrue($client->getResponse()->isRedirect());
$crawler = $client->followRedirect(); $crawler = $client->followRedirect();
// Check the element contains an attribute with value equals "Foo" // check that new data are present
$this->assertGreaterThan(0, $crawler->filter('[value="Foo"]')->count(), 'Missing element [value="Foo"]'); $this->assertGreaterThan(0,
$crawler->filter('dd:contains("January 25, 2015")')->count(),
// Delete the entity 'Missing element dd:contains("January 25, 2015")');
$client->submit($crawler->selectButton('Delete')->form()); $this->assertGreaterThan(0,
$crawler = $client->followRedirect(); $crawler->filter('dd:contains("Foo")')->count(),
'Missing element dd:contains("Foo")');
// Check the entity has been delete on the list
$this->assertNotRegExp('/Foo/', $client->getResponse()->getContent());
} }
*/ /**
*
* @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;
}
} }