123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- <?php
-
- namespace Muzich\CoreBundle\Tests\lib\Security;
-
- use Muzich\CoreBundle\lib\Test\Client;
- use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
- use Muzich\CoreBundle\Security\Context as SecurityContext;
- use Symfony\Component\DomCrawler\Crawler;
-
- class ContextTestCases
- {
-
- protected $client;
- protected $test;
-
- public function __construct(Client $client, WebTestCase $test)
- {
- $this->client = $client;
- $this->test = $test;
- }
-
- private function ajaxResponseSatisfyConditions($response, $success, $condition)
- {
- $response = json_decode($response, true);
-
- if ($response['status'] === 'success' && $success)
- {
- return true;
- }
-
- if ($response['status'] === 'error' && !$success)
- {
- if ($condition && !array_key_exists('error', $response))
- {
- return false;
- }
-
- if ($condition && $response['error'] !== $condition)
- {
- return false;
- }
-
- return true;
- }
-
- return false;
- }
-
- private function responseSatisfyConditions($response, $success, $condition, $user)
- {
- if (($response->getStatusCode() == 200 || $response->getStatusCode() == 302) && $success)
- {
- return true;
- }
-
- if (($response->getStatusCode() != 302 && $response->getStatusCode() != 302) && !$success)
- {
- $security_context = new SecurityContext($user);
- if ($condition && !$security_context->userIsInThisCondition($condition))
- {
- return false;
- }
-
- return true;
- }
- }
-
- public function getAjaxRequestContentResponse($method, $url, $parameters = array())
- {
- $this->test->getClient()->request(
- $method, $url, $parameters, array(),
- array('HTTP_X-Requested-With' => 'XMLHttpRequest')
- );
- return $this->test->getClient()->getResponse()->getContent();
- }
-
- public function addElementResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'POST',
- $this->test->generateUrl('element_add', array('_locale' => 'fr'))
- ),
- $success,
- $condition
- );
- }
-
- public function noteElementResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('ajax_element_add_vote_good', array(
- 'element_id' => 0,
- 'token' => 'notoken'
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function alertCommentResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('ajax_alert_comment', array(
- 'element_id' => 0,
- 'date' => 0,
- 'token' => 'notoken'
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function alertElementResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('ajax_report_element', array(
- 'element_id' => 0,
- 'token' => 'notoken'
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function addTagResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'POST',
- $this->test->generateUrl('ajax_add_tag'),
- array('tag_name' => 'Mon Beau Tag !1245ddregfz')
- ),
- $success,
- $condition
- );
- }
-
- public function proposeElementTagsResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'POST',
- $this->test->generateUrl('ajax_element_propose_tags_proceed',
- array('element_id' => 0, 'token' => 'notoken')
- ),
- array(
- 'element_tag_proposition_0' => array(
- 'tags' => json_encode(array(0, 0))
- )
- )
- ),
- $success,
- $condition
- );
- }
-
- public function addGroupResponseIs($success, $condition)
- {
- $this->test->getClient()->request(
- 'POST',
- $this->test->generateUrl('group_add'),
- array(
- 'group' => array(
- 'name' => 'Un groupe lala45f4rgb1e',
- 'description' => 'description d45fqs4cq6',
- 'tags' => array(),
- '_token' => 'notoken'
- )
- ),
- array(),
- array()
- );
-
- return $this->responseSatisfyConditions(
- $this->test->getClient()->getResponse(),
- $success,
- $condition,
- $this->test->getUser()
- );
- }
-
- public function addCommentResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'POST',
- $this->test->generateUrl('ajax_add_comment', array(
- 'element_id' => 0,
- 'token' => 'notoken'
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function addElementToFavoriteResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('favorite_add', array(
- 'id' => 0,
- 'token' => 'notoken'
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function followUserResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('follow', array(
- 'type' => 'user',
- 'id' => 0,
- 'token' => 'notoken'
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function getFavoritesTagsResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('ajax_get_favorites_tags', array(
- 'favorites' => true
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function playlistAddElementResponseIs($success, $condition)
- {
- $this->playlistAddElement(0, 0);
- return $this->ajaxResponseSatisfyConditions(
- $this->test->getClient()->getResponse()->getContent(),
- $success,
- $condition
- );
- }
-
- public function playlistAddElement($playlist_id, $element_id)
- {
- return $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('playlists_add_element', array(
- 'playlist_id' => $playlist_id,
- 'element_id' => $element_id,
- '_locale' => 'fr'
- ))
- );
- }
-
- public function playlistUpdateOrderResponseIs($success, $condition)
- {
- $this->playlistUpdateOrder(0, array());
- return $this->ajaxResponseSatisfyConditions(
- $this->test->getClient()->getResponse()->getContent(),
- $success,
- $condition
- );
- }
-
- public function playlistUpdateOrder($playlist_id, $elements)
- {
- $elements_ids = array();
- foreach ($elements as $element)
- {
- $elements_ids[] = $element->getId();
- }
-
- return $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('playlist_update_order', array(
- 'playlist_id' => $playlist_id,
- '_locale' => 'fr'
- )),
- array(
- 'elements' => $elements_ids
- )
- );
- }
-
- public function playlistRemoveElementResponseIs($success, $condition)
- {
- $this->playlistRemoveElement(0, 0);
- return $this->ajaxResponseSatisfyConditions(
- $this->test->getClient()->getResponse()->getContent(),
- $success,
- $condition
- );
- }
-
- public function playlistRemoveElement($playlist_id, $element_id)
- {
- return $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('playlist_remove_element', array(
- 'playlist_id' => $playlist_id,
- 'element_id' => $element_id,
- '_locale' => 'fr'
- ))
- );
- }
-
- public function playlistAddElementAndCopy($playlist_id, $element_id)
- {
- return $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('playlists_add_element_and_copy', array(
- 'playlist_id' => $playlist_id,
- 'element_id' => $element_id,
- '_locale' => 'fr'
- ))
- );
- }
-
- public function playlistCreateResponseIs($success, $condition)
- {
- $this->playlistCreate(0, 'my_super_playlist');
- return $this->ajaxResponseSatisfyConditions(
- $this->test->getClient()->getResponse()->getContent(),
- $success,
- $condition
- );
- }
-
- public function playlistCreate($element_id, $playlist_name)
- {
- $this->test->goToPage($this->test->generateUrl('playlists_add_element_prompt', array(
- 'element_id' => $element_id,
- '_locale' => 'fr'
- )));
-
- $response = json_decode($this->test->client->getResponse()->getContent(), true);
- $crawler = new Crawler($response['data']);
-
- $extract = $crawler->filter('input[name="playlist[_token]"]')
- ->extract(array('value'));
- $csrf = $extract[0];
-
- $this->test->crawler = $this->test->client->request(
- 'POST',
- $this->test->generateUrl('playlist_add_element_and_create', array(
- 'element_id' => $element_id,
- '_locale' => 'fr'
- )),
- array(
- 'playlist' => array(
- 'name' => $playlist_name,
- '_token' => $csrf
- )
- ),
- array(),
- array('HTTP_X-Requested-With' => 'XMLHttpRequest')
- );
- }
-
-
- public function playlistCopyResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('playlists_add_element_and_copy', array(
- 'playlist_id' => 0,
- 'element_id' => 0,
- '_locale' => 'fr'
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function playlistDeleteResponseIs($success, $condition)
- {
- $this->playlistDelete(0);
- return $this->responseSatisfyConditions(
- $this->test->getClient()->getResponse(),
- $success,
- $condition,
- $this->test->getUser()
- );
- }
-
- public function playlistDelete($playlist_id)
- {
- $this->test->getClient()->request(
- 'GET',
- $this->test->generateUrl('playlist_delete', array(
- 'playlist_id' => $playlist_id,
- '_locale' => 'fr'
- )),
- array(),
- array(),
- array()
- );
- }
-
- public function playlistUnpickResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('playlist_unpick', array(
- 'playlist_id' => 0,
- '_locale' => 'fr'
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function playlistPickResponseIs($success, $condition)
- {
- return $this->ajaxResponseSatisfyConditions(
- $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('playlist_pick', array(
- 'playlist_id' => 0,
- '_locale' => 'fr'
- ))
- ),
- $success,
- $condition
- );
- }
-
- public function playlistShowResponseIs($success, $condition)
- {
- $this->playlistShow('bux', 0);
-
- return $this->responseSatisfyConditions(
- $this->test->getClient()->getResponse(),
- $success,
- $condition,
- $this->test->getUser()
- );
- }
-
- public function playlistsShow($user_slug)
- {
- $this->test->goToPage($this->test->generateUrl('playlists_user', array(
- 'user_slug' => $user_slug,
- '_locale' => 'fr'
- )));
- }
-
- public function playlistShow($user_slug, $playlist_id)
- {
- $this->test->goToPage($this->test->generateUrl('playlist', array(
- 'user_slug' => $user_slug,
- 'playlist_id' => $playlist_id,
- '_locale' => 'fr'
- )));
- }
-
- public function playlistAutoplayResponseIs($success, $condition)
- {
- $this->playlistAutoplay(0);
- return $this->ajaxResponseSatisfyConditions(
- $this->test->getClient()->getResponse()->getContent(),
- $success,
- $condition
- );
- }
-
- public function playlistAutoplay($playlist_id)
- {
- return $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('playlist_datas_for_autoplay', array(
- 'playlist_id' => $playlist_id,
- '_locale' => 'fr'
- ))
- );
- }
-
- public function playlistPromptResponseIs($success, $condition)
- {
- $this->playlistPrompt(0);
- return $this->ajaxResponseSatisfyConditions(
- $this->test->getClient()->getResponse()->getContent(),
- $success,
- $condition
- );
- }
-
- public function playlistPrompt($element_id)
- {
- return $this->getAjaxRequestContentResponse(
- 'GET',
- $this->test->generateUrl('playlists_add_element_prompt', array(
- 'element_id' => $element_id,
- '_locale' => 'fr'
- ))
- );
- }
-
- }
|