Browse Source

Evolution #124: Du tessst et encore du test: Filtre ajax sur favoris

bastien 13 years ago
parent
commit
ccefac49bc
1 changed files with 261 additions and 179 deletions
  1. 261 179
      src/Muzich/CoreBundle/Tests/Controller/FavoriteControllerTest.php

+ 261 - 179
src/Muzich/CoreBundle/Tests/Controller/FavoriteControllerTest.php View File

@@ -7,216 +7,298 @@ use Muzich\CoreBundle\lib\FunctionalTest;
7 7
 class FavoriteControllerTest extends FunctionalTest
8 8
 {
9 9
  
10
-  /**
11
-   * Test du listing de ses favoris
12
-   */
13
-  public function testMyFavorites()
14
-  {
15
-    $this->client = self::createClient();
16
-    $this->connectUser('bux', 'toor');
17
-    
18
-    // On va cliquer sur le lien 'Mes favoris'
19
-    $this->exist('a[href="'.($url = $this->generateUrl('favorites_my_list')).'"]');
20
-    $link = $this->selectLink('a[href="'.$url.'"]');
21
-    // On clique dessus
22
-    $this->clickOnLink($link);
23
-    $this->isResponseSuccess();
24
-        
25
-    $elements = $this->getDoctrine()->getEntityManager()->createQuery("
26
-      SELECT e FROM MuzichCoreBundle:Element e
27
-      LEFT JOIN e.elements_favorites ef
28
-      WHERE ef.user = :uid
29
-    ")->setParameter('uid', $this->getUser()->getId())
30
-      ->getResult()
31
-    ;
32
-    
33
-    $this->assertTrue(!is_null($elements));
34
-    
35
-    if (count($elements))
36
-    {
37
-      foreach ($elements as $element)
38
-      {
39
-        $this->exist('li:contains("'.$element->getName().'")');
40
-      }
41
-    }
42
-  }
10
+//  /**
11
+//   * Test du listing de ses favoris
12
+//   */
13
+//  public function testMyFavorites()
14
+//  {
15
+//    $this->client = self::createClient();
16
+//    $this->connectUser('bux', 'toor');
17
+//    
18
+//    // On va cliquer sur le lien 'Mes favoris'
19
+//    $this->exist('a[href="'.($url = $this->generateUrl('favorites_my_list')).'"]');
20
+//    $link = $this->selectLink('a[href="'.$url.'"]');
21
+//    // On clique dessus
22
+//    $this->clickOnLink($link);
23
+//    $this->isResponseSuccess();
24
+//        
25
+//    $elements = $this->getDoctrine()->getEntityManager()->createQuery("
26
+//      SELECT e FROM MuzichCoreBundle:Element e
27
+//      LEFT JOIN e.elements_favorites ef
28
+//      WHERE ef.user = :uid
29
+//    ")->setParameter('uid', $this->getUser()->getId())
30
+//      ->getResult()
31
+//    ;
32
+//    
33
+//    $this->assertTrue(!is_null($elements));
34
+//    
35
+//    if (count($elements))
36
+//    {
37
+//      foreach ($elements as $element)
38
+//      {
39
+//        $this->exist('li:contains("'.$element->getName().'")');
40
+//      }
41
+//    }
42
+//  }
43
+//  
44
+//  /**
45
+//   * Test de la page listant les favoris d'un utilisateur
46
+//   */
47
+//  public function testHisFavorites()
48
+//  {
49
+//    $this->client = self::createClient();
50
+//    $this->connectUser('bob', 'toor');
51
+//    
52
+//    $bux = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
53
+//      ->findOneByUsername('bux')
54
+//    ;
55
+//    
56
+//    // On se rend sur sa page des favoris de bux
57
+//    $this->crawler = $this->client->request('GET', $this->generateUrl('favorite_user_list', array('slug' => 'bux')));
58
+//           
59
+//    $elements = $this->getDoctrine()->getEntityManager()->createQuery("
60
+//      SELECT e FROM MuzichCoreBundle:Element e
61
+//      LEFT JOIN e.elements_favorites ef
62
+//      WHERE ef.user = :uid
63
+//    ")->setParameter('uid', $bux->getId())
64
+//      ->getResult()
65
+//    ;
66
+//    
67
+//    $this->assertTrue(!is_null($elements));
68
+//    
69
+//    if (count($elements))
70
+//    {
71
+//      foreach ($elements as $element)
72
+//      {
73
+//        $this->exist('li:contains("'.$element->getName().'")');
74
+//      }
75
+//    }
76
+//  }
77
+//  
78
+//  /**
79
+//   * Test d'ajout en favori un element, puis son retrait
80
+//   * Ce test dépend actuellement du fait que l'élément testé se truove sur la 
81
+//   * page du groupe en question
82
+//   */
83
+//  public function testFavoritesManagement()
84
+//  {
85
+//    $this->client = self::createClient();
86
+//    $this->connectUser('bob', 'toor');
87
+//    
88
+//    // On se rend sur la page du groupe Dudeldrum
89
+//    $DUDELDRUM = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
90
+//      ->findOneBySlug('dudeldrum')
91
+//      ->getSingleResult()
92
+//    ;
93
+//    
94
+//    $element_DUDELDRUM = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
95
+//      ->findOneByName('DUDELDRUM')
96
+//    ;
97
+//    
98
+//    // En base l'enregistrement n'existe pas encore
99
+//    $favorite = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')->findOneBy(array(
100
+//     'user'    => $this->getUser()->getId(),
101
+//     'element' => $element_DUDELDRUM->getId()
102
+//    ));
103
+//    
104
+//    $this->assertTrue(is_null($favorite));
105
+//    
106
+//    $this->crawler = $this->client->request(
107
+//      'GET', 
108
+//      $this->generateUrl('show_group', array('slug' => $DUDELDRUM->getSlug()))
109
+//    );
110
+//    
111
+//    $this->isResponseSuccess();
112
+//    
113
+//    // L'élément est présent sur la page
114
+//    $this->exist('li:contains("DUDELDRUM")');
115
+//    $this->exist('a[href="'.($url = $this->generateUrl('favorite_add', array(
116
+//      'id'    => $element_DUDELDRUM->getId(),
117
+//      'token' => $this->getUser()->getPersonalHash()
118
+//    ))).'"]');
119
+//    $link = $this->selectLink('a[href="'.$url.'"]');
120
+//    $this->clickOnLink($link);
121
+//    
122
+//    $this->isResponseRedirection();
123
+//    $this->followRedirection();
124
+//    $this->isResponseSuccess();
125
+//    
126
+//    // Le lien pour ajouter aux favoris a disparus
127
+//    $this->exist('li:contains("DUDELDRUM")');
128
+//    $this->notExist('a[href="'.$url.'"]');
129
+//    
130
+//    // Il a laissé place aux lien pour le retirer
131
+//    $this->exist('a[href="'.($url_rm = $this->generateUrl('favorite_remove', array(
132
+//      'id'    => $element_DUDELDRUM->getId(),
133
+//      'token' => $this->getUser()->getPersonalHash()
134
+//    ))).'"]');
135
+//    
136
+//    // En base l'enregistrement existe
137
+//    $favorite = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')->findOneBy(array(
138
+//     'user'    => $this->getUser()->getId(),
139
+//     'element' => $element_DUDELDRUM->getId()
140
+//    ));
141
+//    
142
+//    $this->assertTrue(!is_null($favorite));
143
+//    
144
+//    // On se rend sur la page de ses favoris
145
+//    $this->crawler = $this->client->request('GET', $this->generateUrl('favorites_my_list'));
146
+//    
147
+//    $this->exist('li:contains("DUDELDRUM")');
148
+//    
149
+//    // On va maintenant le retirer de nox favoris
150
+//    $this->exist('a[href="'.$url_rm.'"]');
151
+//    $link = $this->selectLink('a[href="'.$url_rm.'"]');
152
+//    $this->clickOnLink($link);
153
+//    
154
+//    $this->isResponseRedirection();
155
+//    $this->followRedirection();
156
+//    $this->isResponseSuccess();
157
+//    
158
+//    $this->NotExist('li:contains("DUDELDRUM")');
159
+//    
160
+//    // En base l'enregistrement a été supprimé
161
+//    $favorite = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')->findOneBy(array(
162
+//     'user'    => $this->getUser()->getId(),
163
+//     'element' => $element_DUDELDRUM->getId()
164
+//    ));
165
+//    
166
+//    $this->assertTrue(is_null($favorite));
167
+//  }
168
+//  
169
+//  public function testAjax()
170
+//  {
171
+//    $this->client = self::createClient();
172
+//    $this->connectUser('bux', 'toor');
173
+//    
174
+//    $bux = $this->getUser();
175
+//    
176
+//    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
177
+//      ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
178
+//    ;
179
+//    
180
+//    // Ajout d'un élément en favoris
181
+//    // Il ajoute cet élément en favoris
182
+//    $url = $this->generateUrl('favorite_add', array(
183
+//      'id'    => $element->getId(),
184
+//      'token' => $bux->getPersonalHash()
185
+//    ));
186
+//    
187
+//    $crawler = $this->client->request('GET', $url, array(), array(), array(
188
+//        'HTTP_X-Requested-With' => 'XMLHttpRequest',
189
+//    ));
190
+//    
191
+//    $this->isResponseSuccess();
192
+//    
193
+//    // On contrôle la présence du favoris
194
+//    $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
195
+//      ->findOneBy(array(
196
+//        'user'    => $bux->getId(),
197
+//        'element' => $element->getId()
198
+//      ));
199
+//    
200
+//    $this->assertTrue(!is_null($fav));
201
+//    
202
+//    // On enlève des favoris
203
+//    $url = $this->generateUrl('favorite_remove', array(
204
+//      'id'    => $element->getId(),
205
+//      'token' => $bux->getPersonalHash()
206
+//    ));
207
+//    
208
+//    $crawler = $this->client->request('GET', $url, array(), array(), array(
209
+//        'HTTP_X-Requested-With' => 'XMLHttpRequest',
210
+//    ));
211
+//    
212
+//    // On contrôle l'absence du favoris
213
+//    $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
214
+//      ->findOneBy(array(
215
+//        'user'    => $bux->getId(),
216
+//        'element' => $element->getId()
217
+//      ));
218
+//    
219
+//    $this->assertTrue(is_null($fav));
220
+//  }
43 221
   
44 222
   /**
45
-   * Test de la page listant les favoris d'un utilisateur
223
+   * Test de la fonction ajax de récupération de plus de favoris
46 224
    */
47
-  public function testHisFavorites()
225
+  public function testFilterElements()
48 226
   {
49 227
     $this->client = self::createClient();
50
-    $this->connectUser('bob', 'toor');
228
+    $this->connectUser('paul', 'toor');
51 229
     
52
-    $bux = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
53
-      ->findOneByUsername('bux')
54
-    ;
230
+    $paul = $this->getUser();
55 231
     
56
-    // On se rend sur sa page des favoris de bux
57
-    $this->crawler = $this->client->request('GET', $this->generateUrl('favorite_user_list', array('slug' => 'bux')));
58
-           
59
-    $elements = $this->getDoctrine()->getEntityManager()->createQuery("
60
-      SELECT e FROM MuzichCoreBundle:Element e
61
-      LEFT JOIN e.elements_favorites ef
62
-      WHERE ef.user = :uid
63
-    ")->setParameter('uid', $bux->getId())
64
-      ->getResult()
232
+    $element_a = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
233
+      ->findOneByName('All Is Full Of Pain')
65 234
     ;
66
-    
67
-    $this->assertTrue(!is_null($elements));
68
-    
69
-    if (count($elements))
70
-    {
71
-      foreach ($elements as $element)
72
-      {
73
-        $this->exist('li:contains("'.$element->getName().'")');
74
-      }
75
-    }
76
-  }
77
-  
78
-  /**
79
-   * Test d'ajout en favori un element, puis son retrait
80
-   * Ce test dépend actuellement du fait que l'élément testé se truove sur la 
81
-   * page du groupe en question
82
-   */
83
-  public function testFavoritesManagement()
84
-  {
85
-    $this->client = self::createClient();
86
-    $this->connectUser('bob', 'toor');
87
-    
88
-    // On se rend sur la page du groupe Dudeldrum
89
-    $DUDELDRUM = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
90
-      ->findOneBySlug('dudeldrum')
91
-      ->getSingleResult()
92
-    ;
93
-    
94
-    $element_DUDELDRUM = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
95
-      ->findOneByName('DUDELDRUM')
235
+    $element_b = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
236
+      ->findOneByName('Heretik System Popof - Resistance')
96 237
     ;
97 238
     
98
-    // En base l'enregistrement n'existe pas encore
99
-    $favorite = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')->findOneBy(array(
100
-     'user'    => $this->getUser()->getId(),
101
-     'element' => $element_DUDELDRUM->getId()
102
-    ));
103
-    
104
-    $this->assertTrue(is_null($favorite));
105
-    
106
-    $this->crawler = $this->client->request(
107
-      'GET', 
108
-      $this->generateUrl('show_group', array('slug' => $DUDELDRUM->getSlug()))
109
-    );
239
+    $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
240
+    $tribe_id   = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
110 241
     
111
-    $this->isResponseSuccess();
112
-    
113
-    // L'élément est présent sur la page
114
-    $this->exist('li:contains("DUDELDRUM")');
115
-    $this->exist('a[href="'.($url = $this->generateUrl('favorite_add', array(
116
-      'id'    => $element_DUDELDRUM->getId(),
117
-      'token' => $this->getUser()->getPersonalHash()
118
-    ))).'"]');
119
-    $link = $this->selectLink('a[href="'.$url.'"]');
120
-    $this->clickOnLink($link);
121
-    
122
-    $this->isResponseRedirection();
123
-    $this->followRedirection();
124
-    $this->isResponseSuccess();
125
-    
126
-    // Le lien pour ajouter aux favoris a disparus
127
-    $this->exist('li:contains("DUDELDRUM")');
128
-    $this->notExist('a[href="'.$url.'"]');
129
-    
130
-    // Il a laissé place aux lien pour le retirer
131
-    $this->exist('a[href="'.($url_rm = $this->generateUrl('favorite_remove', array(
132
-      'id'    => $element_DUDELDRUM->getId(),
133
-      'token' => $this->getUser()->getPersonalHash()
134
-    ))).'"]');
135
-    
136
-    // En base l'enregistrement existe
137
-    $favorite = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')->findOneBy(array(
138
-     'user'    => $this->getUser()->getId(),
139
-     'element' => $element_DUDELDRUM->getId()
140
-    ));
141
-    
142
-    $this->assertTrue(!is_null($favorite));
143
-    
144
-    // On se rend sur la page de ses favoris
242
+    // Ouverture de la page favoris
145 243
     $this->crawler = $this->client->request('GET', $this->generateUrl('favorites_my_list'));
146 244
     
147
-    $this->exist('li:contains("DUDELDRUM")');
148
-    
149
-    // On va maintenant le retirer de nox favoris
150
-    $this->exist('a[href="'.$url_rm.'"]');
151
-    $link = $this->selectLink('a[href="'.$url_rm.'"]');
152
-    $this->clickOnLink($link);
153
-    
154
-    $this->isResponseRedirection();
155
-    $this->followRedirection();
156
-    $this->isResponseSuccess();
157
-    
158
-    $this->NotExist('li:contains("DUDELDRUM")');
245
+    // On doit voir deux elements pour paul
246
+    $this->exist('span.element_name:contains("All Is Full Of Pain")');
247
+    $this->exist('span.element_name:contains("Heretik System Popof - Resistance")');
159 248
     
160
-    // En base l'enregistrement a été supprimé
161
-    $favorite = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')->findOneBy(array(
162
-     'user'    => $this->getUser()->getId(),
163
-     'element' => $element_DUDELDRUM->getId()
249
+    // Récupération de la liste avec la kekete ajax pour Tribe
250
+    $url = $this->generateUrl('favorite_get', array(
251
+      'user_id'       => $paul->getId(), // Utilisateur pour lequel on demande les favoris
252
+      'tags_ids_json' => json_encode(array($tribe_id))
253
+    ));
254
+    $this->crawler = $this->client->request('GET', $url, array(), array(), array(
255
+      'HTTP_X-Requested-With' => 'XMLHttpRequest',
164 256
     ));
165 257
     
166
-    $this->assertTrue(is_null($favorite));
167
-  }
168
-  
169
-  public function testAjax()
170
-  {
171
-    $this->client = self::createClient();
172
-    $this->connectUser('bux', 'toor');
173
-    
174
-    $bux = $this->getUser();
258
+    $response_content = json_decode($this->client->getResponse()->getContent(), true);
259
+    $html = $response_content['html'];
175 260
     
176
-    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
177
-      ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
178
-    ;
261
+    $this->isResponseSuccess();
262
+    $this->assertTrue(strpos($html, 'All Is Full Of Pain') !== false);
263
+    $this->assertTrue(strpos($html, 'Heretik System Popof - Resistance') === false);
179 264
     
180
-    // Ajout d'un élément en favoris
181
-    // Il ajoute cet élément en favoris
182
-    $url = $this->generateUrl('favorite_add', array(
183
-      'id'    => $element->getId(),
184
-      'token' => $bux->getPersonalHash()
265
+    // Récupération de la liste avec la kekete ajax pour Hardtek
266
+    $url = $this->generateUrl('favorite_get', array(
267
+      'user_id'       => $paul->getId(), // Utilisateur pour lequel on demande les favoris
268
+      'tags_ids_json' => json_encode(array($hardtek_id))
185 269
     ));
186 270
     
187
-    $crawler = $this->client->request('GET', $url, array(), array(), array(
188
-        'HTTP_X-Requested-With' => 'XMLHttpRequest',
271
+    $this->crawler = $this->client->request('GET', $url, array(), array(), array(
272
+      'HTTP_X-Requested-With' => 'XMLHttpRequest',
189 273
     ));
190 274
     
191 275
     $this->isResponseSuccess();
192 276
     
193
-    // On contrôle la présence du favoris
194
-    $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
195
-      ->findOneBy(array(
196
-        'user'    => $bux->getId(),
197
-        'element' => $element->getId()
198
-      ));
277
+    $response_content = json_decode($this->client->getResponse()->getContent(), true);
278
+    $html = $response_content['html'];
199 279
     
200
-    $this->assertTrue(!is_null($fav));
280
+    $this->isResponseSuccess();
281
+    $this->assertTrue(strpos($html, 'All Is Full Of Pain') !== false);
282
+    $this->assertTrue(strpos($html, 'Heretik System Popof - Resistance') !== false);
201 283
     
202
-    // On enlève des favoris
203
-    $url = $this->generateUrl('favorite_remove', array(
204
-      'id'    => $element->getId(),
205
-      'token' => $bux->getPersonalHash()
284
+    // Récupération de la liste avec la kekete ajax pour Tribe + Hardtek
285
+    $url = $this->generateUrl('favorite_get', array(
286
+      'user_id'       => $paul->getId(), // Utilisateur pour lequel on demande les favoris
287
+      'tags_ids_json' => json_encode(array($hardtek_id, $tribe_id))
206 288
     ));
207 289
     
208
-    $crawler = $this->client->request('GET', $url, array(), array(), array(
209
-        'HTTP_X-Requested-With' => 'XMLHttpRequest',
290
+    $this->crawler = $this->client->request('GET', $url, array(), array(), array(
291
+      'HTTP_X-Requested-With' => 'XMLHttpRequest',
210 292
     ));
211 293
     
212
-    // On contrôle l'absence du favoris
213
-    $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
214
-      ->findOneBy(array(
215
-        'user'    => $bux->getId(),
216
-        'element' => $element->getId()
217
-      ));
294
+    $this->isResponseSuccess();
295
+    
296
+    $response_content = json_decode($this->client->getResponse()->getContent(), true);
297
+    $html = $response_content['html'];
218 298
     
219
-    $this->assertTrue(is_null($fav));
299
+    $this->isResponseSuccess();
300
+    $this->assertTrue(strpos($html, 'All Is Full Of Pain') !== false);
301
+    $this->assertTrue(strpos($html, 'Heretik System Popof - Resistance') !== false);
220 302
   }
221 303
   
222 304
 }