Browse Source

Evolution #143: Commentaires sur un élément

bastien 13 years ago
parent
commit
4b9d019a5d

+ 2 - 1
app/Resources/translations/elements.fr.yml View File

@@ -36,9 +36,10 @@ element:
36 36
     add_cancel:         Annuler
37 37
     edit_submit:        Envoyer
38 38
     edit_cancel:        Annuler
39
-    add_error:
39
+    errors:
40 40
       min:              Le commentaire doit être de %limit% caractéres minimum
41 41
       max:              Le commentaire doit être de %limit% caractéres maximum
42
+      unknow:           Une erreur est survenue
42 43
   
43 44
 comment:
44 45
   edit:

+ 34 - 8
src/Muzich/CommentBundle/Controller/CommentController.php View File

@@ -32,7 +32,7 @@ class CommentController extends Controller
32 32
       return $this->jsonResponse(array(
33 33
         'status' => 'error',
34 34
         'errors' => array($this->trans(
35
-          'element.comments.add_error.min', 
35
+          'element.comments.errors.min', 
36 36
           array(
37 37
             '%limit%' => $this->container->getParameter('comment_add_min_length')
38 38
           ), 
@@ -45,7 +45,7 @@ class CommentController extends Controller
45 45
       return $this->jsonResponse(array(
46 46
         'status' => 'error',
47 47
         'errors' => array($this->trans(
48
-          'element.comments.add_error.max', 
48
+          'element.comments.errors.max', 
49 49
           array(
50 50
             '%limit%' => $this->container->getParameter('comment_add_max_length')
51 51
           ), 
@@ -94,7 +94,20 @@ class CommentController extends Controller
94 94
     
95 95
     // On met a jour les commentaires
96 96
     $cm = new CommentsManager($element->getComments());
97
-    $cm->delete($this->getUserId(), $date);
97
+    
98
+    
99
+    if (!$cm->delete($this->getUserId(), $date))
100
+    {
101
+      return $this->jsonResponse(array(
102
+        'status' => 'error',
103
+        'errors' => array($this->trans(
104
+          'element.comments.errors.unknow', 
105
+          array(), 
106
+          'elements'
107
+        )
108
+      )));
109
+    }
110
+    
98 111
     $element->setComments($cm->get());
99 112
       
100 113
     $this->getDoctrine()->getEntityManager()->persist($element);
@@ -162,7 +175,7 @@ class CommentController extends Controller
162 175
         'status' => 'error',
163 176
         'dom_id' => $dom_id,
164 177
         'errors' => array($this->trans(
165
-          'element.comments.add_error.min', 
178
+          'element.comments.errors.min', 
166 179
           array(
167 180
             '%limit%' => $this->container->getParameter('comment_add_min_length')
168 181
           ), 
@@ -176,7 +189,7 @@ class CommentController extends Controller
176 189
         'status' => 'error',
177 190
         'dom_id' => $dom_id,
178 191
         'errors' => array($this->trans(
179
-          'element.comments.add_error.max', 
192
+          'element.comments.errors.max', 
180 193
           array(
181 194
             '%limit%' => $this->container->getParameter('comment_add_max_length')
182 195
           ), 
@@ -192,13 +205,26 @@ class CommentController extends Controller
192 205
 
193 206
     $this->getDoctrine()->getEntityManager()->persist($element);
194 207
     $this->getDoctrine()->getEntityManager()->flush();
195
-
208
+    
209
+    if (!($comment_index = $cm->getIndex($this->getUserId(), $date)))
210
+    {
211
+      return $this->jsonResponse(array(
212
+        'status' => 'error',
213
+        'dom_id' => $dom_id,
214
+        'errors' => array($this->trans(
215
+          'element.comments.errors.unknow', 
216
+          array(), 
217
+          'elements'
218
+        )
219
+      )));
220
+    }
221
+    
196 222
     // On récupère le html du li avec le comment pour la réponse
197 223
     $html = $this->render('MuzichCommentBundle:Comment:comment.html.twig', array(
198
-      'comment'     => $cm->get($cm->getIndex($this->getUserId(), $date)),
224
+      'comment'     => $cm->get($comment_index),
199 225
       'element_id'  => $element->getId()
200 226
     ))->getContent();
201
-
227
+    
202 228
     return $this->jsonResponse(array(
203 229
       'status' => 'success',
204 230
       'dom_id' => $dom_id,

+ 13 - 0
src/Muzich/CoreBundle/Managers/CommentsManager.php View File

@@ -72,11 +72,19 @@ class CommentsManager
72 72
         $comments[] = $comment;
73 73
       }
74 74
     }
75
+    
75 76
     $this->comments = $comments;
76 77
   }
77 78
   
79
+  /**
80
+   *
81
+   * @param int $user_id
82
+   * @param string $date
83
+   * @return boolean 
84
+   */
78 85
   public function delete($user_id, $date)
79 86
   {
87
+    $found = false;
80 88
     $comments = array();
81 89
     foreach ($this->comments as $comment)
82 90
     {
@@ -84,8 +92,13 @@ class CommentsManager
84 92
       {
85 93
         $comments[] = $comment;
86 94
       }
95
+      else
96
+      {
97
+        $found = true;
98
+      }
87 99
     }
88 100
     $this->comments = $comments;
101
+    return $found;
89 102
   }
90 103
   
91 104
   public function getIndex($user_id, $date)

+ 210 - 0
src/Muzich/CoreBundle/Tests/Controller/CommentControllerTest.php View File

@@ -0,0 +1,210 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+use Muzich\CoreBundle\Managers\CommentsManager;
7
+
8
+/**
9
+ * Test des commentaires. Doit couvrir:
10
+ * 
11
+ * * La consultation de commentaires en fixtures
12
+ * * L'ajout de commentaire
13
+ * * La modification d'un commentaire
14
+ * * La suppression d'un commentaire
15
+ * 
16
+ */
17
+class CommentControllerTest extends FunctionalTest
18
+{
19
+ 
20
+  public function testView()
21
+  {
22
+    $this->client = self::createClient();
23
+    $this->connectUser('paul', 'toor');
24
+    
25
+    // On est sur la page home, d'après les fixtures on a des coms en dom
26
+    $this->exist('div.comments:contains("C\'est trop bon hein ?")');
27
+    $this->exist('div.comments:contains("C\'est pas mal en effet")');
28
+    $this->exist('li.element a.display_comments');
29
+    $this->exist('li.element a.hide_comments');
30
+    $this->exist('li.element a.add_comment');
31
+    $this->exist('div.comments a.add_comment');
32
+  }
33
+  
34
+  public function testAddEditDelete()
35
+  {
36
+    $this->client = self::createClient();
37
+    $this->connectUser('paul', 'toor');
38
+    
39
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
40
+      ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
41
+    ;
42
+    
43
+    // On va ajouter un commentaire a la toute dernière musique posté par bux
44
+    $this->crawler = $this->client->request(
45
+      'POST', 
46
+      $this->generateUrl('ajax_add_comment', array(
47
+        'element_id' => $element->getId(),
48
+        'token'      => $this->getUser()->getPersonalHash()
49
+      )), 
50
+      array(
51
+          'comment' => "J'ai réécouté et ouaa je kiff BrOOO"
52
+        
53
+      ), 
54
+      array(), 
55
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
56
+    );
57
+    
58
+    $this->isResponseSuccess();
59
+    
60
+    $response = json_decode($this->client->getResponse()->getContent(), true);
61
+    $this->assertEquals($response['status'], 'success');
62
+    
63
+    // On ré-affiche la page home pour voir si le commentaire y est
64
+    $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
65
+    $this->exist('div.comments:contains("J\'ai réécouté et ouaa je kiff BrOOO")');
66
+    
67
+    $extract = $this->crawler->filter('div.comments li:contains("J\'ai réécouté et ouaa je kiff BrOOO")')
68
+      ->extract(array('id'));
69
+    $id = $extract[0];
70
+    
71
+    // Faut que l'on récupère la date
72
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
73
+      ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
74
+    ;
75
+    $cm = new CommentsManager($element->getComments());
76
+    $comment = $cm->getLast();
77
+    
78
+    $this->assertEquals($comment['c'], "J'ai réécouté et ouaa je kiff BrOOO");
79
+        
80
+    // On effectue une modification de ce commentaire
81
+    $this->crawler = $this->client->request(
82
+      'POST', 
83
+      $this->generateUrl('ajax_update_comment', array(
84
+        'element_id' => $element->getId(),
85
+        'date'       => $comment['d'],
86
+        'dom_id'     => $id,
87
+        'token'      => $this->getUser()->getPersonalHash()
88
+      )),
89
+      array(
90
+          'comment' => "Je me modifie mon com kwaa"
91
+      ), 
92
+      array(), 
93
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
94
+    );
95
+    
96
+    $this->isResponseSuccess();
97
+    
98
+    $response = json_decode($this->client->getResponse()->getContent(), true);
99
+    $this->assertEquals($response['status'], 'success');
100
+    
101
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
102
+      ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
103
+    ;
104
+    $cm = new CommentsManager($element->getComments());
105
+    $comment = $cm->getLast();
106
+    
107
+    $this->assertEquals($comment['c'], "Je me modifie mon com kwaa");
108
+    // Il y a une date d'edition
109
+    $this->assertTrue(array_key_exists('e', $comment));
110
+    
111
+    // On ré-affiche la page home pour voir si le commentaire modifié y est
112
+    $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
113
+    $this->exist('div.comments:contains("Je me modifie mon com kwaa")');
114
+    
115
+    // maintenant on le supprime
116
+    $this->crawler = $this->client->request(
117
+      'GET', 
118
+      $this->generateUrl('ajax_delete_comment', array(
119
+        'element_id' => $element->getId(),
120
+        'date'       => $comment['d'],
121
+        'token'      => $this->getUser()->getPersonalHash()
122
+      )),
123
+      array(), 
124
+      array(), 
125
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
126
+    );
127
+    
128
+    $this->isResponseSuccess();
129
+    
130
+    $response = json_decode($this->client->getResponse()->getContent(), true);
131
+    $this->assertEquals($response['status'], 'success');
132
+    
133
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
134
+      ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
135
+    ;
136
+    $cm = new CommentsManager($element->getComments());
137
+    $comment = $cm->getLast();
138
+    
139
+    $this->assertNotEquals($comment['c'], "Je me modifie mon com kwaa");
140
+    
141
+    // On ré-affiche la page home pour voir si le commentaire modifié y est
142
+    $this->crawler = $this->client->request('GET', $this->generateUrl('home'));
143
+    $this->notExist('div.comments:contains("Je me modifie mon com kwaa")');
144
+  }
145
+  
146
+  /**
147
+   * On test ici la sécurité nous empêchant de modifier / supprimer le comm
148
+   * d'un autre.
149
+   */
150
+  public function testFails()
151
+  {
152
+    $this->client = self::createClient();
153
+    $this->connectUser('paul', 'toor');
154
+    
155
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
156
+      ->findOneByName('Babylon Pression - Des Tasers et des Pauvres')
157
+    ;
158
+    
159
+    $cm = new CommentsManager($element->getComments());
160
+    // D'après fixtures: Ce dernier commentaire est à bux
161
+    $comment = $cm->getLast();
162
+    
163
+    $this->assertEquals($comment['c'], "Je répond 13");
164
+    
165
+    // On récupère l'id dom
166
+    $extract = $this->crawler->filter('div.comments li:contains("Je répond 13")')
167
+      ->extract(array('id'));
168
+    $id = $extract[0];
169
+    
170
+    // On essaie de le modifier
171
+    $this->crawler = $this->client->request(
172
+      'POST', 
173
+      $this->generateUrl('ajax_update_comment', array(
174
+        'element_id' => $element->getId(),
175
+        'date'       => $comment['d'],
176
+        'dom_id'     => $id,
177
+        'token'      => $this->getUser()->getPersonalHash()
178
+      )),
179
+      array(
180
+          'comment' => "Je répond 13 HACKED"
181
+      ), 
182
+      array(), 
183
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
184
+    );
185
+    
186
+    $this->isResponseSuccess();
187
+    
188
+    $response = json_decode($this->client->getResponse()->getContent(), true);
189
+    $this->assertEquals($response['status'], 'error');
190
+    
191
+    // On essaie de le supprimer
192
+    $this->crawler = $this->client->request(
193
+      'GET', 
194
+      $this->generateUrl('ajax_delete_comment', array(
195
+        'element_id' => $element->getId(),
196
+        'date'       => $comment['d'],
197
+        'token'      => $this->getUser()->getPersonalHash()
198
+      )),
199
+      array(), 
200
+      array(), 
201
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
202
+    );
203
+    
204
+    $this->isResponseSuccess();
205
+    
206
+    $response = json_decode($this->client->getResponse()->getContent(), true);
207
+    $this->assertEquals($response['status'], 'error');
208
+  }
209
+  
210
+}