123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- <?php
-
- namespace Muzich\CoreBundle\Tests\Controller;
-
- use Muzich\CoreBundle\lib\FunctionalTest;
- use Muzich\CoreBundle\Entity\RegistrationToken;
-
- class UserControllerTest extends FunctionalTest
- {
-
- public function testTagsFavoritesSuccess()
- {
- /**
- * Inscription d'un utilisateur
- */
- $this->client = self::createClient();
-
- $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
- $tribe_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
-
- $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
- $this->isResponseSuccess();
-
- // On a besoin d'un token pour le moment
- $token = new RegistrationToken();
- $token->setToken('hekt78yl789dzafdfz');
- $em = $this->getDoctrine()->getEntityManager();
- $em->persist($token);
- $em->flush();
-
- $this->procedure_registration_success(
- 'raoulc',
- 'raoulc.def4v65sds@gmail.com',
- 'toor',
- 'toor',
- 'hekt78yl789dzafdfz'
- );
-
- // Il ne doit y avoir aucun enregistrements de tags favoris
- $Favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $this->getUser()->getId()
- ))
- ;
-
- $this->assertEquals(0, count($Favorites));
-
- // On a attérit sur la page de présentation et de sleection des tags favoris
- $this->exist('form[action="'.($url = $this->generateUrl('update_tag_favorites')).'"]');
-
- $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
- $form['tag_favorites_form[tags]'] = json_encode(array($hardtek_id,$tribe_id));
- $this->submit($form);
-
- $this->isResponseRedirection();
- $this->followRedirection();
- $this->isResponseSuccess();
-
- // Désormais il y a deux tags favoris pour cet utilisateur
- $Favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $this->getUser()->getId()
- ))
- ;
- $this->assertEquals(2, count($Favorites));
-
- $Favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $this->getUser()->getId(),
- 'tag' => $hardtek_id
- ))
- ;
- $this->assertEquals(1, count($Favorites));
-
- $Favorites = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $this->getUser()->getId(),
- 'tag' => $tribe_id
- ))
- ;
- $this->assertEquals(1, count($Favorites));
- }
-
- /**
- * Test du changement de mot de passe par le baisis de la page 'Mon compte'
- */
- public function testChangePassword()
- {
- $this->client = self::createClient();
- $this->connectUser('bux', 'toor');
-
- // Ouverture de la page Mon compte
- $this->crawler = $this->client->request('GET', $this->generateUrl('my_account'));
-
- $this->exist('form[action="'.($url = $this->generateUrl(
- 'change_password'
- )).'"]');
- $this->exist('form[action="'.$url.'"] input[id="fos_user_change_password_form_current"]');
- $this->exist('form[action="'.$url.'"] input[id="fos_user_change_password_form_new_first"]');
- $this->exist('form[action="'.$url.'"] input[id="fos_user_change_password_form_new_second"]');
- $this->exist('form[action="'.$url.'"] input[type="submit"]');
-
- $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
- $form['fos_user_change_password_form[current]'] = 'toor';
- $form['fos_user_change_password_form[new][first]'] = 'trololo';
- $form['fos_user_change_password_form[new][second]'] = 'trololo';
- $this->submit($form);
-
- $this->isResponseRedirection();
- $this->followRedirection();
- $this->isResponseSuccess();
-
- // On se déconnecte
- $this->disconnectUser();
-
- // Et on se connecte avec le nouveau mot de passe
- $this->connectUser('bux', 'trololo');
- }
-
- /**
- * Test du formulaire de mise a jour des tags par le baisis de la page 'Mon compte'
- */
- public function testUpdateFavoriteTags()
- {
- $this->client = self::createClient();
- $this->connectUser('bob', 'toor');
-
- // Ouverture de la page Mon compte
- $this->crawler = $this->client->request('GET', $this->generateUrl('my_account'));
-
- $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
- $tribe_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
-
- // Bob n'a aucun tag préféré
- $prefereds = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array('user' => $this->getUser()->getId()))
- ;
-
- $this->assertEquals(0, count($prefereds));
-
- $this->exist('form[action="'.($url = $this->generateUrl(
- 'update_tag_favorites', array('redirect' => 'account')
- )).'"]');
- $this->exist('form[action="'.$url.'"] input[type="submit"]');
-
- $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
- $form['tag_favorites_form[tags]'] = json_encode(array($hardtek_id,$tribe_id));
- $this->submit($form);
-
- $this->isResponseRedirection();
- $this->followRedirection();
- $this->isResponseSuccess();
-
- // On a été redirigé sur la page Mon compte
- $this->exist('form[action="'.$url.'"]');
-
- // On vérifie la présence en base des enregistrements
- $prefereds = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array('user' => $this->getUser()->getId()))
- ;
-
- $this->assertEquals(2, count($prefereds));
-
- // On vérifie la présence en base des enregistrements
- $prefereds = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $this->getUser()->getId(),
- 'tag' => $hardtek_id
- ))
- ;
-
- $this->assertEquals(1, count($prefereds));
-
- // On vérifie la présence en base des enregistrements
- $prefereds = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $this->getUser()->getId(),
- 'tag' => $tribe_id
- ))
- ;
-
- $this->assertEquals(1, count($prefereds));
- }
-
- /**
- * Test de al procédure de changement d'email.
- */
- public function testChangeEmail()
- {
- $this->client = self::createClient();
- $this->connectUser('bob', 'toor');
- $bob = $this->findUserByUsername('bob');
-
- // Ouverture de la page Mon compte
- $this->crawler = $this->client->request('GET', $this->generateUrl('my_account'));
-
- // Le mail en cours n'est pas celui que nous voulons mettre
- $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
- // Nous n'avons pas encore demandé de nouveau mail
- $this->assertTrue($bob->getEmailRequested() == null);
- $this->assertTrue($bob->getEmailRequestedDatetime() == null);
-
- // On fait un premier essaie avec une email mal formulé
- $this->exist('form[action="'.($url = $this->generateUrl(
- 'change_email_request'
- )).'"]');
- $this->exist('form[action="'.$url.'"] input[type="submit"]');
-
- $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
- $form['form[email]'] = 'trololololooo@trolo';
- $this->submit($form);
-
- // Il n'y as pas de redirection
- $this->isResponseSuccess();
-
- $bob = $this->findUserByUsername('bob');
- // Les champs n'ont pas bougés
- $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
- // Nous n'avons pas encore demandé de nouveau mail
- $this->assertTrue($bob->getEmailRequested() == null);
- $this->assertTrue($bob->getEmailRequestedDatetime() == null);
-
- $this->exist('form[action="'.($url = $this->generateUrl(
- 'change_email_request'
- )).'"]');
- $this->exist('form[action="'.$url.'"] input[type="submit"]');
-
- $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
- $form['form[email]'] = 'trololololooo@trolo.com';
- $this->submit($form);
-
- // Ce coup-ci c'est bien une redirection
- $this->isResponseRedirection();
-
- // Un mail a été envoyé
- $mc = $this->getMailerMessageDataCollector();
- $this->assertEquals(1, $mc->getMessageCount());
-
- $mails = $mc->getMessages();
- $mail = $mails[0];
-
- // Les champs ont bougés
- $bob = $this->findUserByUsername('bob');
- $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
- $this->assertFalse($bob->getEmailRequested() == null);
- $this->assertTrue($bob->getEmailRequested() == 'trololololooo@trolo.com');
- $this->assertFalse($bob->getEmailRequestedDatetime() == null);
-
- $this->followRedirection();
- $this->isResponseSuccess();
-
- // On ouvre un lien erroné
- $badurl = $this->generateUrl(
- 'change_email_confirm',
- array('token' => $this->getUser()->getConfirmationToken()),
- true
- );
- $this->crawler = $this->client->request('GET', $badurl);
- $this->isResponseRedirection();
- $this->followRedirection();
- $this->isResponseSuccess();
- $this->exist('div.error');
-
- // Et les champs ont pas bougés
- $bob = $this->findUserByUsername('bob');
- $this->assertFalse($bob->getEmail() == 'trololololooo@trolo.com');
- $this->assertFalse($bob->getEmailRequested() == null);
- $this->assertTrue($bob->getEmailRequested() == 'trololololooo@trolo.com');
- $this->assertFalse($bob->getEmailRequestedDatetime() == null);
-
- $this->assertTrue(!is_null(strpos($mail->getBody(), ($url = $this->generateUrl(
- 'change_email_confirm',
- array('token' => $token = hash('sha256', $bob->getConfirmationToken().'trololololooo@trolo.com')),
- true
- )))));
-
- // On ouvre le bon lien
- $this->crawler = $this->client->request('GET', $url);
-
- // C'est un succés
- $this->isResponseRedirection();
- $this->followRedirection();
- $this->isResponseSuccess();
-
- $this->notExist('div.error');
-
- // Et les champs ont bougés
- $bob = $this->findUserByUsername('bob');
- $this->assertTrue($bob->getEmail() == 'trololololooo@trolo.com');
- $this->assertTrue($bob->getEmailRequested() == null);
- $this->assertFalse($bob->getEmailRequestedDatetime() == null);
-
- // Par contre si on refait une demande maintenant ca échoue (délais entre demandes)
- $this->exist('form[action="'.($url = $this->generateUrl(
- 'change_email_request'
- )).'"]');
- $this->exist('form[action="'.$url.'"] input[type="submit"]');
-
- $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
- $form['form[email]'] = 'trololololooo222@trolo.com';
- $this->submit($form);
-
- // Il n'y as pas de redirection
- $this->isResponseRedirection();
- $this->followRedirection();
- $this->isResponseSuccess();
- $this->exist('div.error');
-
- // Et les champs ont bougés
- $bob = $this->findUserByUsername('bob');
- $this->assertTrue($bob->getEmail() == 'trololololooo@trolo.com');
- $this->assertTrue($bob->getEmailRequested() == null);
- $this->assertFalse($bob->getEmailRequestedDatetime() == null);
-
- // Si par contre on manipule le dateTime on pourra
- $bob = $this->findUserByUsername('bob');
- $bob->setEmailRequestedDatetime(
- $this->getUser()->getEmailRequestedDatetime()
- - $this->getContainer()->getParameter('changeemail_security_delay')
- );
-
- $this->getDoctrine()->getEntityManager()->flush();
-
- $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
- $form['form[email]'] = 'trololololooo222@trolo.com';
- $this->submit($form);
-
- // Ce coup-ci c'est bien une redirection
- $this->isResponseRedirection();
-
- // Un mail a été envoyé
- $mc = $this->getMailerMessageDataCollector();
- $this->assertEquals(1, $mc->getMessageCount());
-
- $mails = $mc->getMessages();
- $mail = $mails[0];
-
- $this->assertTrue(!is_null(strpos($mail->getBody(), ($url = $this->generateUrl(
- 'change_email_confirm',
- array('token' => hash('sha256', $this->getUser()->getConfirmationToken().'trololololooo222@trolo.com')),
- true
- )))));
-
- // Les champs ont bougés
- $bob = $this->findUserByUsername('bob');
- $this->assertFalse($bob->getEmail() == 'trololololooo222@trolo.com');
- $this->assertFalse($bob->getEmailRequested() == null);
- $this->assertTrue($bob->getEmailRequested() == 'trololololooo222@trolo.com');
- $this->assertFalse($bob->getEmailRequestedDatetime() == null);
-
- $this->followRedirection();
- $this->isResponseSuccess();
- }
-
- public function testAddElementTagToFavorites()
- {
- $this->client = self::createClient();
- $this->connectUser('paul', 'toor');
-
- $paul = $this->getUser();
-
- // D'après les fixtures paul n'a pas de tags favoris
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $paul->getId()
- ))
- ;
-
- $this->assertEquals(0, count($fav));
-
- // Ajout d'un tag en favoris (ajax)
- $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneByName('Tribe')
- ;
-
- $url = $this->generateUrl('ajax_tag_add_to_favorites', array(
- 'tag_id' => $tribe->getId(),
- 'token' => $paul->getPersonalHash()
- ));
-
- $crawler = $this->client->request('GET', $url, array(), array(), array(
- 'HTTP_X-Requested-With' => 'XMLHttpRequest',
- ));
-
- $this->isResponseSuccess();
-
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $paul->getId()
- ))
- ;
-
- $this->assertEquals(1, count($fav));
- $this->assertEquals('Tribe', $fav[0]->getTag()->getName());
-
- // Si on rajoute le même tag il ne doit pas y avoir de changement
- $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneByName('Tribe')
- ;
-
- $url = $this->generateUrl('ajax_tag_add_to_favorites', array(
- 'tag_id' => $tribe->getId(),
- 'token' => $paul->getPersonalHash()
- ));
-
- $crawler = $this->client->request('GET', $url, array(), array(), array(
- 'HTTP_X-Requested-With' => 'XMLHttpRequest',
- ));
-
- $this->isResponseSuccess();
-
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $paul->getId()
- ))
- ;
-
- $this->assertEquals(1, count($fav));
- $this->assertEquals('Tribe', $fav[0]->getTag()->getName());
-
- // Si on ajoute un nouveau tag
- $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->findOneByName('Hardtek')
- ;
-
- $url = $this->generateUrl('ajax_tag_add_to_favorites', array(
- 'tag_id' => $hardtek->getId(),
- 'token' => $paul->getPersonalHash()
- ));
-
- $crawler = $this->client->request('GET', $url, array(), array(), array(
- 'HTTP_X-Requested-With' => 'XMLHttpRequest',
- ));
-
- $this->isResponseSuccess();
-
- $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersTagsFavorites')
- ->findBy(array(
- 'user' => $paul->getId()
- ))
- ;
-
- $this->assertEquals(2, count($fav));
- $this->assertEquals('Tribe', $fav[0]->getTag()->getName());
- $this->assertEquals('Hardtek', $fav[1]->getTag()->getName());
-
- }
-
- public function testUpdateAddress()
- {
- $this->client = self::createClient();
- $this->connectUser('paul', 'toor');
-
- $paul = $this->getUser();
-
- // D'après les fixtures, pas d'adresse pour paul
- $this->assertEquals($paul->getTown(), null);
- $this->assertEquals($paul->getCountry(), null);
-
- $crawler = $this->client->request(
- 'POST',
- $this->generateUrl('update_address', array('token' => $this->getUser()->getPersonalHash())),
- array(
- 'town' => '',
- 'country' => ''
- ),
- array(),
- array('HTTP_X-Requested-With' => 'XMLHttpRequest')
- );
-
- $this->isResponseSuccess();
- $response = json_decode($this->client->getResponse()->getContent(), true);
-
- $this->assertEquals($response['status'], 'error');
- $this->assertEquals(count($response['errors']), '2');
- $this->assertEquals($response['errors'], array(
- $this->getContainer()->get('translator')->trans('my_account.address.form.errors.notown', array(), 'userui'),
- $this->getContainer()->get('translator')->trans('my_account.address.form.errors.nocountry', array(), 'userui')
- ));
- $paul = $this->getUser();
- $this->assertEquals($paul->getTown(), null);
- $this->assertEquals($paul->getCountry(), null);
-
- /////
-
- $crawler = $this->client->request(
- 'POST',
- $this->generateUrl('update_address', array('token' => $this->getUser()->getPersonalHash())),
- array(
- 'town' => 'peyruis',
- 'country' => ''
- ),
- array(),
- array('HTTP_X-Requested-With' => 'XMLHttpRequest')
- );
-
- $this->isResponseSuccess();
- $response = json_decode($this->client->getResponse()->getContent(), true);
-
- $this->assertEquals($response['status'], 'error');
- $this->assertEquals(count($response['errors']), '1');
- $this->assertEquals($response['errors'], array(
- $this->getContainer()->get('translator')->trans('my_account.address.form.errors.nocountry', array(), 'userui')
- ));
- $paul = $this->getUser();
- $this->assertEquals($paul->getTown(), null);
- $this->assertEquals($paul->getCountry(), null);
-
- /////
-
- $crawler = $this->client->request(
- 'POST',
- $this->generateUrl('update_address', array('token' => $this->getUser()->getPersonalHash())),
- array(
- 'town' => 'peyruis',
- 'country' => 'france'
- ),
- array(),
- array('HTTP_X-Requested-With' => 'XMLHttpRequest')
- );
-
-
- $this->isResponseSuccess();
- $response = json_decode($this->client->getResponse()->getContent(), true);
-
- $paul = $this->getUser();
- $this->assertEquals($response['status'], 'success');
- $this->assertEquals($paul->getTown(), 'peyruis');
- $this->assertEquals($paul->getCountry(), 'france');
-
- }
-
- }
|