ContextTestCases.php 5.7KB

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