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

/modules/gallery/tests/File_Structure_Test.php

http://github.com/gallery/gallery3
PHP | 340 lines | 274 code | 21 blank | 45 comment | 46 complexity | 60c182f99252103e25eded60275bb564 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2013 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. require_once(MODPATH . "gallery/tests/Gallery_Filters.php");
  21. class File_Structure_Test extends Gallery_Unit_Test_Case {
  22. public function no_trailing_closing_php_tag_test() {
  23. $dir = new GalleryCodeFilterIterator(
  24. new RecursiveIteratorIterator(new RecursiveDirectoryIterator(DOCROOT)));
  25. $count = 0;
  26. foreach ($dir as $file) {
  27. $count++;
  28. if (!preg_match("|\.html\.php$|", $file->getPathname())) {
  29. $this->assert_false(
  30. preg_match('/\?\>\s*$/', file_get_contents($file)),
  31. "{$file->getPathname()} ends in ?>");
  32. }
  33. }
  34. $this->assert_true($count > 500, "We should have analyzed at least this 500 files");
  35. $this->assert_true($count < 1000, "We shouldn't be shipping 1000 files!");
  36. }
  37. public function view_files_correct_suffix_test() {
  38. $dir = new GalleryCodeFilterIterator(
  39. new RecursiveIteratorIterator(new RecursiveDirectoryIterator(DOCROOT)));
  40. foreach ($dir as $file) {
  41. if (strpos($file, "views/kohana/error.php")) {
  42. continue;
  43. }
  44. if (strpos($file, "views")) {
  45. $this->assert_true(
  46. preg_match("#/views/.*?\.(html|mrss|txt|json)\.php$#", $file->getPathname()),
  47. "{$file->getPathname()} should end in .{html,mrss,txt,json}.php");
  48. }
  49. }
  50. }
  51. public function no_windows_line_endings_test() {
  52. $dir = new GalleryCodeFilterIterator(
  53. new RecursiveIteratorIterator(new RecursiveDirectoryIterator(DOCROOT)));
  54. foreach ($dir as $file) {
  55. if (preg_match("/\.(php|css|html|js)$/", $file)) {
  56. foreach (file($file) as $line) {
  57. $this->assert_true(substr($line, -2) != "\r\n", "$file has windows style line endings");
  58. }
  59. }
  60. }
  61. }
  62. private function _check_view_preamble($path, &$errors) {
  63. $expected_2 = null;
  64. // The preamble for views is a single line that prevents direct script access
  65. if (strpos($path, SYSPATH) === 0) {
  66. // Kohana preamble
  67. $expected = "<?php defined('SYSPATH') OR die('No direct access allowed.'); ?>\n";
  68. $expected_2 = "<?php defined('SYSPATH') OR die('No direct access allowed.');\n"; // error.php
  69. } else {
  70. // Gallery preamble
  71. // @todo use the same preamble for both!
  72. $expected = "<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n";
  73. }
  74. $fp = fopen($path, "r");
  75. $actual = fgets($fp);
  76. fclose($fp);
  77. if ($expected != $actual && $expected_2 != $actual) {
  78. $errors[] = "$path:1\n expected:\n\t$expected\n actual:\n\t$actual";
  79. }
  80. }
  81. private function _check_php_preamble($path, &$errors) {
  82. $expected_2 = null; $expected_3 = null; $expected_4 = null;
  83. if (strpos($path, SYSPATH) === 0 ||
  84. strpos($path, MODPATH . "unit_test") === 0) {
  85. // Kohana: we only care about the first line
  86. $fp = fopen($path, "r");
  87. $actual = array(fgets($fp));
  88. fclose($fp);
  89. $expected = array("<?php defined('SYSPATH') OR die('No direct script access.');\n");
  90. $expected_2 = array("<?php defined('SYSPATH') OR die('No direct access allowed.');\n");
  91. $expected_3 = array("<?php defined('SYSPATH') or die('No direct access allowed.');\n");
  92. $expected_4 = array("<?php defined('SYSPATH') or die('No direct script access.');\n");
  93. } else if (strpos($path, MODPATH . "forge") === 0 ||
  94. strpos($path, MODPATH . "exif/lib") === 0 ||
  95. strpos($path, MODPATH . "gallery/vendor/joomla") === 0 ||
  96. strpos($path, MODPATH . "gallery_unit_test/vendor") === 0 ||
  97. strpos($path, MODPATH . "gallery/lib/HTMLPurifier") === 0 ||
  98. $path == MODPATH . "user/lib/PasswordHash.php" ||
  99. $path == DOCROOT . "var/database.php") {
  100. // 3rd party module security-only preambles, similar to Gallery's
  101. $expected = array("<?php defined(\"SYSPATH\") or die(\"No direct access allowed.\");\n");
  102. $expected_2 = array("<?php defined('SYSPATH') OR die('No direct access allowed.');\n");
  103. $expected_3 = array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\");\n");
  104. $fp = fopen($path, "r");
  105. $actual = array(fgets($fp));
  106. fclose($fp);
  107. } else if (strpos($path, DOCROOT . "var/logs") === 0) {
  108. // var/logs has the kohana one-liner preamble
  109. $expected = array("<?php defined('SYSPATH') or die('No direct script access.'); ?>\n");
  110. $fp = fopen($path, "r");
  111. $actual = array(fgets($fp));
  112. fclose($fp);
  113. } else if (strpos($path, DOCROOT . "var") === 0) {
  114. // Anything else under var has the Gallery one-liner
  115. $expected = array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n");
  116. $fp = fopen($path, "r");
  117. $actual = array(fgets($fp));
  118. fclose($fp);
  119. } else {
  120. // Gallery: we care about the entire copyright
  121. $actual = $this->_get_preamble($path);
  122. $expected = array(
  123. "<?php defined(\"SYSPATH\") or die(\"No direct script access.\");",
  124. "/**",
  125. " * Gallery - a web based photo album viewer and editor",
  126. " * Copyright (C) 2000-2013 Bharat Mediratta",
  127. " *",
  128. " * This program is free software; you can redistribute it and/or modify",
  129. " * it under the terms of the GNU General Public License as published by",
  130. " * the Free Software Foundation; either version 2 of the License, or (at",
  131. " * your option) any later version.",
  132. " *",
  133. " * This program is distributed in the hope that it will be useful, but",
  134. " * WITHOUT ANY WARRANTY; without even the implied warranty of",
  135. " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU",
  136. " * General Public License for more details.",
  137. " *",
  138. " * You should have received a copy of the GNU General Public License",
  139. " * along with this program; if not, write to the Free Software",
  140. " * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.",
  141. " */",
  142. );
  143. }
  144. if ($expected != $actual && $expected_2 != $actual && $expected_3 != $actual && $expected_4 != $actual) {
  145. $errors[] = "$path:1\n expected\n\t" . join("\n\t", $expected) .
  146. "\n actual:\n\t" . join("\n\t", $actual);
  147. }
  148. }
  149. public function code_files_start_with_preamble_test() {
  150. $dir = new PhpCodeFilterIterator(
  151. new RecursiveIteratorIterator(new RecursiveDirectoryIterator(DOCROOT)));
  152. $errors = array();
  153. foreach ($dir as $file) {
  154. $path = $file->getPathname();
  155. switch ($path) {
  156. case DOCROOT . "installer/database_config.php":
  157. case DOCROOT . "installer/init_var.php":
  158. // Special case views
  159. $this->_check_view_preamble($path, $errors);
  160. break;
  161. case DOCROOT . "index.php":
  162. case DOCROOT . "installer/index.php":
  163. // Front controllers
  164. break;
  165. case DOCROOT . "lib/uploadify/uploadify.swf.php":
  166. case DOCROOT . "lib/uploadify/uploadify.allglyphs.swf.php":
  167. case DOCROOT . "lib/mediaelementjs/flashmediaelement.swf.php":
  168. // SWF wrappers - directly accessible
  169. break;
  170. case DOCROOT . "local.php":
  171. // Special case optional file, not part of the codebase
  172. break;
  173. default:
  174. if (preg_match("/views/", $path)) {
  175. $this->_check_view_preamble($path, $errors);
  176. } else {
  177. $this->_check_php_preamble($path, $errors);
  178. }
  179. }
  180. }
  181. if ($errors) {
  182. $this->assert_false(true, "Preamble errors:\n" . join("\n", $errors));
  183. }
  184. }
  185. public function no_tabs_in_our_code_test() {
  186. $dir = new PhpCodeFilterIterator(
  187. new GalleryCodeFilterIterator(
  188. new RecursiveIteratorIterator(
  189. new RecursiveDirectoryIterator(DOCROOT))));
  190. $errors = array();
  191. foreach ($dir as $file) {
  192. $file_as_string = file_get_contents($file);
  193. if (preg_match('/\t/', $file_as_string)) {
  194. foreach (explode("\n", $file_as_string) as $l => $line) {
  195. if (preg_match('/\t/', $line)) {
  196. $errors[] = "$file:$l has tab(s) ($line)";
  197. }
  198. }
  199. }
  200. $file_as_string = null;
  201. }
  202. if ($errors) {
  203. $this->assert_false(true, "tab(s) found:\n" . join("\n", $errors));
  204. }
  205. }
  206. private function _get_preamble($file) {
  207. $lines = file($file);
  208. $copy = array();
  209. for ($i = 0; $i < count($lines); $i++) {
  210. $copy[] = rtrim($lines[$i]);
  211. if (!strncmp($lines[$i], ' */', 3)) {
  212. return $copy;
  213. }
  214. }
  215. return $copy;
  216. }
  217. public function helpers_are_static_test() {
  218. $dir = new PhpCodeFilterIterator(
  219. new GalleryCodeFilterIterator(
  220. new RecursiveIteratorIterator(
  221. new RecursiveDirectoryIterator(DOCROOT))));
  222. foreach ($dir as $file) {
  223. if (basename(dirname($file)) == "helpers") {
  224. foreach (file($file) as $line) {
  225. $this->assert_true(
  226. !preg_match("/\sfunction\s.*\(/", $line) ||
  227. preg_match("/^\s*(private static function _|static function)/", $line),
  228. "should be \"static function foo\" or \"private static function _foo\":\n" .
  229. "$file\n$line\n");
  230. }
  231. }
  232. }
  233. }
  234. public function module_info_is_well_formed_test() {
  235. $info_files = array_merge(
  236. glob("modules/*/module.info"),
  237. glob("themes/*/module.info"));
  238. $errors = array();
  239. foreach ($info_files as $file) {
  240. foreach (file($file) as $line) {
  241. $parts = explode("=", $line, 2);
  242. if (isset($parts[1])) {
  243. $values[trim($parts[0])] = trim($parts[1]);
  244. }
  245. }
  246. $module = dirname($file);
  247. // Certain keys must exist
  248. foreach (array("name", "description", "version") as $key) {
  249. if (!array_key_exists($key, $values)) {
  250. $errors[] = "$module: missing key $key";
  251. }
  252. }
  253. // Any values containing spaces must be quoted
  254. foreach ($values as $key => $value) {
  255. if (strpos($value, " ") !== false && !preg_match('/^".*"$/', $value)) {
  256. $errors[] = "$module: value for $key must be quoted";
  257. }
  258. }
  259. // The file must parse
  260. if (!is_array(parse_ini_file($file))) {
  261. $errors[] = "$module: info file is not parseable";
  262. }
  263. }
  264. if ($errors) {
  265. $this->assert_true(false, $errors);
  266. }
  267. }
  268. public function all_public_functions_in_test_files_end_in_test() {
  269. // Who tests the tests? :-) (ref: http://www.xkcd.com/1163)
  270. $dir = new PhpCodeFilterIterator(
  271. new GalleryCodeFilterIterator(
  272. new RecursiveIteratorIterator(
  273. new RecursiveDirectoryIterator(DOCROOT))));
  274. foreach ($dir as $file) {
  275. $scan = 0;
  276. if (basename(dirname($file)) == "tests") {
  277. foreach (file($file) as $line) {
  278. if (!substr($file, -9, 9) == "_Test.php") {
  279. continue;
  280. }
  281. if (preg_match("/class.*extends.*Gallery_Unit_Test_Case/", $line)) {
  282. $scan = 1;
  283. } else if (preg_match("/class.*extends/", $line)) {
  284. $scan = 0;
  285. }
  286. if ($scan) {
  287. if (preg_match("/^\s*public\s+function/", $line)) {
  288. $this->assert_true(
  289. preg_match("/^\s*public\s+function (setup|teardown|.*_test)\(\) {/", $line),
  290. "public functions must end in _test:\n$file\n$line\n");
  291. }
  292. }
  293. }
  294. }
  295. }
  296. }
  297. public function no_extra_spaces_at_end_of_line_test() {
  298. $dir = new GalleryCodeFilterIterator(
  299. new RecursiveIteratorIterator(new RecursiveDirectoryIterator(DOCROOT)));
  300. $errors = "";
  301. foreach ($dir as $file) {
  302. if (preg_match("/\.(php|css|html|js)$/", $file)) {
  303. foreach (file($file) as $line_num => $line) {
  304. if ((substr($line, -2) == " \n") || (substr($line, -1) == " ")) {
  305. $errors .= "$file at line " . ($line_num + 1) . "\n";
  306. }
  307. }
  308. }
  309. }
  310. $this->assert_true(empty($errors), "Extra spaces at end of line found at:\n$errors");
  311. }
  312. }