From 06174a4f748919ed0121c84f197026196587563d Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 1 Nov 2008 22:42:59 +0000 Subject: Add a test to verify that we have the right preamble on files. --- core/tests/File_Structure_Test.php | 46 +++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'core/tests/File_Structure_Test.php') diff --git a/core/tests/File_Structure_Test.php b/core/tests/File_Structure_Test.php index c4f8bc01..f4e04b39 100644 --- a/core/tests/File_Structure_Test.php +++ b/core/tests/File_Structure_Test.php @@ -1,4 +1,4 @@ -assert_false( - preg_match("|/views/.*?(?getPathname()), + preg_match("|/views/.*?(?getPathname()), "{$file->getPathname()} should end in .html.php"); } } + + public function code_files_start_with_gallery_preamble_test() { + $dir = new GalleryCodeFilterIterator( + new RecursiveIteratorIterator(new RecursiveDirectoryIterator(DOCROOT))); + + $expected = $this->_get_preamble(__FILE__); + foreach ($dir as $file) { + if (preg_match("/views/", $file->getPathname())) { + // The preamble for views is a single line that prevents direct script access + $lines = file($file->getPathname()); + $this->assert_equal( + "\n", + $lines[0], + "in file: {$file->getPathname()}"); + } else if (preg_match("|\.php$|", $file->getPathname())) { + $actual = $this->_get_preamble($file->getPathname()); + if ($file->getPathName() == DOCROOT . "index.php") { + // index.php allows direct access, so modify our expectations for the first line + $index_expected = $expected; + $index_expected[0] = "assert_equal($index_expected, $actual, "in file: {$file->getPathname()}"); + } else { + // We expect the full preamble in regular PHP files + $actual = $this->_get_preamble($file->getPathname()); + $this->assert_equal($expected, $actual, "in file: {$file->getPathname()}"); + } + } + } + } + + private function _get_preamble($file) { + $lines = file($file); + $copy = array(); + for ($i = 0; $i < count($lines); $i++) { + $copy[] = rtrim($lines[$i]); + if (!strncmp($lines[$i], ' */', 3)) { + return $copy; + } + } + return $copy; + } } class GalleryCodeFilterIterator extends FilterIterator { -- cgit v1.2.3