TestUsefulLinksController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\UsefulLink;
  4. use App\Models\User;
  5. use Illuminate\Foundation\Testing\DatabaseTransactions;
  6. use Illuminate\Support\Str;
  7. use Tests\TestCase;
  8. /**
  9. * Class TestUsefulLinksController
  10. */
  11. class TestUsefulLinksController extends TestCase
  12. {
  13. use DatabaseTransactions;
  14. /**
  15. * @dataProvider accessibleRoutesDataProvider
  16. *
  17. * @param string $method
  18. * @param string $route
  19. * @param int $expectedStatus
  20. */
  21. public function test_accessible_routes(string $method, string $route, int $expectedStatus)
  22. {
  23. UsefulLink::factory()->create([
  24. 'id' => 1,
  25. ]);
  26. $response = $this->actingAs(User::factory()->create([
  27. 'role' => 'admin',
  28. 'pterodactyl_id' => '1',
  29. ]))->{$method}($route);
  30. $response->assertStatus($expectedStatus);
  31. }
  32. /**
  33. * @dataProvider usefulLinkDataProvider
  34. *
  35. * @param array $dataSet
  36. * @param int $expectedCount
  37. * @param bool $assertValidationErrors
  38. */
  39. public function test_creating_useful_link(array $dataSet, int $expectedCount, bool $assertValidationErrors)
  40. {
  41. $response = $this->actingAs($this->getTestUser())->post(route('admin.usefullinks.store'), $dataSet);
  42. if ($assertValidationErrors) {
  43. $response->assertSessionHasErrors();
  44. } else {
  45. $response->assertSessionHasNoErrors();
  46. }
  47. $response->assertRedirect();
  48. $this->assertDatabaseCount('useful_links', $expectedCount);
  49. }
  50. /**
  51. * @dataProvider usefulLinkDataProvider
  52. *
  53. * @param array $dataSet
  54. * @param int $expectedCount
  55. * @param bool $assertValidationErrors
  56. */
  57. public function test_updating_useful_link(array $dataSet, int $expectedCount, bool $assertValidationErrors)
  58. {
  59. $link = UsefulLink::factory()->create([
  60. 'id' => 1,
  61. ]);
  62. $response = $this->actingAs($this->getTestUser())->patch(route('admin.usefullinks.update', $link->id), $dataSet);
  63. if ($assertValidationErrors) {
  64. $response->assertSessionHasErrors();
  65. } else {
  66. $response->assertSessionHasNoErrors();
  67. }
  68. $response->assertRedirect();
  69. $this->assertDatabaseCount('useful_links', 1);
  70. }
  71. public function test_deleting_useful_link()
  72. {
  73. $link = UsefulLink::factory()->create([
  74. 'id' => 1,
  75. ]);
  76. $response = $this->actingAs($this->getTestUser())->delete(route('admin.usefullinks.update', $link->id));
  77. $response->assertRedirect();
  78. $this->assertDatabaseCount('useful_links', 0);
  79. }
  80. /**
  81. * @return User
  82. */
  83. private function getTestUser(): User
  84. {
  85. return User::factory()->create([
  86. 'role' => 'admin',
  87. 'pterodactyl_id' => '1',
  88. ]);
  89. }
  90. /**
  91. * @return array
  92. */
  93. public function usefulLinkDataProvider(): array
  94. {
  95. return [
  96. 'Valid dataset 1' => [
  97. 'dataSet' => [
  98. 'icon' => 'fas fa-user',
  99. 'title' => 'Bitsec.Dev Dashboard',
  100. 'link' => 'https://manage.bitsec.dev.com',
  101. 'description' => Str::random(1500),
  102. ],
  103. 'expectedCount' => 1,
  104. 'assertValidationErrors' => false,
  105. ],
  106. 'Valid dataset 2' => [
  107. 'dataSet' => [
  108. 'icon' => 'fas fa-user',
  109. 'title' => Str::random(30),
  110. 'link' => 'https://somerandomsite.com',
  111. 'description' => Str::random(1500),
  112. ],
  113. 'expectedCount' => 1,
  114. 'assertValidationErrors' => false,
  115. ],
  116. 'Invalid dataset (invalid link)' => [
  117. 'dataSet' => [
  118. 'icon' => 'fas fa-user',
  119. 'title' => 'Some Random Title',
  120. 'link' => '1221',
  121. 'description' => '<p>Some Random HTML</p>',
  122. ],
  123. 'expectedCount' => 0,
  124. 'assertValidationErrors' => true,
  125. ],
  126. 'Invalid dataset (no title)' => [
  127. 'dataSet' => [
  128. 'icon' => 'fas fa-user',
  129. 'title' => '',
  130. 'link' => 'https://somerandomsite.com',
  131. 'description' => '<p>Some Random HTML</p>',
  132. ],
  133. 'expectedCount' => 0,
  134. 'assertValidationErrors' => true,
  135. ],
  136. 'Invalid dataset (to long title)' => [
  137. 'dataSet' => [
  138. 'icon' => 'fas fa-user',
  139. 'title' => Str::random(200),
  140. 'link' => 'https://valid.com',
  141. 'description' => '<p>Some Random HTML</p>',
  142. ],
  143. 'expectedCount' => 0,
  144. 'assertValidationErrors' => true,
  145. ],
  146. 'Invalid dataset (to long description)' => [
  147. 'dataSet' => [
  148. 'icon' => 'fas fa-user',
  149. 'title' => 'Some Random Valid Title',
  150. 'link' => 'https://valid.com',
  151. 'description' => Str::random(2100),
  152. ],
  153. 'expectedCount' => 0,
  154. 'assertValidationErrors' => true,
  155. ],
  156. 'Invalid dataset (no icon)' => [
  157. 'dataSet' => [
  158. 'title' => 'Some Random Valid Title',
  159. 'link' => 'https://valid.com',
  160. 'description' => Str::random(200),
  161. ],
  162. 'expectedCount' => 0,
  163. 'assertValidationErrors' => true,
  164. ],
  165. ];
  166. }
  167. /**
  168. * @return array[]
  169. */
  170. public function accessibleRoutesDataProvider(): array
  171. {
  172. return [
  173. 'index page' => [
  174. 'method' => 'get',
  175. 'route' => '/admin/usefullinks',
  176. 'expectedStatus' => 200,
  177. ],
  178. 'Create page' => [
  179. 'method' => 'get',
  180. 'route' => '/admin/usefullinks/create',
  181. 'expectedStatus' => 200,
  182. ],
  183. 'Edit page' => [
  184. 'method' => 'get',
  185. 'route' => '/admin/usefullinks/1/edit',
  186. 'expectedStatus' => 200,
  187. ],
  188. ];
  189. }
  190. }