Browse Source

Test fonctionel homeFilter.

bastien 13 years ago
parent
commit
5c5bf206ba

+ 76 - 0
src/Muzich/CoreBundle/Tests/Controller/HomeControllerTest.php View File

@@ -0,0 +1,76 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+use Muzich\CoreBundle\Searcher\ElementSearcher;
7
+
8
+class HomeControllerTest extends FunctionalTest
9
+{
10
+  /**
11
+   * Ce test contrôle l'affichage des elements sur la page d'accueil
12
+   * Il modifie egallement le filtre d'éléments
13
+   */
14
+  public function testFilter()
15
+  {
16
+    $this->connectUser('bux', 'toor');
17
+
18
+    // Présence du formulaire d'ajout d'un élément
19
+    $this->exist('form[action="'.($url = $this->generateUrl('element_add')).'"]');
20
+    $this->exist('form[action="'.$url.'"] input[id="element_add_name"]');
21
+    $this->exist('form[action="'.$url.'"] input[id="element_add_url"]');
22
+    $this->exist('form[action="'.$url.'"] select[id="element_add_group"]');
23
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
24
+    
25
+    // Présence du formulaire de filtrage
26
+    $this->exist('form[action="'.($url = $this->generateUrl('search_elements')).'"]');
27
+    $this->exist('form[action="'.$url.'"] select[id="element_search_form_network"]');
28
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
29
+    
30
+    $hardtek_id = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek')->getId();
31
+    $tribe_id   = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')->getId();
32
+    
33
+    // On récupére le formulaire de filtrage
34
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
35
+    
36
+    // On décoche les tags
37
+    foreach ($this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findAll() as $tag)
38
+    {
39
+      $form['element_search_form[tags]['.$tag->getId().']']->untick();
40
+    }
41
+    
42
+    // On met ce que l'on veut dans le form
43
+    $form['element_search_form[network]'] = ElementSearcher::NETWORK_PUBLIC;
44
+    $form['element_search_form[tags]['.$hardtek_id.']'] = $hardtek_id;
45
+    $form['element_search_form[tags]['.$tribe_id.']'] = $tribe_id;
46
+    $this->submit($form);
47
+    
48
+    $this->client->submit($form);
49
+    
50
+    $this->isResponseRedirection();
51
+    $this->followRedirection();
52
+    $this->isResponseSuccess();
53
+    
54
+    $this->assertTrue($this->getSession()->has('user.element_search.params'));
55
+    $this->assertEquals(array(
56
+        'network'   => ElementSearcher::NETWORK_PUBLIC,
57
+        'tags'      => array(
58
+          $hardtek_id, $tribe_id
59
+        ),
60
+        'count'     => $this->getContainer()->getParameter('search_default_count'),
61
+        'user_id'   => null,
62
+        'group_id'  => null,
63
+        'favorite' => false
64
+    ), $this->getSession()->get('user.element_search.params'));
65
+    
66
+    // On fabrique l'ElementSearcher correspondant
67
+    $es = new ElementSearcher();
68
+    $es->init($this->getSession()->get('user.element_search.params'));
69
+    
70
+    foreach ($es->getElements($this->getDoctrine(), $this->getUser()->getId()) as $element)
71
+    {
72
+      $this->exist('html:contains("'.$element->getName().'")');
73
+    }
74
+  }
75
+  
76
+}

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

@@ -30,6 +30,36 @@ class FunctionalTest extends WebTestCase
30 30
     return $this->client->getContainer()->get('security.context')->getToken()->getUser();
31 31
   }
32 32
   
33
+  protected function connectUser($login, $password)
34
+  {
35
+    $this->client = self::createClient();
36
+
37
+    $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
38
+    $this->isResponseSuccess();
39
+
40
+    $this->assertEquals('anon.', $this->getUser());
41
+
42
+    $this->exist('div.login');
43
+    $this->exist('form[action="'.($url = $this->generateUrl('fos_user_security_check')).'"]');
44
+    $this->exist('form[action="'.$url.'"] input[id="username"]');
45
+    $this->exist('form[action="'.$url.'"] input[id="password"]');
46
+    $this->exist('form[action="'.$url.'"] input[id="remember_me"]');
47
+    $this->exist('form[action="'.$url.'"] input[type="submit"]');
48
+
49
+    $form = $this->selectForm('form[action="'.$url.'"] input[type="submit"]');
50
+    $form['_username'] = $login;
51
+    $form['_password'] = $password;
52
+    $form['_remember_me'] = true;
53
+    $this->submit($form);
54
+
55
+    $this->isResponseRedirection();
56
+    $this->followRedirection();
57
+    $this->isResponseSuccess();
58
+
59
+    $user = $this->getUser();
60
+    $this->assertEquals($login, $user->getUsername());
61
+  }
62
+  
33 63
   /**
34 64
    * Generates a URL from the given parameters.
35 65
    *
@@ -44,6 +74,25 @@ class FunctionalTest extends WebTestCase
44 74
     return $this->client->getContainer()->get('router')->generate($route, $parameters, $absolute);
45 75
   }
46 76
   
77
+  protected function getContainer()
78
+  {
79
+    return $this->client->getContainer();
80
+  }
81
+  
82
+  protected function getSession()
83
+  {
84
+    return $this->getContainer()->get('session');
85
+  }
86
+  
87
+  /**
88
+   *
89
+   * @return \Symfony\Bundle\DoctrineBundle\Registry
90
+   */
91
+  protected function getDoctrine()
92
+  {
93
+    return $this->client->getContainer()->get('doctrine');
94
+  }
95
+  
47 96
   /**
48 97
    * Test l'existance d'un element
49 98
    * 
@@ -101,7 +150,7 @@ class FunctionalTest extends WebTestCase
101 150
    */
102 151
   protected function followRedirection()
103 152
   {
104
-    $this->client->followRedirect();
153
+    $this->crawler = $this->client->followRedirect();
105 154
   }
106 155
   
107 156
   /**