SystemMediaCategoryControllerTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\SystemMedia;
  5. use App\Models\SystemMediaCategory;
  6. use Illuminate\Http\UploadedFile;
  7. use Tests\AdminTestCase;
  8. use Illuminate\Foundation\Testing\WithFaker;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Tests\Traits\RequestActions;
  11. class SystemMediaCategoryControllerTest extends AdminTestCase
  12. {
  13. use RefreshDatabase;
  14. use RequestActions;
  15. protected $resourceName = 'system-media-categories';
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->login();
  20. }
  21. protected function createNestedData()
  22. {
  23. SystemMediaCategory::insert([
  24. [
  25. 'id' => 1,
  26. 'parent_id' => 0,
  27. 'name' => 'level 0-1',
  28. ],
  29. [
  30. 'id' => 2,
  31. 'parent_id' => 0,
  32. 'name' => 'level 0-2',
  33. ],
  34. [
  35. 'id' => 3,
  36. 'parent_id' => 1,
  37. 'name' => 'level 1-1',
  38. ],
  39. [
  40. 'id' => 4,
  41. 'parent_id' => 3,
  42. 'name' => 'level 3-1',
  43. ],
  44. ]);
  45. }
  46. public function testStoreValidation()
  47. {
  48. $this->createNestedData();
  49. // parent_id exists
  50. // name max:20
  51. $res = $this->storeResource([
  52. 'parent_id' => 111,
  53. 'name' => str_repeat('a', 21),
  54. ]);
  55. $res->assertJsonValidationErrors(['parent_id', 'name']);
  56. // name 同级 unique
  57. $res = $this->storeResource([
  58. 'parent_id' => 3,
  59. 'name' => 'level 3-1',
  60. ]);
  61. $res->assertJsonValidationErrors(['name']);
  62. // parent_id 0
  63. // name 不同级重复
  64. $res = $this->storeResource([
  65. 'parent_id' => 0,
  66. 'name' => 'level 3-1',
  67. ]);
  68. $res->assertJsonMissingValidationErrors(['parent_id', 'name']);
  69. }
  70. public function testStore()
  71. {
  72. $res = $this->storeResource([
  73. 'name' => 'level 0-1',
  74. ]);
  75. $res->assertStatus(201);
  76. $id1 = $this->getLastInsertId('system_media_categories');
  77. $this->assertDatabaseHas('system_media_categories', [
  78. 'id' => $id1,
  79. 'name' => 'level 0-1',
  80. 'parent_id' => 0,
  81. ]);
  82. $res = $this->storeResource([
  83. 'parent_id' => $id1,
  84. 'name' => 'level 1-1',
  85. ]);
  86. $res->assertStatus(201);
  87. $id2 = $this->getLastInsertId('system_media_categories');
  88. $this->assertDatabaseHas('system_media_categories', [
  89. 'id' => $id2,
  90. 'parent_id' => $id1,
  91. 'name' => 'level 1-1',
  92. ]);
  93. }
  94. public function testUpdate()
  95. {
  96. $id1 = factory(SystemMediaCategory::class)->create(['name' => 'level 0-1'])->id;
  97. $id2 = factory(SystemMediaCategory::class)->create(['name' => 'level 0-2'])->id;
  98. // name 同级 unique
  99. $res = $this->updateResource($id1, [
  100. 'name' => 'level 0-2',
  101. ]);
  102. $res->assertJsonValidationErrors(['name']);
  103. $res = $this->updateResource($id1, [
  104. 'parent_id' => $id2,
  105. 'name' => 'level 0-2',
  106. ]);
  107. $res->assertStatus(201);
  108. $res = $this->updateResource($id1);
  109. $res->assertStatus(201);
  110. $this->assertDatabaseHas('system_media_categories', [
  111. 'id' => $id1,
  112. 'parent_id' => $id2,
  113. 'name' => 'level 0-2',
  114. ]);
  115. }
  116. public function testEdit()
  117. {
  118. $id = factory(SystemMediaCategory::class)->create(['name' => 'level 0-1'])->id;
  119. $res = $this->editResource($id);
  120. $res->assertStatus(200)
  121. ->assertJson([
  122. 'id' => $id,
  123. 'name' => 'level 0-1',
  124. 'parent_id' => 0,
  125. ]);
  126. }
  127. public function testDestroy()
  128. {
  129. $this->createNestedData();
  130. factory(SystemMedia::class)->create(['category_id' => 3]);
  131. $res = $this->destroyResource(1);
  132. $res->assertStatus(204);
  133. // 删除所有子孙分类
  134. $this->assertDatabaseMissing('system_media_categories', ['id' => 1]);
  135. $this->assertDatabaseMissing('system_media_categories', ['id' => 3]);
  136. $this->assertDatabaseMissing('system_media_categories', ['id' => 4]);
  137. // 被删除的分类下的文件的分类 id 设为 0
  138. $this->assertDatabaseHas('system_media', [
  139. 'id' => 1,
  140. 'category_id' => 0,
  141. ]);
  142. $this->assertDatabaseHas('system_media_categories', ['id' => 2]);
  143. }
  144. public function testIndex()
  145. {
  146. $this->createNestedData();
  147. $res = $this->getResources();
  148. $res->assertStatus(200)
  149. ->assertJson([
  150. [
  151. 'id' => 1,
  152. 'parent_id' => 0,
  153. 'name' => 'level 0-1',
  154. 'children' => [
  155. [
  156. 'id' => 3,
  157. 'parent_id' => 1,
  158. 'name' => 'level 1-1',
  159. 'children' => [
  160. [
  161. 'id' => 4,
  162. 'parent_id' => 3,
  163. 'name' => 'level 3-1',
  164. ],
  165. ],
  166. ],
  167. ],
  168. ],
  169. [
  170. 'id' => 2,
  171. 'parent_id' => 0,
  172. 'name' => 'level 0-2',
  173. ],
  174. ]);
  175. $res = $this->getResources(['except' => 1]);
  176. $res->assertStatus(200)
  177. ->assertJson([
  178. [
  179. 'id' => 2,
  180. 'parent_id' => 0,
  181. 'name' => 'level 0-2',
  182. ],
  183. ]);
  184. }
  185. /**
  186. * @param int $cateId
  187. * @param array $data
  188. *
  189. * @return \Illuminate\Foundation\Testing\TestResponse
  190. */
  191. protected function storeSystemMedia($cateId, $data = [])
  192. {
  193. return $this->storeResource(
  194. $data,
  195. $this->resourceName.'.system-media',
  196. ['id' => $cateId]
  197. );
  198. }
  199. public function testStoreSystemMedia()
  200. {
  201. $categoryId = factory(SystemMediaCategory::class)->create()->id;
  202. // file required
  203. $res = $this->storeSystemMedia($categoryId);
  204. $res->assertJsonValidationErrors(['file']);
  205. // file file
  206. $res = $this->storeSystemMedia($categoryId, ['file' => 'not a file']);
  207. $res->assertJsonValidationErrors(['file']);
  208. $file = UploadedFile::fake()->image('avatar.jpg', 200, 200);
  209. $res = $this->storeSystemMedia($categoryId, [
  210. 'file' => $file,
  211. Controller::UPLOAD_FOLDER_FIELD => 'tests',
  212. ]);
  213. $res->assertStatus(201);
  214. $filename = md5_file($file).'.jpg';
  215. $path = Controller::UPLOAD_FOLDER_PREFIX.'/tests/'.$filename;
  216. $this->assertDatabaseHas('system_media', [
  217. 'id' => $this->getLastInsertId('system_media'),
  218. 'category_id' => $categoryId,
  219. 'filename' => $filename,
  220. 'size' => $file->getSize(),
  221. 'ext' => 'jpg',
  222. 'mime_type' => $file->getMimeType(),
  223. 'path' => $path,
  224. ]);
  225. $this->storage->exists($path);
  226. $this->storage->delete($path);
  227. }
  228. protected function systemMediaIndex($params = [])
  229. {
  230. return $this->getResources($params, $this->resourceName.'.system-media');
  231. }
  232. public function testSystemMediaIndex()
  233. {
  234. factory(SystemMediaCategory::class)
  235. ->create()
  236. ->media()
  237. ->createMany([
  238. factory(SystemMedia::class)->make([
  239. 'filename' => 'avatar.jpg',
  240. 'ext' => 'jpg',
  241. ])->toArray(),
  242. factory(SystemMedia::class)->make([
  243. 'filename' => 'funny.gif',
  244. 'ext' => 'gif',
  245. ])->toArray(),
  246. ]);
  247. $categoryId1 = $this->getLastInsertId('system_media_categories');
  248. // 其他分类的图片
  249. factory(SystemMediaCategory::class)
  250. ->create()
  251. ->media()
  252. ->createMany(factory(SystemMedia::class, 2)->make(['ext' => 'jpg'])->toArray());
  253. // ext in 筛选
  254. $res = $this->systemMediaIndex([
  255. 'id' => $categoryId1,
  256. 'ext' => ['jpg'],
  257. ]);
  258. $res->assertStatus(200)
  259. ->assertJsonFragment(['ext' => 'jpg'])
  260. ->assertJsonMissing(['ext' => 'gif']);
  261. $res = $this->systemMediaIndex([
  262. 'id' => $categoryId1,
  263. 'ext' => 'jpg,gif',
  264. ]);
  265. $res->assertStatus(200)
  266. ->assertJsonCount(2, 'data');
  267. // filename like
  268. $res = $this->systemMediaIndex([
  269. 'id' => $categoryId1,
  270. 'filename' => 'ny',
  271. ]);
  272. $res->assertStatus(200)
  273. ->assertJsonFragment(['filename' => 'funny.gif'])
  274. ->assertJsonMissing(['filename' => 'avatar.jpg']);
  275. }
  276. }