ContextTestCases.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. use Symfony\Component\DomCrawler\Crawler;
  7. class ContextTestCases
  8. {
  9. protected $client;
  10. protected $test;
  11. public function __construct(Client $client, WebTestCase $test)
  12. {
  13. $this->client = $client;
  14. $this->test = $test;
  15. }
  16. private function ajaxResponseSatisfyConditions($response, $success, $condition)
  17. {
  18. $response = json_decode($response, true);
  19. if ($response['status'] === 'success' && $success)
  20. {
  21. return true;
  22. }
  23. if ($response['status'] === 'error' && !$success)
  24. {
  25. if ($condition && !array_key_exists('error', $response))
  26. {
  27. return false;
  28. }
  29. if ($condition && $response['error'] !== $condition)
  30. {
  31. return false;
  32. }
  33. return true;
  34. }
  35. return false;
  36. }
  37. private function responseSatisfyConditions($response, $success, $condition, $user)
  38. {
  39. if (($response->getStatusCode() == 200 || $response->getStatusCode() == 302) && $success)
  40. {
  41. return true;
  42. }
  43. if (($response->getStatusCode() != 302 && $response->getStatusCode() != 302) && !$success)
  44. {
  45. $security_context = new SecurityContext($user);
  46. if ($condition && !$security_context->userIsInThisCondition($condition))
  47. {
  48. return false;
  49. }
  50. return true;
  51. }
  52. }
  53. public function getAjaxRequestContentResponse($method, $url, $parameters = array())
  54. {
  55. $this->test->getClient()->request(
  56. $method, $url, $parameters, array(),
  57. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  58. );
  59. return $this->test->getClient()->getResponse()->getContent();
  60. }
  61. public function addElementResponseIs($success, $condition)
  62. {
  63. return $this->ajaxResponseSatisfyConditions(
  64. $this->getAjaxRequestContentResponse(
  65. 'POST',
  66. $this->test->generateUrl('element_add', array('_locale' => 'fr'))
  67. ),
  68. $success,
  69. $condition
  70. );
  71. }
  72. public function noteElementResponseIs($success, $condition)
  73. {
  74. return $this->ajaxResponseSatisfyConditions(
  75. $this->getAjaxRequestContentResponse(
  76. 'GET',
  77. $this->test->generateUrl('ajax_element_add_vote_good', array(
  78. 'element_id' => 0,
  79. 'token' => 'notoken'
  80. ))
  81. ),
  82. $success,
  83. $condition
  84. );
  85. }
  86. public function alertCommentResponseIs($success, $condition)
  87. {
  88. return $this->ajaxResponseSatisfyConditions(
  89. $this->getAjaxRequestContentResponse(
  90. 'GET',
  91. $this->test->generateUrl('ajax_alert_comment', array(
  92. 'element_id' => 0,
  93. 'date' => 0,
  94. 'token' => 'notoken'
  95. ))
  96. ),
  97. $success,
  98. $condition
  99. );
  100. }
  101. public function alertElementResponseIs($success, $condition)
  102. {
  103. return $this->ajaxResponseSatisfyConditions(
  104. $this->getAjaxRequestContentResponse(
  105. 'GET',
  106. $this->test->generateUrl('ajax_report_element', array(
  107. 'element_id' => 0,
  108. 'token' => 'notoken'
  109. ))
  110. ),
  111. $success,
  112. $condition
  113. );
  114. }
  115. public function addTagResponseIs($success, $condition)
  116. {
  117. return $this->ajaxResponseSatisfyConditions(
  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. public function proposeElementTagsResponseIs($success, $condition)
  128. {
  129. return $this->ajaxResponseSatisfyConditions(
  130. $this->getAjaxRequestContentResponse(
  131. 'POST',
  132. $this->test->generateUrl('ajax_element_propose_tags_proceed',
  133. array('element_id' => 0, 'token' => 'notoken')
  134. ),
  135. array(
  136. 'element_tag_proposition_0' => array(
  137. 'tags' => json_encode(array(0, 0))
  138. )
  139. )
  140. ),
  141. $success,
  142. $condition
  143. );
  144. }
  145. public function addGroupResponseIs($success, $condition)
  146. {
  147. $this->test->getClient()->request(
  148. 'POST',
  149. $this->test->generateUrl('group_add'),
  150. array(
  151. 'group' => array(
  152. 'name' => 'Un groupe lala45f4rgb1e',
  153. 'description' => 'description d45fqs4cq6',
  154. 'tags' => array(),
  155. '_token' => 'notoken'
  156. )
  157. ),
  158. array(),
  159. array()
  160. );
  161. return $this->responseSatisfyConditions(
  162. $this->test->getClient()->getResponse(),
  163. $success,
  164. $condition,
  165. $this->test->getUser()
  166. );
  167. }
  168. public function addCommentResponseIs($success, $condition)
  169. {
  170. return $this->ajaxResponseSatisfyConditions(
  171. $this->getAjaxRequestContentResponse(
  172. 'POST',
  173. $this->test->generateUrl('ajax_add_comment', array(
  174. 'element_id' => 0,
  175. 'token' => 'notoken'
  176. ))
  177. ),
  178. $success,
  179. $condition
  180. );
  181. }
  182. public function addElementToFavoriteResponseIs($success, $condition)
  183. {
  184. return $this->ajaxResponseSatisfyConditions(
  185. $this->getAjaxRequestContentResponse(
  186. 'GET',
  187. $this->test->generateUrl('favorite_add', array(
  188. 'id' => 0,
  189. 'token' => 'notoken'
  190. ))
  191. ),
  192. $success,
  193. $condition
  194. );
  195. }
  196. public function followUserResponseIs($success, $condition)
  197. {
  198. return $this->ajaxResponseSatisfyConditions(
  199. $this->getAjaxRequestContentResponse(
  200. 'GET',
  201. $this->test->generateUrl('follow', array(
  202. 'type' => 'user',
  203. 'id' => 0,
  204. 'token' => 'notoken'
  205. ))
  206. ),
  207. $success,
  208. $condition
  209. );
  210. }
  211. public function getFavoritesTagsResponseIs($success, $condition)
  212. {
  213. return $this->ajaxResponseSatisfyConditions(
  214. $this->getAjaxRequestContentResponse(
  215. 'GET',
  216. $this->test->generateUrl('ajax_get_favorites_tags', array(
  217. 'favorites' => true
  218. ))
  219. ),
  220. $success,
  221. $condition
  222. );
  223. }
  224. public function playlistAddElementResponseIs($success, $condition)
  225. {
  226. return $this->ajaxResponseSatisfyConditions(
  227. $this->getAjaxRequestContentResponse(
  228. 'GET',
  229. $this->test->generateUrl('playlists_add_element', array(
  230. 'playlist_id' => 0,
  231. 'element_id' => 0,
  232. '_locale' => 'fr'
  233. ))
  234. ),
  235. $success,
  236. $condition
  237. );
  238. }
  239. public function playlistUpdateOrderResponseIs($success, $condition)
  240. {
  241. return $this->ajaxResponseSatisfyConditions(
  242. $this->getAjaxRequestContentResponse(
  243. 'GET',
  244. $this->test->generateUrl('playlist_update_order', array(
  245. 'playlist_id' => 0,
  246. '_locale' => 'fr'
  247. ))
  248. ),
  249. $success,
  250. $condition
  251. );
  252. }
  253. public function playlistRemoveElementResponseIs($success, $condition)
  254. {
  255. return $this->ajaxResponseSatisfyConditions(
  256. $this->getAjaxRequestContentResponse(
  257. 'GET',
  258. $this->test->generateUrl('playlist_remove_element', array(
  259. 'playlist_id' => 0,
  260. 'element_id' => 0,
  261. '_locale' => 'fr'
  262. ))
  263. ),
  264. $success,
  265. $condition
  266. );
  267. }
  268. public function playlistCreateResponseIs($success, $condition)
  269. {
  270. $this->playlistCreate(0, 'my_super_playlist');
  271. return $this->ajaxResponseSatisfyConditions(
  272. $this->test->getClient()->getResponse()->getContent(),
  273. $success,
  274. $condition
  275. );
  276. }
  277. public function playlistCreate($element_id, $playlist_name)
  278. {
  279. $this->test->goToPage($this->test->generateUrl('playlists_add_element_prompt', array(
  280. 'element_id' => $element_id,
  281. '_locale' => 'fr'
  282. )));
  283. $response = json_decode($this->test->client->getResponse()->getContent(), true);
  284. $crawler = new Crawler($response['data']);
  285. $extract = $crawler->filter('input[name="playlist[_token]"]')
  286. ->extract(array('value'));
  287. $csrf = $extract[0];
  288. $this->test->crawler = $this->test->client->request(
  289. 'POST',
  290. $this->test->generateUrl('playlist_add_element_and_create', array(
  291. 'element_id' => $element_id,
  292. '_locale' => 'fr'
  293. )),
  294. array(
  295. 'playlist' => array(
  296. 'name' => $playlist_name,
  297. '_token' => $csrf
  298. )
  299. ),
  300. array(),
  301. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  302. );
  303. }
  304. public function playlistCopyResponseIs($success, $condition)
  305. {
  306. return $this->ajaxResponseSatisfyConditions(
  307. $this->getAjaxRequestContentResponse(
  308. 'GET',
  309. $this->test->generateUrl('playlists_add_element_and_copy', array(
  310. 'playlist_id' => 0,
  311. 'element_id' => 0,
  312. '_locale' => 'fr'
  313. ))
  314. ),
  315. $success,
  316. $condition
  317. );
  318. }
  319. public function playlistDeleteResponseIs($success, $condition)
  320. {
  321. $this->test->getClient()->request(
  322. 'GET',
  323. $this->test->generateUrl('playlist_delete', array(
  324. 'playlist_id' => 0,
  325. 'element_id' => 0,
  326. '_locale' => 'fr'
  327. )),
  328. array(),
  329. array(),
  330. array()
  331. );
  332. return $this->responseSatisfyConditions(
  333. $this->test->getClient()->getResponse(),
  334. $success,
  335. $condition,
  336. $this->test->getUser()
  337. );
  338. }
  339. public function playlistUnpickResponseIs($success, $condition)
  340. {
  341. return $this->ajaxResponseSatisfyConditions(
  342. $this->getAjaxRequestContentResponse(
  343. 'GET',
  344. $this->test->generateUrl('playlist_unpick', array(
  345. 'playlist_id' => 0,
  346. '_locale' => 'fr'
  347. ))
  348. ),
  349. $success,
  350. $condition
  351. );
  352. }
  353. public function playlistPickResponseIs($success, $condition)
  354. {
  355. return $this->ajaxResponseSatisfyConditions(
  356. $this->getAjaxRequestContentResponse(
  357. 'GET',
  358. $this->test->generateUrl('playlist_pick', array(
  359. 'playlist_id' => 0,
  360. '_locale' => 'fr'
  361. ))
  362. ),
  363. $success,
  364. $condition
  365. );
  366. }
  367. public function playlistShowResponseIs($success, $condition)
  368. {
  369. $this->playlistShow('bux', 0);
  370. return $this->responseSatisfyConditions(
  371. $this->test->getClient()->getResponse(),
  372. $success,
  373. $condition,
  374. $this->test->getUser()
  375. );
  376. }
  377. public function playlistsShow($user_slug)
  378. {
  379. $this->test->goToPage($this->test->generateUrl('playlists_user', array(
  380. 'user_slug' => $user_slug,
  381. '_locale' => 'fr'
  382. )));
  383. }
  384. public function playlistShow($user_slug, $playlist_id)
  385. {
  386. $this->test->goToPage($this->test->generateUrl('playlist', array(
  387. 'user_slug' => $user_slug,
  388. 'playlist_id' => $playlist_id,
  389. '_locale' => 'fr'
  390. )));
  391. }
  392. public function playlistAutoplayResponseIs($success, $condition)
  393. {
  394. $this->playlistAutoplay(0);
  395. return $this->ajaxResponseSatisfyConditions(
  396. $this->test->getClient()->getResponse()->getContent(),
  397. $success,
  398. $condition
  399. );
  400. }
  401. public function playlistAutoplay($playlist_id)
  402. {
  403. return $this->getAjaxRequestContentResponse(
  404. 'GET',
  405. $this->test->generateUrl('playlist_datas_for_autoplay', array(
  406. 'playlist_id' => $playlist_id,
  407. '_locale' => 'fr'
  408. ))
  409. );
  410. }
  411. public function playlistPromptResponseIs($success, $condition)
  412. {
  413. $this->playlistPrompt(0);
  414. return $this->ajaxResponseSatisfyConditions(
  415. $this->test->getClient()->getResponse()->getContent(),
  416. $success,
  417. $condition
  418. );
  419. }
  420. public function playlistPrompt($element_id)
  421. {
  422. return $this->getAjaxRequestContentResponse(
  423. 'GET',
  424. $this->test->generateUrl('playlists_add_element_prompt', array(
  425. 'element_id' => $element_id,
  426. '_locale' => 'fr'
  427. ))
  428. );
  429. }
  430. }