WebTestCaseBase.php 16 KB

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