Browse Source

Evolution #124: Du tessst et encore du test: test fonctionel ajout de tag

bastien 13 years ago
parent
commit
f9ddee054a

+ 158 - 0
src/Muzich/CoreBundle/Tests/Controller/TagsTest.php View File

@@ -0,0 +1,158 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+
7
+class TagsTest extends FunctionalTest
8
+{ 
9
+  /**
10
+   * Test du listing de ses favoris
11
+   */
12
+  public function testAddTag()
13
+  {    
14
+    $this->client = self::createClient();
15
+    $this->connectUser('paul', 'toor');
16
+    
17
+    // On commence par ajouter un tag
18
+    $url = $this->generateUrl('ajax_add_tag', array('name' => 'Mon Beau Tag'));
19
+    
20
+    $crawler = $this->client->request('GET', $url, array(), array(), array(
21
+        'X-Requested-With' => 'XMLHttpRequest',
22
+    ));
23
+    $this->isResponseSuccess();
24
+    
25
+    $response = json_decode($this->client->getResponse()->getContent(), true);
26
+    $this->assertEquals($response['status'], 'success');
27
+    
28
+    $tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
29
+      ->findOneByName('Mon beau tag');
30
+    
31
+    $this->assertTrue(!is_null($tag));
32
+    
33
+    // Paul ajoute un élément avec ce tag
34
+    $this->procedure_add_element(
35
+      'Un nouvel élément', 
36
+      'http://www.youtube.com/watch?v=WC8qb_of04E', 
37
+      array($tag->getId())
38
+    );
39
+    
40
+    $this->isResponseRedirection();
41
+    $this->followRedirection();
42
+    $this->isResponseSuccess();
43
+    
44
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
45
+      ->findOneByName('Un nouvel élément')
46
+    ;
47
+    
48
+    $paul = $this->getUser();
49
+    
50
+    // Il ajoute cet élément en favoris
51
+    $url = $this->generateUrl('favorite_add', array(
52
+      'id'    => $element->getId(),
53
+      'token' => $paul->getPersonalHash()
54
+    ));
55
+    
56
+    $crawler = $this->client->request('GET', $url, array(), array(), array(
57
+        'HTTP_X-Requested-With' => 'XMLHttpRequest',
58
+    ));
59
+    
60
+    $this->isResponseSuccess();
61
+    
62
+    // On contrôle la présence du favoris
63
+    $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
64
+      ->findOneBy(array(
65
+        'user'    => $paul->getId(),
66
+        'element' => $element->getId()
67
+      ));
68
+    
69
+    $this->assertTrue(!is_null($fav));
70
+    
71
+    /*
72
+     * Contrôle de la vue de ce tag
73
+     */
74
+    
75
+    // Paul, l'ayant ajouté peut le voir.
76
+    
77
+    // sur la page home (l'élément qu'il vient d'envoyer)
78
+    $this->client->request('GET', $this->generateUrl('home'));
79
+    $this->isResponseSuccess();
80
+    
81
+    $this->exist('li.element_tag');
82
+    $this->exist('li.element_tag:contains("Mon beau tag")');
83
+    
84
+    // sur sa page de favoris
85
+    $this->client->request('GET', $this->generateUrl('favorites_my_list'));
86
+    $this->isResponseSuccess();
87
+    
88
+    $this->exist('li.element_tag');
89
+    $this->exist('li.element_tag:contains("Mon beau tag")');
90
+    
91
+    // Lors d'une recherche de tag
92
+    $url = $this->generateUrl('search_tag', array(
93
+      'string_search' => 'mon beau tag',
94
+      'timestamp'     => time()
95
+    ));
96
+    $crawler = $this->client->request('GET', $url, array(), array(), array(
97
+        'HTTP_X-Requested-With' => 'XMLHttpRequest'
98
+    ));
99
+    
100
+    $this->isResponseSuccess();
101
+    
102
+    $response = json_decode($this->client->getResponse()->getContent(), true);
103
+    $this->assertTrue($this->findTagNameInResponse($response, 'Mon beau tag', 'data'));
104
+    
105
+    // En revanche, bux ne pourra pas les voirs lui.
106
+    $this->disconnectUser();
107
+    $this->client = self::createClient();
108
+    $this->connectUser('bux', 'toor');
109
+    
110
+    // On enlève les tags pour être sur d'avoir l'élément ajouté par paul
111
+    $this->client->request('GET', $this->generateUrl('filter_clear'));
112
+    $this->client->request('GET', $this->generateUrl('home'));
113
+    $this->isResponseSuccess();
114
+    
115
+    $this->exist('li.element_tag');
116
+    $this->notExist('li.element_tag:contains("Mon beau tag")');
117
+    
118
+    // sur ma page de favoris de paul
119
+    $this->client->request('GET', $this->generateUrl('favorite_user_list', array('slug' => $paul->getSlug())));
120
+    $this->isResponseSuccess();
121
+    
122
+    $this->exist('li.element_tag');
123
+    $this->notExist('li.element_tag:contains("Mon beau tag")');
124
+    
125
+    // Lors d'une recherche de tag
126
+    $url = $this->generateUrl('search_tag', array(
127
+      'string_search' => 'mon beau tag',
128
+      'timestamp'     => time()
129
+    ));
130
+    $crawler = $this->client->request('GET', $url, array(), array(), array(
131
+        'HTTP_X-Requested-With' => 'XMLHttpRequest'
132
+    ));
133
+    
134
+    $this->isResponseSuccess();
135
+    
136
+    $response = json_decode($this->client->getResponse()->getContent(), true);
137
+    $this->assertFalse($this->findTagNameInResponse($response, 'Mon beau tag', 'data'));
138
+  }
139
+  
140
+  protected function findTagNameInResponse($response = array(), $name = null, $i = null)
141
+  {
142
+    if (count($response))
143
+    {
144
+      if (array_key_exists($i, $response))
145
+      {
146
+        foreach ($response[$i] as $tag)
147
+        {
148
+          if ($tag['name'] == $name)
149
+          {
150
+            return true;
151
+          }
152
+        }
153
+        return false;
154
+      }
155
+    }
156
+  }
157
+  
158
+}

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

@@ -172,7 +172,7 @@ class FunctionalTest extends WebTestCase
172 172
    * @param string $name
173 173
    * @param string $url
174 174
    * @param array $tags
175
-   * @param int $group_id 
175
+   * @param string $group_slug 
176 176
    */
177 177
   protected function procedure_add_element($name, $url, $tags, $group_slug = null)
178 178
   {