1
0

TwigViewTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /*
  3. * Copyright 2026 Alessandro Chiapparini <sprint_planer_web@chiapparini.org>
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * See the LICENSE file in the project root for the full license text.
  9. */
  10. declare(strict_types=1);
  11. namespace App\Tests\Http;
  12. use App\Http\View;
  13. use App\Tests\TestCase;
  14. use ReflectionClass;
  15. use ReflectionProperty;
  16. /**
  17. * Smoke tests for the Twig-backed View. Phase 19 swapped the bare-PHP renderer
  18. * for Twig 3 — these confirm that the controllers' (name, $data) call shape
  19. * still produces HTML containing the markers each page is expected to carry.
  20. */
  21. final class TwigViewTest extends TestCase
  22. {
  23. private View $view;
  24. protected function setUp(): void
  25. {
  26. parent::setUp();
  27. $this->view = new View(__DIR__ . '/../../views');
  28. }
  29. private function makeUser(bool $isAdmin = true): object
  30. {
  31. $u = new \stdClass();
  32. $u->id = 1;
  33. $u->email = 'alice@example.com';
  34. $u->displayName = 'Alice';
  35. $u->isAdmin = $isAdmin;
  36. return $u;
  37. }
  38. public function testHomeRendersForSignedInAdmin(): void
  39. {
  40. $html = $this->view->render('home', [
  41. 'title' => 'Sprint Planner',
  42. 'csrfToken' => 'tok',
  43. 'currentUser' => $this->makeUser(true),
  44. 'schemaVersion' => 3,
  45. 'dbPath' => '/tmp/x',
  46. 'appEnv' => 'production',
  47. 'oidcConfigured' => false,
  48. 'localAdminEnabled' => true,
  49. 'authError' => false,
  50. 'sprintRows' => [],
  51. ]);
  52. self::assertStringContainsString('<title>Sprint Planner</title>', $html);
  53. self::assertStringContainsString('Sprints', $html);
  54. self::assertStringContainsString('Alice', $html); // displayName
  55. self::assertStringContainsString('admin', $html); // admin badge
  56. self::assertStringContainsString('/auth/logout', $html);
  57. // Phase 19: vendored Alpine + htmx + sortable scripts must be linked.
  58. self::assertStringContainsString('/assets/js/vendor/alpine-csp.min.js', $html);
  59. self::assertStringContainsString('/assets/js/vendor/htmx.min.js', $html);
  60. }
  61. public function testHomeRendersDeletedSprintFlashWhenSet(): void
  62. {
  63. // R01-N26: the green "Sprint X was deleted" chip is now driven by
  64. // the `deletedSprintName` view variable (sourced from a one-shot
  65. // session flash, not from `?deleted=`). Pin that the chip appears
  66. // for any non-empty string and that the name is HTML-escaped.
  67. $html = $this->view->render('home', [
  68. 'title' => 'Sprint Planner',
  69. 'csrfToken' => 'tok',
  70. 'currentUser' => $this->makeUser(true),
  71. 'schemaVersion' => 3,
  72. 'dbPath' => '/tmp/x',
  73. 'appEnv' => 'production',
  74. 'oidcConfigured' => false,
  75. 'localAdminEnabled' => true,
  76. 'authError' => false,
  77. 'deletedSprintName' => 'Sprint <Alpha>',
  78. 'sprintRows' => [],
  79. ]);
  80. self::assertStringContainsString('was deleted', $html);
  81. self::assertStringContainsString('Sprint &lt;Alpha&gt;', $html);
  82. self::assertStringNotContainsString(
  83. '<b>Sprint <Alpha></b>',
  84. $html,
  85. 'sprint name must be Twig-autoescaped — never rendered raw',
  86. );
  87. }
  88. public function testHomeOmitsDeletedSprintFlashWhenEmpty(): void
  89. {
  90. $html = $this->view->render('home', [
  91. 'title' => 'Sprint Planner',
  92. 'csrfToken' => 'tok',
  93. 'currentUser' => $this->makeUser(true),
  94. 'schemaVersion' => 3,
  95. 'dbPath' => '/tmp/x',
  96. 'appEnv' => 'production',
  97. 'oidcConfigured' => false,
  98. 'localAdminEnabled' => true,
  99. 'authError' => false,
  100. 'deletedSprintName' => '',
  101. 'sprintRows' => [],
  102. ]);
  103. self::assertStringNotContainsString('was deleted', $html);
  104. }
  105. public function testHomeForAnonymousUserHidesRuntimePanel(): void
  106. {
  107. $html = $this->view->render('home', [
  108. 'title' => 'Sprint Planner',
  109. 'csrfToken' => 'tok',
  110. 'currentUser' => null,
  111. 'schemaVersion' => 3,
  112. 'dbPath' => '/var/data/app.sqlite',
  113. 'appEnv' => 'production',
  114. 'oidcConfigured' => true,
  115. 'localAdminEnabled' => true,
  116. 'authError' => false,
  117. 'sprintRows' => [],
  118. ]);
  119. self::assertStringContainsString('Sign in with Microsoft', $html);
  120. // R01-N02: the Runtime <details> panel must not leak PHP_VERSION,
  121. // dbPath, schema version, OIDC/local-admin flags to anonymous visitors.
  122. self::assertStringNotContainsString('Runtime', $html);
  123. self::assertStringNotContainsString('Schema version', $html);
  124. self::assertStringNotContainsString('/var/data/app.sqlite', $html);
  125. self::assertStringNotContainsString(PHP_VERSION, $html);
  126. // R01-N31 falls out of the same gate: no /healthz hint either.
  127. self::assertStringNotContainsString('/healthz', $html);
  128. }
  129. public function testAuditRendersWithEmptyResults(): void
  130. {
  131. $html = $this->view->render('audit/index', [
  132. 'title' => 'Audit',
  133. 'csrfToken' => 'tok',
  134. 'currentUser' => $this->makeUser(true),
  135. 'filters' => [
  136. 'user_email' => '',
  137. 'action' => '',
  138. 'entity_type' => '',
  139. 'entity_id' => '',
  140. 'from_date' => '',
  141. 'to_date' => '',
  142. ],
  143. 'page' => 1,
  144. 'pages' => 1,
  145. 'pageSize' => 50,
  146. 'total' => 0,
  147. 'rows' => [],
  148. 'actions' => ['CREATE', 'UPDATE', 'DELETE'],
  149. 'entityTypes' => ['worker', 'sprint'],
  150. 'users' => [],
  151. ]);
  152. self::assertStringContainsString('Audit log', $html);
  153. self::assertStringContainsString('No audit rows match', $html);
  154. // hx-boost wired on the filter form (Phase 19).
  155. self::assertStringContainsString('hx-boost="true"', $html);
  156. }
  157. public function testSprintsShowRendersTaskGridAndStatusFilter(): void
  158. {
  159. $sprint = (new ReflectionClass(\App\Domain\Sprint::class))->newInstanceWithoutConstructor();
  160. foreach ([
  161. 'id' => 1, 'name' => 'S1',
  162. 'startDate' => '2026-01-01', 'endDate' => '2026-01-15',
  163. 'reserveFraction' => 0.2, 'isArchived' => false, 'createdAt' => '2026-01-01',
  164. ] as $k => $v) {
  165. (new ReflectionProperty($sprint, $k))->setValue($sprint, $v);
  166. }
  167. $weekClass = new ReflectionClass(\App\Domain\SprintWeek::class);
  168. $w = $weekClass->newInstanceWithoutConstructor();
  169. foreach ([
  170. 'id' => 10, 'sprintId' => 1, 'isoWeek' => 1,
  171. 'startDate' => '2026-01-05', 'sortOrder' => 1,
  172. 'maxWorkingDays' => 5, 'activeDaysMask' => 31,
  173. ] as $k => $v) {
  174. (new ReflectionProperty($w, $k))->setValue($w, $v);
  175. }
  176. $swClass = new ReflectionClass(\App\Domain\SprintWorker::class);
  177. $sw = $swClass->newInstanceWithoutConstructor();
  178. foreach ([
  179. 'id' => 100, 'sprintId' => 1, 'workerId' => 50, 'workerName' => 'Bob',
  180. 'sortOrder' => 1, 'rtb' => 0.1,
  181. ] as $k => $v) {
  182. (new ReflectionProperty($sw, $k))->setValue($sw, $v);
  183. }
  184. $html = $this->view->render('sprints/show', [
  185. 'title' => 'S1',
  186. 'csrfToken' => 'tok',
  187. 'currentUser' => $this->makeUser(true),
  188. 'sprint' => $sprint,
  189. 'weeks' => [$w],
  190. 'sprintWorkers' => [$sw],
  191. 'grid' => [100 => [10 => 2.5]],
  192. 'capacity' => [100 => [
  193. 'ressourcen' => 2.5,
  194. 'after_reserves' => 2.0,
  195. 'committed_prio1' => 0.0,
  196. 'available' => 2.0,
  197. ]],
  198. 'tasks' => [],
  199. 'taskGrid' => [],
  200. 'statusGrid' => [],
  201. 'ownerChoices' => [],
  202. 'taskStatusEnabled' => true,
  203. ]);
  204. self::assertStringContainsString('data-sprint-root', $html);
  205. self::assertStringContainsString('data-sprint-id="1"', $html);
  206. self::assertStringContainsString('data-task-status-enabled="1"', $html);
  207. self::assertStringContainsString('Status', $html); // status filter visible
  208. self::assertStringContainsString('No tasks yet', $html);
  209. // Phase 19: page-specific JS still loaded.
  210. self::assertStringContainsString('/assets/js/sprint-planner.js', $html);
  211. }
  212. }