瀏覽代碼

Evolution #700: Test nécessaires

Bastien Sevajol 11 年之前
父節點
當前提交
e6d3a1041c

+ 3 - 2
src/Muzich/CommentBundle/Controller/CommentController.php 查看文件

@@ -6,6 +6,7 @@ use Muzich\CoreBundle\lib\Controller;
6 6
 use Muzich\CoreBundle\Managers\CommentsManager;
7 7
 use Muzich\CoreBundle\Propagator\EventElement;
8 8
 use Muzich\CoreBundle\Security\Context as SecurityContext;
9
+use Symfony\Component\HttpFoundation\Request;
9 10
 
10 11
 class CommentController extends Controller
11 12
 {
@@ -19,9 +20,9 @@ class CommentController extends Controller
19 20
    */
20 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 28
     if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')

+ 2 - 2
src/Muzich/CoreBundle/Controller/CoreController.php 查看文件

@@ -109,9 +109,9 @@ class CoreController extends Controller
109 109
    */
110 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 117
     $user = $this->getUser();

+ 5 - 0
src/Muzich/CoreBundle/Controller/ElementController.php 查看文件

@@ -607,6 +607,11 @@ class ElementController extends Controller
607 607
   
608 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 615
     if (($response = $this->mustBeConnected(true)))
611 616
     {
612 617
       return $response;

+ 0 - 0
src/Muzich/CoreBundle/Form/User/PasswordForm.php 查看文件


+ 12 - 4
src/Muzich/CoreBundle/Security/Context.php 查看文件

@@ -58,8 +58,7 @@ class Context
58 58
         self::CONDITION_USER_NOT_CONNECTED
59 59
       ),
60 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 63
       self::ACTION_USER_FOLLOW => array(
65 64
         self::CONDITION_USER_NOT_CONNECTED
@@ -124,8 +123,7 @@ class Context
124 123
     {
125 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 128
           return $affected_condition;
131 129
         }
@@ -135,6 +133,16 @@ class Context
135 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 146
   protected function isUserNotConnected()
139 147
   {
140 148
     if ($this->anonymous)

+ 103 - 0
src/Muzich/CoreBundle/Tests/Controller/AnonymousTest.php 查看文件

@@ -0,0 +1,103 @@
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 查看文件

@@ -0,0 +1,93 @@
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 查看文件

@@ -1,77 +0,0 @@
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
-}

文件差異過大導致無法顯示
+ 510 - 521
src/Muzich/CoreBundle/Tests/Controller/UserControllerTest.php


+ 3 - 1
src/Muzich/CoreBundle/Tests/Security/ContextTest.php 查看文件

@@ -10,7 +10,9 @@ class ContextTest extends \PHPUnit_Framework_TestCase
10 10
   
11 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 17
     $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD));
16 18
     $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_NOTE));

+ 83 - 0
src/Muzich/CoreBundle/Tests/lib/Security/Context.php 查看文件

@@ -0,0 +1,83 @@
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 查看文件

@@ -0,0 +1,227 @@
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 查看文件

@@ -230,6 +230,11 @@ class Controller extends BaseController
230 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 239
    * Retourne un tabeau avec les tags connus.
235 240
    * TODO: Voir pour que cette info soit stocké (par exemple) dans un champs
@@ -580,7 +585,7 @@ class Controller extends BaseController
580 585
   
581 586
   protected function sendEmailconfirmationEmail($set_send_time = true)
582 587
   {
583
-    $user = $this->getUser();
588
+    $user = $this->getUserRefreshed();
584 589
     
585 590
     $tokenGenerator = $this->container->get('fos_user.util.token_generator');
586 591
     $user->setConfirmationToken($tokenGenerator->generateToken());

+ 44 - 40
src/Muzich/CoreBundle/lib/FunctionalTest.php 查看文件

@@ -24,6 +24,16 @@ class FunctionalTest extends WebTestCase
24 24
    */
25 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 37
   protected function outputDebug($content = null)
28 38
   {
29 39
     $time = time();
@@ -48,11 +58,17 @@ class FunctionalTest extends WebTestCase
48 58
    * 
49 59
    * @return \Muzich\CoreBundle\Entity\User 
50 60
    */
51
-  protected function getUser($username = null)
61
+  public function getUser($username = null)
52 62
   {
53 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 73
     else
58 74
     {
@@ -117,47 +133,41 @@ class FunctionalTest extends WebTestCase
117 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 157
     $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
135 158
     $this->isResponseSuccess();
136 159
     $this->assertEquals('anon.', $this->getUser());
137 160
     
138
-    $url = $this->generateUrl('register');
139 161
     // Les mots de passes sont différents
140 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 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 169
       $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
160
-        ->findOneByUsername($username)
170
+        ->findOneByEmail($email)
161 171
       ;
162 172
 
163 173
       $this->assertTrue(!is_null($db_user));
@@ -168,21 +178,15 @@ class FunctionalTest extends WebTestCase
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 183
     $this->crawler = $this->client->request('GET', $this->generateUrl('index'));
174 184
     $this->isResponseSuccess();
175 185
     $this->assertEquals('anon.', $this->getUser());
176 186
     
177
-    $url = $this->generateUrl('register');
178 187
     // Les mots de passes sont différents
179 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 192
     $this->isResponseSuccess();
@@ -194,7 +198,7 @@ class FunctionalTest extends WebTestCase
194 198
 
195 199
       // L'utilisateur n'est pas enregistré, il ne doit donc pas être en base
196 200
       $db_user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
197
-        ->findOneByUsername($username)
201
+        ->findOneByEmail($email)
198 202
       ;
199 203
 
200 204
       $this->assertTrue(is_null($db_user));
@@ -267,7 +271,7 @@ class FunctionalTest extends WebTestCase
267 271
    * 
268 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,7 +429,7 @@ class FunctionalTest extends WebTestCase
425 429
   /**
426 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 434
     $this->assertTrue($this->client->getResponse()->isSuccessful());
431 435
   }
@@ -532,7 +536,7 @@ class FunctionalTest extends WebTestCase
532 536
       ->findOneBy($params);
533 537
   }
534 538
   
535
-  protected function goToPage($url)
539
+  public function goToPage($url)
536 540
   {
537 541
     $this->crawler = $this->client->request('GET', $url);
538 542
   }

+ 6 - 0
src/Muzich/FavoriteBundle/Controller/FavoriteController.php 查看文件

@@ -9,6 +9,7 @@ use Muzich\CoreBundle\Searcher\ElementSearcher;
9 9
 use Muzich\CoreBundle\Propagator\EventElement;
10 10
 use Muzich\CoreBundle\Entity\User;
11 11
 use Muzich\CoreBundle\lib\Tag as TagLib;
12
+use Muzich\CoreBundle\Security\Context as SecurityContext;
12 13
 
13 14
 //use Muzich\CoreBundle\Entity\Group;
14 15
 //use Muzich\CoreBundle\Form\Group\GroupForm;
@@ -26,6 +27,11 @@ class FavoriteController extends Controller
26 27
    */
27 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 35
     if (($response = $this->mustBeConnected()))
30 36
     {
31 37
       return $response;

+ 5 - 0
src/Muzich/GroupBundle/Controller/DefaultController.php 查看文件

@@ -56,6 +56,11 @@ class DefaultController extends Controller
56 56
     
57 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 64
       throw $this->createNotFoundException();
60 65
     }
61 66
     

+ 16 - 14
src/Muzich/UserBundle/Controller/UserController.php 查看文件

@@ -108,7 +108,7 @@ class UserController extends Controller
108 108
   public function accountAction()
109 109
   {
110 110
     $user = $this->getUser();
111
-    $form_password = $this->getChangePasswordForm();
111
+    $form_password = $this->getChangePasswordForm($user);
112 112
     $form_tags_favorites = $this->getTagsFavoritesForm($user);
113 113
     $change_email_form = $this->getChangeEmailForm();
114 114
     
@@ -124,9 +124,9 @@ class UserController extends Controller
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 132
   protected function getAvatarForm()
@@ -294,19 +294,13 @@ class UserController extends Controller
294 294
   {
295 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 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 304
     $form->bind($request);
311 305
     
312 306
     if ($form->isValid())
@@ -507,7 +501,7 @@ class UserController extends Controller
507 501
     }
508 502
     
509 503
     // En cas d'échec
510
-    $form_password = $this->getChangePasswordForm();
504
+    $form_password = $this->getChangePasswordForm($user);
511 505
     $form_tags_favorites = $this->getTagsFavoritesForm($user);
512 506
     
513 507
     return $this->container->get('templating')->renderResponse(
@@ -519,7 +513,8 @@ class UserController extends Controller
519 513
         'form_tags_favorites_name' => $form_tags_favorites->getName(),
520 514
         'favorite_tags_id'         => $this->getTagsFavorites(),
521 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,6 +808,13 @@ class UserController extends Controller
813 808
   public function confirmEmailAction(Request $request, $token)
814 809
   {
815 810
     $user = $this->getUser();
811
+    
812
+    /** Bug */
813
+    if ($this->container->getParameter('env') == 'test')
814
+    {
815
+      $user = $this->getUserRefreshed();
816
+    }
817
+    
816 818
     if ($token == hash('sha256', $user->getConfirmationToken().$user->getEmail()))
817 819
     {
818 820
       $user->setEmailConfirmed(true);

+ 0 - 0
src/Muzich/UserBundle/Resources/views/Account/email_not_confirmed.html.twig 查看文件