Quellcode durchsuchen

Evolution #700: Test nécessaires

Bastien Sevajol vor 12 Jahren
Ursprung
Commit
e6d3a1041c

+ 3 - 2
src/Muzich/CommentBundle/Controller/CommentController.php Datei anzeigen

6
 use Muzich\CoreBundle\Managers\CommentsManager;
6
 use Muzich\CoreBundle\Managers\CommentsManager;
7
 use Muzich\CoreBundle\Propagator\EventElement;
7
 use Muzich\CoreBundle\Propagator\EventElement;
8
 use Muzich\CoreBundle\Security\Context as SecurityContext;
8
 use Muzich\CoreBundle\Security\Context as SecurityContext;
9
+use Symfony\Component\HttpFoundation\Request;
9
 
10
 
10
 class CommentController extends Controller
11
 class CommentController extends Controller
11
 {
12
 {
19
    */
20
    */
20
   public function addAction($element_id, $token)
21
   public function addAction($element_id, $token)
21
   {
22
   {
22
-    if (($response = $this->mustBeConnected(true)))
23
+    if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_COMMENT_ADD)) !== false)
23
     {
24
     {
24
-      return $response;
25
+      return $this->jsonResponseError($non_condition);
25
     }
26
     }
26
     
27
     
27
     if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
28
     if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')

+ 2 - 2
src/Muzich/CoreBundle/Controller/CoreController.php Datei anzeigen

109
    */
109
    */
110
   public function followAction($type, $id, $token)
110
   public function followAction($type, $id, $token)
111
   {
111
   {
112
-    if (($response = $this->mustBeConnected()))
112
+    if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_USER_FOLLOW)) !== false)
113
     {
113
     {
114
-      return $response;
114
+      return $this->jsonResponseError($non_condition);
115
     }
115
     }
116
     
116
     
117
     $user = $this->getUser();
117
     $user = $this->getUser();

+ 5 - 0
src/Muzich/CoreBundle/Controller/ElementController.php Datei anzeigen

607
   
607
   
608
   public function proposeTagsProceedAction($element_id, $token)
608
   public function proposeTagsProceedAction($element_id, $token)
609
   {
609
   {
610
+    if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION)) !== false)
611
+    {
612
+      return $this->jsonResponseError($non_condition);
613
+    }
614
+    
610
     if (($response = $this->mustBeConnected(true)))
615
     if (($response = $this->mustBeConnected(true)))
611
     {
616
     {
612
       return $response;
617
       return $response;

+ 0 - 0
src/Muzich/CoreBundle/Form/User/PasswordForm.php Datei anzeigen


+ 12 - 4
src/Muzich/CoreBundle/Security/Context.php Datei anzeigen

58
         self::CONDITION_USER_NOT_CONNECTED
58
         self::CONDITION_USER_NOT_CONNECTED
59
       ),
59
       ),
60
       self::ACTION_COMMENT_ADD => array(
60
       self::ACTION_COMMENT_ADD => array(
61
-        self::CONDITION_USER_NOT_CONNECTED,
62
-        self::CONDITION_USER_EMAIL_NOT_CONFIRMED
61
+        self::CONDITION_USER_NOT_CONNECTED
63
       ),
62
       ),
64
       self::ACTION_USER_FOLLOW => array(
63
       self::ACTION_USER_FOLLOW => array(
65
         self::CONDITION_USER_NOT_CONNECTED
64
         self::CONDITION_USER_NOT_CONNECTED
124
     {
123
     {
125
       foreach (self::$affecteds_actions[$affect][$action] as $affected_condition)
124
       foreach (self::$affecteds_actions[$affect][$action] as $affected_condition)
126
       {
125
       {
127
-        $affected_condition_method = 'is'.$affected_condition;
128
-        if ($this->$affected_condition_method())
126
+        if ($this->userIsInThisCondition($affected_condition))
129
         {
127
         {
130
           return $affected_condition;
128
           return $affected_condition;
131
         }
129
         }
135
     return false;
133
     return false;
136
   }
134
   }
137
   
135
   
136
+  public function userIsInThisCondition($condition)
137
+  {
138
+    $affected_condition_method = 'is'.$condition;
139
+    if ($this->$affected_condition_method())
140
+    {
141
+      return true;
142
+    }
143
+    return false;
144
+  }
145
+  
138
   protected function isUserNotConnected()
146
   protected function isUserNotConnected()
139
   {
147
   {
140
     if ($this->anonymous)
148
     if ($this->anonymous)

+ 103 - 0
src/Muzich/CoreBundle/Tests/Controller/AnonymousTest.php Datei anzeigen

1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+use Muzich\CoreBundle\Tests\lib\Security\Context as SecurityContextTest;
7
+use Muzich\CoreBundle\Security\Context as SecurityContext;
8
+
9
+class NoPassTest extends FunctionalTest
10
+{
11
+  
12
+  protected $security_context_test;
13
+  
14
+  protected function init()
15
+  {
16
+    $this->client = self::createClient();
17
+    $this->security_context_test = new SecurityContextTest($this->client, $this);
18
+  }
19
+  
20
+  public function testLimitedActionsForAnonymous()
21
+  {
22
+    $this->init();
23
+    $this->checkUserIsAnonymous();
24
+    $this->checkUserCantMakeProhibedActionsForAnonymous();
25
+    $this->registerUser('dijarr@mail.com');
26
+    $this->checkUserIsNotProhibedForAnonymousActions();
27
+  }
28
+  
29
+  protected function checkUserIsAnonymous()
30
+  {
31
+    $this->assertEquals('anon.', $this->getUser());
32
+  }
33
+  
34
+  protected function checkUserCantMakeProhibedActionsForAnonymous()
35
+  {
36
+    $this->checkUserProhibedActionStatus(true);
37
+  }
38
+  
39
+  protected function checkUserProhibedActionStatus($match)
40
+  {
41
+    $this->security_context_test->testUserCantMakeActionStatus( 
42
+      SecurityContext::ACTION_ELEMENT_ADD, 
43
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
44
+      $match
45
+    );
46
+    $this->security_context_test->testUserCantMakeActionStatus( 
47
+      SecurityContext::ACTION_ELEMENT_NOTE, 
48
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
49
+      $match
50
+    );
51
+    $this->security_context_test->testUserCantMakeActionStatus( 
52
+      SecurityContext::ACTION_COMMENT_ALERT, 
53
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
54
+      $match
55
+    );
56
+    $this->security_context_test->testUserCantMakeActionStatus( 
57
+      SecurityContext::ACTION_ELEMENT_ALERT, 
58
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
59
+      $match
60
+    );
61
+    $this->security_context_test->testUserCantMakeActionStatus( 
62
+      SecurityContext::ACTION_TAG_ADD, 
63
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
64
+      $match
65
+    );
66
+    $this->security_context_test->testUserCantMakeActionStatus( 
67
+      SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION, 
68
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
69
+      $match
70
+    );
71
+    $this->security_context_test->testUserCantMakeActionStatus( 
72
+      SecurityContext::ACTION_GROUP_ADD, 
73
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
74
+      $match
75
+    );
76
+    $this->security_context_test->testUserCantMakeActionStatus( 
77
+      SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES, 
78
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
79
+      $match
80
+    );
81
+    $this->security_context_test->testUserCantMakeActionStatus( 
82
+      SecurityContext::ACTION_COMMENT_ADD, 
83
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
84
+      $match
85
+    );
86
+    $this->security_context_test->testUserCantMakeActionStatus( 
87
+      SecurityContext::ACTION_USER_FOLLOW, 
88
+      SecurityContext::CONDITION_USER_NOT_CONNECTED,
89
+      $match
90
+    );
91
+  }
92
+  
93
+  protected function registerUser($email)
94
+  {
95
+    $this->procedure_registration_success($email);
96
+  }
97
+  
98
+  protected function checkUserIsNotProhibedForAnonymousActions()
99
+  {
100
+    $this->checkUserProhibedActionStatus(false);
101
+  }
102
+  
103
+}

+ 93 - 0
src/Muzich/CoreBundle/Tests/Controller/NoPassTest.php Datei anzeigen

1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+use Muzich\CoreBundle\Tests\lib\Security\Context as SecurityContextTest;
7
+use Muzich\CoreBundle\Security\Context as SecurityContext;
8
+
9
+class NoPassTest extends FunctionalTest
10
+{
11
+  
12
+  protected $security_context_test;
13
+  
14
+  protected function init()
15
+  {
16
+    $this->client = self::createClient();
17
+    $this->security_context_test = new SecurityContextTest($this->client, $this);
18
+  }
19
+  
20
+  public function testConfirmationEmail()
21
+  {
22
+    $this->init();
23
+    $this->registerUser('francky@mail.com');
24
+    $this->checkUserEmailIsNotConfirmed();
25
+    $this->checkUserCantMakeProhibedActionsForEmailNotConfirmed();
26
+    $this->confirmEmail();
27
+    $this->checkUserEmailIsConfirmed();
28
+    $this->checkUserisNotProhibedForActionsBlockedByEmailNotConfirmed();
29
+  }
30
+  
31
+  protected function registerUser($email)
32
+  {
33
+    $this->procedure_registration_success($email);
34
+  }
35
+  
36
+  protected function checkUserEmailIsNotConfirmed()
37
+  {
38
+    $this->security_context_test->userIsInConditionEmailNotConfirmed($this->getUser());
39
+  }
40
+  
41
+  protected function checkUserCantMakeProhibedActionsForEmailNotConfirmed()
42
+  {
43
+    $this->checkUserProhibedActionStatus(true);
44
+  }
45
+  
46
+  protected function checkUserProhibedActionStatus($match)
47
+  {
48
+    foreach (array(
49
+      SecurityContext::ACTION_ELEMENT_ADD, 
50
+      SecurityContext::ACTION_ELEMENT_NOTE,
51
+      SecurityContext::ACTION_COMMENT_ALERT,
52
+      SecurityContext::ACTION_ELEMENT_ALERT,
53
+      SecurityContext::ACTION_TAG_ADD,
54
+      SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION,
55
+      SecurityContext::ACTION_GROUP_ADD
56
+    ) as $action)
57
+    {
58
+      $this->security_context_test->testUserCantMakeActionStatus( 
59
+        $action, 
60
+        SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED,
61
+        $match
62
+      );
63
+    }
64
+  }
65
+  
66
+  protected function confirmEmail()
67
+  {
68
+    $token = hash('sha256', $this->getUser()->getConfirmationToken().$this->getUser()->getEmail());
69
+    $this->goToPage($this->generateUrl('email_confirm', array('token' => $token)));
70
+    $this->isResponseRedirection();
71
+  }
72
+  
73
+  protected function checkUserEmailIsConfirmed()
74
+  {
75
+    $this->security_context_test->userIsNotInConditionEmailNotConfirmed($this->getUser());
76
+  }
77
+  
78
+  protected function checkUserisNotProhibedForActionsBlockedByEmailNotConfirmed()
79
+  {
80
+    $this->checkUserProhibedActionStatus(false);
81
+  }
82
+  
83
+  public function testSetPassword()
84
+  {
85
+    
86
+  }
87
+  
88
+  public function testSetUsername()
89
+  {
90
+    
91
+  }
92
+  
93
+}

+ 0 - 77
src/Muzich/CoreBundle/Tests/Controller/RegistrationTokenTest.php Datei anzeigen

1
-<?php
2
-
3
-namespace Muzich\CoreBundle\Tests\Controller;
4
-
5
-use Muzich\CoreBundle\lib\FunctionalTest;
6
-use Muzich\CoreBundle\Entity\RegistrationToken;
7
-
8
-class UserControllerTest extends FunctionalTest
9
-{
10
-  
11
-  public function testRegistrationToken()
12
-  {
13
-    $this->client = self::createClient();
14
-    $token = new RegistrationToken();
15
-    $token_name = 'token_test_3_max_'.time();
16
-    $token->setToken($token_name);
17
-    $token->setCountMax(3);
18
-    $em = $this->getDoctrine()->getEntityManager();
19
-    $em->persist($token);
20
-    $em->flush();
21
-    
22
-    $this->procedure_registration_success(
23
-      'user1', 
24
-      'user1@mail.com', 
25
-      'toor', 
26
-      'toor',
27
-      $token_name
28
-    );
29
-    
30
-    $this->disconnectUser();
31
-    
32
-    $this->procedure_registration_success(
33
-      'user2', 
34
-      'user2@mail.com', 
35
-      'toor', 
36
-      'toor',
37
-      $token_name
38
-    );
39
-    
40
-    $this->disconnectUser();
41
-    
42
-    $this->procedure_registration_success(
43
-      'user3', 
44
-      'user3@mail.com', 
45
-      'toor', 
46
-      'toor',
47
-      $token_name
48
-    );
49
-    
50
-    $this->disconnectUser();
51
-    
52
-    $this->procedure_registration_failure(
53
-      'user4', 
54
-      'user4@mail.com', 
55
-      'toor', 
56
-      'toor',
57
-      $token_name
58
-    );
59
-        
60
-    $this->procedure_registration_failure(
61
-      'user5', 
62
-      'user5@mail.com', 
63
-      'toor', 
64
-      'toor',
65
-      $token_name
66
-    );
67
-        
68
-    $this->procedure_registration_failure(
69
-      'user6', 
70
-      'user6@mail.com', 
71
-      'toor', 
72
-      'toor',
73
-      ''
74
-    );
75
-  }
76
-  
77
-}

Datei-Diff unterdrückt, da er zu groß ist
+ 510 - 521
src/Muzich/CoreBundle/Tests/Controller/UserControllerTest.php


+ 3 - 1
src/Muzich/CoreBundle/Tests/Security/ContextTest.php Datei anzeigen

10
   
10
   
11
   public function testActionsWithNotConfirmedEmailUser()
11
   public function testActionsWithNotConfirmedEmailUser()
12
   {
12
   {
13
-    $secutiry_context = new SecurityContext(new User());
13
+    $user_not_confirmed_email = new User();
14
+    $user_not_confirmed_email->setEmailConfirmed(false);
15
+    $secutiry_context = new SecurityContext($user_not_confirmed_email);
14
     
16
     
15
     $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD));
17
     $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD));
16
     $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_NOTE));
18
     $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_NOTE));

+ 83 - 0
src/Muzich/CoreBundle/Tests/lib/Security/Context.php Datei anzeigen

1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\lib\Security;
4
+
5
+use Muzich\CoreBundle\lib\Test\Client;
6
+use Muzich\CoreBundle\Entity\User;
7
+use Muzich\CoreBundle\Security\Context as SecurityContext;
8
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
9
+use Muzich\CoreBundle\Tests\lib\Security\ContextTestCases as SecurityContextTestCases;
10
+
11
+class Context
12
+{
13
+  
14
+  protected $test;
15
+  protected $security_context_tests;
16
+  
17
+  public function __construct(Client $client, WebTestCase $test)
18
+  {
19
+    $this->test = $test;
20
+    $this->security_context_tests = new SecurityContextTestCases($client, $test);
21
+  }
22
+  
23
+  public function userIsInConditionEmailNotConfirmed(User $user)
24
+  {
25
+    return $this->userIsInCondition($user, SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED);
26
+  }
27
+  
28
+  public function userIsNotInConditionEmailNotConfirmed(User $user)
29
+  {
30
+    return !$this->userIsInCondition($user, SecurityContext::CONDITION_USER_EMAIL_NOT_CONFIRMED);
31
+  }
32
+  
33
+  protected function userIsInCondition(User $user, $condition)
34
+  {
35
+    $security_context = new SecurityContext($user);
36
+    return $security_context->userIsInThisCondition($condition);
37
+  }
38
+  
39
+  public function testUserCantMakeActionStatus($action, $condition, $match)
40
+  {
41
+    $this->test->assertEquals($match, $this->testActionResponseInPratice($action, $condition, false));
42
+  }
43
+  
44
+  private function testActionResponseInPratice($action, $condition, $success)
45
+  {
46
+    switch ($action)
47
+    {
48
+      case SecurityContext::ACTION_ELEMENT_ADD:
49
+        return $this->security_context_tests->addElementResponseIs($success, $condition);
50
+      break;
51
+      case SecurityContext::ACTION_ELEMENT_NOTE:
52
+        return $this->security_context_tests->noteElementResponseIs($success, $condition);
53
+      break;
54
+      case SecurityContext::ACTION_COMMENT_ALERT:
55
+        return $this->security_context_tests->alertCommentResponseIs($success, $condition);
56
+      break;
57
+      case SecurityContext::ACTION_ELEMENT_ALERT:
58
+        return $this->security_context_tests->alertElementResponseIs($success, $condition);
59
+      break;
60
+      case SecurityContext::ACTION_TAG_ADD:
61
+        return $this->security_context_tests->addTagResponseIs($success, $condition);
62
+      break;
63
+      case SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION:
64
+        return $this->security_context_tests->proposeElementTagsResponseIs($success, $condition);
65
+      break;
66
+      case SecurityContext::ACTION_GROUP_ADD:
67
+        return $this->security_context_tests->addGroupResponseIs($success, $condition);
68
+      break;
69
+      case SecurityContext::ACTION_COMMENT_ADD:
70
+        return $this->security_context_tests->addCommentResponseIs($success, $condition);
71
+      break;
72
+      case SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES:
73
+        return $this->security_context_tests->addElementToFavoriteResponseIs($success, $condition);
74
+      break;
75
+      case SecurityContext::ACTION_USER_FOLLOW:
76
+        return $this->security_context_tests->followUserResponseIs($success, $condition);
77
+      break;
78
+      default:
79
+        throw new \Exception('Action unknow');
80
+    }
81
+  }
82
+  
83
+}

+ 227 - 0
src/Muzich/CoreBundle/Tests/lib/Security/ContextTestCases.php Datei anzeigen

1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\lib\Security;
4
+
5
+use Muzich\CoreBundle\lib\Test\Client;
6
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7
+use Muzich\CoreBundle\Security\Context as SecurityContext;
8
+
9
+class ContextTestCases
10
+{
11
+  
12
+  protected $client;
13
+  protected $test;
14
+  
15
+  public function __construct(Client $client, WebTestCase $test)
16
+  {
17
+    $this->client = $client;
18
+    $this->test = $test;
19
+  }
20
+  
21
+  private function responseSatisfyConditions($response, $success, $condition)
22
+  {
23
+    $response = json_decode($response, true);
24
+    
25
+    if ($response['status'] === 'success' && $success)
26
+    {
27
+      return true;
28
+    }
29
+    
30
+    if ($response['status'] === 'error' && !$success)
31
+    {
32
+      if ($condition && !array_key_exists('error', $response))
33
+      {
34
+        return false;
35
+      }
36
+      
37
+      if ($condition && $response['error'] !== $condition)
38
+      {
39
+        return false;
40
+      }
41
+      
42
+      return true;
43
+    }
44
+    
45
+    return false;
46
+  }
47
+  
48
+  public function getAjaxRequestContentResponse($method, $url, $parameters = array())
49
+  {
50
+    $this->test->getClient()->request(
51
+      $method, $url, $parameters, array(), 
52
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
53
+    );
54
+    return $this->test->getClient()->getResponse()->getContent();
55
+  }
56
+  
57
+  public function addElementResponseIs($success, $condition)
58
+  {
59
+    return $this->responseSatisfyConditions(
60
+      $this->getAjaxRequestContentResponse(
61
+        'POST',
62
+        $this->test->generateUrl('element_add', array('_locale' => 'fr'))
63
+      ), 
64
+      $success, 
65
+      $condition
66
+    );
67
+  }
68
+  
69
+  public function noteElementResponseIs($success, $condition)
70
+  {
71
+    return $this->responseSatisfyConditions(
72
+      $this->getAjaxRequestContentResponse(
73
+        'GET',
74
+        $this->test->generateUrl('ajax_element_add_vote_good', array(
75
+          'element_id' => 0,
76
+          'token' => 'notoken'
77
+        ))
78
+      ), 
79
+      $success, 
80
+      $condition
81
+    );
82
+  }
83
+  
84
+  public function alertCommentResponseIs($success, $condition)
85
+  {
86
+    return $this->responseSatisfyConditions(
87
+      $this->getAjaxRequestContentResponse(
88
+        'GET',
89
+        $this->test->generateUrl('ajax_alert_comment', array(
90
+          'element_id' => 0,
91
+          'date'       => 0,
92
+          'token'      => 'notoken'
93
+        ))
94
+      ), 
95
+      $success, 
96
+      $condition
97
+    );
98
+  }
99
+  
100
+  public function alertElementResponseIs($success, $condition)
101
+  {
102
+    return $this->responseSatisfyConditions(
103
+      $this->getAjaxRequestContentResponse(
104
+        'GET',
105
+        $this->test->generateUrl('ajax_report_element', array(
106
+          'element_id' => 0,
107
+          'token'      => 'notoken'
108
+        ))
109
+      ), 
110
+      $success, 
111
+      $condition
112
+    );
113
+  }
114
+  
115
+  public function addTagResponseIs($success, $condition)
116
+  {
117
+    return $this->responseSatisfyConditions(
118
+      $this->getAjaxRequestContentResponse(
119
+        'POST',
120
+        $this->test->generateUrl('ajax_add_tag'),
121
+        array('tag_name' => 'Mon Beau Tag !1245ddregfz')
122
+      ), 
123
+      $success, 
124
+      $condition
125
+    );
126
+  }
127
+  
128
+  public function proposeElementTagsResponseIs($success, $condition)
129
+  {
130
+    return $this->responseSatisfyConditions(
131
+      $this->getAjaxRequestContentResponse(
132
+        'POST',
133
+        $this->test->generateUrl('ajax_element_propose_tags_proceed', 
134
+          array('element_id' => 0, 'token' => 'notoken')
135
+        ),
136
+        array(
137
+          'element_tag_proposition_0' => array(
138
+            'tags' => json_encode(array(0, 0))
139
+          )
140
+        )
141
+      ), 
142
+      $success, 
143
+      $condition
144
+    );
145
+  }
146
+  
147
+  public function addGroupResponseIs($success, $condition)
148
+  {
149
+    $this->test->getClient()->request(
150
+      'POST', 
151
+      $this->test->generateUrl('group_add'), 
152
+      array(
153
+        'group' => array(
154
+          'name' => 'Un groupe lala45f4rgb1e',
155
+          'description' => 'description d45fqs4cq6',
156
+          'tags' => array(),
157
+          '_token' => 'notoken'
158
+        )
159
+      ), 
160
+      array(), 
161
+      array()
162
+    );
163
+    
164
+    if ($this->test->getClient()->getResponse()->getStatusCode() == 200 && $success)
165
+    {
166
+      return true;
167
+    }
168
+    
169
+    if ($this->test->getClient()->getResponse()->getStatusCode() != 200 && !$success)
170
+    {
171
+      $security_context = new SecurityContext($this->test->getUser());
172
+      if ($condition && !$security_context->userIsInThisCondition($condition))
173
+      {
174
+        return false;
175
+      }
176
+      
177
+      return true;
178
+    }
179
+  }
180
+  
181
+  public function addCommentResponseIs($success, $condition)
182
+  {
183
+    return $this->responseSatisfyConditions(
184
+      $this->getAjaxRequestContentResponse(
185
+        'POST',
186
+        $this->test->generateUrl('ajax_add_comment', array(
187
+          'element_id' => 0,
188
+          'token'      => 'notoken'
189
+        ))
190
+      ), 
191
+      $success, 
192
+      $condition
193
+    );
194
+  }
195
+  
196
+  public function addElementToFavoriteResponseIs($success, $condition)
197
+  {
198
+    return $this->responseSatisfyConditions(
199
+      $this->getAjaxRequestContentResponse(
200
+        'GET',
201
+        $this->test->generateUrl('favorite_add', array(
202
+          'id'    => 0,
203
+          'token' => 'notoken'
204
+        ))
205
+      ), 
206
+      $success, 
207
+      $condition
208
+    );
209
+  }
210
+  
211
+  public function followUserResponseIs($success, $condition)
212
+  {
213
+    return $this->responseSatisfyConditions(
214
+      $this->getAjaxRequestContentResponse(
215
+        'GET',
216
+        $this->test->generateUrl('follow', array(
217
+          'type' => 'user', 
218
+          'id' => 0,
219
+          'token' => 'notoken'
220
+        ))
221
+      ), 
222
+      $success, 
223
+      $condition
224
+    );
225
+  }
226
+  
227
+}

+ 6 - 1
src/Muzich/CoreBundle/lib/Controller.php Datei anzeigen

230
     throw new \Exception('User not connected');
230
     throw new \Exception('User not connected');
231
   }
231
   }
232
   
232
   
233
+  protected function getUserRefreshed()
234
+  {
235
+    return $this->getUser(false, array(), true);
236
+  }
237
+  
233
   /**
238
   /**
234
    * Retourne un tabeau avec les tags connus.
239
    * Retourne un tabeau avec les tags connus.
235
    * TODO: Voir pour que cette info soit stocké (par exemple) dans un champs
240
    * TODO: Voir pour que cette info soit stocké (par exemple) dans un champs
580
   
585
   
581
   protected function sendEmailconfirmationEmail($set_send_time = true)
586
   protected function sendEmailconfirmationEmail($set_send_time = true)
582
   {
587
   {
583
-    $user = $this->getUser();
588
+    $user = $this->getUserRefreshed();
584
     
589
     
585
     $tokenGenerator = $this->container->get('fos_user.util.token_generator');
590
     $tokenGenerator = $this->container->get('fos_user.util.token_generator');
586
     $user->setConfirmationToken($tokenGenerator->generateToken());
591
     $user->setConfirmationToken($tokenGenerator->generateToken());

+ 44 - 40
src/Muzich/CoreBundle/lib/FunctionalTest.php Datei anzeigen

24
    */
24
    */
25
   protected $crawler;
25
   protected $crawler;
26
   
26
   
27
+  public function getClient()
28
+  {
29
+    return $this->client;
30
+  }
31
+  
32
+  public function getCrawler()
33
+  {
34
+    return $this->crawler;
35
+  }
36
+  
27
   protected function outputDebug($content = null)
37
   protected function outputDebug($content = null)
28
   {
38
   {
29
     $time = time();
39
     $time = time();
48
    * 
58
    * 
49
    * @return \Muzich\CoreBundle\Entity\User 
59
    * @return \Muzich\CoreBundle\Entity\User 
50
    */
60
    */
51
-  protected function getUser($username = null)
61
+  public function getUser($username = null)
52
   {
62
   {
53
     if (!$username)
63
     if (!$username)
54
     {
64
     {
55
-      return $this->client->getContainer()->get('security.context')->getToken()->getUser();
65
+      $token = $this->client->getContainer()->get('security.context')->getToken();
66
+      if ($token)
67
+      {
68
+        return $token->getUser();
69
+      }
70
+      
71
+      return 'anon.';
56
     }
72
     }
57
     else
73
     else
58
     {
74
     {
117
     $this->crawler = $this->client->request('GET', $this->generateUrl('fos_user_security_logout'));
133
     $this->crawler = $this->client->request('GET', $this->generateUrl('fos_user_security_logout'));
118
   }
134
   }
119
   
135
   
120
-  protected function validate_registrate_user_form($form, $username, $email, $pass1, $pass2, $token)
136
+  protected function validate_registrate_user_form($email)
121
   {
137
   {
122
-    $form['fos_user_registration_form[username]'] = $username;
123
-    $form['fos_user_registration_form[email]'] = $email;
124
-    $form['fos_user_registration_form[plainPassword][first]'] = $pass1;
125
-    // Un des mots de passe est incorrect
126
-    $form['fos_user_registration_form[plainPassword][second]'] = $pass2;
127
-    $form['fos_user_registration_form[token]'] = $token;
128
-    $form['fos_user_registration_form[cgu_accepted]']->tick();
129
-    $this->submit($form);
138
+    $extract = $this->crawler->filter('input[name="muzich_user_registration[_token]"]')
139
+      ->extract(array('value'));
140
+    $csrf = $extract[0];
141
+    $this->crawler = $this->client->request(
142
+      'POST', 
143
+      $this->generateUrl('register'),
144
+      array(
145
+        'muzich_user_registration' => array(
146
+          'email' => $email,
147
+          '_token' => $csrf
148
+        )
149
+      ), 
150
+      array(), 
151
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
152
+    );
130
   }
153
   }
131
   
154
   
132
-  protected function procedure_registration_success($username, $email, $pass1, $pass2, $token)
155
+  protected function procedure_registration_success($email)
133
   {
156
   {
134
     $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
157
     $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
135
     $this->isResponseSuccess();
158
     $this->isResponseSuccess();
136
     $this->assertEquals('anon.', $this->getUser());
159
     $this->assertEquals('anon.', $this->getUser());
137
     
160
     
138
-    $url = $this->generateUrl('register');
139
     // Les mots de passes sont différents
161
     // Les mots de passes sont différents
140
     $this->validate_registrate_user_form(
162
     $this->validate_registrate_user_form(
141
-      $this->selectForm('form[action="'.$url.'"] input[type="submit"]'), 
142
-      $username, 
143
-      $email, 
144
-      $pass1,
145
-      $pass2,
146
-      $token
163
+      $email
147
     );
164
     );
148
     
165
     
149
-    $this->isResponseRedirection();
150
-    $this->followRedirection();
151
-    $this->isResponseSuccess();
152
-
153
     if ('anon.' != ($user = $this->getUser()))
166
     if ('anon.' != ($user = $this->getUser()))
154
     {
167
     {
155
-      // Nous ne sommes pas identifiés
156
-      $this->assertEquals($username, $user->getUsername());
157
-
158
-      // L'utilisateur n'est pas enregistré, il ne doit donc pas être en base
168
+      $this->assertEquals($email, $user->getEmail());
159
       $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
169
       $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
160
-        ->findOneByUsername($username)
170
+        ->findOneByEmail($email)
161
       ;
171
       ;
162
 
172
 
163
       $this->assertTrue(!is_null($db_user));
173
       $this->assertTrue(!is_null($db_user));
168
     }
178
     }
169
   }
179
   }
170
   
180
   
171
-  protected function procedure_registration_failure($username, $email, $pass1, $pass2, $token)
181
+  protected function procedure_registration_failure($email)
172
   {
182
   {
173
     $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
183
     $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
174
     $this->isResponseSuccess();
184
     $this->isResponseSuccess();
175
     $this->assertEquals('anon.', $this->getUser());
185
     $this->assertEquals('anon.', $this->getUser());
176
     
186
     
177
-    $url = $this->generateUrl('register');
178
     // Les mots de passes sont différents
187
     // Les mots de passes sont différents
179
     $this->validate_registrate_user_form(
188
     $this->validate_registrate_user_form(
180
-      $this->selectForm('form[action="'.$url.'"] input[type="submit"]'), 
181
-      $username, 
182
-      $email, 
183
-      $pass1,
184
-      $pass2,
185
-      $token
189
+      $email
186
     );
190
     );
187
     
191
     
188
     $this->isResponseSuccess();
192
     $this->isResponseSuccess();
194
 
198
 
195
       // L'utilisateur n'est pas enregistré, il ne doit donc pas être en base
199
       // L'utilisateur n'est pas enregistré, il ne doit donc pas être en base
196
       $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
200
       $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
197
-        ->findOneByUsername($username)
201
+        ->findOneByEmail($email)
198
       ;
202
       ;
199
 
203
 
200
       $this->assertTrue(is_null($db_user));
204
       $this->assertTrue(is_null($db_user));
267
    * 
271
    * 
268
    * @return string (url generated)
272
    * @return string (url generated)
269
    */
273
    */
270
-  protected function generateUrl($route, $parameters = array(), $absolute = false)
274
+  public function generateUrl($route, $parameters = array(), $absolute = false)
271
   {
275
   {
272
     
276
     
273
     /**
277
     /**
425
   /**
429
   /**
426
    * Contrôle que le CodeStatus de la Response correspond bien a celle d'un Ok
430
    * Contrôle que le CodeStatus de la Response correspond bien a celle d'un Ok
427
    */
431
    */
428
-  protected function isResponseSuccess()
432
+  public function isResponseSuccess()
429
   {
433
   {
430
     $this->assertTrue($this->client->getResponse()->isSuccessful());
434
     $this->assertTrue($this->client->getResponse()->isSuccessful());
431
   }
435
   }
532
       ->findOneBy($params);
536
       ->findOneBy($params);
533
   }
537
   }
534
   
538
   
535
-  protected function goToPage($url)
539
+  public function goToPage($url)
536
   {
540
   {
537
     $this->crawler = $this->client->request('GET', $url);
541
     $this->crawler = $this->client->request('GET', $url);
538
   }
542
   }

+ 6 - 0
src/Muzich/FavoriteBundle/Controller/FavoriteController.php Datei anzeigen

9
 use Muzich\CoreBundle\Propagator\EventElement;
9
 use Muzich\CoreBundle\Propagator\EventElement;
10
 use Muzich\CoreBundle\Entity\User;
10
 use Muzich\CoreBundle\Entity\User;
11
 use Muzich\CoreBundle\lib\Tag as TagLib;
11
 use Muzich\CoreBundle\lib\Tag as TagLib;
12
+use Muzich\CoreBundle\Security\Context as SecurityContext;
12
 
13
 
13
 //use Muzich\CoreBundle\Entity\Group;
14
 //use Muzich\CoreBundle\Entity\Group;
14
 //use Muzich\CoreBundle\Form\Group\GroupForm;
15
 //use Muzich\CoreBundle\Form\Group\GroupForm;
26
    */
27
    */
27
   public function addAction($id, $token)
28
   public function addAction($id, $token)
28
   {
29
   {
30
+    if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES)) !== false)
31
+    {
32
+      return $this->jsonResponseError($non_condition);
33
+    }
34
+    
29
     if (($response = $this->mustBeConnected()))
35
     if (($response = $this->mustBeConnected()))
30
     {
36
     {
31
       return $response;
37
       return $response;

+ 5 - 0
src/Muzich/GroupBundle/Controller/DefaultController.php Datei anzeigen

56
     
56
     
57
 if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_GROUP_ADD)) !== false)
57
 if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_GROUP_ADD)) !== false)
58
     {
58
     {
59
+      if ($request->isXmlHttpRequest())
60
+      {
61
+        return $this->jsonResponseError($non_condition);
62
+      }
63
+      
59
       throw $this->createNotFoundException();
64
       throw $this->createNotFoundException();
60
     }
65
     }
61
     
66
     

+ 16 - 14
src/Muzich/UserBundle/Controller/UserController.php Datei anzeigen

108
   public function accountAction()
108
   public function accountAction()
109
   {
109
   {
110
     $user = $this->getUser();
110
     $user = $this->getUser();
111
-    $form_password = $this->getChangePasswordForm();
111
+    $form_password = $this->getChangePasswordForm($user);
112
     $form_tags_favorites = $this->getTagsFavoritesForm($user);
112
     $form_tags_favorites = $this->getTagsFavoritesForm($user);
113
     $change_email_form = $this->getChangeEmailForm();
113
     $change_email_form = $this->getChangeEmailForm();
114
     
114
     
124
     );
124
     );
125
   }
125
   }
126
   
126
   
127
-  protected function getChangePasswordForm()
127
+  protected function getChangePasswordForm(User $user)
128
   {
128
   {
129
-    return $this->createForm(new PasswordForm(), $this->getUser());
129
+    return $this->createForm(new PasswordForm(), $user);
130
   }
130
   }
131
   
131
   
132
   protected function getAvatarForm()
132
   protected function getAvatarForm()
294
   {
294
   {
295
     $user = $this->getUser();
295
     $user = $this->getUser();
296
     
296
     
297
-    /**
298
-     * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
299
-     * Docrine le voit si on faire une requete directe.
300
-     */
297
+    /** Bug */
301
     if ($this->container->getParameter('env') == 'test')
298
     if ($this->container->getParameter('env') == 'test')
302
     {
299
     {
303
-      $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
304
-        $this->container->get('security.context')->getToken()->getUser()->getId(),
305
-        array()
306
-      )->getSingleResult();
300
+      $user = $this->getUserRefreshed();
307
     }
301
     }
308
     
302
     
309
-    $form = $this->getChangePasswordForm();
303
+    $form = $this->getChangePasswordForm($user);
310
     $form->bind($request);
304
     $form->bind($request);
311
     
305
     
312
     if ($form->isValid())
306
     if ($form->isValid())
507
     }
501
     }
508
     
502
     
509
     // En cas d'échec
503
     // En cas d'échec
510
-    $form_password = $this->getChangePasswordForm();
504
+    $form_password = $this->getChangePasswordForm($user);
511
     $form_tags_favorites = $this->getTagsFavoritesForm($user);
505
     $form_tags_favorites = $this->getTagsFavoritesForm($user);
512
     
506
     
513
     return $this->container->get('templating')->renderResponse(
507
     return $this->container->get('templating')->renderResponse(
519
         'form_tags_favorites_name' => $form_tags_favorites->getName(),
513
         'form_tags_favorites_name' => $form_tags_favorites->getName(),
520
         'favorite_tags_id'         => $this->getTagsFavorites(),
514
         'favorite_tags_id'         => $this->getTagsFavorites(),
521
         'change_email_form'        => $change_email_form->createView(),
515
         'change_email_form'        => $change_email_form->createView(),
522
-        'avatar_form'              => $this->getAvatarForm()->createView()
516
+        'avatar_form'              => $this->getAvatarForm()->createView(),
517
+        'preferences_form'         => $this->getPreferencesForm()->createView()
523
       )
518
       )
524
     );
519
     );
525
   }
520
   }
813
   public function confirmEmailAction(Request $request, $token)
808
   public function confirmEmailAction(Request $request, $token)
814
   {
809
   {
815
     $user = $this->getUser();
810
     $user = $this->getUser();
811
+    
812
+    /** Bug */
813
+    if ($this->container->getParameter('env') == 'test')
814
+    {
815
+      $user = $this->getUserRefreshed();
816
+    }
817
+    
816
     if ($token == hash('sha256', $user->getConfirmationToken().$user->getEmail()))
818
     if ($token == hash('sha256', $user->getConfirmationToken().$user->getEmail()))
817
     {
819
     {
818
       $user->setEmailConfirmed(true);
820
       $user->setEmailConfirmed(true);

+ 0 - 0
src/Muzich/UserBundle/Resources/views/Account/email_not_confirmed.html.twig Datei anzeigen