Browse Source

Ajout de tests d'inscriptions.

bastien 12 years ago
parent
commit
878f19e226

+ 160 - 0
app/Resources/FOSUserBundle/config/validation.xml View File

@@ -0,0 +1,160 @@
1
+<?xml version="1.0" ?>
2
+<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
3
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+    xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
5
+        http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
6
+
7
+    <class name="FOS\UserBundle\Model\User">
8
+
9
+        <constraint name="FOS\UserBundle\Validator\Unique">
10
+            <option name="property">usernameCanonical</option>
11
+            <option name="message">The username is already used</option>
12
+            <option name="groups">
13
+                <value>Registration</value>
14
+                <value>Profile</value>
15
+            </option>
16
+        </constraint>
17
+
18
+        <constraint name="FOS\UserBundle\Validator\Unique">
19
+            <option name="property">emailCanonical</option>
20
+            <option name="message">The email is already used</option>
21
+            <option name="groups">
22
+                <value>Registration</value>
23
+                <value>Profile</value>
24
+            </option>
25
+        </constraint>
26
+
27
+        <property name="username">
28
+            <constraint name="NotBlank">
29
+                <option name="message">Please enter a username</option>
30
+                <option name="groups">
31
+                    <value>Registration</value>
32
+                    <value>Profile</value>
33
+                </option>
34
+            </constraint>
35
+            <constraint name="MinLength">
36
+                <option name="limit">6</option>
37
+                <option name="message">The username is too short</option>
38
+                <option name="groups">
39
+                    <value>Registration</value>
40
+                    <value>Profile</value>
41
+                </option>
42
+            </constraint>
43
+            <constraint name="MaxLength">
44
+                <option name="limit">32</option>
45
+                <option name="message">The username is too long</option>
46
+                <option name="groups">
47
+                    <value>Registration</value>
48
+                    <value>Profile</value>
49
+                </option>
50
+            </constraint>
51
+        </property>
52
+
53
+        <property name="email">
54
+            <constraint name="NotBlank">
55
+                <option name="message">Please enter an email</option>
56
+                <option name="groups">
57
+                    <value>Registration</value>
58
+                    <value>Profile</value>
59
+                </option>
60
+            </constraint>
61
+            <constraint name="MinLength">
62
+                <option name="limit">2</option>
63
+                <option name="message">The email is too short</option>
64
+                <option name="groups">
65
+                    <value>Registration</value>
66
+                    <value>Profile</value>
67
+                </option>
68
+            </constraint>
69
+            <constraint name="MaxLength">
70
+                <option name="limit">255</option>
71
+                <option name="message">The email is too long</option>
72
+                <option name="groups">
73
+                    <value>Registration</value>
74
+                    <value>Profile</value>
75
+                </option>
76
+            </constraint>
77
+            <constraint name="Email">
78
+                <option name="message">The email is not valid</option>
79
+                <option name="groups">
80
+                    <value>Registration</value>
81
+                    <value>Profile</value>
82
+                </option>
83
+            </constraint>
84
+        </property>
85
+
86
+        <property name="plainPassword">
87
+            <constraint name="NotBlank">
88
+                <option name="message">Please enter a password</option>
89
+                <option name="groups">Registration</option>
90
+            </constraint>
91
+            <constraint name="MinLength">
92
+                <option name="limit">2</option>
93
+                <option name="message">The password is too short</option>
94
+                <option name="groups">
95
+                    <value>Registration</value>
96
+                    <value>Profile</value>
97
+                </option>
98
+            </constraint>
99
+        </property>
100
+    </class>
101
+
102
+    <class name="FOS\UserBundle\Form\Model\CheckPassword">
103
+        <constraint name="FOS\UserBundle\Validator\Password">
104
+            <option name="passwordProperty">current</option>
105
+            <option name="userProperty">user</option>
106
+            <option name="groups">
107
+                <value>ChangePassword</value>
108
+                <value>Profile</value>
109
+            </option>
110
+        </constraint>
111
+    </class>
112
+
113
+    <class name="FOS\UserBundle\Form\Model\ChangePassword">
114
+        <property name="new">
115
+            <constraint name="NotBlank">
116
+                <option name="message">Please enter a new password</option>
117
+                <option name="groups">ChangePassword</option>
118
+            </constraint>
119
+            <constraint name="MinLength">
120
+                <option name="limit">2</option>
121
+                <option name="message">The new password is too short</option>
122
+                <option name="groups">ChangePassword</option>
123
+            </constraint>
124
+        </property>
125
+    </class>
126
+
127
+    <class name="FOS\UserBundle\Form\Model\ResetPassword">
128
+        <property name="new">
129
+            <constraint name="NotBlank">
130
+                <option name="message">Please enter a new password</option>
131
+                <option name="groups">ResetPassword</option>
132
+            </constraint>
133
+            <constraint name="MinLength">
134
+                <option name="limit">2</option>
135
+                <option name="message">The new password is too short</option>
136
+                <option name="groups">ResetPassword</option>
137
+            </constraint>
138
+        </property>
139
+    </class>
140
+
141
+    <class name="FOS\UserBundle\Model\Group">
142
+        <property name="name">
143
+            <constraint name="NotBlank">
144
+                <option name="message">Please enter a name</option>
145
+                <option name="groups">Registration</option>
146
+            </constraint>
147
+            <constraint name="MinLength">
148
+                <option name="limit">2</option>
149
+                <option name="message">The name is too short</option>
150
+                <option name="groups">Registration</option>
151
+            </constraint>
152
+            <constraint name="MaxLength">
153
+                <option name="limit">255</option>
154
+                <option name="message">The name is too long</option>
155
+                <option name="groups">Registration</option>
156
+            </constraint>
157
+        </property>
158
+    </class>
159
+
160
+</constraint-mapping>

+ 4 - 0
catalog.xml View File

@@ -0,0 +1,4 @@
1
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
+<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog" prefer="system">
3
+    <nextCatalog catalog="nbproject/private/retriever/catalog.xml"/>
4
+</catalog>

+ 7 - 0
src/Muzich/CoreBundle/Resources/config/validation.yml View File

@@ -0,0 +1,7 @@
1
+##
2
+
3
+Muzich\CoreBundle\Entity\User:
4
+  properties:
5
+    username:
6
+      - MinLength: { limit: 3}
7
+      - MaxLength: { limit: 32}

+ 88 - 15
src/Muzich/CoreBundle/Tests/Controller/IndexControllerTest.php View File

@@ -6,7 +6,7 @@ use Muzich\CoreBundle\lib\FunctionalTest;
6 6
 
7 7
 class IndexControllerTest extends FunctionalTest
8 8
 {
9
-  public function testIdentification()
9
+  public function testIdentificationSuccess()
10 10
   {
11 11
     /**
12 12
      * Test de l'identification de paul
@@ -39,7 +39,33 @@ class IndexControllerTest extends FunctionalTest
39 39
     $this->assertEquals('paul', $user->getUsername());
40 40
   }
41 41
   
42
-  public function testRegistration()
42
+  public function testIdentificationFail()
43
+  {
44
+    /**
45
+     * Test de l'identification de paul, avec erreur
46
+     */
47
+    $this->client = self::createClient();
48
+
49
+    $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
50
+    $this->isResponseSuccess();
51
+
52
+    $this->assertEquals('anon.', $this->getUser());
53
+
54
+    $form = $this->selectForm('form[action="'.$this->generateUrl('fos_user_security_check').'"] input[type="submit"]');
55
+    $form['_username'] = 'paul';
56
+    $form['_password'] = 'toorr';
57
+    $form['_remember_me'] = true;
58
+    $this->submit($form);
59
+
60
+    $this->isResponseRedirection();
61
+    $this->followRedirection();
62
+    $this->isResponseSuccess();
63
+
64
+    $user = $this->getUser();
65
+    $this->assertEquals('anon.', $this->getUser());
66
+  }
67
+  
68
+  public function testRegistrationSuccess()
43 69
   {
44 70
     /**
45 71
      * Inscription d'un utilisateur
@@ -59,27 +85,74 @@ class IndexControllerTest extends FunctionalTest
59 85
     $this->exist('form[action="'.$url.'"] input[id="fos_user_registration_form_plainPassword_second"]');
60 86
     $this->exist('form[action="'.$url.'"] input[type="submit"]');
61 87
     
62
-    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
63
-    $form['fos_user_registration_form[username]'] = 'raoul';
64
-    $form['fos_user_registration_form[email]'] = 'raoul.45gf64z@gmail.com';
65
-    $form['fos_user_registration_form[plainPassword][first]'] = 'toor';
66
-    $form['fos_user_registration_form[plainPassword][second]'] = 'toor';
67
-    $this->submit($form);
88
+    $this->validate_registrate_user_form(
89
+      $this->selectForm('form[action="'.$url.'"] input[type="submit"]'), 
90
+      'raoula', 
91
+      'raoul.45gf64z@gmail.com', 
92
+      'toor',
93
+      'toor'
94
+    );
68 95
     
69 96
     $this->isResponseRedirection();
70 97
     $this->followRedirection();
71 98
     $this->isResponseSuccess();
72 99
 
73 100
     $user = $this->getUser();
74
-    $this->assertEquals('raoul', $user->getUsername());
101
+    $this->assertEquals('raoula', $user->getUsername());
75 102
     
76
-    /*
77
-     * TODO: Vérifier les données en base
78
-     */
103
+    // L'utilisateur est enregistré, il doit donc être en base
104
+    $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
105
+      ->findOneByUsername('raoula')
106
+    ;
107
+    
108
+    $this->assertTrue(!is_null($db_user));
109
+    if ($db_user)
110
+    {
111
+      $this->assertEquals('raoula', $db_user->getUsername());
112
+    }
79 113
   }
80 114
   
81
-  /*
82
-   * TODO: Vérifier le comportement des formulaires lorsque l'on rentre de mauvaises données
83
-   */
115
+  public function testRegistrationFailure()
116
+  {
117
+    
118
+    /**
119
+     * Inscription d'un utilisateur
120
+     */
121
+    $this->client = self::createClient();
122
+
123
+    // Mots de passe différents
124
+    $this->procedure_registration_failure(
125
+      'raoulb', 
126
+      'raoulb.def4v65sds@gmail.com', 
127
+      'toor', 
128
+      'toorr'
129
+    );
130
+
131
+//    // Pseudo trop court
132
+//    $this->procedure_registration_failure(
133
+//      'ra', 
134
+//      'raoulb.def4v65sds@gmail.com', 
135
+//      'toor', 
136
+//      'toor'
137
+//    );
138
+//    
139
+//    // Pseudo trop long
140
+//    $this->procedure_registration_failure(
141
+//      'raouuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu'
142
+//         .'uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu'
143
+//         .'uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuul', 
144
+//      'raoulb.def4v65sds@gmail.com', 
145
+//      'toor', 
146
+//      'toor'
147
+//    );
148
+
149
+    // Email invalide
150
+    $this->procedure_registration_failure(
151
+      'raoulc', 
152
+      'raoulb.def4v65sds@gmail', 
153
+      'toor', 
154
+      'toor'
155
+    );
156
+  }
84 157
   
85 158
 }

+ 46 - 0
src/Muzich/CoreBundle/lib/FunctionalTest.php View File

@@ -60,6 +60,52 @@ class FunctionalTest extends WebTestCase
60 60
     $this->assertEquals($login, $user->getUsername());
61 61
   }
62 62
   
63
+  protected function validate_registrate_user_form($form, $username, $email, $pass1, $pass2)
64
+  {
65
+    $form['fos_user_registration_form[username]'] = $username;
66
+    $form['fos_user_registration_form[email]'] = $email;
67
+    $form['fos_user_registration_form[plainPassword][first]'] = $pass1;
68
+    // Un des mots de passe est incorrect
69
+    $form['fos_user_registration_form[plainPassword][second]'] = $pass2;
70
+    $this->submit($form);
71
+  }
72
+  
73
+  protected function procedure_registration_failure($username, $email, $pass1, $pass2)
74
+  {
75
+    $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
76
+    $this->isResponseSuccess();
77
+    $this->assertEquals('anon.', $this->getUser());
78
+    
79
+    $url = $this->generateUrl('register');
80
+    // Les mots de passes sont différents
81
+    $this->validate_registrate_user_form(
82
+      $this->selectForm('form[action="'.$url.'"] input[type="submit"]'), 
83
+      $username, 
84
+      $email, 
85
+      $pass1,
86
+      $pass2
87
+    );
88
+    
89
+    $this->isResponseSuccess();
90
+
91
+    if ('anon.' === ($user = $this->getUser()))
92
+    {
93
+      // Nous ne sommes pas identifiés
94
+      $this->assertEquals('anon.', $user);
95
+
96
+      // L'utilisateur n'est pas enregistré, il ne doit donc pas être en base
97
+      $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
98
+        ->findOneByUsername($username)
99
+      ;
100
+
101
+      $this->assertTrue(is_null($db_user));
102
+    }
103
+    else
104
+    {
105
+      $this->assertTrue(false);
106
+    }
107
+  }
108
+  
63 109
   /**
64 110
    * Generates a URL from the given parameters.
65 111
    *