WebTestCaseBase.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace WebserviceBundle\tests;
  3. use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
  4. use Symfony\Component\BrowserKit\Cookie;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  7. use Symfony\Bundle\FrameworkBundle\Client;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use ReflectionClass;
  10. use Symfony\Component\Security\Core\User\User;
  11. class WebTestCaseBase extends WebTestCase
  12. {
  13. /**
  14. * @var Client Cliente web.
  15. */
  16. private $client;
  17. /**
  18. * @var bool Me dice si estamos en el modulo base.
  19. */
  20. protected $isModuleBase;
  21. /**
  22. * @var int Contiene el id de usuario a simular
  23. */
  24. protected $idUser;
  25. /**
  26. * Funcion que crea un nuevo cliente para realizar consultas. Para cada consulta web necesito crear un nuevo cliente.
  27. * @param bool $new Me dice si tengo que crear un nuevo cliente.
  28. * @return Client Retorna el cliente.
  29. */
  30. protected function getClient($new = false)
  31. {
  32. if (!$this->client || $new) {
  33. // por defecto es environment=test y debug = true
  34. $this->client = static::createClient();
  35. }
  36. return $this->client;
  37. }
  38. /**
  39. * Funcion que busca un servicio dentro del container.
  40. * @param mixed $service Contiene el nombre del servicio.
  41. * @return mixed Retorna el servicio.
  42. */
  43. protected function get($service)
  44. {
  45. return $this->getClient()->getContainer()->get($service);
  46. }
  47. /**
  48. * @return mixed Retorna el manager de doctrine.
  49. */
  50. protected function getDoctrineManager()
  51. {
  52. return $this->get('doctrine')->getManager();
  53. }
  54. /**
  55. * @return bool Retorna TRUE si estoy en el modulo base.
  56. */
  57. public function isModuleBase()
  58. {
  59. if ($this->isModuleBase == null) {
  60. $em = $this->getDoctrineManager();
  61. if ($em == null) {
  62. $this->isModuleBase = false;
  63. } else {
  64. $this->isModuleBase = array_key_exists('BaseTenancyBundle', $this->getClient()->getContainer()->getParameter('kernel.bundles'));
  65. }
  66. }
  67. return $this->isModuleBase;
  68. }
  69. /**
  70. * Se creo este metodo para crear objeto desde un string para no tener conflictos en otros modulos.
  71. * @param string $repository Contiene el objeto del repositorio.
  72. * @return mixed Crea una instancia de una objeto.
  73. */
  74. public function getObject($repository)
  75. {
  76. $obj = $this->getDoctrineManager()->getMetadataFactory()->getMetadataFor($repository)->getName();
  77. $rc = new ReflectionClass($obj);
  78. return $rc->newInstance();
  79. }
  80. /**
  81. * Funcion que crea el servicio de tenencias. Por defecto crea la tenencia base.
  82. * @param int $current Contiene la tenencia actual.
  83. * @param array $tenancies Contiene las tenencias. Ej. array(array('id'=>1,'name'=>'base'))
  84. */
  85. protected function fakeTenancyService($current = 1, $tenancies = array())
  86. {
  87. $service_tenancy = $this->get('base_tenancy.tenancy_service');
  88. if (count($tenancies) == 0) {
  89. $tenancies [] = array('id' => 1, 'name' => 'Tenencia Base');
  90. }
  91. if ($this->isModuleBase()) {
  92. // tengo que cargar las tenencias que no existan en la base de datos.
  93. $em = $this->getDoctrineManager();
  94. $user = $em->getRepository("BaseUserBundle:User")->findOneBy(array('id' => $this->idUser));
  95. if (!$user) {
  96. // el usuario no existe, entonces lo creo
  97. $user = $this->getObject("BaseUserBundle:User");
  98. $user->setUsername("adminpruebas");
  99. $user->setPassword("adminpass");
  100. $user->setEnabled(true);
  101. $user->setEmail("pepe@pepe.com");
  102. $em->persist($user);
  103. $em->flush();
  104. }
  105. $arrayTenancies = new ArrayCollection();
  106. foreach ($tenancies as $tenancyData) {
  107. $tenancy = $em->getRepository("BaseTenancyBundle:Tenancy")->findOneBy(array('id' => $tenancyData['id']));
  108. if (!$tenancy) {
  109. // la tenencia no existe, entonces la creo
  110. $tenancy = $this->getObject("BaseTenancyBundle:Tenancy");
  111. $tenancy->setId($tenancyData['id']);
  112. $tenancy->setName($tenancyData['name']);
  113. $tenancy->setEnabled(true);
  114. $em->persist($tenancy);
  115. $em->flush();
  116. }
  117. $ten_user = $em->createQueryBuilder()
  118. ->select('t')
  119. ->from('BaseTenancyBundle:Tenancy', 't')
  120. ->join('t.users', 'ut',
  121. \Doctrine\ORM\Query\Expr\Join::WITH,
  122. $em->getExpressionBuilder()->eq('ut.id', $this->idUser))
  123. ->where($em->getExpressionBuilder()->eq('t.id', $tenancyData['id']))
  124. ->getQuery()
  125. ->execute();
  126. if (!$ten_user) {
  127. $arrayTenancies[] = $tenancy;
  128. }
  129. }
  130. // actualizo las tenencias asociadas al usuario
  131. $user->setTenancies($arrayTenancies);
  132. $em->flush();
  133. } else {
  134. // seteo las tenencias como array en el usuario
  135. $this->get('security.token_storage')->getToken()->getUser()->setTenancies($tenancies);
  136. }
  137. $service_tenancy->setTenancy($current);
  138. }
  139. /**
  140. * Se crean los datos para hacer el fake al oauth
  141. * @param int $idUser Contiene el id de usuario a simular.
  142. * @throws \ErrorException Lanza una excepcion durante las distintas validaciones.
  143. */
  144. protected function FakeLogin($idUser = 1)
  145. {
  146. $this->idUser = $idUser;
  147. // obtengo la session
  148. $session = $this->get('session');
  149. if ($session != null) {
  150. // busco el usuario logueado
  151. $em = $this->getDoctrineManager();
  152. if ($em != null) {
  153. // FOS\UserBundle\Model\UserManager
  154. // FOS\UserBundle\Doctrine\UserManager
  155. if ($this->isModuleBase()) {
  156. $userManager = $this->get('fos_user.user_manager');
  157. if ($userManager != null) {
  158. try {
  159. $user = $userManager->findUserBy(array('id' => $this->idUser));
  160. } catch (\Throwable $t) {
  161. $user = null;
  162. }
  163. if (!$user) {
  164. // el usuario no existe por lo tanto lo creo. Deberia ver el tema de las tenencias
  165. // no tengo la oportunidad de probarlo
  166. $user = $userManager->createUser();
  167. $cargo = false;
  168. try {
  169. $em = $this->getDoctrineManager();
  170. $userDB = $em->getRepository("BaseUserBundle:User")->findOneBy(array('id' => $this->idUser));
  171. if ($userDB) {
  172. $cargo = true;
  173. $user->setEmail($userDB->email);
  174. $user->Id = $idUser;
  175. $user->setUsername($userDB->username);
  176. $user->setPlainPassword($userDB->plainPassword);
  177. $user->setEnabled(true);
  178. $user->setRoles($userDB->getRoles());
  179. // este comando ademas se setear los datos, los persiste en la base de datos.
  180. $userManager->updateUser($user);
  181. }
  182. } catch (\Throwable $t) {
  183. }
  184. if (!$cargo) {
  185. // no se cargo el usuario por algun error. Lo hago manual.
  186. $user->setEmail("pepe@pepe.com");
  187. $user->Id = $idUser;
  188. $user->setUsername('admin');
  189. $user->setPlainPassword('adminpass');
  190. $user->setEnabled(true);
  191. $user->setRoles(array("ROLE_USER"));
  192. $userManager->updateCanonicalFields($user);
  193. $userManager->updatePassword($user);
  194. }
  195. }
  196. // contiene el nombre de firewall a utilizar
  197. $firewall = 'main';
  198. // busco el token en el security
  199. $tokenStorage = $this->get('security.token_storage');
  200. if ($tokenStorage != null) {
  201. $token = null;
  202. if (!$tokenStorage->getToken()) {
  203. // no tengo el token, entonces lo creo
  204. $token = new UsernamePasswordToken($user, null, $firewall, array('ROLE_SUPER_ADMIN'));
  205. } else {
  206. // utilizo el token que yo esta creado
  207. $token = $tokenStorage->getToken();
  208. }
  209. // seteo en la session el firewall que voy a utilizar y el token
  210. $session->set('_security_' . $firewall, serialize($token));
  211. $session->save();
  212. // creo una cookie con los datos de la session.
  213. $cookie = new Cookie($session->getName(), $session->getId());
  214. $this->getClient()->getCookieJar()->set($cookie);
  215. // seteo el token en el security.token_storage
  216. $tokenStorage->setToken($token);
  217. } else {
  218. throw new \ErrorException("Error al obtener el token storage.");
  219. }
  220. } else {
  221. throw new \ErrorException("Error al obtener el fos user manager.");
  222. }
  223. } else {
  224. // no estoy en el modulo base
  225. // creo la clase de esta forma para no utilizar el "use" porque va a traer conflicto en otros modulos
  226. $class = "Base\OAuthClientBundle\Security\Core\User\CustomOAuthUser";
  227. $user = new $class('admin');
  228. // no se cargo el usuario por algun error. Lo hago manual.
  229. $user->Id = $idUser;
  230. $user->setRoles(array("ROLE_USER"));
  231. // contiene el nombre de firewall a utilizar
  232. $firewall = 'main';
  233. // busco el token en el security
  234. $tokenStorage = $this->get('security.token_storage');
  235. if ($tokenStorage != null) {
  236. $token = null;
  237. // no tengo el token, entonces lo creo
  238. $token = new UsernamePasswordToken($user, null, $firewall, array('ROLE_SUPER_ADMIN'));
  239. // seteo en la session el firewall que voy a utilizar y el token
  240. $session->set('_security_' . $firewall, serialize($token));
  241. $session->save();
  242. // creo una cookie con los datos de la session.
  243. $cookie = new Cookie($session->getName(), $session->getId());
  244. $this->getClient()->getCookieJar()->set($cookie);
  245. // seteo el token en el security.token_storage
  246. $tokenStorage->setToken($token);
  247. }
  248. }
  249. } else {
  250. throw new \ErrorException("Error al obtener doctrine.");
  251. }
  252. } else {
  253. throw new \ErrorException("Error al obtener la session.");
  254. }
  255. }
  256. /**
  257. * Crea las tenencias.
  258. * @param int $quantity Contiene la cantidad de tenencias a crear por defecto.
  259. * @return array Retorna una matriz con las tenencias a crear.
  260. * @throws \Exception Lanza un excepcion en caso de no poder crear las tenencias.
  261. */
  262. protected function generateTenancies($quantity = 0)
  263. {
  264. if ($quantity <= 0) {
  265. throw new \Exception("No se pasaron los argumentos de forma correcta.");
  266. } else {
  267. $resp = array();
  268. for ($i = 0; $i < $quantity; $i++) {
  269. if ($i == 1) {
  270. $resp[$i] = array('id' => $i, 'name' => 'Tenencia Base');
  271. } else {
  272. $resp[$i] = array('id' => $i, 'name' => 'Tenencia ' . $i);
  273. }
  274. }
  275. return $resp;
  276. }
  277. }
  278. /**
  279. * Cambia los datos para realizar una modificacion
  280. * @param array $original Contiene los datos originales.
  281. * @param array $newData Contiene los nuevos datos a setear.
  282. * @return array|string Retorna el array con los datos o el valor de la key pasada como parametro.
  283. * @throws \Exception Lanza un excepcion en caso de no encontrar la key.
  284. */
  285. protected function obtainDataChange($original, $newData)
  286. {
  287. foreach ($newData as $k => $v) {
  288. $original[$k] = $v;
  289. }
  290. return $original;
  291. }
  292. /**
  293. * Genera el filtro de busqueda filters
  294. * @param array $data Contiene los datos a buscar de la forma clave -> valor.
  295. * @param bool $qbcriteria Me dice si utiliza qb-criteria.
  296. * @return string Retorna el string generado.
  297. */
  298. protected function generateFilters($data, $qbcriteria = true)
  299. {
  300. $resp = '?';
  301. if ($qbcriteria) {
  302. $resp .= 'filters[qb-criteria]';
  303. }
  304. if ($data != null && count($data) > 0) {
  305. foreach ($data as $k => $v) {
  306. $resp .= '&filters[' . $k . ']=' . $v;
  307. }
  308. }
  309. return $resp;
  310. }
  311. /**
  312. * Funcion que busca el valor de una propiedad dentro del response.
  313. * @param Response $response Contiene el response.
  314. * @param string $property Contiene el nombre de la propiedad.
  315. * @param string $object Contiene el nombre del objeto del cual voy a obtener la propiedad.
  316. * @return null|mixed Retorna el valor de la propiedad.
  317. */
  318. protected function getProperty(Response $response, $property, $object = null)
  319. {
  320. $resp = json_decode($response->getContent(), true);
  321. if (count($resp) > 0) {
  322. if ($object != null) {
  323. // busco el objecto
  324. foreach ($resp as $k => $v) {
  325. if ($k == $object) {
  326. $resp = $v;
  327. break;
  328. }
  329. }
  330. }
  331. foreach ($resp as $v) {
  332. try {
  333. if (isset($v[$property])) {
  334. return $v[$property];
  335. }
  336. } catch (\Throwable $t) {
  337. }
  338. }
  339. return null;
  340. } else {
  341. if (isset($resp[$property])) {
  342. return $resp[$property];
  343. } else {
  344. return null;
  345. }
  346. }
  347. }
  348. /**
  349. * Funcion que inicializa por defecto las variables.
  350. * Crea el client para el request.
  351. * Hace el fake del login.
  352. * Crea 1 tenencia por defecto (TENENCIA BASE = 1).
  353. */
  354. protected function initDefault()
  355. {
  356. // creo un nuevo cliente para consultar. Lo tengo que crear para cada consulta.
  357. $this->getClient(true);
  358. // hago fake del login
  359. $this->FakeLogin();
  360. // creo el servicio de tenencias
  361. $this->fakeTenancyService();
  362. }
  363. }