ParametersTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Tests\Application;
  3. use Zenstruck\Foundry\Proxy;
  4. use App\Entity\Access\Access;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use ApiPlatform\Symfony\Bundle\Test\Client;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
  9. use Symfony\Contracts\HttpClient\ResponseInterface;
  10. class ParametersTest extends ApiTestCase
  11. {
  12. protected Access | Proxy | null $user = null;
  13. private $securityToken;
  14. protected Client $client;
  15. private EntityManagerInterface $em;
  16. protected function setUp(): void
  17. {
  18. self::bootKernel();
  19. $this->em = self::getContainer()->get(EntityManagerInterface::class);
  20. $this->client = static::createClient();
  21. }
  22. // public function testGetParameters(): void
  23. // {
  24. // $this->loginAs($this->user);
  25. // $this->assertResponseIsSuccessful();
  26. // $this->get('/api/parameters/6262');
  27. // $this->assertResponseStatusCodeSame(200);
  28. // $this->assertJsonContains([
  29. // '@context' => '/api/contexts/Parameters',
  30. // '@id' => '/api/parameters/6262',
  31. // '@type' => 'Parameters',
  32. // 'id' => 6262,
  33. // 'organization' => '/api/organizations/616013',
  34. // 'financialDate' => '2024-01-23T00:00:00+00:00',
  35. // 'musicalDate' => '2024-01-23T00:00:00+00:00',
  36. // 'startCourseDate' => '2024-01-23T00:00:00+00:00',
  37. // 'endCourseDate' => '2024-01-23T00:00:00+00:00',
  38. // 'average' => 20,
  39. // 'editCriteriaNotationByAdminOnly'=> true,
  40. // 'smsSenderName' => 'MySender',
  41. // 'logoDonorsMove' => false,
  42. // 'subDomain' => 'subdomain',
  43. // 'website' => 'https://www.example.com',
  44. // 'otherWebsite' => 'https://www.otherwebsite.com',
  45. // 'customDomain' => 'https://www.customdomain.com',
  46. // 'desactivateOpentalentSiteWeb' => false,
  47. // 'bulletinPeriod' => 'SomePeriod',
  48. // 'bulletinWithTeacher' => false,
  49. // 'bulletinPrintAddress' => false,
  50. // 'bulletinSignatureDirector' => true,
  51. // 'bulletinDisplayLevelAcquired' => true,
  52. // 'bulletinShowEducationWithoutEvaluation' => false,
  53. // 'bulletinViewTestResults' => false,
  54. // 'bulletinShowAbsences' => false,
  55. // 'bulletinShowAverages' => true,
  56. // 'bulletinOutput' => 'SomeOutput',
  57. // 'bulletinEditWithoutEvaluation' => true,
  58. // 'bulletinReceiver' => 'STUDENTS_AND_THEIR_GUARDIANS',
  59. // 'bulletinCriteriaSort' => 'BY_CRITERIA_INSERT',
  60. // 'usernameSMS' => 'username',
  61. // 'passwordSMS' => 'password',
  62. // 'showAdherentList' => true,
  63. // 'studentsAreAdherents' => false,
  64. // 'timezone' => 'Europe/Paris',
  65. // 'educationPeriodicity' => 'ANNUAL',
  66. // 'advancedEducationNotationType' => 'BY_EDUCATION',
  67. // 'sendAttendanceEmail' => false,
  68. // 'sendAttendanceSms' => false,
  69. // 'generateAttendanceReport' => true,
  70. // 'consultPedagogicResult' => true,
  71. // 'consultTeacherListing' => true,
  72. // 'periodValidation' => true,
  73. // 'notifyAdministrationAbsence' => false,
  74. // ]);
  75. // }
  76. // public function testBadPutBulletinCriteriasort()
  77. // {
  78. // $this->loginAs($this->user);
  79. // $this->put('/api/parameters/6262', [
  80. // 'bulletinCriteriaSort' => 'BAD_CRITERIA_SORT'
  81. // ]);
  82. // $this->assertResponseStatusCodeSame(400);
  83. // }
  84. public function testBadadvancedEducationNotationType()
  85. {
  86. $this->loginAs($this->user);
  87. $this->put('/api/parameters/6262', [
  88. 'bulletinCriteriaSort' => 'bad_advancedEducation'
  89. ]);
  90. $this->assertResponseStatusCodeSame(400);
  91. }
  92. // public function testDeleteParameters()
  93. // {
  94. // $this->loginAs($this->user);
  95. // $this->delete('/api/parameters/6262');
  96. // $this->assertResponseStatusCodeSame(405);
  97. // }
  98. // public function testBadTimeZone()
  99. // {
  100. // $this->loginAs($this->user);
  101. // $this->put('/api/parameters/6262', [
  102. // 'timezone' => 'bad_timezone'
  103. // ]);
  104. // $this->assertResponseStatusCodeSame(500);
  105. // }
  106. // public function testBadPeridicity()
  107. // {
  108. // $this->loginAs($this->user);
  109. // $this->put('/api/parameters/6262', [
  110. // 'educationPeriodicity' => 'bad_periodicity'
  111. // ]);
  112. // $this->assertResponseStatusCodeSame(500);
  113. // }
  114. public function loginAs()
  115. {
  116. // on récupère l'access qui a l'id 641003 dans l'entity manager
  117. $access = $this->em->getRepository(Access::class)->find(642459);
  118. $person = $access->getPerson();
  119. $response = $this->post(
  120. '/login_check',
  121. ['username' => $person->getUsername(), 'password' => $person->getPassword()]
  122. );
  123. $content = $response->getContent();
  124. $this->securityToken = json_decode($content)->token;
  125. $this->user = $access;
  126. }
  127. /**
  128. * Send a GET request and return the response parsed content
  129. *
  130. * @param string $route
  131. * @param array<mixed> $headers
  132. * @return ResponseInterface
  133. */
  134. protected function get(string $route, array $headers = []): ResponseInterface
  135. {
  136. return $this->request(
  137. Request::METHOD_GET,
  138. $route,
  139. null,
  140. $headers
  141. );
  142. }
  143. /**
  144. * Send a POST request and return the response parsed content
  145. *
  146. * @param string $route
  147. * @param array<mixed> $data
  148. * @param array<mixed> $headers
  149. * @return ResponseInterface
  150. */
  151. protected function post(string $route, array $data, array $headers = []): ResponseInterface
  152. {
  153. return $this->request(
  154. Request::METHOD_POST,
  155. $route,
  156. $data,
  157. $headers
  158. );
  159. }
  160. /**
  161. * Send a requests, parse the hydra response and return an object or a Collection
  162. *
  163. * @param string $method
  164. * @param string $route
  165. * @param array<mixed> $data
  166. * @param array<mixed> $headers
  167. * @return ResponseInterface
  168. */
  169. protected function request(string $method, string $route, array | null $data = null, array $headers = []): ResponseInterface
  170. {
  171. if ($this->user) {
  172. $headers = array_merge(
  173. ['x-accessid' => $this->user->getId(), 'authorization' => 'BEARER ' . $this->securityToken],
  174. $headers
  175. );
  176. }
  177. $parameters = ['headers' => $headers];
  178. if ($data) {
  179. $parameters['json'] = $data;
  180. }
  181. return $this->client->request(
  182. $method,
  183. $route,
  184. $parameters
  185. );
  186. }
  187. /**
  188. * Send a PUT request and return the response parsed content
  189. *
  190. * @param string $route
  191. * @param array<mixed> $data
  192. * @param array<mixed> $headers
  193. * @return ResponseInterface
  194. */
  195. protected function put(string $route, array $data, array $headers = []): ResponseInterface
  196. {
  197. return $this->request(
  198. Request::METHOD_PUT,
  199. $route,
  200. $data,
  201. $headers
  202. );
  203. }
  204. /**
  205. * Send a DELETE request and return the response parsed content
  206. *
  207. * @param string $route
  208. * @param array<mixed> $headers
  209. * @return ResponseInterface
  210. */
  211. protected function delete(string $route, array $headers = []): ResponseInterface
  212. {
  213. return $this->request(
  214. Request::METHOD_DELETE,
  215. $route,
  216. null,
  217. $headers
  218. );
  219. }
  220. /**
  221. * Assert that the response has the expected status code and is well formated
  222. *
  223. * @param string $resourceClass
  224. * @param int $expectedStatus
  225. * @return void
  226. */
  227. protected function validateCollectionSchema(string $resourceClass, int $expectedStatus = 200): void
  228. {
  229. $this->assertResponseStatusCodeSame($expectedStatus);
  230. if ($expectedStatus == 200) {
  231. $this->assertResponseIsSuccessful();
  232. }
  233. // Asserts that the returned content type is JSON-LD (the default)
  234. $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
  235. // Asserts that the returned JSON is validated by the JSON Schema generated for this resource by API Platform
  236. // >>> Issue with the json typed PublicStructure::addresses properties
  237. // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
  238. }
  239. }