PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/ZendTest/Filter/File/RenameUploadTest.php

https://bitbucket.org/gencer/zf2
PHP | 359 lines | 219 code | 50 blank | 90 comment | 2 complexity | c02bf844bcbc01b34459211b1d290130 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Filter\File;
  10. use Zend\Filter\File\RenameUpload as FileRenameUpload;
  11. /**
  12. * @group Zend_Filter
  13. */
  14. class RenameUploadTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * Path to test files
  18. *
  19. * @var string
  20. */
  21. protected $_filesPath;
  22. /**
  23. * Testfile
  24. *
  25. * @var string
  26. */
  27. protected $_oldFile;
  28. /**
  29. * Testfile
  30. *
  31. * @var string
  32. */
  33. protected $_newFile;
  34. /**
  35. * Testdirectory
  36. *
  37. * @var string
  38. */
  39. protected $_newDir;
  40. /**
  41. * Testfile in Testdirectory
  42. *
  43. * @var string
  44. */
  45. protected $_newDirFile;
  46. /**
  47. * Sets the path to test files
  48. *
  49. * @return void
  50. */
  51. public function setUp()
  52. {
  53. $this->_filesPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'RenameUploadTest';
  54. $this->_newDir = $this->_filesPath . DIRECTORY_SEPARATOR . '_testDir2';
  55. $this->tearDown();
  56. mkdir($this->_filesPath);
  57. mkdir($this->_newDir);
  58. $this->_oldFile = $this->_filesPath . DIRECTORY_SEPARATOR . 'testfile.txt';
  59. $this->_newFile = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile.xml';
  60. $this->_newDirFile = $this->_newDir . DIRECTORY_SEPARATOR . 'testfile.txt';
  61. touch($this->_oldFile);
  62. }
  63. /**
  64. * Sets the path to test files
  65. *
  66. * @return void
  67. */
  68. public function tearDown()
  69. {
  70. $this->removeDir($this->_newDir);
  71. $this->removeDir($this->_filesPath);
  72. }
  73. protected function removeDir($dir)
  74. {
  75. if (! is_dir($dir)) {
  76. return;
  77. }
  78. foreach (glob($dir . DIRECTORY_SEPARATOR . '*') as $file) {
  79. if (is_file($file)) {
  80. unlink($file);
  81. }
  82. }
  83. rmdir($dir);
  84. }
  85. /**
  86. * Test single parameter filter
  87. *
  88. * @return void
  89. */
  90. public function testThrowsExceptionWithNonUploadedFile()
  91. {
  92. $filter = new FileRenameUpload($this->_newFile);
  93. $this->assertEquals($this->_newFile, $filter->getTarget());
  94. $this->assertEquals('falsefile', $filter('falsefile'));
  95. $this->setExpectedException(
  96. 'Zend\Filter\Exception\RuntimeException', 'could not be renamed'
  97. );
  98. $this->assertEquals($this->_newFile, $filter($this->_oldFile));
  99. }
  100. /**
  101. * @return void
  102. */
  103. public function testOptions()
  104. {
  105. $filter = new FileRenameUpload($this->_newFile);
  106. $this->assertEquals($this->_newFile, $filter->getTarget());
  107. $this->assertFalse($filter->getUseUploadName());
  108. $this->assertFalse($filter->getOverwrite());
  109. $this->assertFalse($filter->getRandomize());
  110. $filter = new FileRenameUpload(array(
  111. 'target' => $this->_oldFile,
  112. 'use_upload_name' => true,
  113. 'overwrite' => true,
  114. 'randomize' => true,
  115. ));
  116. $this->assertEquals($this->_oldFile, $filter->getTarget());
  117. $this->assertTrue($filter->getUseUploadName());
  118. $this->assertTrue($filter->getOverwrite());
  119. $this->assertTrue($filter->getRandomize());
  120. }
  121. /**
  122. * @return void
  123. */
  124. public function testStringConstructorParam()
  125. {
  126. $filter = new RenameUploadMock($this->_newFile);
  127. $this->assertEquals($this->_newFile, $filter->getTarget());
  128. $this->assertEquals($this->_newFile, $filter($this->_oldFile));
  129. $this->assertEquals('falsefile', $filter('falsefile'));
  130. }
  131. /**
  132. * @return void
  133. */
  134. public function testStringConstructorWithFilesArray()
  135. {
  136. $filter = new RenameUploadMock($this->_newFile);
  137. $this->assertEquals($this->_newFile, $filter->getTarget());
  138. $this->assertEquals(
  139. array(
  140. 'tmp_name' => $this->_newFile,
  141. 'name' => $this->_newFile,
  142. ),
  143. $filter(array(
  144. 'tmp_name' => $this->_oldFile,
  145. 'name' => $this->_newFile,
  146. ))
  147. );
  148. $this->assertEquals('falsefile', $filter('falsefile'));
  149. }
  150. /**
  151. * @return void
  152. */
  153. public function testArrayConstructorParam()
  154. {
  155. $filter = new RenameUploadMock(array(
  156. 'target' => $this->_newFile,
  157. ));
  158. $this->assertEquals($this->_newFile, $filter->getTarget());
  159. $this->assertEquals($this->_newFile, $filter($this->_oldFile));
  160. $this->assertEquals('falsefile', $filter('falsefile'));
  161. }
  162. /**
  163. * @return void
  164. */
  165. public function testConstructTruncatedTarget()
  166. {
  167. $filter = new FileRenameUpload('*');
  168. $this->assertEquals('*', $filter->getTarget());
  169. $this->assertEquals($this->_oldFile, $filter($this->_oldFile));
  170. $this->assertEquals('falsefile', $filter('falsefile'));
  171. }
  172. /**
  173. * @return void
  174. */
  175. public function testTargetDirectory()
  176. {
  177. $filter = new RenameUploadMock($this->_newDir);
  178. $this->assertEquals($this->_newDir, $filter->getTarget());
  179. $this->assertEquals($this->_newDirFile, $filter($this->_oldFile));
  180. $this->assertEquals('falsefile', $filter('falsefile'));
  181. }
  182. /**
  183. * @return void
  184. */
  185. public function testOverwriteWithExistingFile()
  186. {
  187. $filter = new RenameUploadMock(array(
  188. 'target' => $this->_newFile,
  189. 'overwrite' => true,
  190. ));
  191. copy($this->_oldFile, $this->_newFile);
  192. $this->assertEquals($this->_newFile, $filter->getTarget());
  193. $this->assertEquals($this->_newFile, $filter($this->_oldFile));
  194. }
  195. /**
  196. * @return void
  197. */
  198. public function testCannotOverwriteExistingFile()
  199. {
  200. $filter = new RenameUploadMock(array(
  201. 'target' => $this->_newFile,
  202. 'overwrite' => false,
  203. ));
  204. copy($this->_oldFile, $this->_newFile);
  205. $this->assertEquals($this->_newFile, $filter->getTarget());
  206. $this->assertFalse($filter->getOverwrite());
  207. $this->setExpectedException(
  208. 'Zend\Filter\Exception\InvalidArgumentException', 'already exists'
  209. );
  210. $this->assertEquals($this->_newFile, $filter($this->_oldFile));
  211. }
  212. /**
  213. * @return void
  214. */
  215. public function testGetRandomizedFile()
  216. {
  217. $fileNoExt = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile';
  218. $filter = new RenameUploadMock(array(
  219. 'target' => $this->_newFile,
  220. 'randomize' => true,
  221. ));
  222. $this->assertRegExp('#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}\.xml#', $filter($this->_oldFile));
  223. }
  224. public function testGetFileWithOriginalExtension()
  225. {
  226. $fileNoExt = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile';
  227. $filter = new RenameUploadMock(array(
  228. 'target' => $this->_newFile,
  229. 'use_upload_extension' => true,
  230. 'randomize' => false,
  231. ));
  232. $oldFilePathInfo = pathinfo($this->_oldFile);
  233. $this->assertRegExp(
  234. '#' . str_replace('\\', '\\\\', $fileNoExt) . '.'.$oldFilePathInfo['extension'].'#',
  235. $filter($this->_oldFile)
  236. );
  237. }
  238. public function testGetRandomizedFileWithOriginalExtension()
  239. {
  240. $fileNoExt = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile';
  241. $filter = new RenameUploadMock(array(
  242. 'target' => $this->_newFile,
  243. 'use_upload_extension' => true,
  244. 'randomize' => true,
  245. ));
  246. $oldFilePathInfo = pathinfo($this->_oldFile);
  247. $this->assertRegExp(
  248. '#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}\.'.$oldFilePathInfo['extension'].'#',
  249. $filter($this->_oldFile)
  250. );
  251. }
  252. /**
  253. * @return void
  254. */
  255. public function testGetRandomizedFileWithoutExtension()
  256. {
  257. $fileNoExt = $this->_filesPath . DIRECTORY_SEPARATOR . 'newfile';
  258. $filter = new RenameUploadMock(array(
  259. 'target' => $fileNoExt,
  260. 'randomize' => true,
  261. ));
  262. $this->assertRegExp('#' . str_replace('\\', '\\\\', $fileNoExt) . '_.{13}#', $filter($this->_oldFile));
  263. }
  264. /**
  265. * @return void
  266. */
  267. public function testInvalidConstruction()
  268. {
  269. $this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'Invalid target');
  270. $filter = new FileRenameUpload(1234);
  271. }
  272. /**
  273. * @return void
  274. */
  275. public function testCanFilterMultipleTimesWithSameResult()
  276. {
  277. $filter = new RenameUploadMock(array(
  278. 'target' => $this->_newFile,
  279. 'randomize' => true,
  280. ));
  281. $firstResult = $filter($this->_oldFile);
  282. $this->assertContains('newfile', $firstResult);
  283. $secondResult = $filter($this->_oldFile);
  284. $this->assertSame($firstResult, $secondResult);
  285. }
  286. public function returnUnfilteredDataProvider()
  287. {
  288. return array(
  289. array(null),
  290. array(new \stdClass()),
  291. array(array(
  292. $this->_oldFile,
  293. 'something invalid'
  294. ))
  295. );
  296. }
  297. /**
  298. * @dataProvider returnUnfilteredDataProvider
  299. * @return void
  300. */
  301. public function testReturnUnfiltered($input)
  302. {
  303. $filter = new RenameUploadMock(array(
  304. 'target' => $this->_newFile,
  305. 'randomize' => true,
  306. ));
  307. $this->assertEquals($input, $filter($input));
  308. }
  309. }