ContextTestCases.php 13KB

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