PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Test/Case/Lib/MediaValidationTest.php

https://github.com/bmcclure/CakePHP-Media-Plugin
PHP | 305 lines | 205 code | 76 blank | 24 comment | 0 complexity | 177d3973c04c6c629960939468db5854 MD5 | raw file
  1. <?php
  2. /**
  3. * Media Validation Test Case File
  4. *
  5. * Copyright (c) 2007-2012 David Persson
  6. *
  7. * Distributed under the terms of the MIT License.
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * PHP 5
  11. * CakePHP 2
  12. *
  13. * @copyright 2007-2012 David Persson <davidpersson@gmx.de>
  14. * @link http://github.com/davidpersson/media
  15. * @package Media.Test.Case.Lib
  16. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  17. */
  18. App::uses('MediaValidation', 'Media.Lib');
  19. require_once dirname(dirname(__FILE__)) . DS . 'constants.php';
  20. require_once dirname(dirname(dirname(__FILE__))) . DS . 'Fixture' . DS . 'TestData.php';
  21. /**
  22. * Transfer Validation Test Case Class
  23. *
  24. * @package Media.Test.Case.Lib
  25. */
  26. class MediaValidationTest extends CakeTestCase {
  27. /**
  28. * @var TestData
  29. */
  30. public $Data;
  31. public function setUp() {
  32. parent::setUp();
  33. $this->Data = new TestData();
  34. }
  35. public function tearDown() {
  36. parent::tearDown();
  37. $this->Data->cleanUp();
  38. }
  39. public function testMimeType() {
  40. $check = 'image/png';
  41. $result = MediaValidation::mimeType($check);
  42. $this->assertTrue($result);
  43. $check = 'image/png';
  44. $result = MediaValidation::mimeType($check, array('image/png'));
  45. $this->assertFalse($result);
  46. $check = 'image/png';
  47. $result = MediaValidation::mimeType($check, array('image/png'), array('image/png'));
  48. $this->assertFalse($result);
  49. $check = 'in/val/id';
  50. $result = MediaValidation::mimeType($check);
  51. $this->assertFalse($result);
  52. $check = '';
  53. $result = MediaValidation::mimeType($check);
  54. $this->assertFalse($result);
  55. }
  56. public function testExtension() {
  57. $check = 'png';
  58. $result = MediaValidation::extension($check);
  59. $this->assertTrue($result);
  60. $check = 'tar.gz';
  61. $result = MediaValidation::extension($check, false, array('tar', 'gz'));
  62. $this->assertFalse($result);
  63. $check = 'tar.gz';
  64. $result = MediaValidation::extension($check, false, array('tar.gz'));
  65. $this->assertTrue($result);
  66. $check = 'png';
  67. $result = MediaValidation::extension($check, array('png'));
  68. $this->assertFalse($result);
  69. $check = 'png';
  70. $result = MediaValidation::extension($check, array('png'), array('png'));
  71. $this->assertFalse($result);
  72. $check = 'in.va.lid';
  73. $result = MediaValidation::extension($check);
  74. $this->assertFalse($result);
  75. $check = '.inva.lid';
  76. $result = MediaValidation::extension($check);
  77. $this->assertFalse($result);
  78. $check = '';
  79. $result = MediaValidation::extension($check);
  80. $this->assertFalse($result);
  81. $check = false;
  82. $result = MediaValidation::extension($check);
  83. $this->assertFalse($result);
  84. $check = true;
  85. $result = MediaValidation::extension($check);
  86. $this->assertFalse($result);
  87. $check = true;
  88. $result = MediaValidation::extension($check);
  89. $this->assertFalse($result);
  90. $deny = array('bin', 'class', 'dll', 'dms', 'exe', 'lha');
  91. $allow = array('pdf');
  92. $check = 'tmp';
  93. $result = MediaValidation::extension($check, $deny, $allow);
  94. $this->assertFalse($result);
  95. $check = 'tmp';
  96. $result = MediaValidation::extension($check);
  97. $this->assertTrue($result);
  98. $deny = array('bin', 'class', 'dll', 'dms', 'exe', 'lha');
  99. $allow = array('pdf', 'tmp');
  100. $check = 'tmp';
  101. $result = MediaValidation::extension($check, $deny, $allow);
  102. $this->assertTrue($result);
  103. $deny = array('bin', 'class', 'dll', 'dms', 'exe', 'lha');
  104. $allow = array('*');
  105. $check = 'tmp';
  106. $result = MediaValidation::extension($check, $deny, $allow);
  107. $this->assertTrue($result);
  108. }
  109. public function testSize() {
  110. $result = MediaValidation::size('1M', '2M');
  111. $this->assertTrue($result);
  112. $result = MediaValidation::size('1K', '2M');
  113. $this->assertTrue($result);
  114. $result = MediaValidation::size('1M', '1K');
  115. $this->assertFalse($result);
  116. $result = MediaValidation::size('1048576', '2M');
  117. $this->assertTrue($result);
  118. $result = MediaValidation::size(1048576, '2M');
  119. $this->assertTrue($result);
  120. $result = MediaValidation::size('1M', '1M');
  121. $this->assertTrue($result);
  122. $result = MediaValidation::size('1048576', '1M');
  123. $this->assertTrue($result);
  124. $result = MediaValidation::size(1048576, 10);
  125. $this->assertFalse($result);
  126. $result = MediaValidation::size('', '2M');
  127. $this->assertFalse($result);
  128. }
  129. public function testLocation() {
  130. $result = MediaValidation::location(TMP);
  131. $this->assertFalse($result);
  132. $result = MediaValidation::location(TMP, true);
  133. $this->assertTrue($result);
  134. $topMostDir = current(explode(DS, TMP)) . DS;
  135. $result = MediaValidation::location(TMP, array($topMostDir));
  136. $this->assertTrue($result);
  137. $result = MediaValidation::location(TMP . DS . DS . DS, array($topMostDir));
  138. $this->assertTrue($result);
  139. $result = MediaValidation::location(TMP . DS . 'file.png', array($topMostDir));
  140. $this->assertTrue($result);
  141. $result = MediaValidation::location(TMP, array(TMP . 'subdir'));
  142. $this->assertFalse($result);
  143. $result = MediaValidation::location('http://cakeforge.org', true);
  144. $this->assertTrue($result);
  145. $result = MediaValidation::location('http://cakeforge.org');
  146. $this->assertFalse($result);
  147. $result = MediaValidation::location('http://cakeforge.org', array(TMP));
  148. $this->assertFalse($result);
  149. $result = MediaValidation::location('http://cakeforge.org', array(TMP, 'http://'));
  150. $this->assertTrue($result);
  151. $result = MediaValidation::location('http://cakeforge.org', 'http://rosa');
  152. $this->assertFalse($result);
  153. $result = MediaValidation::location('http://cakeforge.org', 'http://cakeforge.org');
  154. $this->assertTrue($result);
  155. $result = MediaValidation::location('http://cakeforge.org/bla/?x=?$§c $%.org', 'http://cakeforge.org');
  156. $this->assertFalse($result);
  157. $result = MediaValidation::location('http://cakeforge.org/bla', 'http://cakeforge.org');
  158. $this->assertTrue($result);
  159. $result = MediaValidation::location('http://cakeforge.org/bla?x=do', 'http://cakeforge.org');
  160. $this->assertTrue($result);
  161. }
  162. public function testAccess() {
  163. $result = MediaValidation::access('0444', 'r');
  164. $this->assertTrue($result);
  165. $result = MediaValidation::access(0444, 'r');
  166. $this->assertTrue($result);
  167. $result = MediaValidation::access('0004', 'r');
  168. $this->assertTrue($result);
  169. $result = MediaValidation::access('0111', 'r');
  170. $this->assertFalse($result);
  171. $result = MediaValidation::access('0222', 'w');
  172. $this->assertTrue($result);
  173. $result = MediaValidation::access('0002', 'w');
  174. $this->assertTrue($result);
  175. $result = MediaValidation::access('0111', 'w');
  176. $this->assertFalse($result);
  177. }
  178. public function testPermission() {
  179. $result = MediaValidation::permission('0111');
  180. $this->assertFalse($result);
  181. $result = MediaValidation::permission(0111);
  182. $this->assertFalse($result);
  183. $result = MediaValidation::permission('0111', '-x');
  184. $this->assertFalse($result);
  185. $result = MediaValidation::permission('0111', '-x');
  186. $this->assertFalse($result);
  187. $result = MediaValidation::permission('0000', '-x');
  188. $this->assertTrue($result);
  189. $result = MediaValidation::permission('0666', '-x');
  190. $this->assertTrue($result);
  191. }
  192. public function testFile() {
  193. $file = __FILE__;
  194. $result = MediaValidation::file($file);
  195. $this->assertTrue($result);
  196. $file = $this->Data->getFile('image-jpg.jpg');
  197. $result = MediaValidation::file($file, false);
  198. $this->assertTrue($result);
  199. $file = $this->Data->settings['base'] . 'i-am-not-a-file.png';
  200. $result = MediaValidation::file($file);
  201. $this->assertFalse($result);
  202. $file = TMP;
  203. $result = MediaValidation::file($file);
  204. $this->assertFalse($result);
  205. $file = TMP;
  206. $result = MediaValidation::file($file, false);
  207. $this->assertTrue($result);
  208. }
  209. public function testFolder() {
  210. $file = dirname(__FILE__);
  211. $result = MediaValidation::folder($file);
  212. $this->assertTrue($result);
  213. $file = $this->Data->getFile('image-jpg.jpg');
  214. $result = MediaValidation::folder($file, false);
  215. $this->assertTrue($result);
  216. $file = $this->Data->settings['base'] . 'i-am-not-a-file.png';
  217. $result = MediaValidation::folder($file);
  218. $this->assertFalse($result);
  219. $file = TMP;
  220. $result = MediaValidation::folder($file);
  221. $this->assertTrue($result);
  222. $file = TMP;
  223. $result = MediaValidation::folder($file, false);
  224. $this->assertTrue($result);
  225. $file = TMP . DS . DS . DS;
  226. $result = MediaValidation::folder($file, false);
  227. $this->assertTrue($result);
  228. }
  229. }