Преглед изворни кода

Evolution #124: Du tessst et encore du test: ajout d'élément ajax

bastien пре 12 година
родитељ
комит
87f4c5d070

+ 8 - 0
src/Muzich/CoreBundle/Controller/CoreController.php Прегледај датотеку

@@ -145,6 +145,9 @@ class CoreController extends Controller
145 145
    */
146 146
   public function elementAddAction($group_slug)
147 147
   {   
148
+    
149
+    //die(var_dump($this->getRequest()->getParameter()));
150
+    
148 151
     if ($this->getUser() == 'anon.')
149 152
     {
150 153
       if ($this->getRequest()->isXmlHttpRequest())
@@ -246,10 +249,15 @@ class CoreController extends Controller
246 249
         $validator = $this->container->get('validator');
247 250
         $errorList = $validator->validate($form);
248 251
 
252
+        $errors = array();
249 253
         foreach ($errorList as $error)
250 254
         {
251 255
           $errors[] = $this->trans($error->getMessage(), array(), 'validators');
252 256
         }
257
+        foreach ($form->getErrors() as $error)
258
+        {
259
+          $errors[] = $this->trans($error->getMessageTemplate(), array(), 'validators');
260
+        }
253 261
         
254 262
         return $this->jsonResponse(array(
255 263
           'status' => 'error',

+ 217 - 0
src/Muzich/CoreBundle/Tests/Controller/ElementControllerTest.php Прегледај датотеку

@@ -0,0 +1,217 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+
7
+class ElementControllerTest extends FunctionalTest
8
+{ 
9
+  
10
+  public function testAddElementAjax()
11
+  {
12
+    $this->client = self::createClient();
13
+    $this->connectUser('joelle', 'toor');
14
+    
15
+    $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
16
+    $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
17
+      
18
+    // L'élément n'existe pas encore
19
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
20
+      ->findOneByName('Musique qui dechire')
21
+    ;
22
+    $this->assertTrue(is_null($element));
23
+    
24
+    // On commence par ajouter un tag
25
+    $url = $this->generateUrl('element_add');
26
+   
27
+    $extract = $this->crawler->filter('input[name="element_add[_token]"]')
28
+      ->extract(array('value'));
29
+    $csrf = $extract[0];
30
+    
31
+    $crawler = $this->client->request(
32
+      'POST', 
33
+      $url, 
34
+      array(
35
+          'element_add' => array(
36
+              '_token' => $csrf,
37
+              'name'   => 'Musique qui dechire',
38
+              'url'    => 'http://www.youtube.com/watch?v=WC8qb_of04E',
39
+              'tags'   => json_encode(array($hardtek->getId(), $tribe->getId()))
40
+          )
41
+        
42
+      ), 
43
+      array(), 
44
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
45
+    );
46
+    
47
+    $this->isResponseSuccess();
48
+    
49
+    $response = json_decode($this->client->getResponse()->getContent(), true);
50
+    $this->assertEquals($response['status'], 'success');
51
+    
52
+    
53
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
54
+      ->findOneByName('Musique qui dechire')
55
+    ;
56
+    $this->assertTrue(!is_null($element));
57
+    
58
+  }
59
+  
60
+  public function testAddElementInGroupAjax()
61
+  {
62
+    $this->client = self::createClient();
63
+    $this->connectUser('joelle', 'toor');
64
+    
65
+    $Fans_de_psytrance = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
66
+    $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
67
+    $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
68
+      
69
+    // L'élément n'existe pas encore
70
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
71
+      ->findOneByName('Musique qui dechire dans psytrance')
72
+    ;
73
+    $this->assertTrue(is_null($element));
74
+    
75
+    // On commence par ajouter un tag
76
+    $url = $this->generateUrl('element_add', array('group_slug' => $Fans_de_psytrance->getSlug()));
77
+   
78
+    $extract = $this->crawler->filter('input[name="element_add[_token]"]')
79
+      ->extract(array('value'));
80
+    $csrf = $extract[0];
81
+    
82
+    $crawler = $this->client->request(
83
+      'POST', 
84
+      $url, 
85
+      array(
86
+          'element_add' => array(
87
+              '_token' => $csrf,
88
+              'name'   => 'Musique qui dechire dans psytrance',
89
+              'url'    => 'http://www.youtube.com/watch?v=WC8qb_of04E',
90
+              'tags'   => json_encode(array($hardtek->getId(), $tribe->getId()))
91
+          )
92
+        
93
+      ), 
94
+      array(), 
95
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
96
+    );
97
+    $this->outputDebug($this->client->getResponse()->getContent());
98
+    $this->isResponseSuccess();
99
+    
100
+    $response = json_decode($this->client->getResponse()->getContent(), true);
101
+    $this->assertEquals($response['status'], 'success');
102
+    
103
+    
104
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
105
+      ->findOneBy(array(
106
+        'name' => 'Musique qui dechire dans psytrance', 
107
+        'group' => $Fans_de_psytrance->getId()
108
+      ))
109
+    ;
110
+    $this->assertTrue(!is_null($element));
111
+    
112
+  }
113
+  
114
+  public function testAddElementAjaxFail()
115
+  {
116
+    $this->client = self::createClient();
117
+    $this->connectUser('joelle', 'toor');
118
+    
119
+    $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
120
+    $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
121
+      
122
+    // L'élément n'existe pas encore
123
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
124
+      ->findOneByName('Musique qui dechire')
125
+    ;
126
+    $this->assertTrue(is_null($element));
127
+    
128
+    // On commence par ajouter un tag
129
+    $url = $this->generateUrl('element_add');
130
+   
131
+    $extract = $this->crawler->filter('input[name="element_add[_token]"]')
132
+      ->extract(array('value'));
133
+    $csrf = $extract[0];
134
+    
135
+    $crawler = $this->client->request(
136
+      'POST', 
137
+      $url, 
138
+      array(
139
+          'element_add' => array(
140
+              '_token' => $csrf,
141
+              'name'   => 'Musique qui dechire',
142
+              'url'    => 'http://www',
143
+              'tags'   => json_encode(array($hardtek->getId(), $tribe->getId()))
144
+          )
145
+        
146
+      ), 
147
+      array(), 
148
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
149
+    );
150
+    
151
+    $this->isResponseSuccess();
152
+    
153
+    $response = json_decode($this->client->getResponse()->getContent(), true);
154
+    $this->assertEquals($response['status'], 'error');
155
+    
156
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
157
+      ->findOneByName('Musique qui dechire')
158
+    ;
159
+    $this->assertTrue(is_null($element));
160
+    
161
+  }
162
+  
163
+  public function testAddElementInGroupAjaxFail()
164
+  {
165
+    $this->client = self::createClient();
166
+    $this->connectUser('joelle', 'toor');
167
+    
168
+    $Fans_de_psytrance = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')->findOneByName('Fans de psytrance');
169
+    $hardtek = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek');
170
+    $tribe = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe');
171
+      
172
+    // L'élément n'existe pas encore
173
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
174
+      ->findOneByName('zo')
175
+    ;
176
+    $this->assertTrue(is_null($element));
177
+    
178
+    // On commence par ajouter un tag
179
+    $url = $this->generateUrl('element_add', array('group_slug' => $Fans_de_psytrance->getSlug()));
180
+   
181
+    $extract = $this->crawler->filter('input[name="element_add[_token]"]')
182
+      ->extract(array('value'));
183
+    $csrf = $extract[0];
184
+    
185
+    $crawler = $this->client->request(
186
+      'POST', 
187
+      $url, 
188
+      array(
189
+          'element_add' => array(
190
+              '_token' => $csrf,
191
+              'name'   => 'zo',
192
+              'url'    => 'http://www.youtube.com/watch?v=WC8qb_of04E',
193
+              'tags'   => json_encode(array($hardtek->getId(), $tribe->getId()))
194
+          )
195
+        
196
+      ), 
197
+      array(), 
198
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
199
+    );
200
+    
201
+    $this->isResponseSuccess();
202
+    
203
+    $response = json_decode($this->client->getResponse()->getContent(), true);
204
+    $this->assertEquals($response['status'], 'error');
205
+    
206
+    
207
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
208
+      ->findOneBy(array(
209
+        'name' => 'zo', 
210
+        'group' => $Fans_de_psytrance->getId()
211
+      ))
212
+    ;
213
+    $this->assertTrue(is_null($element));
214
+    
215
+  }
216
+  
217
+}