ContextTestCases.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. $this->playlistAddElement(0, 0);
  227. return $this->ajaxResponseSatisfyConditions(
  228. $this->test->getClient()->getResponse()->getContent(),
  229. $success,
  230. $condition
  231. );
  232. }
  233. public function playlistAddElement($playlist_id, $element_id)
  234. {
  235. return $this->getAjaxRequestContentResponse(
  236. 'GET',
  237. $this->test->generateUrl('playlists_add_element', array(
  238. 'playlist_id' => $playlist_id,
  239. 'element_id' => $element_id,
  240. '_locale' => 'fr',
  241. 'token' => $this->test->getToken()
  242. ))
  243. );
  244. }
  245. public function playlistUpdateOrderResponseIs($success, $condition)
  246. {
  247. $this->playlistUpdateOrder(0, array());
  248. return $this->ajaxResponseSatisfyConditions(
  249. $this->test->getClient()->getResponse()->getContent(),
  250. $success,
  251. $condition
  252. );
  253. }
  254. public function playlistUpdateOrder($playlist_id, $elements)
  255. {
  256. $elements_ids = array();
  257. foreach ($elements as $element)
  258. {
  259. $elements_ids[] = $element->getId();
  260. }
  261. return $this->getAjaxRequestContentResponse(
  262. 'GET',
  263. $this->test->generateUrl('playlist_update_order', array(
  264. 'playlist_id' => $playlist_id,
  265. '_locale' => 'fr',
  266. 'token' => $this->test->getToken()
  267. )),
  268. array(
  269. 'elements' => $elements_ids
  270. )
  271. );
  272. }
  273. public function playlistRemoveElementResponseIs($success, $condition)
  274. {
  275. $this->playlistRemoveElement(0, 0);
  276. return $this->ajaxResponseSatisfyConditions(
  277. $this->test->getClient()->getResponse()->getContent(),
  278. $success,
  279. $condition
  280. );
  281. }
  282. public function playlistRemoveElement($playlist_id, $index)
  283. {
  284. return $this->getAjaxRequestContentResponse(
  285. 'GET',
  286. $this->test->generateUrl('playlist_remove_element', array(
  287. 'playlist_id' => $playlist_id,
  288. 'index' => $index,
  289. '_locale' => 'fr',
  290. 'token' => $this->test->getToken()
  291. ))
  292. );
  293. }
  294. public function playlistAddElementAndCopy($playlist_id, $element_id)
  295. {
  296. return $this->getAjaxRequestContentResponse(
  297. 'GET',
  298. $this->test->generateUrl('playlists_add_element_and_copy', array(
  299. 'playlist_id' => $playlist_id,
  300. 'element_id' => $element_id,
  301. '_locale' => 'fr',
  302. 'token' => $this->test->getToken()
  303. ))
  304. );
  305. }
  306. public function playlistCreateResponseIs($success, $condition)
  307. {
  308. $this->playlistCreate(0, 'my_super_playlist');
  309. return $this->ajaxResponseSatisfyConditions(
  310. $this->test->getClient()->getResponse()->getContent(),
  311. $success,
  312. $condition
  313. );
  314. }
  315. public function playlistCreate($element_id, $playlist_name)
  316. {
  317. $this->test->goToPage($this->test->generateUrl('playlists_add_element_prompt', array(
  318. 'element_id' => $element_id,
  319. '_locale' => 'fr'
  320. )));
  321. $response = json_decode($this->test->client->getResponse()->getContent(), true);
  322. $crawler = new Crawler($response['data']);
  323. $extract = $crawler->filter('input[name="playlist[_token]"]')
  324. ->extract(array('value'));
  325. $csrf = $extract[0];
  326. $this->test->crawler = $this->test->client->request(
  327. 'POST',
  328. $this->test->generateUrl('playlist_add_element_and_create', array(
  329. 'element_id' => $element_id,
  330. '_locale' => 'fr'
  331. )),
  332. array(
  333. 'playlist' => array(
  334. 'name' => $playlist_name,
  335. '_token' => $csrf
  336. )
  337. ),
  338. array(),
  339. array('HTTP_X-Requested-With' => 'XMLHttpRequest')
  340. );
  341. }
  342. public function playlistCopyResponseIs($success, $condition)
  343. {
  344. return $this->ajaxResponseSatisfyConditions(
  345. $this->getAjaxRequestContentResponse(
  346. 'GET',
  347. $this->test->generateUrl('playlists_add_element_and_copy', array(
  348. 'playlist_id' => 0,
  349. 'element_id' => 0,
  350. '_locale' => 'fr',
  351. 'token' => $this->test->getToken()
  352. ))
  353. ),
  354. $success,
  355. $condition
  356. );
  357. }
  358. public function playlistDeleteResponseIs($success, $condition)
  359. {
  360. $this->playlistDelete(0);
  361. return $this->responseSatisfyConditions(
  362. $this->test->getClient()->getResponse(),
  363. $success,
  364. $condition,
  365. $this->test->getUser()
  366. );
  367. }
  368. public function playlistDelete($playlist_id)
  369. {
  370. $this->test->getClient()->request(
  371. 'GET',
  372. $this->test->generateUrl('playlist_delete', array(
  373. 'playlist_id' => $playlist_id,
  374. '_locale' => 'fr',
  375. 'token' => $this->test->getToken()
  376. )),
  377. array(),
  378. array(),
  379. array()
  380. );
  381. }
  382. public function playlistUnpickResponseIs($success, $condition)
  383. {
  384. $this->playlistUnPick(0);
  385. return $this->responseSatisfyConditions(
  386. $this->test->getClient()->getResponse(),
  387. $success,
  388. $condition,
  389. $this->test->getUser()
  390. );
  391. }
  392. public function playlistUnPick($playlist_id)
  393. {
  394. $this->test->goToPage($this->test->generateUrl('playlist_unpick', array(
  395. 'playlist_id' => $playlist_id,
  396. '_locale' => 'fr',
  397. 'token' => $this->test->getToken()
  398. )));
  399. }
  400. public function playlistPickResponseIs($success, $condition)
  401. {
  402. $this->playlistPick(0);
  403. return $this->ajaxResponseSatisfyConditions(
  404. $this->test->getClient()->getResponse()->getContent(),
  405. $success,
  406. $condition
  407. );
  408. }
  409. public function playlistPick($playlist_id)
  410. {
  411. return $this->getAjaxRequestContentResponse(
  412. 'GET',
  413. $this->test->generateUrl('playlist_pick', array(
  414. 'playlist_id' => $playlist_id,
  415. '_locale' => 'fr',
  416. 'token' => $this->test->getToken()
  417. ))
  418. );
  419. }
  420. public function playlistShowResponseIs($success, $condition)
  421. {
  422. $this->playlistShow('bux', 0);
  423. return $this->responseSatisfyConditions(
  424. $this->test->getClient()->getResponse(),
  425. $success,
  426. $condition,
  427. $this->test->getUser()
  428. );
  429. }
  430. public function playlistsShow($user_slug)
  431. {
  432. $this->test->goToPage($this->test->generateUrl('playlists_user', array(
  433. 'user_slug' => $user_slug,
  434. '_locale' => 'fr'
  435. )));
  436. }
  437. public function playlistShow($user_slug, $playlist_id)
  438. {
  439. $this->test->goToPage($this->test->generateUrl('playlist', array(
  440. 'user_slug' => $user_slug,
  441. 'playlist_id' => $playlist_id,
  442. '_locale' => 'fr'
  443. )));
  444. }
  445. public function playlistAutoplayResponseIs($success, $condition)
  446. {
  447. $this->playlistAutoplay(0);
  448. return $this->ajaxResponseSatisfyConditions(
  449. $this->test->getClient()->getResponse()->getContent(),
  450. $success,
  451. $condition
  452. );
  453. }
  454. public function playlistAutoplay($playlist_id)
  455. {
  456. return $this->getAjaxRequestContentResponse(
  457. 'GET',
  458. $this->test->generateUrl('playlist_datas_for_autoplay', array(
  459. 'playlist_id' => $playlist_id,
  460. '_locale' => 'fr'
  461. ))
  462. );
  463. }
  464. public function playlistPromptResponseIs($success, $condition)
  465. {
  466. $this->playlistPrompt(0);
  467. return $this->ajaxResponseSatisfyConditions(
  468. $this->test->getClient()->getResponse()->getContent(),
  469. $success,
  470. $condition
  471. );
  472. }
  473. public function playlistPrompt($element_id)
  474. {
  475. return $this->getAjaxRequestContentResponse(
  476. 'GET',
  477. $this->test->generateUrl('playlists_add_element_prompt', array(
  478. 'element_id' => $element_id,
  479. '_locale' => 'fr'
  480. ))
  481. );
  482. }
  483. }