TestUsefulLinksController.php 6.2 KB

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