UploadableEntityTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <?php
  2. namespace Gedmo\Uploadable;
  3. use Tool\BaseTestCaseORM,
  4. Doctrine\Common\EventManager,
  5. Doctrine\Common\Util\Debug,
  6. Uploadable\Fixture\Entity\Image,
  7. Uploadable\Fixture\Entity\Article,
  8. Uploadable\Fixture\Entity\File,
  9. Uploadable\Fixture\Entity\FileWithoutPath,
  10. Uploadable\Fixture\Entity\FileWithSha1Name,
  11. Uploadable\Fixture\Entity\FileWithAlphanumericName,
  12. Uploadable\Fixture\Entity\FileWithCustomFilenameGenerator,
  13. Uploadable\Fixture\Entity\FileAppendNumber,
  14. Uploadable\Fixture\Entity\FileWithMaxSize,
  15. Uploadable\Fixture\Entity\FileWithAllowedTypes,
  16. Uploadable\Fixture\Entity\FileWithDisallowedTypes,
  17. Gedmo\Uploadable\Stub\UploadableListenerStub,
  18. Gedmo\Uploadable\Stub\MimeTypeGuesserStub,
  19. Gedmo\Uploadable\FileInfo\FileInfoArray;
  20. /**
  21. * These are tests for Uploadable behavior
  22. *
  23. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  24. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  25. * @link http://www.gediminasm.org
  26. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  27. */
  28. class UploadableEntityTest extends BaseTestCaseORM
  29. {
  30. const IMAGE_CLASS = 'Uploadable\Fixture\Entity\Image';
  31. const ARTICLE_CLASS = 'Uploadable\Fixture\Entity\Article';
  32. const FILE_CLASS = 'Uploadable\Fixture\Entity\File';
  33. const FILE_APPEND_NUMBER_CLASS = 'Uploadable\Fixture\Entity\FileAppendNumber';
  34. const FILE_WITHOUT_PATH_CLASS = 'Uploadable\Fixture\Entity\FileWithoutPath';
  35. const FILE_WITH_SHA1_NAME_CLASS = 'Uploadable\Fixture\Entity\FileWithSha1Name';
  36. const FILE_WITH_ALPHANUMERIC_NAME_CLASS = 'Uploadable\Fixture\Entity\FileWithAlphanumericName';
  37. const FILE_WITH_CUSTOM_FILENAME_GENERATOR_CLASS = 'Uploadable\Fixture\Entity\FileWithCustomFilenameGenerator';
  38. const FILE_WITH_MAX_SIZE_CLASS = 'Uploadable\Fixture\Entity\FileWithMaxSize';
  39. const FILE_WITH_ALLOWED_TYPES_CLASS = 'Uploadable\Fixture\Entity\FileWithAllowedTypes';
  40. const FILE_WITH_DISALLOWED_TYPES_CLASS = 'Uploadable\Fixture\Entity\FileWithDisallowedTypes';
  41. /**
  42. * @var UploadableListener
  43. */
  44. private $listener;
  45. private $testFile;
  46. private $testFile2;
  47. private $testFile3;
  48. private $testFileWithoutExt;
  49. private $testFileWithSpaces;
  50. private $destinationTestDir;
  51. private $destinationTestFile;
  52. private $destinationTestFile2;
  53. private $destinationTestFile3;
  54. private $destinationTestFileWithoutExt;
  55. private $destinationTestFileWithSpaces;
  56. private $testFilename;
  57. private $testFilename2;
  58. private $testFilename3;
  59. private $testFilenameWithoutExt;
  60. private $testFilenameWithSpaces;
  61. private $testFileSize;
  62. private $testFileMimeType;
  63. protected function setUp()
  64. {
  65. parent::setUp();
  66. $evm = new EventManager;
  67. $this->listener = new UploadableListenerStub();
  68. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub('text/plain'));
  69. $evm->addEventSubscriber($this->listener);
  70. $config = $this->getMockAnnotatedConfig();
  71. $this->em = $this->getMockSqliteEntityManager($evm, $config);
  72. $this->testFile = __DIR__.'/../../data/test.txt';
  73. $this->testFile2 = __DIR__.'/../../data/test2.txt';
  74. $this->testFile3 = __DIR__.'/../../data/test_3.txt';
  75. $this->testFileWithoutExt = __DIR__.'/../../data/test4';
  76. $this->testFileWithSpaces = __DIR__.'/../../data/test with spaces.txt';
  77. $this->destinationTestDir = __DIR__.'/../../temp/uploadable';
  78. $this->destinationTestFile = $this->destinationTestDir.'/test.txt';
  79. $this->destinationTestFile2 = $this->destinationTestDir.'/test2.txt';
  80. $this->destinationTestFile3 = $this->destinationTestDir.'/test_3.txt';
  81. $this->destinationTestFileWithoutExt = $this->destinationTestDir.'/test4';
  82. $this->destinationTestFileWithSpaces = $this->destinationTestDir.'/test with spaces';
  83. $this->testFilename = substr($this->testFile, strrpos($this->testFile, '/') + 1);
  84. $this->testFilename2 = substr($this->testFile2, strrpos($this->testFile2, '/') + 1);
  85. $this->testFilename3 = substr($this->testFile3, strrpos($this->testFile3, '/') + 1);
  86. $this->testFilenameWithoutExt = substr($this->testFileWithoutExt, strrpos($this->testFileWithoutExt, '/') + 1);
  87. $this->testFilenameWithSpaces= substr($this->testFileWithSpaces, strrpos($this->testFileWithSpaces, '/') + 1);
  88. $this->testFileSize = 4;
  89. $this->testFileMimeType = 'text/plain';
  90. $this->clearFilesAndDirectories();
  91. if (!is_dir($this->destinationTestDir)) {
  92. mkdir($this->destinationTestDir);
  93. };
  94. }
  95. public function tearDown()
  96. {
  97. $this->clearFilesAndDirectories();
  98. }
  99. public function testUploadableEntity()
  100. {
  101. // INSERTION of an Uploadable Entity
  102. // If there was no uploaded file, we do nothing
  103. $image = new Image();
  104. $image->setTitle('123');
  105. $this->em->persist($image);
  106. $this->em->flush();
  107. $this->assertNull($image->getFilePath());
  108. // If there is an uploaded file, we process it
  109. $fileInfo = $this->generateUploadedFile();
  110. $image2 = new Image();
  111. $image2->setTitle('456');
  112. $this->listener->addEntityFileInfo($image2, $fileInfo);
  113. $this->em->persist($image2);
  114. $this->em->flush();
  115. $this->em->refresh($image2);
  116. // We need to set this again because of the recent refresh
  117. $firstFile = $image2->getFilePath();
  118. $this->assertPathEquals($image2->getPath().'/'.$fileInfo['name'], $image2->getFilePath());
  119. $this->assertTrue(is_file($firstFile));
  120. $this->assertEquals($fileInfo['size'], $image2->getSize());
  121. $this->assertEquals($fileInfo['type'], $image2->getMime());
  122. // UPDATE of an Uploadable Entity
  123. // We change the "uploaded" file
  124. $fileInfo['tmp_name'] = $this->testFile2;
  125. $fileInfo['name'] = $this->testFilename2;
  126. // We use a FileInfoInterface instance here
  127. $this->listener->addEntityFileInfo($image2, new FileInfoArray($fileInfo));
  128. $this->em->flush();
  129. $this->em->refresh($image2);
  130. $lastFile = $image2->getFilePath();
  131. $this->assertPathEquals($image2->getPath().'/'.$fileInfo['name'], $image2->getFilePath());
  132. $this->assertTrue(is_file($lastFile));
  133. // First file should be removed on update
  134. $this->assertFalse(is_file($firstFile));
  135. // REMOVAL of an Uploadable Entity
  136. $this->em->remove($image2);
  137. $this->em->flush();
  138. $this->assertFalse(is_file($lastFile));
  139. }
  140. public function testUploadableEntityWithCompositePath()
  141. {
  142. // We set the default path on the listener
  143. $this->listener->setDefaultPath($this->destinationTestDir);
  144. // If there is an uploaded file, we process it
  145. $fileInfo = $this->generateUploadedFile();
  146. $image2 = new Image();
  147. $image2->setUseBasePath(true);
  148. $image2->setTitle('456');
  149. $this->listener->addEntityFileInfo($image2, $fileInfo);
  150. $this->em->persist($image2);
  151. $this->em->flush();
  152. $this->em->refresh($image2);
  153. // We need to set this again because of the recent refresh
  154. $firstFile = $image2->getFilePath();
  155. $this->assertPathEquals($image2->getPath($this->destinationTestDir).'/'.$fileInfo['name'], $image2->getFilePath());
  156. $this->assertTrue(is_file($firstFile));
  157. $this->assertEquals($fileInfo['size'], $image2->getSize());
  158. $this->assertEquals($fileInfo['type'], $image2->getMime());
  159. // UPDATE of an Uploadable Entity
  160. // We change the "uploaded" file
  161. $fileInfo['tmp_name'] = $this->testFile2;
  162. $fileInfo['name'] = $this->testFilename2;
  163. // We use a FileInfoInterface instance here
  164. $this->listener->addEntityFileInfo($image2, new FileInfoArray($fileInfo));
  165. $this->em->flush();
  166. $this->em->refresh($image2);
  167. $lastFile = $image2->getFilePath();
  168. $this->assertPathEquals($image2->getPath($this->destinationTestDir).'/'.$fileInfo['name'], $image2->getFilePath());
  169. $this->assertTrue(is_file($lastFile));
  170. // First file should be removed on update
  171. $this->assertFalse(is_file($firstFile));
  172. // REMOVAL of an Uploadable Entity
  173. $this->em->remove($image2);
  174. $this->em->flush();
  175. $this->assertFalse(is_file($lastFile));
  176. }
  177. public function testEntityWithUploadableEntities()
  178. {
  179. $artRepo = $this->em->getRepository(self::ARTICLE_CLASS);
  180. $article = new Article();
  181. $article->setTitle('Test');
  182. $file1 = new File();
  183. $file2 = new File();
  184. $file3 = new File();
  185. $article->addFile($file1);
  186. $article->addFile($file2);
  187. $article->addFile($file3);
  188. $filesArrayIndex = 'file';
  189. $fileInfo = $this->generateUploadedFile($filesArrayIndex);
  190. $fileInfo2 = $this->generateUploadedFile($filesArrayIndex);
  191. $fileInfo3 = $this->generateUploadedFile($filesArrayIndex);
  192. $this->listener->addEntityFileInfo($file1, $fileInfo);
  193. $this->listener->addEntityFileInfo($file2, $fileInfo2);
  194. $this->listener->addEntityFileInfo($file3, $fileInfo3);
  195. $this->em->persist($article);
  196. $this->em->flush();
  197. $art = $artRepo->findOneByTitle('Test');
  198. $files = $art->getFiles();
  199. $file1Path = $file1->getPath().'/'.$fileInfo['name'];
  200. $file2Path = $file2->getPath().'/'.$fileInfo['name'];
  201. $file3Path = $file3->getPath().'/'.$fileInfo['name'];
  202. $this->assertPathEquals($file1Path, $files[0]->getFilePath());
  203. $this->assertPathEquals($file2Path, $files[1]->getFilePath());
  204. $this->assertPathEquals($file3Path, $files[2]->getFilePath());
  205. }
  206. /**
  207. * @expectedException Gedmo\Exception\UploadableNoPathDefinedException
  208. */
  209. public function testNoPathDefinedOnEntityOrListenerThrowsException()
  210. {
  211. $file = new FileWithoutPath();
  212. $fileInfo = $this->generateUploadedFile();
  213. $this->listener->addEntityFileInfo($file, $fileInfo);
  214. $this->em->persist($file);
  215. $this->em->flush();
  216. }
  217. public function testNoPathDefinedOnEntityButDefinedOnListenerUsesDefaultPath()
  218. {
  219. // We set the default path on the listener
  220. $this->listener->setDefaultPath($this->destinationTestDir);
  221. $file = new FileWithoutPath();
  222. $fileInfo = $this->generateUploadedFile();
  223. $this->listener->addEntityFileInfo($file, $fileInfo);
  224. $this->em->persist($file);
  225. $this->em->flush();
  226. $this->em->refresh($file);
  227. $this->assertPathEquals($this->destinationTestFile, $file->getFilePath());
  228. }
  229. public function testCallbackIsCalledIfItsSetOnEntity()
  230. {
  231. $file = new File();
  232. $fileInfo = $this->generateUploadedFile();
  233. $this->listener->addEntityFileInfo($file, $fileInfo);
  234. $this->em->persist($file);
  235. $this->em->flush();
  236. $this->assertTrue($file->callbackWasCalled);
  237. }
  238. /**
  239. * @dataProvider uploadExceptionsProvider
  240. */
  241. public function testUploadExceptions($error, $exceptionClass)
  242. {
  243. $this->setExpectedException($exceptionClass);
  244. $file = new File();
  245. $fileInfo = $this->generateUploadedFile();
  246. $fileInfo['error'] = $error;
  247. $this->listener->addEntityFileInfo($file, $fileInfo);
  248. $this->em->persist($file);
  249. $this->em->flush();
  250. }
  251. public function testSettingAnotherDefaultFileInfoClass()
  252. {
  253. $fileInfoStubClass = 'Gedmo\Uploadable\Stub\FileInfoStub';
  254. $this->listener->setDefaultFileInfoClass($fileInfoStubClass);
  255. $file = new File();
  256. $fileInfo = $this->generateUploadedFile();
  257. $this->listener->addEntityFileInfo($file, $fileInfo);
  258. $fileInfo = $this->listener->getEntityFileInfo($file);
  259. $this->assertInstanceOf($fileInfoStubClass, $fileInfo);
  260. }
  261. public function testFileWithFilenameSha1Generator()
  262. {
  263. $file = new FileWithSha1Name();
  264. $fileInfo = $this->generateUploadedFile();
  265. $this->listener->addEntityFileInfo($file, $fileInfo);
  266. $this->em->persist($file);
  267. $this->em->flush();
  268. $this->em->refresh($file);
  269. $sha1String = substr($file->getFilePath(), strrpos($file->getFilePath(), '/') + 1);
  270. $sha1String = str_replace('.txt', '', $sha1String);
  271. $this->assertRegExp('/[a-z0-9]{40}/', $sha1String);
  272. }
  273. public function testFileWithFilenameAlphanumericGenerator()
  274. {
  275. $file = new FileWithAlphanumericName();
  276. $fileInfo = $this->generateUploadedFile('image', $this->testFile3, $this->testFilename3);
  277. $this->listener->addEntityFileInfo($file, $fileInfo);
  278. $this->em->persist($file);
  279. $this->em->flush();
  280. $this->em->refresh($file);
  281. $filename = substr($file->getFilePath(), strrpos($file->getFilePath(), '/') + 1);
  282. $this->assertEquals('test-3.txt', $filename);
  283. }
  284. public function testFileWithCustomFilenameGenerator()
  285. {
  286. $file = new FileWithCustomFilenameGenerator();
  287. $fileInfo = $this->generateUploadedFile();
  288. $this->listener->addEntityFileInfo($file, $fileInfo);
  289. $this->em->persist($file);
  290. $this->em->flush();
  291. $this->em->refresh($file);
  292. $filename = substr($file->getFilePath(), strrpos($file->getFilePath(), '/') + 1);
  293. $this->assertEquals('123.txt', $filename);
  294. }
  295. public function testUploadFileWithoutExtension()
  296. {
  297. $file = new File();
  298. $fileInfo = $this->generateUploadedFile('image', $this->testFileWithoutExt, $this->testFilenameWithoutExt);
  299. $this->listener->addEntityFileInfo($file, $fileInfo);
  300. $this->em->persist($file);
  301. $this->em->flush();
  302. $this->em->refresh($file);
  303. $filePath = $file->getPath().'/'.$fileInfo['name'];
  304. $this->assertPathEquals($filePath, $file->getFilePath());
  305. }
  306. /**
  307. * @expectedException Gedmo\Exception\UploadableFileAlreadyExistsException
  308. */
  309. public function testFileAlreadyExistsException()
  310. {
  311. $file = new Image();
  312. $file->setTitle('test');
  313. $fileInfo = $this->generateUploadedFile('image', $this->testFileWithoutExt, $this->testFilenameWithoutExt);
  314. $this->listener->addEntityFileInfo($file, $fileInfo);
  315. $this->em->persist($file);
  316. $this->em->flush();
  317. $this->listener->addEntityFileInfo($file, $fileInfo);
  318. $this->em->flush();
  319. }
  320. public function test_removeFile_ifItsNotAFileThenReturnFalse()
  321. {
  322. $this->assertFalse($this->listener->removeFile('non_existent_file'));
  323. }
  324. public function test_moveFile_usingAppendNumberOptionAppendsNumberToFilenameIfItAlreadyExists()
  325. {
  326. $file = new FileAppendNumber();
  327. $file2 = new FileAppendNumber();
  328. $file->setTitle('test');
  329. $file2->setTitle('test2');
  330. $fileInfo = $this->generateUploadedFile();
  331. $this->listener->addEntityFileInfo($file, $fileInfo);
  332. $this->em->persist($file);
  333. $this->em->flush();
  334. $this->listener->addEntityFileInfo($file2, $fileInfo);
  335. $this->em->persist($file2);
  336. $this->em->flush();
  337. $this->em->refresh($file2);
  338. $filename = substr($file2->getFilePath(), strrpos($file2->getFilePath(), '/') + 1);
  339. $this->assertEquals('test-2.txt', $filename);
  340. }
  341. /**
  342. * @expectedException Gedmo\Exception\UploadableUploadException
  343. */
  344. public function test_moveFile_ifUploadedFileCantBeMovedThrowException()
  345. {
  346. $this->listener->returnFalseOnMoveUploadedFile = true;
  347. $file = new Image();
  348. $file->setTitle('test');
  349. $fileInfo = $this->generateUploadedFile();
  350. $this->listener->addEntityFileInfo($file, $fileInfo);
  351. $this->em->persist($file);
  352. $this->em->flush();
  353. }
  354. /**
  355. * @expectedException RuntimeException
  356. */
  357. public function test_addEntityFileInfo_ifFileInfoIsNotValidThrowException()
  358. {
  359. $this->listener->addEntityFileInfo(new Image, 'invalidFileInfo');
  360. }
  361. /**
  362. * @expectedException RuntimeException
  363. */
  364. public function test_getEntityFileInfo_ifTheresNoFileInfoForEntityThrowException()
  365. {
  366. $this->listener->getEntityFileInfo(new Image);
  367. }
  368. /**
  369. * @expectedException Gedmo\Exception\UploadableMaxSizeException
  370. */
  371. public function test_fileExceedingMaximumAllowedSizeThrowsException()
  372. {
  373. // We set the default path on the listener
  374. $this->listener->setDefaultPath($this->destinationTestDir);
  375. $file = new FileWithMaxSize();
  376. $fileInfo = $this->generateUploadedFile();
  377. $this->listener->addEntityFileInfo($file, $fileInfo);
  378. $this->em->persist($file);
  379. $this->em->flush();
  380. }
  381. public function test_fileNotExceedingMaximumAllowedSizeDoesntThrowException()
  382. {
  383. // We set the default path on the listener
  384. $this->listener->setDefaultPath($this->destinationTestDir);
  385. $file = new FileWithMaxSize();
  386. $size = 0.0001;
  387. $fileInfo = $this->generateUploadedFile('image', false, false, array('size' => $size));
  388. $this->listener->addEntityFileInfo($file, $fileInfo);
  389. $this->em->persist($file);
  390. $this->em->flush();
  391. $this->em->refresh($file);
  392. $this->assertEquals($size, $file->getFileSize());
  393. }
  394. /**
  395. * @expectedException Gedmo\Exception\UploadableCouldntGuessMimeTypeException
  396. */
  397. public function test_ifMimeTypeGuesserCantResolveTypeThrowException()
  398. {
  399. // We set the default path on the listener
  400. $this->listener->setDefaultPath($this->destinationTestDir);
  401. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub(null));
  402. $file = new FileWithAllowedTypes();
  403. $fileInfo = $this->generateUploadedFile();
  404. $this->listener->addEntityFileInfo($file, $fileInfo);
  405. $this->em->persist($file);
  406. $this->em->flush();
  407. }
  408. /**
  409. * @expectedException Gedmo\Exception\UploadableInvalidMimeTypeException
  410. */
  411. public function test_allowedTypesOption_ifMimeTypeIsInvalidThrowException()
  412. {
  413. // We set the default path on the listener
  414. $this->listener->setDefaultPath($this->destinationTestDir);
  415. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub('text/css'));
  416. $file = new FileWithAllowedTypes();
  417. $fileInfo = $this->generateUploadedFile();
  418. $this->listener->addEntityFileInfo($file, $fileInfo);
  419. $this->em->persist($file);
  420. $this->em->flush();
  421. }
  422. public function test_allowedTypesOption_ifMimeTypeIsValidThenDontThrowException()
  423. {
  424. // We set the default path on the listener
  425. $this->listener->setDefaultPath($this->destinationTestDir);
  426. $file = new FileWithAllowedTypes();
  427. $fileInfo = $this->generateUploadedFile();
  428. $this->listener->addEntityFileInfo($file, $fileInfo);
  429. $this->em->persist($file);
  430. $this->em->flush();
  431. }
  432. /**
  433. * @expectedException Gedmo\Exception\UploadableInvalidMimeTypeException
  434. */
  435. public function test_disallowedTypesOption_ifMimeTypeIsInvalidThrowException()
  436. {
  437. // We set the default path on the listener
  438. $this->listener->setDefaultPath($this->destinationTestDir);
  439. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub('text/css'));
  440. $file = new FileWithDisallowedTypes();
  441. $fileInfo = $this->generateUploadedFile();
  442. $this->listener->addEntityFileInfo($file, $fileInfo);
  443. $this->em->persist($file);
  444. $this->em->flush();
  445. }
  446. public function test_disallowedTypesOption_ifMimeTypeIsValidThenDontThrowException()
  447. {
  448. // We set the default path on the listener
  449. $this->listener->setDefaultPath($this->destinationTestDir);
  450. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub('video/jpeg'));
  451. $file = new FileWithDisallowedTypes();
  452. $fileInfo = $this->generateUploadedFile();
  453. $this->listener->addEntityFileInfo($file, $fileInfo);
  454. $this->em->persist($file);
  455. $this->em->flush();
  456. }
  457. /**
  458. * @expectedException Gedmo\Exception\InvalidArgumentException
  459. * @dataProvider invalidFileInfoClassesProvider
  460. */
  461. public function test_setDefaultFileInfoClass_throwExceptionIfInvalidClassArePassed($class)
  462. {
  463. $this->listener->setDefaultFileInfoClass($class);
  464. }
  465. public function test_setDefaultFileInfoClass_setClassIfClassIsValid()
  466. {
  467. $validClass = 'Gedmo\\Uploadable\\FileInfo\\FileInfoArray';
  468. $this->listener->setDefaultFileInfoClass($validClass);
  469. $this->assertEquals($validClass, $this->listener->getDefaultFileInfoClass());
  470. }
  471. public function test_useGeneratedFilenameWhenAppendingNumbers()
  472. {
  473. // We set the default path on the listener
  474. $this->listener->setDefaultPath($this->destinationTestDir);
  475. $file = new FileWithAlphanumericName();
  476. $fileInfo = $this->generateUploadedFile('file', $this->testFileWithSpaces, $this->testFilenameWithSpaces);
  477. $this->listener->addEntityFileInfo($file, $fileInfo);
  478. $this->em->persist($file);
  479. $this->em->flush();
  480. $filePath = $file->getPath().'/'.str_replace(' ', '-', $fileInfo['name']);
  481. $this->assertPathEquals($filePath, $file->getFilePath());
  482. $file = new FileWithAlphanumericName();
  483. $this->listener->addEntityFileInfo($file, $fileInfo);
  484. $this->em->persist($file);
  485. $this->em->flush();
  486. $filePath = $file->getPath().'/'.str_replace(' ', '-', str_replace('.txt', '-2.txt', $fileInfo['name']));
  487. $this->assertPathEquals($filePath, $file->getFilePath());
  488. }
  489. // Data Providers
  490. public function invalidFileInfoClassesProvider()
  491. {
  492. return array(
  493. array(''),
  494. array(false),
  495. array(null),
  496. array('FakeFileInfo'),
  497. array(array()),
  498. array(new \DateTime())
  499. );
  500. }
  501. public function uploadExceptionsProvider()
  502. {
  503. return array(
  504. array(1, 'Gedmo\Exception\UploadableIniSizeException'),
  505. array(2, 'Gedmo\Exception\UploadableFormSizeException'),
  506. array(3, 'Gedmo\Exception\UploadablePartialException'),
  507. array(4, 'Gedmo\Exception\UploadableNoFileException'),
  508. array(6, 'Gedmo\Exception\UploadableNoTmpDirException'),
  509. array(7, 'Gedmo\Exception\UploadableCantWriteException'),
  510. array(8, 'Gedmo\Exception\UploadableExtensionException'),
  511. array(999, 'Gedmo\Exception\UploadableUploadException')
  512. );
  513. }
  514. // Util
  515. private function generateUploadedFile($index = 'image', $filePath = false, $filename = false, array $info = array())
  516. {
  517. $defaultInfo = array(
  518. 'tmp_name' => !$filePath ? $this->testFile : $filePath,
  519. 'name' => !$filename ? $this->testFilename : $filename,
  520. 'size' => $this->testFileSize,
  521. 'type' => $this->testFileMimeType,
  522. 'error' => 0
  523. );
  524. $info = array_merge($defaultInfo, $info);
  525. return $info;
  526. }
  527. protected function getUsedEntityFixtures()
  528. {
  529. return array(
  530. self::IMAGE_CLASS,
  531. self::ARTICLE_CLASS,
  532. self::FILE_CLASS,
  533. self::FILE_WITHOUT_PATH_CLASS,
  534. self::FILE_APPEND_NUMBER_CLASS,
  535. self::FILE_WITH_ALPHANUMERIC_NAME_CLASS,
  536. self::FILE_WITH_SHA1_NAME_CLASS,
  537. self::FILE_WITH_CUSTOM_FILENAME_GENERATOR_CLASS,
  538. self::FILE_WITH_MAX_SIZE_CLASS,
  539. self::FILE_WITH_ALLOWED_TYPES_CLASS,
  540. self::FILE_WITH_DISALLOWED_TYPES_CLASS
  541. );
  542. }
  543. private function clearFilesAndDirectories()
  544. {
  545. if (is_dir($this->destinationTestDir)) {
  546. $iter = new \DirectoryIterator($this->destinationTestDir);
  547. foreach ($iter as $fileInfo) {
  548. if (!$fileInfo->isDot()) {
  549. @unlink($fileInfo->getPathname());
  550. }
  551. }
  552. }
  553. }
  554. protected function assertPathEquals($expected, $path, $message = '')
  555. {
  556. $this->assertEquals($expected, $path, $message);
  557. }
  558. }
  559. class FakeFileInfo
  560. {
  561. }
  562. class FakeFilenameGenerator implements \Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorInterface
  563. {
  564. public static function generate($filename, $extension, $object = null)
  565. {
  566. return '123.txt';
  567. }
  568. }