WebTestCaseBase.php 17 KB

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