3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
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.
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.
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.
21
GalleryCoreApi::requireOnce('modules/core/classes/GalleryTemplate.class');
24
* Test Template functionality
25
* @package GalleryCore
27
* @author Bharat Mediratta <bharat@menalto.com>
28
* @version $Revision: 15513 $
30
class TemplateAuditTest extends CodeAuditTestCase {
32
function TemplateAuditTest($methodName) {
35
$this->CodeAuditTestCase($methodName);
37
$platform =& $gallery->getPlatform();
38
$this->_lineEnding = $platform->getLineEnding();
40
$this->_revisionPattern = ' \* \$Revision(:.+)?\$';
42
$lines[] = $this->_revisionPattern;
43
$lines[] = ' \* If you want to customize this file, do not edit it directly since ' .
45
$lines[] = ' \* may overwrite it. Instead, copy it into a new directory called ' .
46
'"local" and edit that';
47
$lines[] = ' \* version. Gallery will look for that file first and use it if it exists.';
50
$this->_revisionPlaceHolder = ' * REVISION PLACEHOLDER';
51
$this->_headerPattern = join("(\r\n|\n)", $lines);
52
$this->_headerLines = $lines;
53
$this->_expectedHeaderLines = $lines;
54
array_splice($this->_expectedHeaderLines, 1, 1, $this->_revisionPlaceHolder);
56
$this->_exceptions = array();
57
$this->_exceptions['header'] = array();
59
$this->_exception['hiddenFormVars'] = array(
60
'modules/digibug/templates/DigibugPrintPhotos.tpl',
61
'modules/shutterfly/templates/PrintPhotos.tpl',
62
'modules/uploadapplet/templates/ItemAddUploadApplet.tpl'
65
/* Get rid of backslashes so that we can perform string compares later */
66
for ($i = 0; $i < sizeof($this->_expectedHeaderLines); $i++) {
67
$this->_expectedHeaderLines[$i] = stripslashes($this->_expectedHeaderLines[$i]);
70
$this->_errorCount = 0;
74
* @see CodeAuditTestCase::shouldCheckFile
76
function shouldCheckFile($fileName) {
77
if (preg_match('#Storage.schema\.tpl$|tools/repository/.*tpl#', $fileName)
78
|| !parent::shouldCheckFile($fileName)) {
81
if (substr($fileName, -4) == '.tpl') {
89
* @see CodeAuditTestCase::checkFile
91
function checkFile($fileName, $buffer) {
92
$truncatedFileName = substr($fileName,
93
strlen(dirname(dirname(dirname(dirname($fileName)))))+1);
95
$this->_checkHiddenFormVars($truncatedFileName, $buffer);
97
if (preg_match('/^' . $this->_headerPattern . '/', $buffer)) {
100
/* Grab the expected number of lines and compare them */
101
$actual = split("(\r\n|\n)", $buffer, sizeof($this->_expectedHeaderLines)+1);
102
array_splice($actual, sizeof($this->_expectedHeaderLines), 1);
104
/* Get rid of the revision line, since we can't compare that */
105
for ($i = 0; $i < sizeof($actual); $i++) {
106
if (strstr($actual[$i], 'Revision')) {
107
list ($revision) = array_splice($actual, $i, 1, $this->_revisionPlaceHolder);
112
if (empty($revision)) {
113
$this->assert(false, sprintf('[%s] Missing revision line', $truncatedFileName));
114
} else if (!preg_match('/^' . $this->_revisionPattern . '/', $revision)) {
115
$this->assert(false, sprintf('[%s] Invalid revision line: [%s]<br/>' .
116
'Expected pattern: [%s]',
118
$revision, $this->_revisionPattern));
121
if (!isset($this->_exceptions['header'][$truncatedFileName])) {
122
$this->assertEquals($this->_expectedHeaderLines, $actual,
123
sprintf('[%s] Malformed template header', $truncatedFileName));
124
$this->_errorCount++;
127
if ($this->_errorCount > 3) {
132
function _checkHiddenFormVars($fileName, $buffer) {
134
$hasHiddenFormVars = false;
136
$lines = split("(\r\n|\n)", $buffer);
138
if (in_array($fileName, $this->_exception['hiddenFormVars'])) {
142
for ($i = 0; $i < count($lines); $i++) {
143
if (preg_match('/<form\s/', $lines[$i], $matches)) {
147
if (preg_match('/hiddenFormVars/', $lines[$i], $matches)) {
148
$hasHiddenFormVars = true;
150
if (preg_match('/<\/form>/', $lines[$i], $matches)) {
151
$this->assert($hasHiddenFormVars, "$fileName: Form (line $formStartLine) is " .
152
"missing hidden form vars");
154
$hasHiddenFormVars = false;